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.InvalidOperationException;
024import org.nuxeo.ecm.automation.OperationContext;
025import org.nuxeo.ecm.automation.core.Constants;
026import org.nuxeo.ecm.automation.core.annotations.Context;
027import org.nuxeo.ecm.automation.core.annotations.Operation;
028import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
029import org.nuxeo.ecm.automation.core.annotations.Param;
030import org.nuxeo.ecm.core.api.Blob;
031import org.nuxeo.runtime.api.Framework;
032
033/**
034 * Renames the {@link FileSystemItem} with the given id with the given name for the currently authenticated user.
035 *
036 * @author Antoine Taillefer
037 */
038@Operation(id = NuxeoDriveRename.ID, category = Constants.CAT_SERVICES, label = "Nuxeo Drive: Rename")
039public class NuxeoDriveRename {
040
041    public static final String ID = "NuxeoDrive.Rename";
042
043    @Context
044    protected OperationContext ctx;
045
046    @Param(name = "id")
047    protected String id;
048
049    @Param(name = "name")
050    protected String name;
051
052    @OperationMethod
053    public Blob run() throws InvalidOperationException, IOException {
054
055        FileSystemItemManager fileSystemItemManager = Framework.getLocalService(FileSystemItemManager.class);
056        FileSystemItem fsItem;
057        try {
058            fsItem = fileSystemItemManager.rename(id, name, ctx.getPrincipal());
059        } catch (UnsupportedOperationException e) {
060            throw new InvalidOperationException(e);
061        }
062
063        return NuxeoDriveOperationHelper.asJSONBlob(fsItem);
064    }
065
066}