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.core.versioning;
013
014import java.io.Serializable;
015import java.util.Map;
016
017import org.apache.commons.logging.Log;
018import org.apache.commons.logging.LogFactory;
019import org.nuxeo.ecm.core.api.DocumentModel;
020import org.nuxeo.ecm.core.api.VersioningOption;
021import org.nuxeo.ecm.core.api.model.PropertyNotFoundException;
022import org.nuxeo.ecm.core.model.Document;
023
024/**
025 * Compatibility implementation of the versioning service in Nuxeo.
026 */
027public class CompatVersioningService extends StandardVersioningService {
028
029    private static final Log log = LogFactory.getLog(CompatVersioningService.class);
030
031    @Override
032    public String getVersionLabel(DocumentModel doc) {
033        try {
034            return getMajor(doc) + "." + getMinor(doc);
035        } catch (PropertyNotFoundException e) {
036            return "";
037        }
038    }
039
040    @Override
041    protected void setInitialVersion(Document doc) {
042        setVersion(doc, 1, 0);
043    }
044
045    @Override
046    public boolean isPreSaveDoingCheckOut(Document doc, boolean isDirty, VersioningOption option,
047            Map<String, Serializable> options) {
048        option = validateOption(doc, option);
049        boolean increment = option != VersioningOption.NONE;
050        return increment || (isDirty && !doc.isCheckedOut());
051    }
052
053    /*
054     * Create a pre-save snapshot, and re-checkout the document if there's a pending save or we want to increment the
055     * version.
056     */
057    @Override
058    public VersioningOption doPreSave(Document doc, boolean isDirty, VersioningOption option, String checkinComment,
059            Map<String, Serializable> options) {
060        option = validateOption(doc, option);
061        boolean increment = option != VersioningOption.NONE;
062        if (increment) {
063            if (doc.isCheckedOut()) {
064                doc.checkIn(null, checkinComment); // auto-label
065            }
066        }
067        if (!doc.isCheckedOut() && (isDirty || increment)) {
068            doc.checkOut();
069        }
070        return option;
071    }
072
073    @Override
074    public Document doPostSave(Document doc, VersioningOption option, String checkinComment,
075            Map<String, Serializable> options) {
076        if (!doc.isCheckedOut()) {
077            return null;
078        }
079        // option = validateOption(doc, option);
080        incrementByOption(doc, option);
081        followTransitionByOption(doc, option);
082        return null;
083    }
084
085    @Override
086    public Document doCheckIn(Document doc, VersioningOption option, String checkinComment) {
087        return doc.checkIn(null, checkinComment); // auto-label
088    }
089
090    @Override
091    public void doCheckOut(Document doc) {
092        doc.checkOut();
093        // set version number to that of the last version + inc minor
094        try {
095            Document last = doc.getLastVersion();
096            if (last != null) {
097                setVersion(doc, getMajor(last), getMinor(last) + 1);
098            }
099        } catch (PropertyNotFoundException e) {
100            // ignore
101        }
102    }
103
104}