001/*
002 * (C) Copyright 2020 Nuxeo (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 *     Thomas Roger <troger@nuxeo.com>
018 */
019
020package org.nuxeo.ecm.automation.core.operations.services;
021
022import java.io.IOException;
023
024import org.nuxeo.ecm.automation.AutomationService;
025import org.nuxeo.ecm.automation.OperationContext;
026import org.nuxeo.ecm.automation.core.Constants;
027import org.nuxeo.ecm.automation.core.annotations.Context;
028import org.nuxeo.ecm.automation.core.annotations.Operation;
029import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
030import org.nuxeo.ecm.automation.core.annotations.Param;
031import org.nuxeo.ecm.automation.core.util.DocumentHelper;
032import org.nuxeo.ecm.automation.core.util.Properties;
033import org.nuxeo.ecm.core.api.CoreSession;
034import org.nuxeo.ecm.core.api.DocumentModel;
035import org.nuxeo.ecm.platform.filemanager.api.FileManager;
036
037/**
038 * Use {@link FileManager} to create a folder and set multiple properties on it.
039 *
040 * @since 11.1
041 */
042@Operation(id = FileManagerCreateFolder.ID, category = Constants.CAT_SERVICES, label = "Create Folder", description = "Create a Folder using the FileManagerService and set multiple properties on it."
043        + "<p>The properties are specified as <i>key=value</i> pairs separated by a new line. "
044        + "The key used for a property is the property xpath. "
045        + "To specify multi-line values you can use a \\ character followed by a new line. "
046        + "<p>Example:<pre>dc:title=The Folder Title<br>dc:description=foo bar</pre>"
047        + "For updating a date, you will need to expose the value as ISO 8601 format, "
048        + "for instance : <p>Example:<pre>dc:title=The Folder Title<br>dc:issued=@{org.nuxeo.ecm.core.schema.utils.DateParser.formatW3CDateTime(CurrentDate.date)}</pre>"
049        + "<p>To update a multi-valued field with multiple values:<pre>custom:multivalued=a,b,c,d</pre>"
050        + "<p>Returns back the created folder.")
051public class FileManagerCreateFolder {
052
053    public static final String ID = "FileManager.CreateFolder";
054
055    @Context
056    protected CoreSession session;
057
058    @Context
059    protected AutomationService as;
060
061    @Context
062    protected FileManager fileManager;
063
064    @Context
065    protected OperationContext context;
066
067    @Param(name = "title")
068    protected String title;
069
070    @Param(name = "overwrite", required = false, description = "Whether to overwrite an existing folder with the same title, defaults to false")
071    protected boolean overwrite = false;
072
073    @Param(name = "properties", required = false)
074    protected Properties properties;
075
076    @OperationMethod
077    public DocumentModel run(DocumentModel parent) throws IOException {
078        DocumentModel doc = fileManager.createFolder(session, title, parent.getPathAsString(), overwrite);
079
080        if (properties != null) {
081            DocumentHelper.setProperties(session, doc, properties);
082            if (doc.isDirty()) {
083                doc = session.saveDocument(doc);
084            }
085        }
086
087        return doc;
088    }
089}