001/*
002 * (C) Copyright 2006-2011 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 *     bstefanescu
018 */
019package org.nuxeo.ecm.automation.core.operations.document;
020
021import java.io.IOException;
022
023import org.nuxeo.ecm.automation.core.Constants;
024import org.nuxeo.ecm.automation.core.annotations.Context;
025import org.nuxeo.ecm.automation.core.annotations.Operation;
026import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
027import org.nuxeo.ecm.automation.core.annotations.Param;
028import org.nuxeo.ecm.automation.core.collectors.DocumentModelCollector;
029import org.nuxeo.ecm.automation.core.util.DocumentHelper;
030import org.nuxeo.ecm.automation.core.util.Properties;
031import org.nuxeo.ecm.core.api.CoreSession;
032import org.nuxeo.ecm.core.api.DocumentModel;
033import org.nuxeo.ecm.core.api.DocumentRef;
034
035/**
036 * Create a document into the input document
037 *
038 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
039 */
040@Operation(id = CreateDocument.ID, category = Constants.CAT_DOCUMENT, label = "Create", description = "Create a new document "
041        + "in the input folder. You can initialize the document properties using the "
042        + "'properties' parameter. The properties are specified as <i>key=value</i> pairs "
043        + "separated by a new line. The key used for a property is the property xpath. To "
044        + "specify multi-line values, you can use a \\ character followed by a new line. "
045        + "<p>Example:<pre>dc:title=The Document Title<br>dc:description=foo bar</pre>. "
046        + "Returns the created document.")
047public class CreateDocument {
048
049    public static final String ID = "Document.Create";
050
051    @Context
052    protected CoreSession session;
053
054    @Param(name = "type")
055    protected String type;
056
057    @Param(name = "name", required = false)
058    protected String name;
059
060    @Param(name = "properties", required = false)
061    protected Properties content;
062
063    @OperationMethod(collector = DocumentModelCollector.class)
064    public DocumentModel run(DocumentModel doc) throws IOException {
065        if (name == null) {
066            name = "Untitled";
067        }
068        DocumentModel newDoc = session.createDocumentModel(doc.getPathAsString(), name, type);
069        if (content != null) {
070            DocumentHelper.setProperties(session, newDoc, content);
071        }
072        return session.createDocument(newDoc);
073    }
074
075    @OperationMethod(collector = DocumentModelCollector.class)
076    public DocumentModel run(DocumentRef doc) throws IOException {
077        return run(session.getDocument(doc));
078    }
079
080}