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 org.apache.logging.log4j.LogManager;
022import org.apache.logging.log4j.Logger;
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.NuxeoPrincipal;
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 Logger log = LogManager.getLogger(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 trash, 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            log.debug("Document {} is not a Collection, it cannot be adapted as a FileSystemItem.", doc::getId);
061            return false;
062        }
063        // Check HiddenInNavigation
064        if (doc.hasFacet("HiddenInNavigation")) {
065            log.debug("Document {} is HiddenInNavigation, it cannot be adapted as a FileSystemItem.", doc::getId);
066            return false;
067        }
068        // Check is document is in the trash
069        if (!includeDeleted && doc.isTrashed()) {
070            log.debug("Document {} is in the trash, it cannot be adapted as a FileSystemItem.", doc::getId);
071            return false;
072        }
073        if (!relaxSyncRootConstraint) {
074            // Check synchronization root registered for the current user
075            NuxeoDriveManager nuxeoDriveManager = Framework.getService(NuxeoDriveManager.class);
076            NuxeoPrincipal principal = doc.getPrincipal();
077            boolean isSyncRoot = nuxeoDriveManager.isSynchronizationRoot(principal, doc);
078            if (!isSyncRoot) {
079                log.debug(
080                        "Document {} is not a registered synchronization root for user {}, it cannot be adapted as a FileSystemItem.",
081                        doc::getId, principal::getName);
082                return false;
083            }
084        }
085        return true;
086    }
087
088    @Override
089    protected FileSystemItem adaptDocument(DocumentModel doc, boolean forceParentItem, FolderItem parentItem,
090            boolean relaxSyncRootConstraint, boolean getLockInfo) {
091        return new CollectionSyncRootFolderItem(name, parentItem, doc, relaxSyncRootConstraint, getLockInfo);
092    }
093
094}