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.ecm.core.api.LifeCycleConstants;
033import org.nuxeo.runtime.api.Framework;
034
035/**
036 * {@link FileSystemItemFactory} for a collection synchronization root {@link FolderItem}.
037 *
038 * @author Antoine Taillefer
039 * @since 6.0
040 */
041public class CollectionSyncRootFolderItemFactory extends DefaultSyncRootFolderItemFactory {
042
043    private static final Log log = LogFactory.getLog(CollectionSyncRootFolderItemFactory.class);
044
045    public static final String FACTORY_NAME = "collectionSyncRootFolderItemFactory";
046
047    /**
048     * The factory considers that a {@link DocumentModel} is adaptable as a {@link FileSystemItem} if:
049     * <ul>
050     * <li>It is a Collection</li>
051     * <li>AND it is not HiddenInNavigation</li>
052     * <li>AND it is not in the "deleted" life cycle state, unless {@code includeDeleted} is true</li>
053     * <li>AND it is a synchronization root registered for the current user, unless {@code relaxSyncRootConstraint} is
054     * true</li>
055     * </ul>
056     */
057    @Override
058    public boolean isFileSystemItem(DocumentModel doc, boolean includeDeleted, boolean relaxSyncRootConstraint) {
059
060        // Check Collection
061        if (!Framework.getService(CollectionManager.class).isCollection(doc)) {
062            if (log.isDebugEnabled()) {
063                log.debug(String.format("Document %s is not a Collection, it cannot be adapted as a FileSystemItem.",
064                        doc.getId()));
065            }
066            return false;
067        }
068        // Check HiddenInNavigation
069        if (doc.hasFacet("HiddenInNavigation")) {
070            if (log.isDebugEnabled()) {
071                log.debug(String.format("Document %s is HiddenInNavigation, it cannot be adapted as a FileSystemItem.",
072                        doc.getId()));
073            }
074            return false;
075        }
076        // Check "deleted" life cycle state
077        if (!includeDeleted && LifeCycleConstants.DELETED_STATE.equals(doc.getCurrentLifeCycleState())) {
078            if (log.isDebugEnabled()) {
079                log.debug(String.format(
080                        "Document %s is in the '%s' life cycle state, it cannot be adapted as a FileSystemItem.",
081                        doc.getId(), LifeCycleConstants.DELETED_STATE));
082            }
083            return false;
084        }
085        if (!relaxSyncRootConstraint) {
086            // Check synchronization root registered for the current user
087            NuxeoDriveManager nuxeoDriveManager = Framework.getLocalService(NuxeoDriveManager.class);
088            Principal principal = doc.getCoreSession().getPrincipal();
089            boolean isSyncRoot = nuxeoDriveManager.isSynchronizationRoot(principal, doc);
090            if (!isSyncRoot) {
091                if (log.isDebugEnabled()) {
092                    log.debug(String.format(
093                            "Document %s is not a registered synchronization root for user %s, it cannot be adapted as a FileSystemItem.",
094                            doc.getId(), principal.getName()));
095                }
096                return false;
097            }
098        }
099        return true;
100    }
101
102    @Override
103    protected FileSystemItem adaptDocument(DocumentModel doc, boolean forceParentItem, FolderItem parentItem,
104            boolean relaxSyncRootConstraint, boolean getLockInfo) {
105        return new CollectionSyncRootFolderItem(name, parentItem, doc, relaxSyncRootConstraint, getLockInfo);
106    }
107
108}