001/**
002 * Copyright 2005-2012 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.krms.api.repository.agenda;
017
018import java.io.Serializable;
019import java.util.ArrayList;
020import java.util.Collection;
021import java.util.Collections;
022import java.util.List;
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.XmlElements;
029import javax.xml.bind.annotation.XmlRootElement;
030import javax.xml.bind.annotation.XmlType;
031
032import org.apache.commons.lang.StringUtils;
033import org.kuali.rice.core.api.CoreConstants;
034import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
035import org.kuali.rice.core.api.mo.ModelBuilder;
036
037/**
038 * Concrete model object implementation of KRMS Repository AgendaTreeDefinition
039 * immutable. 
040 * Instances of Agenda can be (un)marshalled to and from XML.
041 *
042 * @see AgendaDefinitionContract
043 *
044 * @author Kuali Rice Team (rice.collab@kuali.org)
045 */
046@XmlRootElement(name = AgendaTreeDefinition.Constants.ROOT_ELEMENT_NAME)
047@XmlAccessorType(XmlAccessType.NONE)
048@XmlType(name = AgendaTreeDefinition.Constants.TYPE_NAME, propOrder = {
049                AgendaTreeDefinition.Elements.AGENDA_ID,
050                AgendaTreeDefinition.Elements.ENTRIES,
051                CoreConstants.CommonElements.FUTURE_ELEMENTS
052})
053public final class AgendaTreeDefinition extends AbstractDataTransferObject {
054        
055        private static final long serialVersionUID = 3355519740298280591L;
056
057        @XmlElement(name = Elements.AGENDA_ID, required = false)
058        private final String agendaId;
059        
060        @XmlElements(value = {
061            @XmlElement(name = Elements.RULE, type = AgendaTreeRuleEntry.class, required = false),
062            @XmlElement(name = Elements.SUB_AGENDA, type = AgendaTreeSubAgendaEntry.class, required = false)
063        })
064        private final List<AgendaTreeEntryDefinitionContract> entries;
065                
066        @SuppressWarnings("unused")
067    @XmlAnyElement
068    private final Collection<org.w3c.dom.Element> _futureElements = null;
069        
070        /** 
071     * This constructor should never be called.  
072     * It is only present for use during JAXB unmarshalling. 
073     */
074    private AgendaTreeDefinition() {
075        this.agendaId = null;
076        this.entries = null;
077    }
078
079    /**
080     * Constructs a AgendaTreeDefinition from the given builder.
081     * This constructor is private and should only ever be invoked from the builder.
082     *
083     * @param builder the Builder from which to construct the AgendaTreeDefinition
084     */
085    private AgendaTreeDefinition(Builder builder) {
086        this.agendaId = builder.getAgendaId();
087        this.entries = builder.getEntries();
088    }
089
090    /**
091     * Returns the agendaId
092     * @return agendaId of the AgendaTreeDefinition
093     */
094    public String getAgendaId() {
095        return agendaId;
096    }
097
098    /**
099     * Returns the {@link AgendaTreeEntryDefinitionContract}s
100     * @return List<{@link AgendaTreeEntryDefinitionContract}>s
101     */
102        public List<AgendaTreeEntryDefinitionContract> getEntries() {
103                if (entries == null){
104                        return Collections.emptyList();
105                }
106                return Collections.unmodifiableList(entries);
107        }
108
109
110    /**
111     * This builder is used to construct instances of AgendaTreeDefinition.
112     */
113    public static class Builder implements ModelBuilder, Serializable {
114                        
115                private static final long serialVersionUID = 7981215392039022620L;
116                
117                private String agendaId;
118                private List<AgendaTreeEntryDefinitionContract> entries;
119
120                /**
121                 * Private constructor for creating a builder with all of it's required attributes.
122                 */
123        private Builder() {
124                this.entries = new ArrayList<AgendaTreeEntryDefinitionContract>();
125        }
126
127        /**
128         * Create a new Builder
129         * @return a new Builder
130         */
131        public static Builder create(){
132                return new Builder();
133        }
134
135        /**
136         * Sets the agendaId to the given parameter
137         * @param agendaId to set the apendaId value to, must not be null or blank
138         * @thows IllegalArgumentException if the agendaId is null or blank
139         */
140        public void setAgendaId(String agendaId) {
141                        if (StringUtils.isBlank(agendaId)) {
142                                throw new IllegalArgumentException("agendaItemId was null or blank");
143                        }
144                this.agendaId = agendaId;
145        }
146
147        /**
148         * Adds the given {@link AgendaTreeRuleEntry} to the entries.
149         * @param ruleEntry {@link AgendaTreeRuleEntry} to be added to the entries, must not be null
150         * @thows IllegalArgumentException if the ruleEntry is null
151         */
152        public void addRuleEntry(AgendaTreeRuleEntry ruleEntry) {
153                if (ruleEntry == null) {
154                        throw new IllegalArgumentException("ruleEntry was null");
155                }
156                entries.add(ruleEntry);
157        }
158
159        /**
160         * Adds the given {@link AgendaTreeSubAgendaEntry} to the entries.
161         * @param subAgendaEntry {@link AgendaTreeSubAgendaEntry} to add to the entries, must not be null
162         * @thows IllegalArgumentException if the subAgendaEntry is null
163         */
164        public void addSubAgendaEntry(AgendaTreeSubAgendaEntry subAgendaEntry) {
165                if (subAgendaEntry == null) {
166                        throw new IllegalArgumentException("subAgendaEntry was null");
167                }
168                entries.add(subAgendaEntry);
169        }
170
171        /**
172         * Returns the agendaId
173         * @return agendaId
174         */
175        public String getAgendaId() {
176                return this.agendaId;
177        }
178
179        /**
180         * Returns the list of {@link AgendaTreeEntryDefinitionContract}s entries
181         * @return List<{@link AgendaTreeEntryDefinitionContract}> of entries
182         */
183        public List<AgendaTreeEntryDefinitionContract> getEntries() {
184                return this.entries;
185        }
186
187        @Override
188        public AgendaTreeDefinition build() {
189            return new AgendaTreeDefinition(this);
190        }
191                
192    }
193        
194        /**
195         * Defines some internal constants used on this class.
196         */
197        static class Constants {
198                final static String ROOT_ELEMENT_NAME = "agendaTreeDefinition";
199                final static String TYPE_NAME = "AgendaTreeDefinition";
200        }
201        
202        /**
203         * A private class which exposes constants which define the XML element names to use
204         * when this object is marshalled to XML.
205         */
206        static class Elements {
207                final static String AGENDA_ID = "agendaId";
208                final static String ENTRIES = "entries";
209                final static String RULE = "rule";
210                final static String SUB_AGENDA = "subAgenda";
211        }
212
213}