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