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