001/*
002 * (C) Copyright 2017 Nuxeo (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 *     Kevin Leturc
018 */
019package org.nuxeo.ecm.core.versioning;
020
021import java.io.Serializable;
022import java.util.ArrayList;
023import java.util.LinkedList;
024import java.util.List;
025
026import org.nuxeo.common.xmap.annotation.XNode;
027import org.nuxeo.common.xmap.annotation.XNodeList;
028import org.nuxeo.common.xmap.annotation.XObject;
029import org.nuxeo.ecm.core.api.VersioningOption;
030
031/**
032 * Descriptor used to restrict versioning options for a given lifeCycleState
033 *
034 * @since 9.1
035 */
036@XObject("options")
037public class VersioningRestrictionOptionsDescriptor implements Serializable {
038
039    private static final long serialVersionUID = 1L;
040
041    @XNode("@lifeCycleState")
042    protected String lifeCycleState;
043
044    @XNodeList(value = "option", componentType = OptionDescriptor.class, type = ArrayList.class)
045    protected List<OptionDescriptor> optionDescriptors;
046
047    public String getLifeCycleState() {
048        return lifeCycleState;
049    }
050
051    public List<VersioningOption> getOptions() {
052        List<VersioningOption> options = new LinkedList<>();
053        for (OptionDescriptor optionDescriptor : optionDescriptors) {
054            if (optionDescriptor.defaultOpt) {
055                options.add(0, optionDescriptor.option);
056            } else {
057                options.add(optionDescriptor.option);
058            }
059        }
060        return options;
061    }
062
063    @XObject("option")
064    protected static class OptionDescriptor implements Serializable {
065
066        private static final long serialVersionUID = 1L;
067
068        @XNode("@default")
069        protected boolean defaultOpt;
070
071        @XNode
072        protected VersioningOption option;
073
074    }
075
076}