001/*
002 * (C) Copyright 2006-2010 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 *     Dragos Mihalache
018 *     Florent Guillaume
019 */
020package org.nuxeo.ecm.platform.versioning.service;
021
022import org.nuxeo.ecm.core.api.DocumentModel;
023import org.nuxeo.ecm.core.api.VersioningOption;
024import org.nuxeo.ecm.core.versioning.VersioningService;
025import org.nuxeo.ecm.platform.versioning.api.VersionIncEditOptions;
026import org.nuxeo.ecm.platform.versioning.api.VersioningActions;
027import org.nuxeo.ecm.platform.versioning.api.VersioningManager;
028import org.nuxeo.runtime.api.Framework;
029import org.nuxeo.runtime.model.DefaultComponent;
030
031/**
032 * Versions management component implementation.
033 */
034public class VersioningManagerImpl extends DefaultComponent implements VersioningManager {
035
036    public static final String COMPONENT_ID = "org.nuxeo.ecm.platform.versioning.VersioningManager";
037
038    @Override
039    public VersionIncEditOptions getVersionIncEditOptions(DocumentModel doc) {
040        VersionIncEditOptions options = new VersionIncEditOptions();
041        VersioningService service = Framework.getService(VersioningService.class);
042        for (VersioningOption option : service.getSaveOptions(doc)) {
043            VersioningActions action;
044            switch (option) {
045            case MINOR:
046                action = VersioningActions.ACTION_INCREMENT_MINOR;
047                break;
048            case MAJOR:
049                action = VersioningActions.ACTION_INCREMENT_MAJOR;
050                break;
051            default:
052                action = VersioningActions.ACTION_NO_INCREMENT;
053            }
054            if (option == service.getSaveOptions(doc).get(0)) {
055                options.setDefaultVersioningAction(action);
056            }
057            options.addOption(action);
058        }
059
060        return options;
061    }
062
063    @Override
064    public String getVersionLabel(DocumentModel doc) {
065        return doc.getVersionLabel();
066    }
067
068    @Override
069    @Deprecated
070    public DocumentModel incrementMajor(DocumentModel doc) {
071        setVersion(doc, getValidMajor(doc) + 1, 0);
072        return doc;
073    }
074
075    @Override
076    @Deprecated
077    public DocumentModel incrementMinor(DocumentModel doc) {
078        doc.setPropertyValue(VersioningService.MINOR_VERSION_PROP, Long.valueOf(getValidMinor(doc) + 1));
079        return doc;
080    }
081
082    private static void setVersion(DocumentModel doc, long major, long minor) {
083        doc.setPropertyValue(VersioningService.MAJOR_VERSION_PROP, Long.valueOf(major));
084        doc.setPropertyValue(VersioningService.MINOR_VERSION_PROP, Long.valueOf(minor));
085    }
086
087    private static long getValidVersion(DocumentModel doc, String propName) {
088        Object propVal = doc.getPropertyValue(propName);
089        if (propVal == null || !(propVal instanceof Long)) {
090            return 0;
091        } else {
092            return ((Long) propVal).longValue();
093        }
094    }
095
096    private static long getValidMajor(DocumentModel doc) {
097        return getValidVersion(doc, VersioningService.MAJOR_VERSION_PROP);
098    }
099
100    private static long getValidMinor(DocumentModel doc) {
101        return getValidVersion(doc, VersioningService.MINOR_VERSION_PROP);
102    }
103
104    @Override
105    @Deprecated
106    public String getMajorVersionPropertyName(String documentType) {
107        return VersioningService.MAJOR_VERSION_PROP;
108    }
109
110    @Override
111    @Deprecated
112    public String getMinorVersionPropertyName(String documentType) {
113        return VersioningService.MINOR_VERSION_PROP;
114    }
115
116}