001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.automation.core.operations.document;
020
021import java.util.Locale;
022import java.util.Map;
023
024import org.nuxeo.ecm.automation.OperationContext;
025import org.nuxeo.ecm.automation.core.Constants;
026import org.nuxeo.ecm.automation.core.annotations.Context;
027import org.nuxeo.ecm.automation.core.annotations.Operation;
028import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
029import org.nuxeo.ecm.automation.core.annotations.Param;
030import org.nuxeo.ecm.automation.core.collectors.DocumentModelCollector;
031import org.nuxeo.ecm.core.api.CoreSession;
032import org.nuxeo.ecm.core.api.DocumentModel;
033import org.nuxeo.ecm.core.api.DocumentRef;
034import org.nuxeo.ecm.core.api.VersioningOption;
035
036/**
037 * Check in the input document.
038 */
039@Operation(id = CheckInDocument.ID, category = Constants.CAT_DOCUMENT, label = "Check In", description = "Checks in the input document. Returns back the document.")
040public class CheckInDocument {
041
042    public static final String ID = "Document.CheckIn";
043
044    public static final Map<String, VersioningOption> options = Map.of("none", VersioningOption.NONE, "minor",
045            VersioningOption.MINOR, "major", VersioningOption.MAJOR);
046
047    @Context
048    protected OperationContext ctx;
049
050    @Context
051    protected CoreSession session;
052
053    @Param(name = "version", required = true, values = { "none", "minor", "major" }, order = 0)
054    protected String version;
055
056    @Param(name = "comment", required = false, order = 1)
057    protected String comment;
058
059    @Param(name = "versionVarName", required = false, order = 2)
060    protected String versionVarName;
061
062    protected VersioningOption getVersioningOption() {
063        return options.getOrDefault(version.toLowerCase(Locale.ENGLISH), VersioningOption.MINOR);
064    }
065
066    @OperationMethod(collector = DocumentModelCollector.class)
067    public DocumentModel run(DocumentRef doc) {
068        if (session.isCheckedOut(doc)) {
069            DocumentRef ver = session.checkIn(doc, getVersioningOption(), comment);
070            if (versionVarName != null) {
071                ctx.put(versionVarName, ver);
072            }
073        } else {
074            if (versionVarName != null) {
075                ctx.put(versionVarName, session.getLastDocumentVersion(doc));
076            }
077        }
078        return session.getDocument(doc);
079    }
080
081    @OperationMethod(collector = DocumentModelCollector.class)
082    public DocumentModel run(DocumentModel doc) {
083        if (doc.isCheckedOut()) {
084            DocumentRef ver = session.checkIn(doc.getRef(), getVersioningOption(), comment);
085            doc.refresh(DocumentModel.REFRESH_STATE, null);
086            if (versionVarName != null) {
087                ctx.put(versionVarName, ver);
088            }
089        } else {
090            if (versionVarName != null) {
091                ctx.put(versionVarName, session.getLastDocumentVersion(doc.getRef()));
092            }
093        }
094        return doc;
095    }
096
097}