001/*
002 * (C) Copyright 2006-2010 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Dragos Mihalache
016 *     Florent Guillaume
017 */
018package org.nuxeo.ecm.platform.versioning.service;
019
020import org.nuxeo.ecm.core.api.DocumentModel;
021import org.nuxeo.ecm.core.api.VersioningOption;
022import org.nuxeo.ecm.core.versioning.VersioningService;
023import org.nuxeo.ecm.platform.versioning.api.VersionIncEditOptions;
024import org.nuxeo.ecm.platform.versioning.api.VersioningActions;
025import org.nuxeo.ecm.platform.versioning.api.VersioningManager;
026import org.nuxeo.runtime.api.Framework;
027import org.nuxeo.runtime.model.DefaultComponent;
028
029/**
030 * Versions management component implementation.
031 */
032public class VersioningManagerImpl extends DefaultComponent implements VersioningManager {
033
034    public static final String COMPONENT_ID = "org.nuxeo.ecm.platform.versioning.VersioningManager";
035
036    @Override
037    public VersionIncEditOptions getVersionIncEditOptions(DocumentModel doc) {
038        VersionIncEditOptions options = new VersionIncEditOptions();
039        VersioningService service = Framework.getService(VersioningService.class);
040        for (VersioningOption option : service.getSaveOptions(doc)) {
041            VersioningActions action;
042            switch (option) {
043            case MINOR:
044                action = VersioningActions.ACTION_INCREMENT_MINOR;
045                break;
046            case MAJOR:
047                action = VersioningActions.ACTION_INCREMENT_MAJOR;
048                break;
049            default:
050                action = VersioningActions.ACTION_NO_INCREMENT;
051            }
052            if (option == service.getSaveOptions(doc).get(0)) {
053                options.setDefaultVersioningAction(action);
054            }
055            options.addOption(action);
056        }
057
058        return options;
059    }
060
061    @Override
062    public String getVersionLabel(DocumentModel doc) {
063        return doc.getVersionLabel();
064    }
065
066    @Override
067    @Deprecated
068    public DocumentModel incrementMajor(DocumentModel doc) {
069        setVersion(doc, getValidMajor(doc) + 1, 0);
070        return doc;
071    }
072
073    @Override
074    @Deprecated
075    public DocumentModel incrementMinor(DocumentModel doc) {
076        doc.setPropertyValue(VersioningService.MINOR_VERSION_PROP, Long.valueOf(getValidMinor(doc) + 1));
077        return doc;
078    }
079
080    private static void setVersion(DocumentModel doc, long major, long minor) {
081        doc.setPropertyValue(VersioningService.MAJOR_VERSION_PROP, Long.valueOf(major));
082        doc.setPropertyValue(VersioningService.MINOR_VERSION_PROP, Long.valueOf(minor));
083    }
084
085    private static long getValidVersion(DocumentModel doc, String propName) {
086        Object propVal = doc.getPropertyValue(propName);
087        if (propVal == null || !(propVal instanceof Long)) {
088            return 0;
089        } else {
090            return ((Long) propVal).longValue();
091        }
092    }
093
094    private static long getValidMajor(DocumentModel doc) {
095        return getValidVersion(doc, VersioningService.MAJOR_VERSION_PROP);
096    }
097
098    private static long getValidMinor(DocumentModel doc) {
099        return getValidVersion(doc, VersioningService.MINOR_VERSION_PROP);
100    }
101
102    @Override
103    @Deprecated
104    public String getMajorVersionPropertyName(String documentType) {
105        return VersioningService.MAJOR_VERSION_PROP;
106    }
107
108    @Override
109    @Deprecated
110    public String getMinorVersionPropertyName(String documentType) {
111        return VersioningService.MINOR_VERSION_PROP;
112    }
113
114}