/*
 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-2.1.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     Antoine Taillefer <ataillefer@nuxeo.com>
 */
package org.nuxeo.ecm.automation.core.operations.document;

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.core.api.CoreSession;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.DocumentRef;

/**
 * Restores the input document to the given version.
 *
 * @since 5.7.3
 * @author Antoine Taillefer
 */
@Operation(id = RestoreVersion.ID, category = Constants.CAT_DOCUMENT, label = "Restore Version", description = "Restores the input document to the given version (path or UUID). The restored document is automatically saved if the 'save' parameter is true, else you need to save it later using the Save operation. Returns the restored document.")
public class RestoreVersion {

    public static final String ID = "Document.RestoreVersion";

    @Context
    protected CoreSession session;

    @Param(name = "version")
    protected DocumentRef version; // path or UUID

    @Param(name = "save", required = false, values = "true")
    protected boolean save = true;

    @OperationMethod
    public DocumentModel run(DocumentModel doc) throws Exception {
        DocumentModel restoredDoc = session.restoreToVersion(doc.getRef(),
                version);
        if (save) {
            restoredDoc = session.saveDocument(restoredDoc);
        }
        return restoredDoc;
    }

}

@@ -1105,51 +1105,8 @@
 del;
-%0Aimport org.nuxeo.ecm.core.api.DocumentRef;
 %0A%0A/*
@@ -1115,33 +1115,25 @@
  * Restores 
-the input
+a
  document to
@@ -1137,29 +1137,29 @@
  to the 
-given
+input
  version
 .%0A *%0A * 
@@ -1150,16 +1150,25 @@
  version
+ document
 .%0A *%0A * 
@@ -1334,25 +1334,17 @@
 estores 
-the input
+a
  documen
@@ -1356,13 +1356,13 @@
 the 
-given
+input
  ver
@@ -1370,22 +1370,16 @@
 ion 
-(path or UUID)
+document
 . Th
@@ -1692,89 +1692,8 @@
 n;%0A%0A
-    @Param(name = %22version%22)%0A    protected DocumentRef version; // path or UUID%0A%0A
     
@@ -1849,19 +1849,23 @@
 ntModel 
-doc
+version
 ) throws
@@ -1877,16 +1877,93 @@
 ption %7B%0A
+        DocumentModel liveDoc = session.getSourceDocument(version.getRef());%0A
         
@@ -2015,17 +2015,21 @@
 Version(
-d
+liveD
 oc.getRe
@@ -2056,16 +2056,25 @@
  version
+.getRef()
 );%0A     

Improve Document.RestoreVersion: pass version to restore as operation input and use it to deduce live doc