001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Antoine Taillefer <ataillefer@nuxeo.com>
018 */
019package org.nuxeo.drive.service.impl;
020
021import java.security.Principal;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.nuxeo.drive.adapter.FileSystemItem;
026import org.nuxeo.drive.adapter.FolderItem;
027import org.nuxeo.drive.adapter.impl.CollectionSyncRootFolderItem;
028import org.nuxeo.drive.service.FileSystemItemFactory;
029import org.nuxeo.drive.service.NuxeoDriveManager;
030import org.nuxeo.ecm.collections.api.CollectionManager;
031import org.nuxeo.ecm.core.api.DocumentModel;
032import org.nuxeo.runtime.api.Framework;
033
034/**
035 * {@link FileSystemItemFactory} for a collection synchronization root {@link FolderItem}.
036 *
037 * @author Antoine Taillefer
038 * @since 6.0
039 */
040public class CollectionSyncRootFolderItemFactory extends DefaultSyncRootFolderItemFactory {
041
042    private static final Log log = LogFactory.getLog(CollectionSyncRootFolderItemFactory.class);
043
044    public static final String FACTORY_NAME = "collectionSyncRootFolderItemFactory";
045
046    /**
047     * The factory considers that a {@link DocumentModel} is adaptable as a {@link FileSystemItem} if:
048     * <ul>
049     * <li>It is a Collection</li>
050     * <li>AND it is not HiddenInNavigation</li>
051     * <li>AND it is not in the trash, unless {@code includeDeleted} is true</li>
052     * <li>AND it is a synchronization root registered for the current user, unless {@code relaxSyncRootConstraint} is
053     * true</li>
054     * </ul>
055     */
056    @Override
057    public boolean isFileSystemItem(DocumentModel doc, boolean includeDeleted, boolean relaxSyncRootConstraint) {
058
059        // Check Collection
060        if (!Framework.getService(CollectionManager.class).isCollection(doc)) {
061            if (log.isDebugEnabled()) {
062                log.debug(String.format("Document %s is not a Collection, it cannot be adapted as a FileSystemItem.",
063                        doc.getId()));
064            }
065            return false;
066        }
067        // Check HiddenInNavigation
068        if (doc.hasFacet("HiddenInNavigation")) {
069            if (log.isDebugEnabled()) {
070                log.debug(String.format("Document %s is HiddenInNavigation, it cannot be adapted as a FileSystemItem.",
071                        doc.getId()));
072            }
073            return false;
074        }
075        // Check is document is in the trash
076        if (!includeDeleted && doc.isTrashed()) {
077            if (log.isDebugEnabled()) {
078                log.debug(String.format("Document %s is in the trash, it cannot be adapted as a FileSystemItem.",
079                        doc.getId()));
080            }
081            return false;
082        }
083        if (!relaxSyncRootConstraint) {
084            // Check synchronization root registered for the current user
085            NuxeoDriveManager nuxeoDriveManager = Framework.getService(NuxeoDriveManager.class);
086            Principal principal = doc.getCoreSession().getPrincipal();
087            boolean isSyncRoot = nuxeoDriveManager.isSynchronizationRoot(principal, doc);
088            if (!isSyncRoot) {
089                if (log.isDebugEnabled()) {
090                    log.debug(String.format(
091                            "Document %s is not a registered synchronization root for user %s, it cannot be adapted as a FileSystemItem.",
092                            doc.getId(), principal.getName()));
093                }
094                return false;
095            }
096        }
097        return true;
098    }
099
100    @Override
101    protected FileSystemItem adaptDocument(DocumentModel doc, boolean forceParentItem, FolderItem parentItem,
102            boolean relaxSyncRootConstraint, boolean getLockInfo) {
103        return new CollectionSyncRootFolderItem(name, parentItem, doc, relaxSyncRootConstraint, getLockInfo);
104    }
105
106}