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