001/*
002 * (C) Copyright 2013 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-2.1.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.userworkspace.factory;
018
019import java.security.Principal;
020
021import org.nuxeo.drive.adapter.FolderItem;
022import org.nuxeo.drive.hierarchy.userworkspace.adapter.UserWorkspaceSyncRootParentFolderItem;
023import org.nuxeo.drive.service.FileSystemItemFactory;
024import org.nuxeo.drive.service.FileSystemItemManager;
025import org.nuxeo.drive.service.impl.AbstractVirtualFolderItemFactory;
026import org.nuxeo.ecm.core.api.NuxeoException;
027import org.nuxeo.runtime.api.Framework;
028
029/**
030 * User workspace based implementation of {@link FileSystemItemFactory} for the parent {@link FolderItem} of the user's
031 * synchronization roots.
032 *
033 * @author Antoine Taillefer
034 */
035public class UserWorkspaceSyncRootParentFactory extends AbstractVirtualFolderItemFactory {
036
037    @Override
038    public FolderItem getVirtualFolderItem(Principal principal) {
039        FileSystemItemManager fileSystemItemManager = Framework.getLocalService(FileSystemItemManager.class);
040        FolderItem topLevelFolder = fileSystemItemManager.getTopLevelFolder(principal);
041        if (topLevelFolder == null) {
042            throw new NuxeoException(
043                    "Found no top level folder item. Please check your contribution to the following extension point: <extension target=\"org.nuxeo.drive.service.FileSystemItemAdapterService\" point=\"topLevelFolderItemFactory\">.");
044        }
045        return new UserWorkspaceSyncRootParentFolderItem(getName(), principal, topLevelFolder.getId(),
046                topLevelFolder.getPath(), folderName);
047    }
048
049}