001/*
002 * (C) Copyright 2012-2013 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.operations;
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.runtime.api.Framework;
031
032/**
033 * Deletes the document backing the {@link FileSystemItem} with the given id.
034 *
035 * @author Antoine Taillefer
036 */
037@Operation(id = NuxeoDriveDelete.ID, category = Constants.CAT_SERVICES, label = "Nuxeo Drive: Delete", description = "Delete the document backing the file system item with the given id.")
038public class NuxeoDriveDelete {
039
040    public static final String ID = "NuxeoDrive.Delete";
041
042    @Context
043    protected OperationContext ctx;
044
045    @Param(name = "id", description = "Id of the file system item backed by the document to delete.")
046    protected String id; // NOSONAR
047
048    /**
049     * @since 6.0
050     */
051    @Param(name = "parentId", required = false, description = "Optional id of the file system item backed by the parent container of the document to delete." //
052            + " For optimization purpose.")
053    protected String parentId;
054
055    @OperationMethod
056    public void run() throws InvalidOperationException {
057        FileSystemItemManager fileSystemItemManager = Framework.getService(FileSystemItemManager.class);
058        try {
059            if (parentId == null) {
060                fileSystemItemManager.delete(id, ctx.getPrincipal());
061            } else {
062                fileSystemItemManager.delete(id, parentId, ctx.getPrincipal());
063            }
064
065        } catch (UnsupportedOperationException e) {
066            throw new InvalidOperationException(e);
067        }
068    }
069
070}