001/*
002 * (C) Copyright 2012 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.util.Map;
022
023import org.apache.logging.log4j.LogManager;
024import org.apache.logging.log4j.Logger;
025import org.nuxeo.drive.adapter.FileSystemItem;
026import org.nuxeo.drive.adapter.FolderItem;
027import org.nuxeo.drive.service.FileSystemItemFactory;
028import org.nuxeo.drive.service.NuxeoDriveManager;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.NuxeoPrincipal;
031import org.nuxeo.runtime.api.Framework;
032
033/**
034 * Base {@link FileSystemItemFactory} for a synchronization root {@link FolderItem}.
035 *
036 * @author Antoine Taillefer
037 */
038public abstract class AbstractSyncRootFolderItemFactory extends AbstractFileSystemItemFactory {
039
040    private static final Logger log = LogManager.getLogger(AbstractSyncRootFolderItemFactory.class);
041
042    /**
043     * Returns the parent {@link FileSystemItem}.
044     */
045    protected abstract FolderItem getParentItem(DocumentModel doc);
046
047    /**
048     * No parameters by default.
049     */
050    @Override
051    public void handleParameters(Map<String, String> parameters) {
052        // Nothing to do as no parameters are contributed to the factory
053        if (!parameters.isEmpty()) {
054            throw new IllegalArgumentException(
055                    "Parameter map is not empty whereas no parameters are contributed to the factory.");
056        }
057        log.debug("Factory {} has no parameters to handle.", this::getName);
058    }
059
060    /**
061     * The factory considers that a {@link DocumentModel} is adaptable as a {@link FileSystemItem} if:
062     * <ul>
063     * <li>It is Folderish</li>
064     * <li>AND it is not a version nor a proxy</li>
065     * <li>AND it is not HiddenInNavigation</li>
066     * <li>AND it is not in the trash, unless {@code includeDeleted} is true</li>
067     * <li>AND it is a synchronization root registered for the current user, unless {@code relaxSyncRootConstraint} is
068     * true</li>
069     * </ul>
070     */
071    @Override
072    public boolean isFileSystemItem(DocumentModel doc, boolean includeDeleted, boolean relaxSyncRootConstraint) {
073
074        // Check Folderish
075        if (!doc.isFolder()) {
076            log.debug("Document {} is not Folderish, it cannot be adapted as a FileSystemItem.", doc::getId);
077            return false;
078        }
079        // Check version
080        if (doc.isVersion()) {
081            log.debug("Document {} is a version, it cannot be adapted as a FileSystemItem.", doc::getId);
082            return false;
083        }
084        // Check proxy
085        if (doc.isProxy()) {
086            log.debug("Document {} is a proxy, it cannot be adapted as a FileSystemItem.", doc::getId);
087            return false;
088        }
089        // Check HiddenInNavigation
090        if (doc.hasFacet("HiddenInNavigation")) {
091            log.debug("Document {} is HiddenInNavigation, it cannot be adapted as a FileSystemItem.", doc::getId);
092            return false;
093        }
094        // Check if document is in the trash
095        if (!includeDeleted && doc.isTrashed()) {
096
097            log.debug("Document {} is in the trash, it cannot be adapted as a FileSystemItem.", doc::getId);
098            return false;
099        }
100        if (!relaxSyncRootConstraint) {
101            // Check synchronization root registered for the current user
102            NuxeoDriveManager nuxeoDriveManager = Framework.getService(NuxeoDriveManager.class);
103            NuxeoPrincipal principal = doc.getPrincipal();
104            boolean isSyncRoot = nuxeoDriveManager.isSynchronizationRoot(principal, doc);
105            if (!isSyncRoot) {
106                log.debug(
107                        "Document {} is not a registered synchronization root for user {}, it cannot be adapted as a FileSystemItem.",
108                        doc::getId, principal::getName);
109                return false;
110            }
111        }
112        return true;
113    }
114
115    /**
116     * Force parent using {@link #getParentItem(DocumentModel)}.
117     */
118    @Override
119    public FileSystemItem getFileSystemItem(DocumentModel doc, boolean includeDeleted) {
120        return getFileSystemItem(doc, getParentItem(doc), includeDeleted);
121    }
122
123    /**
124     * Force parent using {@link #getParentItem(DocumentModel)}.
125     */
126    @Override
127    public FileSystemItem getFileSystemItem(DocumentModel doc, boolean includeDeleted,
128            boolean relaxSyncRootConstraint) {
129        return getFileSystemItem(doc, getParentItem(doc), includeDeleted, relaxSyncRootConstraint);
130    }
131
132    /**
133     * Force parent using {@link #getParentItem(DocumentModel)}.
134     */
135    @Override
136    public FileSystemItem getFileSystemItem(DocumentModel doc, boolean includeDeleted, boolean relaxSyncRootConstraint,
137            boolean getLockInfo) {
138        return getFileSystemItem(doc, getParentItem(doc), includeDeleted, relaxSyncRootConstraint, getLockInfo);
139    }
140
141}