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