001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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 */
016package org.kuali.rice.kew.api.action;
017
018import java.io.Serializable;
019import java.util.Collection;
020import java.util.Collections;
021import java.util.HashSet;
022import java.util.Set;
023
024import javax.xml.bind.annotation.XmlAccessType;
025import javax.xml.bind.annotation.XmlAccessorType;
026import javax.xml.bind.annotation.XmlAnyElement;
027import javax.xml.bind.annotation.XmlElement;
028import javax.xml.bind.annotation.XmlRootElement;
029import javax.xml.bind.annotation.XmlType;
030
031import org.kuali.rice.core.api.CoreConstants;
032import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
033import org.kuali.rice.core.api.mo.ModelBuilder;
034import org.w3c.dom.Element;
035
036@XmlRootElement(name = ValidActions.Constants.ROOT_ELEMENT_NAME)
037@XmlAccessorType(XmlAccessType.NONE)
038@XmlType(name = ValidActions.Constants.TYPE_NAME, propOrder = {
039    ValidActions.Elements.VALID_ACTION_CODES,
040    CoreConstants.CommonElements.FUTURE_ELEMENTS
041})
042public final class ValidActions extends AbstractDataTransferObject {
043
044        private static final long serialVersionUID = 8074175291030982905L;
045
046        @XmlElement(name = Elements.VALID_ACTION_CODE, required = false)
047    private final Set<String> validActionCodes;
048    
049    @SuppressWarnings("unused")
050    @XmlAnyElement
051    private final Collection<Element> _futureElements = null;
052
053    /**
054     * Private constructor used only by JAXB.
055     */
056    private ValidActions() {
057        this.validActionCodes = null;
058    }
059
060    private ValidActions(Builder builder) {
061        Set<ActionType> validActions = builder.getValidActions();
062        Set<String> validActionCodes = new HashSet<String>();
063        for (ActionType actionType : validActions) {
064                validActionCodes.add(actionType.getCode());
065        }
066        this.validActionCodes = validActionCodes;
067    }
068
069    public Set<ActionType> getValidActions() {
070        if (validActionCodes == null) {
071                return Collections.emptySet();
072        }
073        Set<ActionType> validActions = new HashSet<ActionType>();
074        for (String validActionCode : validActionCodes) {
075                // ignore action codes that we don't understand because they could be coming from a later version of KEW
076                ActionType actionType = ActionType.fromCode(validActionCode, true);
077                if (actionType != null) {
078                        validActions.add(actionType);
079                }
080        }
081        return Collections.unmodifiableSet(validActions);
082    }
083
084    /**
085     * A builder which can be used to construct {@link ValidActions} instances.
086     */
087    public final static class Builder implements Serializable, ModelBuilder {
088
089                private static final long serialVersionUID = -3227993220281961077L;
090
091                private Set<ActionType> validActions;
092
093        private Builder() {
094                setValidActions(new HashSet<ActionType>());
095        }
096
097        public static Builder create() {
098            return new Builder();
099        }
100
101        public ValidActions build() {
102            return new ValidActions(this);
103        }
104
105        public Set<ActionType> getValidActions() {
106            return this.validActions;
107        }
108
109        public void setValidActions(Set<ActionType> validActions) {
110                if (validActions == null) {
111                        throw new IllegalArgumentException("validActions was null");
112                }
113            this.validActions = new HashSet<ActionType>(validActions);
114        }
115        
116        public void addValidAction(ActionType validAction) {
117                if (validAction == null) {
118                        throw new IllegalArgumentException("validAction was null");
119                }
120                validActions.add(validAction);
121        }
122
123    }
124
125    /**
126     * Defines some internal constants used on this class.
127     */
128    static class Constants {
129        final static String ROOT_ELEMENT_NAME = "validActions";
130        final static String TYPE_NAME = "ValidActionsType";
131    }
132
133
134    /**
135     * A private class which exposes constants which define the XML element names to use when this object is marshalled to XML.
136     */
137    static class Elements {
138        final static String VALID_ACTION_CODES = "validActionCodes";
139        final static String VALID_ACTION_CODE = "validActionCode";
140    }
141
142}
143