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