View Javadoc

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