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.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023import org.nuxeo.drive.adapter.FileSystemItem;
024import org.nuxeo.drive.adapter.RootlessItemException;
025import org.nuxeo.drive.service.FileSystemItemManager;
026import org.nuxeo.ecm.automation.OperationContext;
027import org.nuxeo.ecm.automation.core.Constants;
028import org.nuxeo.ecm.automation.core.annotations.Context;
029import org.nuxeo.ecm.automation.core.annotations.Operation;
030import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
031import org.nuxeo.ecm.automation.core.annotations.Param;
032import org.nuxeo.ecm.core.api.Blob;
033import org.nuxeo.runtime.api.Framework;
034
035/**
036 * Checks if the {@link FileSystemItem} with the given source id can be moved to the {@link FileSystemItem} with the
037 * given destination id for the currently authenticated user.
038 *
039 * @author Antoine Taillefer
040 */
041@Operation(id = NuxeoDriveCanMove.ID, category = Constants.CAT_SERVICES, label = "Nuxeo Drive: Can move")
042public class NuxeoDriveCanMove {
043
044    private static final Log log = LogFactory.getLog(NuxeoDriveCanMove.class);
045
046    public static final String ID = "NuxeoDrive.CanMove";
047
048    @Context
049    protected OperationContext ctx;
050
051    @Param(name = "srcId")
052    protected String srcId;
053
054    @Param(name = "destId")
055    protected String destId;
056
057    @OperationMethod
058    public Blob run() throws IOException {
059        boolean canMove = false;
060        try {
061            FileSystemItemManager fileSystemItemManager = Framework.getLocalService(FileSystemItemManager.class);
062            canMove = fileSystemItemManager.canMove(srcId, destId, ctx.getPrincipal());
063        } catch (RootlessItemException e) {
064            // can happen if srcId or destId no longer match a document under an
065            // active sync root: just return false in that case.
066            if (log.isDebugEnabled()) {
067                log.debug(String.format("Cannot move %s to %s: %s", srcId, destId, e.getMessage()), e);
068            }
069        }
070        return NuxeoDriveOperationHelper.asJSONBlob(canMove);
071    }
072
073}