001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Antoine Taillefer <ataillefer@nuxeo.com>
016 */
017package org.nuxeo.drive.service.impl;
018
019import java.security.Principal;
020
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023import org.nuxeo.drive.adapter.FileSystemItem;
024import org.nuxeo.drive.adapter.FolderItem;
025import org.nuxeo.drive.adapter.impl.CollectionSyncRootFolderItem;
026import org.nuxeo.drive.service.FileSystemItemFactory;
027import org.nuxeo.drive.service.NuxeoDriveManager;
028import org.nuxeo.ecm.collections.api.CollectionManager;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.LifeCycleConstants;
031import org.nuxeo.runtime.api.Framework;
032
033/**
034 * {@link FileSystemItemFactory} for a collection synchronization root {@link FolderItem}.
035 *
036 * @author Antoine Taillefer
037 * @since 6.0
038 */
039public class CollectionSyncRootFolderItemFactory extends DefaultSyncRootFolderItemFactory {
040
041    private static final Log log = LogFactory.getLog(CollectionSyncRootFolderItemFactory.class);
042
043    public static final String FACTORY_NAME = "collectionSyncRootFolderItemFactory";
044
045    /**
046     * The factory considers that a {@link DocumentModel} is adaptable as a {@link FileSystemItem} if:
047     * <ul>
048     * <li>It is a Collection</li>
049     * <li>AND it is not HiddenInNavigation</li>
050     * <li>AND it is not in the "deleted" life cycle state, unless {@code includeDeleted} is true</li>
051     * <li>AND it is a synchronization root registered for the current user, unless {@code relaxSyncRootConstraint} is
052     * true</li>
053     * </ul>
054     */
055    @Override
056    public boolean isFileSystemItem(DocumentModel doc, boolean includeDeleted, boolean relaxSyncRootConstraint) {
057
058        // Check Collection
059        if (!Framework.getService(CollectionManager.class).isCollection(doc)) {
060            if (log.isDebugEnabled()) {
061                log.debug(String.format("Document %s is not a Collection, it cannot be adapted as a FileSystemItem.",
062                        doc.getId()));
063            }
064            return false;
065        }
066        // Check HiddenInNavigation
067        if (doc.hasFacet("HiddenInNavigation")) {
068            if (log.isDebugEnabled()) {
069                log.debug(String.format("Document %s is HiddenInNavigation, it cannot be adapted as a FileSystemItem.",
070                        doc.getId()));
071            }
072            return false;
073        }
074        // Check "deleted" life cycle state
075        if (!includeDeleted && LifeCycleConstants.DELETED_STATE.equals(doc.getCurrentLifeCycleState())) {
076            if (log.isDebugEnabled()) {
077                log.debug(String.format(
078                        "Document %s is in the '%s' life cycle state, it cannot be adapted as a FileSystemItem.",
079                        doc.getId(), LifeCycleConstants.DELETED_STATE));
080            }
081            return false;
082        }
083        if (!relaxSyncRootConstraint) {
084            // Check synchronization root registered for the current user
085            NuxeoDriveManager nuxeoDriveManager = Framework.getLocalService(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) {
103        return new CollectionSyncRootFolderItem(name, parentItem, doc, relaxSyncRootConstraint);
104    }
105
106}