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.operations;
018
019import java.io.IOException;
020
021import org.nuxeo.drive.adapter.FileSystemItem;
022import org.nuxeo.drive.service.FileSystemItemManager;
023import org.nuxeo.ecm.automation.OperationContext;
024import org.nuxeo.ecm.automation.core.Constants;
025import org.nuxeo.ecm.automation.core.annotations.Context;
026import org.nuxeo.ecm.automation.core.annotations.Operation;
027import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
028import org.nuxeo.ecm.automation.core.annotations.Param;
029import org.nuxeo.ecm.core.api.Blob;
030import org.nuxeo.runtime.api.Framework;
031
032/**
033 * Get the {@link FileSystemItem} with the given id for the currently authenticated user.
034 *
035 * @author Antoine Taillefer
036 */
037@Operation(id = NuxeoDriveGetFileSystemItem.ID, category = Constants.CAT_SERVICES, label = "Nuxeo Drive: Get file system item")
038public class NuxeoDriveGetFileSystemItem {
039
040    public static final String ID = "NuxeoDrive.GetFileSystemItem";
041
042    @Context
043    protected OperationContext ctx;
044
045    @Param(name = "id")
046    protected String id;
047
048    /**
049     * @since 6.0
050     */
051    @Param(name = "parentId", required = false)
052    protected String parentId;
053
054    @OperationMethod
055    public Blob run() throws IOException {
056
057        FileSystemItemManager fileSystemItemManager = Framework.getLocalService(FileSystemItemManager.class);
058        FileSystemItem fsItem;
059        if (parentId == null) {
060            fsItem = fileSystemItemManager.getFileSystemItemById(id, ctx.getPrincipal());
061        } else {
062            fsItem = fileSystemItemManager.getFileSystemItemById(id, parentId, ctx.getPrincipal());
063        }
064        return NuxeoDriveOperationHelper.asJSONBlob(fsItem);
065    }
066
067}