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 org.nuxeo.ecm.automation.OperationContext;
022import org.nuxeo.ecm.automation.core.Constants;
023import org.nuxeo.ecm.automation.core.annotations.Context;
024import org.nuxeo.ecm.automation.core.annotations.Operation;
025import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
026import org.nuxeo.ecm.automation.core.annotations.Param;
027import org.nuxeo.ecm.automation.core.collectors.DocumentModelCollector;
028import org.nuxeo.ecm.core.api.CoreSession;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.DocumentRef;
031import org.nuxeo.ecm.core.api.VersioningOption;
032
033/**
034 * Check in the input document.
035 */
036@Operation(id = CheckInDocument.ID, category = Constants.CAT_DOCUMENT, label = "Check In", description = "Checks in the input document. Returns back the document.")
037public class CheckInDocument {
038
039    public static final String ID = "Document.CheckIn";
040
041    @Context
042    protected OperationContext ctx;
043
044    @Context
045    protected CoreSession session;
046
047    @Param(name = "version", required = true, values = { "minor", "major" }, order = 0)
048    protected String version;
049
050    @Param(name = "comment", required = false, order = 1)
051    protected String comment;
052
053    @Param(name = "versionVarName", required = false, order = 2)
054    protected String versionVarName;
055
056    protected VersioningOption getVersioningOption() {
057        return "major".equalsIgnoreCase(version) ? VersioningOption.MAJOR : VersioningOption.MINOR;
058    }
059
060    @OperationMethod(collector = DocumentModelCollector.class)
061    public DocumentModel run(DocumentRef doc) {
062        if (session.isCheckedOut(doc)) {
063            DocumentRef ver = session.checkIn(doc, getVersioningOption(), comment);
064            if (versionVarName != null) {
065                ctx.put(versionVarName, ver);
066            }
067        } else {
068            if (versionVarName != null) {
069                ctx.put(versionVarName, session.getLastDocumentVersion(doc));
070            }
071        }
072        return session.getDocument(doc);
073    }
074
075    @OperationMethod(collector = DocumentModelCollector.class)
076    public DocumentModel run(DocumentModel doc) {
077        if (doc.isCheckedOut()) {
078            DocumentRef ver = session.checkIn(doc.getRef(), getVersioningOption(), comment);
079            doc.refresh(DocumentModel.REFRESH_STATE, null);
080            if (versionVarName != null) {
081                ctx.put(versionVarName, ver);
082            }
083        } else {
084            if (versionVarName != null) {
085                ctx.put(versionVarName, session.getLastDocumentVersion(doc.getRef()));
086            }
087        }
088        return doc;
089    }
090
091}