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.nuxeo.drive.adapter.FileSystemItem;
024import org.nuxeo.drive.adapter.FolderItem;
025import org.nuxeo.drive.adapter.impl.DefaultSyncRootFolderItem;
026import org.nuxeo.drive.service.FileSystemItemAdapterService;
027import org.nuxeo.drive.service.FileSystemItemFactory;
028import org.nuxeo.drive.service.impl.AbstractSyncRootFolderItemFactory;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.NuxeoException;
031import org.nuxeo.runtime.api.Framework;
032
033/**
034 * User workspace based implementation of {@link FileSystemItemFactory} for a synchronization root {@link FolderItem}.
035 *
036 * @author Antoine Taillefer
037 */
038public class UserWorkspaceSyncRootFactory extends AbstractSyncRootFolderItemFactory {
039
040    protected static final String SYNC_ROOT_PARENT_FACTORY_PARAM = "syncRootParentFactory";
041
042    protected String syncRootParentFactoryName;
043
044    /*------------------- AbstractFileSystemItemFactory ---------------------*/
045    @Override
046    public void handleParameters(Map<String, String> parameters) {
047        String syncRootParentFactoryParam = parameters.get(SYNC_ROOT_PARENT_FACTORY_PARAM);
048        if (StringUtils.isEmpty(syncRootParentFactoryParam)) {
049            throw new NuxeoException(
050                    String.format(
051                            "Factory %s has no %s parameter, please provide it in the factory contribution to set the name of the factory for the parent folder of the synchronization roots.",
052                            getName(), SYNC_ROOT_PARENT_FACTORY_PARAM));
053        }
054        syncRootParentFactoryName = syncRootParentFactoryParam;
055    }
056
057    @Override
058    protected FileSystemItem adaptDocument(DocumentModel doc, boolean forceParentItem, FolderItem parentItem,
059            boolean relaxSyncRootConstraint) {
060        return new DefaultSyncRootFolderItem(name, parentItem, doc, relaxSyncRootConstraint);
061    }
062
063    /*------------------ AbstractSyncRootFolderItemFactory ------------------*/
064    @Override
065    protected FolderItem getParentItem(DocumentModel doc) {
066        Principal principal = doc.getCoreSession().getPrincipal();
067        FolderItem parent = getFileSystemAdapterService().getVirtualFolderItemFactory(syncRootParentFactoryName).getVirtualFolderItem(
068                principal);
069        if (parent == null) {
070            throw new NuxeoException(String.format(
071                    "Cannot find the parent of document %s: virtual folder from factory %s.", doc.getId(),
072                    syncRootParentFactoryName));
073        }
074        return parent;
075    }
076
077    protected FileSystemItemAdapterService getFileSystemAdapterService() {
078        return Framework.getLocalService(FileSystemItemAdapterService.class);
079    }
080
081}