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