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.util.Map;
022
023import org.apache.commons.lang3.StringUtils;
024import org.apache.logging.log4j.LogManager;
025import org.apache.logging.log4j.Logger;
026import org.nuxeo.drive.adapter.FileSystemItem;
027import org.nuxeo.drive.adapter.FolderItem;
028import org.nuxeo.drive.hierarchy.permission.adapter.UserSyncRootParentFolderItem;
029import org.nuxeo.drive.hierarchy.userworkspace.adapter.UserWorkspaceHelper;
030import org.nuxeo.drive.service.FileSystemItemFactory;
031import org.nuxeo.drive.service.FileSystemItemManager;
032import org.nuxeo.drive.service.VirtualFolderItemFactory;
033import org.nuxeo.drive.service.impl.AbstractFileSystemItemFactory;
034import org.nuxeo.ecm.core.api.CoreInstance;
035import org.nuxeo.ecm.core.api.CoreSession;
036import org.nuxeo.ecm.core.api.DocumentModel;
037import org.nuxeo.ecm.core.api.NuxeoException;
038import org.nuxeo.ecm.core.api.NuxeoPrincipal;
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 Logger log = LogManager.getLogger(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            log.trace("Document {} is not a user workspace, it cannot be adapted as a FileSystemItem.", doc::getId);
075            return false;
076        }
077        // Check trashed state
078        if (!includeDeleted && doc.isTrashed()) {
079            log.debug("Document {} is in the trash, it cannot be adapted as a FileSystemItem.", doc::getId);
080            return false;
081        }
082        return true;
083    }
084
085    @Override
086    protected FileSystemItem adaptDocument(DocumentModel doc, boolean forceParentItem, FolderItem parentItem,
087            boolean relaxSyncRootConstraint, boolean getLockInfo) {
088        return new UserSyncRootParentFolderItem(getName(), doc, parentItem, folderName, relaxSyncRootConstraint,
089                getLockInfo);
090    }
091
092    /*------------------- FileSystemItemFactory ------------------- */
093    /**
094     * Force parent item using {@link #getTopLevelFolderItem(NuxeoPrincipal)}.
095     */
096    @Override
097    public FileSystemItem getFileSystemItem(DocumentModel doc, boolean includeDeleted) {
098        NuxeoPrincipal principal = doc.getPrincipal();
099        return getFileSystemItem(doc, getTopLevelFolderItem(principal), includeDeleted);
100    }
101
102    @Override
103    public FileSystemItem getFileSystemItem(DocumentModel doc, boolean includeDeleted,
104            boolean relaxSyncRootConstraint) {
105        NuxeoPrincipal principal = doc.getPrincipal();
106        return getFileSystemItem(doc, getTopLevelFolderItem(principal), includeDeleted, relaxSyncRootConstraint);
107    }
108
109    @Override
110    public FileSystemItem getFileSystemItem(DocumentModel doc, boolean includeDeleted, boolean relaxSyncRootConstraint,
111            boolean getLockInfo) {
112        NuxeoPrincipal principal = doc.getPrincipal();
113        return getFileSystemItem(doc, getTopLevelFolderItem(principal), includeDeleted, relaxSyncRootConstraint,
114                getLockInfo);
115    }
116
117    /*------------------- VirtualFolderItemFactory ------------------- */
118    @Override
119    public FolderItem getVirtualFolderItem(NuxeoPrincipal principal) {
120        RepositoryManager repositoryManager = Framework.getService(RepositoryManager.class);
121        // TODO: handle multiple repositories
122        CoreSession session = CoreInstance.getCoreSession(repositoryManager.getDefaultRepositoryName(), principal);
123        UserWorkspaceService userWorkspaceService = Framework.getService(UserWorkspaceService.class);
124        DocumentModel userWorkspace = userWorkspaceService.getCurrentUserPersonalWorkspace(session);
125        if (userWorkspace == null) {
126            throw new NuxeoException(
127                    String.format("No personal workspace found for user %s.", principal.getName()));
128        }
129        return (FolderItem) getFileSystemItem(userWorkspace);
130    }
131
132    @Override
133    public String getFolderName() {
134        return folderName;
135    }
136
137    @Override
138    public void setFolderName(String folderName) {
139        this.folderName = folderName;
140    }
141
142    /*------------------- Protected ------------------- */
143    protected FolderItem getTopLevelFolderItem(NuxeoPrincipal principal) {
144        FolderItem topLevelFolder = Framework.getService(FileSystemItemManager.class).getTopLevelFolder(principal);
145        if (topLevelFolder == null) {
146            throw new NuxeoException("Found no top level folder item. Please check your "
147                    + "contribution to the following extension point:"
148                    + " <extension target=\"org.nuxeo.drive.service.FileSystemItemAdapterService\""
149                    + " point=\"topLevelFolderItemFactory\">.");
150        }
151        return topLevelFolder;
152    }
153
154}