001/*
002 * (C) Copyright 2013-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.userworkspace.factory;
020
021import java.util.Map;
022
023import org.apache.commons.lang3.StringUtils;
024import org.nuxeo.drive.adapter.FileSystemItem;
025import org.nuxeo.drive.adapter.FolderItem;
026import org.nuxeo.drive.adapter.impl.DefaultSyncRootFolderItem;
027import org.nuxeo.drive.service.FileSystemItemAdapterService;
028import org.nuxeo.drive.service.FileSystemItemFactory;
029import org.nuxeo.drive.service.impl.AbstractSyncRootFolderItemFactory;
030import org.nuxeo.ecm.core.api.DocumentModel;
031import org.nuxeo.ecm.core.api.NuxeoException;
032import org.nuxeo.ecm.core.api.NuxeoPrincipal;
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(String.format(
052                    "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.",
053                    getName(), SYNC_ROOT_PARENT_FACTORY_PARAM));
054        }
055        syncRootParentFactoryName = syncRootParentFactoryParam;
056    }
057
058    @Override
059    protected FileSystemItem adaptDocument(DocumentModel doc, boolean forceParentItem, FolderItem parentItem,
060            boolean relaxSyncRootConstraint, boolean getLockInfo) {
061        return new DefaultSyncRootFolderItem(name, parentItem, doc, relaxSyncRootConstraint, getLockInfo);
062    }
063
064    /*------------------ AbstractSyncRootFolderItemFactory ------------------*/
065    @Override
066    protected FolderItem getParentItem(DocumentModel doc) {
067        NuxeoPrincipal principal = doc.getPrincipal();
068        FolderItem parent = getFileSystemAdapterService().getVirtualFolderItemFactory(syncRootParentFactoryName)
069                                                         .getVirtualFolderItem(principal);
070        if (parent == null) {
071            throw new NuxeoException(
072                    String.format("Cannot find the parent of document %s: virtual folder from factory %s.", doc.getId(),
073                            syncRootParentFactoryName));
074        }
075        return parent;
076    }
077
078    protected FileSystemItemAdapterService getFileSystemAdapterService() {
079        return Framework.getService(FileSystemItemAdapterService.class);
080    }
081
082}