001/*
002 * (C) Copyright 2012 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.permission.factory;
020
021import java.security.Principal;
022import java.util.ArrayList;
023import java.util.Arrays;
024import java.util.List;
025import java.util.Map;
026
027import org.apache.commons.lang.StringUtils;
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.nuxeo.drive.adapter.FolderItem;
031import org.nuxeo.drive.hierarchy.permission.adapter.PermissionTopLevelFolderItem;
032import org.nuxeo.drive.service.TopLevelFolderItemFactory;
033import org.nuxeo.drive.service.impl.AbstractVirtualFolderItemFactory;
034
035/**
036 * User workspace and permission based implementation of the {@link TopLevelFolderItemFactory}.
037 *
038 * @author Antoine Taillefer
039 */
040public class PermissionTopLevelFactory extends AbstractVirtualFolderItemFactory implements TopLevelFolderItemFactory {
041
042    private static final Log log = LogFactory.getLog(PermissionTopLevelFactory.class);
043
044    protected static final String CHILDREN_FACTORIES_PARAM = "childrenFactories";
045
046    protected List<String> childrenFactoryNames = new ArrayList<String>();
047
048    /*---------------------- FileSystemItemFactory ---------------*/
049    @Override
050    public void handleParameters(Map<String, String> parameters) {
051        super.handleParameters(parameters);
052        // Look for the "childrenFactories" parameter
053        String childrenFactoriesParam = parameters.get(CHILDREN_FACTORIES_PARAM);
054        if (!StringUtils.isEmpty(childrenFactoriesParam)) {
055            childrenFactoryNames.addAll(Arrays.asList(childrenFactoriesParam.split(",")));
056        } else {
057            log.warn(String.format(
058                    "Factory %s has no %s parameter, please provide one in the factory contribution using a comma separated list to set the children factory names.",
059                    getName(), CHILDREN_FACTORIES_PARAM));
060        }
061    }
062
063    /*---------------------- VirtualFolderItemFactory ---------------*/
064    @Override
065    public FolderItem getVirtualFolderItem(Principal principal) {
066        return getTopLevelFolderItem(principal);
067    }
068
069    /*----------------------- TopLevelFolderItemFactory ---------------------*/
070    @Override
071    public FolderItem getTopLevelFolderItem(Principal principal) {
072        return new PermissionTopLevelFolderItem(getName(), principal, getFolderName(), childrenFactoryNames);
073    }
074
075}