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 */
018
019package org.nuxeo.ecm.platform.versioning.api;
020
021import java.io.Serializable;
022import java.util.ArrayList;
023import java.util.LinkedHashMap;
024import java.util.List;
025import java.util.Map;
026
027/**
028 * This class composes a result of versioning interrogation about what increment options are available.
029 *
030 * @see org.nuxeo.ecm.platform.versioning.api.VersioningActions
031 */
032public class VersionIncEditOptions implements Serializable {
033
034    private static final long serialVersionUID = 1L;
035
036    private VersioningActions defaultVersioningAction;
037
038    private final List<VersioningActions> options = new ArrayList<VersioningActions>();
039
040    /**
041     * Returns action to be presented by default to user.
042     * <p>
043     * Since 5.7.3, returns {@link VersioningActions#ACTION_NO_INCREMENT} by default instead of null, when not set.
044     */
045    public VersioningActions getDefaultVersioningAction() {
046        if (defaultVersioningAction == null) {
047            return VersioningActions.ACTION_NO_INCREMENT;
048        }
049        return defaultVersioningAction;
050    }
051
052    public void setDefaultVersioningAction(VersioningActions defaultVersioningAction) {
053        this.defaultVersioningAction = defaultVersioningAction;
054    }
055
056    public void addOption(VersioningActions option) {
057        options.add(option);
058    }
059
060    public List<VersioningActions> getOptions() {
061        return options;
062    }
063
064    /**
065     * Returns true if some incrementation options are defined.
066     *
067     * @since 5.7.3
068     */
069    public boolean hasOptions() {
070        return options != null && !options.isEmpty();
071    }
072
073    /**
074     * Returns the versioning selection options for display.
075     *
076     * @since 5.7.3
077     */
078    public Map<String, String> getOptionsForDisplay() {
079        Map<String, String> map = new LinkedHashMap<String, String>();
080        if (options != null) {
081            for (VersioningActions option : options) {
082                String label = "label.versioning.option." + option.toString();
083                map.put(option.name(), label);
084            }
085        }
086        return map;
087    }
088
089    @Override
090    public String toString() {
091        return getClass().getSimpleName() + '(' + options + ')';
092    }
093
094}