View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.core.api.uif;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.commons.lang.builder.EqualsBuilder;
20  import org.apache.commons.lang.builder.HashCodeBuilder;
21  import org.apache.commons.lang.builder.ToStringBuilder;
22  import org.kuali.rice.core.api.CoreConstants;
23  import org.kuali.rice.core.api.mo.ModelBuilder;
24  import org.kuali.rice.core.api.mo.ModelObjectComplete;
25  import org.kuali.rice.core.api.util.jaxb.MapStringStringAdapter;
26  import org.w3c.dom.Element;
27  
28  import javax.xml.bind.annotation.XmlAccessType;
29  import javax.xml.bind.annotation.XmlAccessorType;
30  import javax.xml.bind.annotation.XmlAnyElement;
31  import javax.xml.bind.annotation.XmlElement;
32  import javax.xml.bind.annotation.XmlRootElement;
33  import javax.xml.bind.annotation.XmlType;
34  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
35  import java.util.Collection;
36  import java.util.Collections;
37  import java.util.HashMap;
38  import java.util.Map;
39  
40  /**
41   * A select control type.
42   */
43  @XmlRootElement(name = RemotableSelectGroup.Constants.ROOT_ELEMENT_NAME)
44  @XmlAccessorType(XmlAccessType.NONE)
45  @XmlType(name = RemotableSelectGroup.Constants.TYPE_NAME, propOrder = {
46          RemotableSelectGroup.Elements.KEY_LABELS,
47          RemotableSelectGroup.Elements.LABEL,
48  		CoreConstants.CommonElements.FUTURE_ELEMENTS })
49  public final class RemotableSelectGroup implements RemotableSelectGroupContract, ModelObjectComplete {
50  
51      @XmlElement(name = Elements.KEY_LABELS, required = true)
52      @XmlJavaTypeAdapter(value = MapStringStringAdapter.class)
53      private final Map<String, String> keyLabels;
54  
55      @XmlElement(name = Elements.LABEL, required = false)
56      private final String label;
57  
58      @SuppressWarnings("unused")
59      @XmlAnyElement
60      private final Collection<Element> _futureElements = null;
61  
62      /**
63       * Should only be invoked by JAXB.
64       */
65      @SuppressWarnings("unused")
66      private RemotableSelectGroup() {
67          label = null;
68          keyLabels = null;
69      }
70  
71      private RemotableSelectGroup(Builder b) {
72          label = b.label;
73          keyLabels = b.keyLabels;
74      }
75  
76      @Override
77      public Map<String, String> getKeyLabels() {
78          return keyLabels;
79      }
80  
81      @Override
82      public String getLabel() {
83          return label;
84      }
85  
86      @Override
87      public int hashCode() {
88          return HashCodeBuilder.reflectionHashCode(this, Constants.HASH_CODE_EQUALS_EXCLUDE);
89      }
90  
91      @Override
92      public boolean equals(Object obj) {
93          return EqualsBuilder.reflectionEquals(obj, this, Constants.HASH_CODE_EQUALS_EXCLUDE);
94      }
95  
96      @Override
97      public String toString() {
98          return ToStringBuilder.reflectionToString(this);
99      }
100 
101     public static final class Builder implements RemotableSelectGroupContract, ModelBuilder {
102         private String label;
103         private Map<String, String> keyLabels;
104 
105         private Builder(Map<String, String> keyLabels, String label) {
106             setKeyLabels(keyLabels);
107             setLabel(label);
108         }
109 
110         public static Builder create(Map<String, String> keyLabels, String label) {
111             return new Builder(keyLabels, label);
112         }
113 
114         @Override
115         public String getLabel() {
116             return label;
117         }
118 
119         public void setLabel(String label) {
120             if (StringUtils.isBlank(label)) {
121                 throw new IllegalArgumentException("label was blank");
122             }
123 
124             this.label = label;
125         }
126 
127         @Override
128         public Map<String, String> getKeyLabels() {
129             return keyLabels;
130         }
131 
132         public void setKeyLabels(Map<String, String> keyLabels) {
133             if (keyLabels == null || keyLabels.isEmpty()) {
134                 throw new IllegalArgumentException("keyLabels must be non-null & non-empty");
135             }
136 
137             this.keyLabels = Collections.unmodifiableMap(new HashMap<String, String>(keyLabels));
138         }
139 
140         @Override
141         public RemotableSelectGroup build() {
142             return new RemotableSelectGroup(this);
143         }
144     }
145 
146     /**
147      * Defines some internal constants used on this class.
148      */
149     static final class Constants {
150         static final String TYPE_NAME = "SelectGroupType";
151         final static String ROOT_ELEMENT_NAME = "selectGroup";
152         final static String[] HASH_CODE_EQUALS_EXCLUDE = {CoreConstants.CommonElements.FUTURE_ELEMENTS};
153     }
154 
155     static final class Elements {
156         static final String LABEL = "label";
157         static final String KEY_LABELS = "keyLabels";
158     }
159 }