001package org.nuxeo.snapshot.operation;
002
003import org.nuxeo.ecm.automation.core.annotations.Operation;
004import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
005import org.nuxeo.ecm.automation.core.annotations.Param;
006import org.nuxeo.ecm.automation.core.collectors.DocumentModelCollector;
007import org.nuxeo.ecm.core.api.DocumentModel;
008import org.nuxeo.ecm.core.api.NuxeoException;
009import org.nuxeo.ecm.core.api.VersioningOption;
010import org.nuxeo.snapshot.Snapshotable;
011
012import static org.nuxeo.ecm.automation.core.Constants.CAT_DOCUMENT;
013import static org.nuxeo.ecm.core.api.VersioningOption.MINOR;
014
015@Operation(id = CreateTreeSnapshot.ID, category = CAT_DOCUMENT, label = "Create snapshot", description = "Create a tree snapshot, input document must be eligible to Snapshotable adapter and output will the snapshot")
016public class CreateTreeSnapshot {
017    public static final String ID = "Document.CreateTreeSnapshot";
018
019    @Param(name = "versioning option", required = false)
020    String versioningOption = MINOR.name();
021
022    @OperationMethod(collector = DocumentModelCollector.class)
023    public DocumentModel run(DocumentModel doc) {
024        Snapshotable adapter = doc.getAdapter(Snapshotable.class);
025        if (adapter == null) {
026            throw new NuxeoException("Unable to get Snapshotable adapter with document: " + doc.getPathAsString());
027        }
028        return adapter.createSnapshot(VersioningOption.valueOf(versioningOption)).getDocument();
029    }
030}