View Javadoc

1   /**
2    * Copyright 2005-2011 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.kim.api.role;
17  
18  import org.apache.commons.collections.CollectionUtils;
19  import org.apache.commons.lang.StringUtils;
20  import org.apache.commons.lang.builder.EqualsBuilder;
21  import org.apache.commons.lang.builder.HashCodeBuilder;
22  import org.apache.commons.lang.builder.ToStringBuilder;
23  import org.kuali.rice.core.api.CoreConstants;
24  import org.kuali.rice.core.api.membership.MemberType;
25  import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
26  import org.kuali.rice.core.api.mo.ModelBuilder;
27  import org.kuali.rice.core.api.mo.ModelObjectComplete;
28  import org.kuali.rice.core.api.util.jaxb.MapStringStringAdapter;
29  import org.kuali.rice.kim.api.KimConstants;
30  import org.kuali.rice.kim.api.common.delegate.DelegateType;
31  import org.kuali.rice.kim.api.common.delegate.DelegateTypeContract;
32  import org.w3c.dom.Element;
33  
34  import javax.xml.bind.annotation.XmlAccessType;
35  import javax.xml.bind.annotation.XmlAccessorType;
36  import javax.xml.bind.annotation.XmlAnyElement;
37  import javax.xml.bind.annotation.XmlElement;
38  import javax.xml.bind.annotation.XmlRootElement;
39  import javax.xml.bind.annotation.XmlType;
40  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
41  import java.util.ArrayList;
42  import java.util.Collection;
43  import java.util.Collections;
44  import java.util.List;
45  import java.util.Map;
46  
47  @XmlRootElement(name = RoleMembership.Constants.ROOT_ELEMENT_NAME)
48  @XmlAccessorType(XmlAccessType.NONE)
49  @XmlType(name = RoleMembership.Constants.TYPE_NAME, propOrder = {
50          RoleMembership.Elements.ROLE_ID,
51          RoleMembership.Elements.ROLE_MEMBER_ID,
52          RoleMembership.Elements.EMBEDDED_ROLE_ID,
53          RoleMembership.Elements.MEMBER_ID,
54          RoleMembership.Elements.MEMBER_TYPE_CODE,
55          RoleMembership.Elements.ROLE_SORTING_CODE,
56          RoleMembership.Elements.QUALIFIER,
57          RoleMembership.Elements.DELEGATES,
58          CoreConstants.CommonElements.FUTURE_ELEMENTS
59  })
60  public class RoleMembership extends AbstractDataTransferObject implements RoleMembershipContract {
61      private static final long serialVersionUID = 1L;
62  
63      @XmlElement(name=Elements.ROLE_ID)
64      private final String roleId;
65  
66      @XmlElement(name=Elements.ROLE_MEMBER_ID)
67      private final String roleMemberId;
68  
69      @XmlElement(name=Elements.EMBEDDED_ROLE_ID)
70      private final String embeddedRoleId;
71  
72      @XmlElement(name=Elements.MEMBER_ID, required = true)
73      private final String memberId;
74  
75      @XmlElement(name=Elements.MEMBER_TYPE_CODE, required = true)
76      private final String memberTypeCode;
77  
78      @XmlElement(name=Elements.ROLE_SORTING_CODE)
79      private final String roleSortingCode;
80  
81      @XmlElement(name=Elements.QUALIFIER)
82      @XmlJavaTypeAdapter(value = MapStringStringAdapter.class)
83      private final Map<String, String> qualifier;
84  
85      @XmlElement(name=Elements.DELEGATES)
86      private final List<DelegateType> delegates;
87  
88      @SuppressWarnings("unused")
89      @XmlAnyElement
90      private final Collection<Element> _futureElements = null;
91  
92      /**
93       * Private constructor for JAXB only
94       */
95      @SuppressWarnings("unused")
96      private RoleMembership() {
97          roleId = null;
98          roleMemberId = null;
99          embeddedRoleId = null;
100         memberId = null;
101         memberTypeCode = null;
102         roleSortingCode = null;
103         qualifier = null;
104         delegates = null;
105     }
106 
107     private RoleMembership(Builder b) {
108         roleId = b.getRoleId();
109         roleMemberId = b.getRoleMemberId();
110         embeddedRoleId = b.getEmbeddedRoleId();
111         memberId = b.getMemberId();
112         memberTypeCode = b.getMemberType().getCode();
113         roleSortingCode = b.getRoleSortingCode();
114         qualifier = b.getQualifier();
115 
116         delegates = new ArrayList<DelegateType>();
117         if (!CollectionUtils.isEmpty(b.getDelegates())) {
118             for (DelegateType.Builder delegateBuilder : b.getDelegates()) {
119                 delegates.add(delegateBuilder.build());
120             }
121         }
122     }
123 
124     public String getRoleId() {
125         return roleId;
126     }
127 
128     public String getRoleMemberId() {
129         return roleMemberId;
130     }
131 
132     public String getEmbeddedRoleId() {
133         return embeddedRoleId;
134     }
135 
136     public String getMemberId() {
137         return memberId;
138     }
139 
140     public MemberType getMemberType() {
141         return MemberType.fromCode(memberTypeCode);
142     }
143 
144     public String getRoleSortingCode() {
145         return roleSortingCode;
146     }
147 
148     public Map<String, String> getQualifier() {
149         return qualifier;
150     }
151 
152     public List<DelegateType> getDelegates() {
153         return Collections.unmodifiableList(delegates);
154     }
155 
156 
157     public static final class Builder implements ModelBuilder, RoleMembershipContract, ModelObjectComplete {
158         private String roleId;
159         private String roleMemberId;
160         private String embeddedRoleId;
161         private String memberId;
162         private MemberType memberType;
163         private String roleSortingCode;
164         private Map<String, String> qualifier;
165         private List<DelegateType.Builder> delegates;
166 
167         private Builder() {
168         }
169 
170         public static Builder create(String roleId, String roleMemberId, String memberId, MemberType memberType,
171                                      Map<String, String> qualifier) {
172 
173             Builder b = new Builder();
174             b.setRoleId(roleId);
175             b.setRoleMemberId(roleMemberId);
176             b.setMemberId(memberId);
177             b.setMemberType(memberType);
178             b.setQualifier(qualifier);
179             return b;
180         }
181 
182         public static Builder create(RoleMembershipContract contract) {
183             Builder b = new Builder();
184             b.setRoleId(contract.getRoleId());
185             b.setRoleMemberId(contract.getRoleMemberId());
186             b.setEmbeddedRoleId(contract.getEmbeddedRoleId());
187             b.setMemberId(contract.getMemberId());
188             b.setMemberType(contract.getMemberType());
189             b.setRoleSortingCode(contract.getRoleSortingCode());
190             b.setQualifier(contract.getQualifier());
191 
192             List<DelegateType.Builder> delegateBuilders = new ArrayList<DelegateType.Builder>();
193             if (!CollectionUtils.isEmpty(contract.getDelegates())) {
194                 for (DelegateTypeContract delegateContract : contract.getDelegates()) {
195                     delegateBuilders.add(DelegateType.Builder.create(delegateContract));
196                 }
197             }
198             b.setDelegates(delegateBuilders);
199 
200             return b;
201         }
202 
203         @Override
204         public RoleMembership build() {
205             return new RoleMembership(this);
206         }
207 
208         public String getRoleId() {
209             return this.roleId;
210         }
211 
212         public void setRoleId(String roleId) {
213             this.roleId = roleId;
214         }
215 
216         public Map<String, String> getQualifier() {
217             return this.qualifier;
218         }
219 
220         public void setQualifier(Map<String, String> qualifier) {
221             this.qualifier = qualifier;
222         }
223 
224         public List<DelegateType.Builder> getDelegates() {
225             return this.delegates;
226         }
227 
228         public void setDelegates(List<DelegateType.Builder> delegates) {
229             this.delegates = delegates;
230         }
231 
232         public String getRoleMemberId() {
233             return this.roleMemberId;
234         }
235 
236         public void setRoleMemberId(String roleMemberId) {
237             this.roleMemberId = roleMemberId;
238         }
239 
240         public String getMemberId() {
241             return this.memberId;
242         }
243 
244         public void setMemberId(String memberId) {
245             if (StringUtils.isEmpty(memberId)) {
246                 throw new IllegalArgumentException("memberId cannot be empty or null");
247             }
248             this.memberId = memberId;
249         }
250 
251         public MemberType getMemberType() {
252             return this.memberType;
253         }
254 
255         public void setMemberType(MemberType memberType) {
256             if (memberType == null) {
257                 throw new IllegalArgumentException("memberTypeCode cannot be null");
258             }
259             this.memberType = memberType;
260         }
261 
262         public String getEmbeddedRoleId() {
263             return this.embeddedRoleId;
264         }
265 
266         public void setEmbeddedRoleId(String embeddedRoleId) {
267             this.embeddedRoleId = embeddedRoleId;
268         }
269 
270         public String getRoleSortingCode() {
271             return this.roleSortingCode;
272         }
273 
274         public void setRoleSortingCode(String roleSortingCode) {
275             this.roleSortingCode = roleSortingCode;
276         }
277 
278         @Override
279         public int hashCode() {
280             return HashCodeBuilder.reflectionHashCode(this);
281         }
282 
283         @Override
284         public boolean equals(Object obj) {
285             return EqualsBuilder.reflectionEquals(obj, this);
286         }
287 
288         @Override
289         public String toString() {
290             return ToStringBuilder.reflectionToString(this);
291         }
292 
293 
294     }
295 
296     /**
297      * A private class which exposes constants which define the XML element names to use
298      * when this object is marshalled to XML.
299      */
300     static class Elements {
301         final static String ROLE_ID = "roleId";
302         final static String ROLE_MEMBER_ID = "roleMemberId";
303         final static String EMBEDDED_ROLE_ID = "embeddedRoleId";
304         final static String MEMBER_ID = "memberId";
305         final static String MEMBER_TYPE_CODE = "memberTypeCode";
306         final static String ROLE_SORTING_CODE = "roleSortingCode";
307         final static String QUALIFIER = "qualifier";
308         final static String DELEGATES = "delegates";
309     }
310 
311     /**
312      * Defines some internal constants used on this class.
313      */
314     static class Constants {
315         final static String ROOT_ELEMENT_NAME = "roleMembership";
316         final static String TYPE_NAME = "RoleMembershipType";
317     }
318 
319     public static class Cache {
320         public static final String NAME = KimConstants.Namespaces.KIM_NAMESPACE_2_0 + "/" + RoleMembership.Constants.TYPE_NAME;
321     }
322 }