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 org.nuxeo.ecm.automation.core.Constants;
022import org.nuxeo.ecm.automation.core.annotations.Context;
023import org.nuxeo.ecm.automation.core.annotations.Operation;
024import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
025import org.nuxeo.ecm.automation.core.annotations.Param;
026import org.nuxeo.ecm.automation.core.collectors.DocumentModelCollector;
027import org.nuxeo.ecm.automation.core.util.DocumentHelper;
028import org.nuxeo.ecm.core.api.CoreSession;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.NuxeoException;
031import org.nuxeo.ecm.core.api.VersioningOption;
032import org.nuxeo.ecm.core.schema.FacetNames;
033import org.nuxeo.ecm.core.versioning.VersioningService;
034
035/**
036 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
037 */
038@Operation(id = CreateVersion.ID, category = Constants.CAT_DOCUMENT, label = "Snapshot Version", description = "Create a new version for the input document. Any modification made on the document by the chain will be automatically saved. Increment version if this was specified through the 'snapshot' parameter. Returns the live document (not the version).")
039public class CreateVersion {
040
041    public static final String ID = "Document.CreateVersion";
042
043    @Context
044    protected CoreSession session;
045
046    @Param(name = "increment", required = false, widget = Constants.W_OPTION, values = { "None", "Minor", "Major" })
047    protected String snapshot = "None";
048
049    @Param(name = "saveDocument", required = false, widget = Constants.W_CHECK, description = "Save the document in the session after versioning")
050    protected boolean saveDocument = true;
051
052    @OperationMethod(collector = DocumentModelCollector.class)
053    public DocumentModel run(DocumentModel doc) {
054        if (!doc.hasFacet(FacetNames.VERSIONABLE)) {
055            throw new NuxeoException(String.format(
056                    "The document (id:'%s') with title '%s' doesn't have 'versionable' facet", doc.getId(),
057                    doc.getTitle()));
058        }
059        VersioningOption vo;
060        if ("Minor".equalsIgnoreCase(snapshot)) {
061            vo = VersioningOption.MINOR;
062        } else if ("Major".equalsIgnoreCase(snapshot)) {
063            vo = VersioningOption.MAJOR;
064        } else {
065            vo = null;
066        }
067        if (vo != null) {
068            doc.putContextData(VersioningService.VERSIONING_OPTION, vo);
069        }
070
071        if (saveDocument) {
072            return DocumentHelper.saveDocument(session, doc);
073        } else {
074            return doc;
075        }
076    }
077
078}