/*
* (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Contributors:
* bstefanescu
*/
package org.nuxeo.ecm.automation.core.operations.document;
import java.io.IOException;
import org.nuxeo.ecm.automation.core.Constants;
import org.nuxeo.ecm.automation.core.annotations.Context;
import org.nuxeo.ecm.automation.core.annotations.Operation;
import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
import org.nuxeo.ecm.automation.core.annotations.Param;
import org.nuxeo.ecm.automation.core.collectors.DocumentModelCollector;
import org.nuxeo.ecm.automation.core.util.DocumentHelper;
import org.nuxeo.ecm.automation.core.util.Properties;
import org.nuxeo.ecm.core.api.CoreSession;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.DocumentRef;
/**
* Create a document into the input document
*
* @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
*/
@Operation(id = CreateDocument.ID, category = Constants.CAT_DOCUMENT, label = "Create", description = "Create a new document "
+ "in the input folder. You can initialize the document properties using the "
+ "'properties' parameter. The properties are specified as <i>key=value</i> pairs "
+ "separated by a new line. The key used for a property is the property xpath. To "
+ "specify multi-line values, you can use a \\ character followed by a new line. "
+ "<p>Example:<pre>dc:title=The Document Title<br>dc:description=foo bar</pre>. "
+ "Returns the created document.")
public class CreateDocument {
public static final String ID = "Document.Create";
@Context
protected CoreSession session;
@Param(name = "type")
protected String type;
@Param(name = "name", required = false)
protected String name;
@Param(name = "properties", required = false)
protected Properties content;
@OperationMethod(collector = DocumentModelCollector.class)
public DocumentModel run(DocumentModel doc) throws IOException {
if (name == null) {
name = "Untitled";
}
DocumentModel newDoc = session.createDocumentModel(doc.getPathAsString(), name, type);
if (content != null) {
DocumentHelper.setProperties(session, newDoc, content);
}
return session.createDocument(newDoc);
}
@OperationMethod(collector = DocumentModelCollector.class)
public DocumentModel run(DocumentRef doc) throws IOException {
return run(session.getDocument(doc));
}
}