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.hierarchy.permission.factory;
020
021import java.security.Principal;
022import java.util.Map;
023
024import org.apache.commons.lang.StringUtils;
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027import org.nuxeo.drive.adapter.FileSystemItem;
028import org.nuxeo.drive.adapter.FolderItem;
029import org.nuxeo.drive.hierarchy.permission.adapter.UserSyncRootParentFolderItem;
030import org.nuxeo.drive.hierarchy.userworkspace.adapter.UserWorkspaceHelper;
031import org.nuxeo.drive.service.FileSystemItemFactory;
032import org.nuxeo.drive.service.FileSystemItemManager;
033import org.nuxeo.drive.service.VirtualFolderItemFactory;
034import org.nuxeo.drive.service.impl.AbstractFileSystemItemFactory;
035import org.nuxeo.ecm.core.api.CoreInstance;
036import org.nuxeo.ecm.core.api.CoreSession;
037import org.nuxeo.ecm.core.api.DocumentModel;
038import org.nuxeo.ecm.core.api.LifeCycleConstants;
039import org.nuxeo.ecm.core.api.NuxeoException;
040import org.nuxeo.ecm.core.api.repository.RepositoryManager;
041import org.nuxeo.ecm.platform.userworkspace.api.UserWorkspaceService;
042import org.nuxeo.runtime.api.Framework;
043
044/**
045 * User workspace based implementation of {@link FileSystemItemFactory} for the parent {@link FolderItem} of the user's
046 * synchronization roots.
047 *
048 * @author Antoine Taillefer
049 */
050public class UserSyncRootParentFactory extends AbstractFileSystemItemFactory implements VirtualFolderItemFactory {
051
052    private static final Log log = LogFactory.getLog(UserSyncRootParentFactory.class);
053
054    protected static final String FOLDER_NAME_PARAM = "folderName";
055
056    protected String folderName;
057
058    /*------------------- AbstractFileSystemItemFactory ------------------- */
059    @Override
060    public void handleParameters(Map<String, String> parameters) {
061        // Look for the "folderName" parameter
062        String folderNameParam = parameters.get(FOLDER_NAME_PARAM);
063        if (StringUtils.isEmpty(folderNameParam)) {
064            throw new NuxeoException(String.format("Factory %s has no %s parameter, please provide one.", getName(),
065                    FOLDER_NAME_PARAM));
066        }
067        folderName = folderNameParam;
068    }
069
070    @Override
071    public boolean isFileSystemItem(DocumentModel doc, boolean includeDeleted, boolean relaxSyncRootConstraint) {
072        // Check user workspace
073        boolean isUserWorkspace = UserWorkspaceHelper.isUserWorkspace(doc);
074        if (!isUserWorkspace) {
075            if (log.isTraceEnabled()) {
076                log.trace(String.format(
077                        "Document %s is not a user workspace, it cannot be adapted as a FileSystemItem.", doc.getId()));
078            }
079            return false;
080        }
081        // Check "deleted" life cycle state
082        if (!includeDeleted && LifeCycleConstants.DELETED_STATE.equals(doc.getCurrentLifeCycleState())) {
083            if (log.isDebugEnabled()) {
084                log.debug(String.format(
085                        "Document %s is in the '%s' life cycle state, it cannot be adapted as a FileSystemItem.",
086                        doc.getId(), LifeCycleConstants.DELETED_STATE));
087            }
088            return false;
089        }
090        return true;
091    }
092
093    @Override
094    protected FileSystemItem adaptDocument(DocumentModel doc, boolean forceParentItem, FolderItem parentItem,
095            boolean relaxSyncRootConstraint, boolean getLockInfo) {
096        return new UserSyncRootParentFolderItem(getName(), doc, parentItem, folderName, relaxSyncRootConstraint,
097                getLockInfo);
098    }
099
100    /*------------------- FileSystemItemFactory ------------------- */
101    /**
102     * Force parent item using {@link #getTopLevelFolderItem(Principal)}.
103     */
104    @Override
105    public FileSystemItem getFileSystemItem(DocumentModel doc, boolean includeDeleted) {
106        Principal principal = doc.getCoreSession().getPrincipal();
107        return getFileSystemItem(doc, getTopLevelFolderItem(principal), includeDeleted);
108    }
109
110    @Override
111    public FileSystemItem getFileSystemItem(DocumentModel doc, boolean includeDeleted, boolean relaxSyncRootConstraint) {
112        Principal principal = doc.getCoreSession().getPrincipal();
113        return getFileSystemItem(doc, getTopLevelFolderItem(principal), includeDeleted, relaxSyncRootConstraint);
114    }
115
116    @Override
117    public FileSystemItem getFileSystemItem(DocumentModel doc, boolean includeDeleted, boolean relaxSyncRootConstraint,
118            boolean getLockInfo) {
119        Principal principal = doc.getCoreSession().getPrincipal();
120        return getFileSystemItem(doc, getTopLevelFolderItem(principal), includeDeleted, relaxSyncRootConstraint,
121                getLockInfo);
122    }
123
124    /*------------------- VirtualFolderItemFactory ------------------- */
125    @Override
126    public FolderItem getVirtualFolderItem(Principal principal) {
127        RepositoryManager repositoryManager = Framework.getLocalService(RepositoryManager.class);
128        // TODO: handle multiple repositories
129        try (CoreSession session = CoreInstance.openCoreSession(repositoryManager.getDefaultRepositoryName(), principal)) {
130            UserWorkspaceService userWorkspaceService = Framework.getLocalService(UserWorkspaceService.class);
131            DocumentModel userWorkspace = userWorkspaceService.getCurrentUserPersonalWorkspace(session, null);
132            if (userWorkspace == null) {
133                throw new NuxeoException(String.format("No personal workspace found for user %s.", principal.getName()));
134            }
135            return (FolderItem) getFileSystemItem(userWorkspace);
136        }
137    }
138
139    @Override
140    public String getFolderName() {
141        return folderName;
142    }
143
144    @Override
145    public void setFolderName(String folderName) {
146        this.folderName = folderName;
147    }
148
149    /*------------------- Protected ------------------- */
150    protected FolderItem getTopLevelFolderItem(Principal principal) {
151        FolderItem topLevelFolder = Framework.getLocalService(FileSystemItemManager.class).getTopLevelFolder(principal);
152        if (topLevelFolder == null) {
153            throw new NuxeoException("Found no top level folder item. Please check your "
154                    + "contribution to the following extension point:"
155                    + " <extension target=\"org.nuxeo.drive.service.FileSystemItemAdapterService\""
156                    + " point=\"topLevelFolderItemFactory\">.");
157        }
158        return topLevelFolder;
159    }
160
161}