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