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