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