001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Florent Guillaume
011 */
012package org.nuxeo.ecm.automation.core.operations.document;
013
014import org.nuxeo.ecm.automation.OperationContext;
015import org.nuxeo.ecm.automation.core.Constants;
016import org.nuxeo.ecm.automation.core.annotations.Context;
017import org.nuxeo.ecm.automation.core.annotations.Operation;
018import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
019import org.nuxeo.ecm.automation.core.annotations.Param;
020import org.nuxeo.ecm.automation.core.collectors.DocumentModelCollector;
021import org.nuxeo.ecm.core.api.CoreSession;
022import org.nuxeo.ecm.core.api.DocumentModel;
023import org.nuxeo.ecm.core.api.DocumentRef;
024import org.nuxeo.ecm.core.api.VersioningOption;
025
026/**
027 * Check in the input document.
028 */
029@Operation(id = CheckInDocument.ID, category = Constants.CAT_DOCUMENT, label = "Check In", description = "Checks in the input document. Returns back the document.")
030public class CheckInDocument {
031
032    public static final String ID = "Document.CheckIn";
033
034    @Context
035    protected OperationContext ctx;
036
037    @Context
038    protected CoreSession session;
039
040    @Param(name = "version", required = true, values = { "minor", "major" }, order = 0)
041    protected String version;
042
043    @Param(name = "comment", required = false, order = 1)
044    protected String comment;
045
046    @Param(name = "versionVarName", required = false, order = 2)
047    protected String versionVarName;
048
049    protected VersioningOption getVersioningOption() {
050        return "major".equalsIgnoreCase(version) ? VersioningOption.MAJOR : VersioningOption.MINOR;
051    }
052
053    @OperationMethod(collector = DocumentModelCollector.class)
054    public DocumentModel run(DocumentRef doc) {
055        if (session.isCheckedOut(doc)) {
056            DocumentRef ver = session.checkIn(doc, getVersioningOption(), comment);
057            if (versionVarName != null) {
058                ctx.put(versionVarName, ver);
059            }
060        } else {
061            if (versionVarName != null) {
062                ctx.put(versionVarName, session.getLastDocumentVersion(doc));
063            }
064        }
065        return session.getDocument(doc);
066    }
067
068    @OperationMethod(collector = DocumentModelCollector.class)
069    public DocumentModel run(DocumentModel doc) {
070        if (doc.isCheckedOut()) {
071            DocumentRef ver = session.checkIn(doc.getRef(), getVersioningOption(), comment);
072            doc.refresh(DocumentModel.REFRESH_STATE, null);
073            if (versionVarName != null) {
074                ctx.put(versionVarName, ver);
075            }
076        } else {
077            if (versionVarName != null) {
078                ctx.put(versionVarName, session.getLastDocumentVersion(doc.getRef()));
079            }
080        }
081        return doc;
082    }
083
084}