001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 */
012package org.nuxeo.ecm.automation.core.operations.document;
013
014import java.io.IOException;
015
016import org.nuxeo.ecm.automation.core.Constants;
017import org.nuxeo.ecm.automation.core.annotations.Context;
018import org.nuxeo.ecm.automation.core.annotations.Operation;
019import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
020import org.nuxeo.ecm.automation.core.annotations.Param;
021import org.nuxeo.ecm.automation.core.collectors.DocumentModelCollector;
022import org.nuxeo.ecm.automation.core.util.DocumentHelper;
023import org.nuxeo.ecm.automation.core.util.Properties;
024import org.nuxeo.ecm.core.api.CoreSession;
025import org.nuxeo.ecm.core.api.DocumentModel;
026import org.nuxeo.ecm.core.api.DocumentRef;
027
028/**
029 * Create a document into the input document
030 *
031 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
032 */
033@Operation(id = CreateDocument.ID, category = Constants.CAT_DOCUMENT, label = "Create", description = "Create a new document "
034        + "in the input folder. You can initialize the document properties using the "
035        + "'properties' parameter. The properties are specified as <i>key=value</i> pairs "
036        + "separated by a new line. The key used for a property is the property xpath. To "
037        + "specify multi-line values, you can use a \\ character followed by a new line. "
038        + "<p>Example:<pre>dc:title=The Document Title<br>dc:description=foo bar</pre>. "
039        + "Returns the created document.")
040public class CreateDocument {
041
042    public static final String ID = "Document.Create";
043
044    @Context
045    protected CoreSession session;
046
047    @Param(name = "type")
048    protected String type;
049
050    @Param(name = "name", required = false)
051    protected String name;
052
053    @Param(name = "properties", required = false)
054    protected Properties content;
055
056    @OperationMethod(collector = DocumentModelCollector.class)
057    public DocumentModel run(DocumentModel doc) throws IOException {
058        if (name == null) {
059            name = "Untitled";
060        }
061        DocumentModel newDoc = session.createDocumentModel(doc.getPathAsString(), name, type);
062        if (content != null) {
063            DocumentHelper.setProperties(session, newDoc, content);
064        }
065        return session.createDocument(newDoc);
066    }
067
068    @OperationMethod(collector = DocumentModelCollector.class)
069    public DocumentModel run(DocumentRef doc) throws IOException {
070        return run(session.getDocument(doc));
071    }
072
073}