001/*
002 * (C) Copyright 2013 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-2.1.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.hierarchy.userworkspace.factory;
018
019import java.security.Principal;
020import java.util.Map;
021
022import org.apache.commons.lang.StringUtils;
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.nuxeo.drive.adapter.FileSystemItem;
026import org.nuxeo.drive.adapter.FolderItem;
027import org.nuxeo.drive.hierarchy.userworkspace.adapter.UserWorkspaceHelper;
028import org.nuxeo.drive.hierarchy.userworkspace.adapter.UserWorkspaceTopLevelFolderItem;
029import org.nuxeo.drive.service.TopLevelFolderItemFactory;
030import org.nuxeo.drive.service.impl.AbstractFileSystemItemFactory;
031import org.nuxeo.ecm.core.api.CoreInstance;
032import org.nuxeo.ecm.core.api.CoreSession;
033import org.nuxeo.ecm.core.api.DocumentModel;
034import org.nuxeo.ecm.core.api.NuxeoException;
035import org.nuxeo.ecm.core.api.repository.RepositoryManager;
036import org.nuxeo.ecm.platform.userworkspace.api.UserWorkspaceService;
037import org.nuxeo.runtime.api.Framework;
038
039/**
040 * User workspace based implementation of the {@link TopLevelFolderItemFactory}.
041 *
042 * @author Antoine Taillefer
043 */
044public class UserWorkspaceTopLevelFactory extends AbstractFileSystemItemFactory implements TopLevelFolderItemFactory {
045
046    private static final Log log = LogFactory.getLog(UserWorkspaceTopLevelFactory.class);
047
048    protected static final String FOLDER_NAME_PARAM = "folderName";
049
050    protected static final String SYNC_ROOT_PARENT_FACTORY_PARAM = "syncRootParentFactory";
051
052    protected static final String DEFAULT_FOLDER_NAME = "Nuxeo Drive";
053
054    protected String folderName = DEFAULT_FOLDER_NAME;
055
056    protected String syncRootParentFactoryName;
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            folderName = folderNameParam;
065        } else {
066            log.info(String.format(
067                    "Factory %s has no %s parameter, you can provide one in the factory contribution to avoid using the default value '%s'.",
068                    getName(), FOLDER_NAME_PARAM, DEFAULT_FOLDER_NAME));
069        }
070        // Look for the "syncRootParentFactory" parameter
071        String syncRootParentFactoryParam = parameters.get(SYNC_ROOT_PARENT_FACTORY_PARAM);
072        if (!StringUtils.isEmpty(syncRootParentFactoryParam)) {
073            syncRootParentFactoryName = syncRootParentFactoryParam;
074        } else {
075            log.warn(String.format(
076                    "Factory %s has no %s parameter, please provide one in the factory contribution to set the name of the synchronization root parent factory.",
077                    getName(), SYNC_ROOT_PARENT_FACTORY_PARAM));
078        }
079    }
080
081    @Override
082    public boolean isFileSystemItem(DocumentModel doc, boolean includeDeleted, boolean relaxSyncRootConstraint) {
083        // Check user workspace
084        boolean isUserWorkspace = UserWorkspaceHelper.isUserWorkspace(doc);
085        if (!isUserWorkspace) {
086            if (log.isTraceEnabled()) {
087                log.trace(String.format(
088                        "Document %s is not a user workspace, it cannot be adapted as a FileSystemItem.", doc.getId()));
089            }
090            return false;
091        }
092        return true;
093    }
094
095    @Override
096    protected FileSystemItem adaptDocument(DocumentModel doc, boolean forceParentItem, FolderItem parentItem,
097            boolean relaxSyncRootConstraint) {
098        return new UserWorkspaceTopLevelFolderItem(getName(), doc, folderName, syncRootParentFactoryName,
099                relaxSyncRootConstraint);
100    }
101
102    /*---------------------- VirtualFolderItemFactory ---------------*/
103    @Override
104    public FolderItem getVirtualFolderItem(Principal principal) {
105        return getTopLevelFolderItem(principal);
106    }
107
108    @Override
109    public String getFolderName() {
110        return folderName;
111    }
112
113    @Override
114    public void setFolderName(String folderName) {
115        this.folderName = folderName;
116    }
117
118    /*----------------------- TopLevelFolderItemFactory ---------------------*/
119    @Override
120    public FolderItem getTopLevelFolderItem(Principal principal) {
121        RepositoryManager repositoryManager = Framework.getLocalService(RepositoryManager.class);
122        // TODO: handle multiple repositories
123        try (CoreSession session = CoreInstance.openCoreSession(repositoryManager.getDefaultRepositoryName(), principal)) {
124            UserWorkspaceService userWorkspaceService = Framework.getLocalService(UserWorkspaceService.class);
125            DocumentModel userWorkspace = userWorkspaceService.getCurrentUserPersonalWorkspace(session, null);
126            if (userWorkspace == null) {
127                throw new NuxeoException(String.format("No personal workspace found for user %s.", principal.getName()));
128            }
129            return (FolderItem) getFileSystemItem(userWorkspace);
130        }
131    }
132
133}