001/*
002 * (C) Copyright 2012 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 java.io.IOException;
022
023import org.nuxeo.drive.adapter.FileSystemItem;
024import org.nuxeo.drive.adapter.FolderItem;
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.ecm.core.api.Blobs;
034import org.nuxeo.runtime.api.Framework;
035
036/**
037 * Creates a container with the given name as title in the container backing the {@link FileSystemItem} with the given
038 * id.
039 *
040 * @author Antoine Taillefer
041 */
042@Operation(id = NuxeoDriveCreateFolder.ID, category = Constants.CAT_SERVICES, label = "Nuxeo Drive: Create folder", description = "Create a container with the given name as title in the container backing the file system item with the given id." //
043        + " Return the file system item backed by the created container as a JSON blob.")
044public class NuxeoDriveCreateFolder {
045
046    public static final String ID = "NuxeoDrive.CreateFolder";
047
048    @Context
049    protected OperationContext ctx;
050
051    @Param(name = "parentId", description = "Id of the file system item backed by the parent container.")
052    protected String parentId;
053
054    @Param(name = "name", description = "Title of the container to create.")
055    protected String name;
056
057    /**
058     * @since 9.1
059     */
060    @Param(name = "overwrite", required = false, description = "Optional, whether to overwrite an existing container with the same title.", values = "false")
061    protected boolean overwrite;
062
063    @OperationMethod
064    public Blob run() throws IOException {
065        FileSystemItemManager fileSystemItemManager = Framework.getService(FileSystemItemManager.class);
066        FolderItem folderItem = fileSystemItemManager.createFolder(parentId, name, ctx.getPrincipal(), overwrite);
067        return Blobs.createJSONBlobFromValue(folderItem);
068    }
069
070}