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.adapter;
018
019import java.security.Principal;
020import java.util.ArrayList;
021import java.util.List;
022
023import org.nuxeo.drive.adapter.FileSystemItem;
024import org.nuxeo.drive.adapter.FolderItem;
025import org.nuxeo.drive.adapter.impl.AbstractVirtualFolderItem;
026import org.nuxeo.drive.service.VirtualFolderItemFactory;
027
028/**
029 * User workspace and permission based implementation of the top level {@link FolderItem}.
030 * <p>
031 * Implements the following tree:
032 *
033 * <pre>
034 * Nuxeo Drive
035 *  |-- My Docs (= user workspace if synchronized else user synchronization roots)
036 *  |      |-- Folder 1
037 *  |      |-- Folder 2
038 *  |      |-- ...
039 *  |-- Other Docs (= user's shared synchronized roots with ReadWrite permission)
040 *  |      |-- Other folder 1
041 *  |      |-- Other folder 2
042 *  |      |-- ...
043 * </pre>
044 *
045 * @author Antoine Taillefer
046 */
047public class PermissionTopLevelFolderItem extends AbstractVirtualFolderItem {
048
049    private static final long serialVersionUID = 5179858544427598560L;
050
051    protected List<String> childrenFactoryNames;
052
053    public PermissionTopLevelFolderItem(String factoryName, Principal principal, String folderName,
054            List<String> childrenFactoryNames) {
055        super(factoryName, principal, null, null, folderName);
056        this.childrenFactoryNames = childrenFactoryNames;
057    }
058
059    protected PermissionTopLevelFolderItem() {
060        // Needed for JSON deserialization
061    }
062
063    @Override
064    public List<FileSystemItem> getChildren() {
065
066        List<FileSystemItem> children = new ArrayList<FileSystemItem>();
067        for (String childFactoryName : childrenFactoryNames) {
068            VirtualFolderItemFactory factory = getFileSystemItemAdapterService().getVirtualFolderItemFactory(
069                    childFactoryName);
070            FolderItem child = factory.getVirtualFolderItem(principal);
071            if (child != null) {
072                children.add(child);
073            }
074        }
075        return children;
076    }
077
078}