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