001/*
002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Antoine Taillefer <ataillefer@nuxeo.com>
016 */
017package org.nuxeo.ecm.automation.core.operations.document;
018
019import org.nuxeo.ecm.automation.core.Constants;
020import org.nuxeo.ecm.automation.core.annotations.Context;
021import org.nuxeo.ecm.automation.core.annotations.Operation;
022import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
023import org.nuxeo.ecm.automation.core.annotations.Param;
024import org.nuxeo.ecm.core.api.CoreSession;
025import org.nuxeo.ecm.core.api.DocumentModel;
026
027/**
028 * Restores a document to the input version document.
029 *
030 * @since 5.7.3
031 * @author Antoine Taillefer
032 */
033@Operation(id = RestoreVersion.ID, category = Constants.CAT_DOCUMENT, label = "Restore Version", description = "Restores a document to the input version document. If createVersion is true, a version of the live document will be created before restoring it to the input version. If checkout is true, a checkout will be processed after restoring the document, visible in the UI by the '+' symbol beside the version number. Returns the restored document.")
034public class RestoreVersion {
035
036    public static final String ID = "Document.RestoreVersion";
037
038    @Context
039    protected CoreSession session;
040
041    @Param(name = "createVersion", required = false, values = "false")
042    protected boolean createVersion = false;
043
044    @Param(name = "checkout", required = false, values = "false")
045    protected boolean checkout = false;
046
047    @OperationMethod
048    public DocumentModel run(DocumentModel version) {
049        DocumentModel liveDoc = session.getSourceDocument(version.getRef());
050        return session.restoreToVersion(liveDoc.getRef(), version.getRef(), !createVersion, !checkout);
051    }
052
053}