cancel
Showing results for 
Search instead for 
Did you mean: 

Automation service - Restore Version

mcaissie_
Confirmed Champ
Confirmed Champ

Hi, Is there an automation service allowing to restore a previous version of a document ? By restore , i mean set an older version as the current document.

If not , and i want to add an operation , could someone point me on what Nuxeo classes i will need to do so.

tx Michel

1 ACCEPTED ANSWER

mcaissie_
Confirmed Champ
Confirmed Champ

Great it works just fine !

@Operation(id = RestoreDocument.ID,
    category = Constants.CAT_DOCUMENT,
    label = "RestoreDocument",
    description = "Restore the input Document version.")
public class RestoreDocument {

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

    @Context
    protected CoreSession session;

    @Param(name = "versionDocumentId")
    protected String versionDocumentId;

    @OperationMethod
    public DocumentModel run(DocumentModel doc) throws Exception {
        List<DocumentModel> versions = session.getVersions(doc.getRef());
        if (versions != null) {
            for (DocumentModel version : versions) {
                if (version.getId().equals(versionDocumentId)) {
                    session.restoreToVersion(doc.getRef(), version.getRef());
                    return version;
                }
            }
        }
        throw new Exception("Version Document Id not found: " + versionDocumentId);
    }
}

View answer in original post

5 REPLIES 5

Florent_Guillau
World-Class Innovator
World-Class Innovator

There is no such operation in default Nuxeo.

You could easily create one, the basic API to use is CoreSession.restoreToVersion.

mcaissie_
Confirmed Champ
Confirmed Champ

Great it works just fine !

@Operation(id = RestoreDocument.ID,
    category = Constants.CAT_DOCUMENT,
    label = "RestoreDocument",
    description = "Restore the input Document version.")
public class RestoreDocument {

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

    @Context
    protected CoreSession session;

    @Param(name = "versionDocumentId")
    protected String versionDocumentId;

    @OperationMethod
    public DocumentModel run(DocumentModel doc) throws Exception {
        List<DocumentModel> versions = session.getVersions(doc.getRef());
        if (versions != null) {
            for (DocumentModel version : versions) {
                if (version.getId().equals(versionDocumentId)) {
                    session.restoreToVersion(doc.getRef(), version.getRef());
                    return version;
                }
            }
        }
        throw new Exception("Version Document Id not found: " + versionDocumentId);
    }
}

I'd like to try your code!

Hi, I would like to know how to do this in version 7.2?

maumig_
Confirmed Champ
Confirmed Champ

I'd like to try your code! 🙂 But I have not figured out how to add this operation and how to call it by a remote automation client. I have read the documentation on how to "contribute an operation" (http://doc.nuxeo.com/display/NXDOC/Contributing+an+Operation) but is not clear for me where how and where add the operation... in which xml? in a jar?

thanks in advance.

Getting started

Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.