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