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.common.delegate;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.joda.time.DateTime;
20  import org.kuali.rice.core.api.CoreConstants;
21  import org.kuali.rice.core.api.membership.MemberType;
22  import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
23  import org.kuali.rice.core.api.mo.ModelBuilder;
24  import org.kuali.rice.core.api.mo.common.active.InactivatableFromToUtils;
25  import org.kuali.rice.core.api.util.jaxb.DateTimeAdapter;
26  import org.kuali.rice.core.api.util.jaxb.MapStringStringAdapter;
27  import org.kuali.rice.kim.api.KimConstants;
28  import org.w3c.dom.Element;
29  
30  import javax.xml.bind.annotation.XmlAccessType;
31  import javax.xml.bind.annotation.XmlAccessorType;
32  import javax.xml.bind.annotation.XmlAnyElement;
33  import javax.xml.bind.annotation.XmlElement;
34  import javax.xml.bind.annotation.XmlRootElement;
35  import javax.xml.bind.annotation.XmlType;
36  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
37  import java.io.Serializable;
38  import java.util.Collection;
39  import java.util.Map;
40  
41  @XmlRootElement(name = DelegateMember.Constants.ROOT_ELEMENT_NAME)
42  @XmlAccessorType(XmlAccessType.NONE)
43  @XmlType(name = DelegateMember.Constants.TYPE_NAME, propOrder = {
44          DelegateMember.Elements.DELEGATION_MEMBER_ID,
45          DelegateMember.Elements.DELEGATION_ID,
46          DelegateMember.Elements.MEMBER_ID,
47          DelegateMember.Elements.ROLE_MEMBER_ID,
48          DelegateMember.Elements.TYPE_CODE,
49          DelegateMember.Elements.ATTRIBUTES,
50          CoreConstants.CommonElements.ACTIVE_FROM_DATE,
51          CoreConstants.CommonElements.ACTIVE_TO_DATE,
52          CoreConstants.CommonElements.VERSION_NUMBER,
53          CoreConstants.CommonElements.FUTURE_ELEMENTS
54  })
55  public final class DelegateMember extends AbstractDataTransferObject
56          implements DelegateMemberContract {
57  
58      @XmlElement(name = Elements.DELEGATION_MEMBER_ID, required = false)
59      private final String delegationMemberId;
60  
61      @XmlElement(name = Elements.DELEGATION_ID, required = false)
62      private final String delegationId;
63  
64      @XmlElement(name = Elements.MEMBER_ID, required = false)
65      private final String memberId;
66  
67      @XmlElement(name = Elements.ROLE_MEMBER_ID, required = false)
68      private final String roleMemberId;
69  
70      @XmlElement(name = Elements.TYPE_CODE, required = false)
71      private final String typeCode;
72  
73      @XmlElement(name = Elements.ATTRIBUTES, required = false)
74      @XmlJavaTypeAdapter(value = MapStringStringAdapter.class)
75      private final Map<String, String> attributes;
76  
77      @XmlJavaTypeAdapter(DateTimeAdapter.class)
78      @XmlElement(name = CoreConstants.CommonElements.ACTIVE_FROM_DATE)
79      private final DateTime activeFromDate;
80  
81      @XmlJavaTypeAdapter(DateTimeAdapter.class)
82      @XmlElement(name = CoreConstants.CommonElements.ACTIVE_TO_DATE)
83      private final DateTime activeToDate;
84  
85      @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER)
86      private final Long versionNumber;
87  
88      @SuppressWarnings("unused")
89      @XmlAnyElement
90      private final Collection<Element> _futureElements = null;
91  
92      /**
93       * Private constructor used only by JAXB.
94       */
95      @SuppressWarnings("unused")
96      private DelegateMember() {
97          this.delegationMemberId = null;
98          this.delegationId = null;
99          this.memberId = null;
100         this.roleMemberId = null;
101         this.typeCode = null;
102         this.versionNumber = null;
103         this.activeFromDate = null;
104         this.activeToDate = null;
105         this.attributes = null;
106     }
107 
108     private DelegateMember(Builder builder) {
109         this.delegationMemberId = builder.getDelegationMemberId();
110         this.delegationId = builder.getDelegationId();
111         this.memberId = builder.getMemberId();
112         this.roleMemberId = builder.getRoleMemberId();
113         if (builder.getType() == null) {
114             this.typeCode = null;
115         } else {
116             this.typeCode = builder.getType().getCode();
117         }
118         this.versionNumber = builder.getVersionNumber();
119         this.activeFromDate = builder.getActiveFromDate();
120         this.activeToDate = builder.getActiveToDate();
121         this.attributes = builder.getAttributes();
122     }
123 
124 
125     @Override
126     public String getDelegationMemberId() {
127         return this.delegationMemberId;
128     }
129 
130     @Override
131     public String getDelegationId() {
132         return this.delegationId;
133     }
134 
135     @Override
136     public MemberType getType() {
137         return MemberType.fromCode(this.typeCode);
138     }
139 
140     @Override
141     public String getRoleMemberId() {
142         return this.roleMemberId;
143     }
144 
145     @Override
146     public String getMemberId() {
147         return this.memberId;
148     }
149 
150     @Override
151     public Long getVersionNumber() {
152         return this.versionNumber;
153     }
154 
155     @Override
156     public DateTime getActiveFromDate() {
157         return activeFromDate;
158     }
159 
160     @Override
161     public DateTime getActiveToDate() {
162         return activeToDate;
163     }
164 
165     /**
166      * @return the attributes
167      */
168     @Override
169     public Map<String, String> getAttributes() {
170         return this.attributes;
171     }
172 
173     @Override
174     public boolean isActive(DateTime activeAsOfDate) {
175         return InactivatableFromToUtils.isActive(activeFromDate, activeToDate, activeAsOfDate);
176     }
177 
178     @Override
179     public boolean isActive() {
180         return InactivatableFromToUtils.isActive(activeFromDate, activeToDate, null);
181     }
182 
183     /**
184      * A builder which can be used to construct {@link DelegateMember} instances.  Enforces the constraints of the {@link DelegateMemberContract}.
185      */
186     public static final class Builder implements Serializable, ModelBuilder, DelegateMemberContract {
187 
188         private String delegationMemberId;
189         private String delegationId;
190         private String memberId;
191         private String roleMemberId;
192         private MemberType type;
193         private Map<String, String> attributes;
194         private DateTime activeFromDate;
195         private DateTime activeToDate;
196         private Long versionNumber;
197 
198         private Builder() {
199 
200         }
201 
202         public static Builder create() {
203             return new Builder();
204         }
205 
206         public static Builder create(DelegateMemberContract contract) {
207             if (contract == null) {
208                 throw new IllegalArgumentException("contract was null");
209             }
210             Builder builder = create();
211             builder.setDelegationMemberId(contract.getDelegationMemberId());
212             builder.setDelegationId(contract.getDelegationId());
213             builder.setMemberId(contract.getMemberId());
214             builder.setRoleMemberId(contract.getRoleMemberId());
215             builder.setAttributes(contract.getAttributes());
216             builder.setType(contract.getType());
217             builder.setActiveFromDate(contract.getActiveFromDate());
218             builder.setActiveToDate(contract.getActiveToDate());
219             builder.setVersionNumber(contract.getVersionNumber());
220             return builder;
221         }
222 
223         public DelegateMember build() {
224             return new DelegateMember(this);
225         }
226 
227         @Override
228         public String getDelegationMemberId() {
229             return this.delegationMemberId;
230         }
231 
232         public void setDelegationMemberId(String delegationMemberId) {
233             if (StringUtils.isWhitespace(delegationMemberId)) {
234                 throw new IllegalArgumentException("delegationMemberId cannot be whitespace");
235             }
236             this.delegationMemberId = delegationMemberId;
237         }
238 
239         public String getDelegationId() {
240             return delegationId;
241         }
242 
243         public void setDelegationId(String delegationId) {
244             this.delegationId = delegationId;
245         }
246 
247         public String getMemberId() {
248             return memberId;
249         }
250 
251         public void setMemberId(String memberId) {
252             this.memberId = memberId;
253         }
254 
255         public String getRoleMemberId() {
256             return roleMemberId;
257         }
258 
259         public void setRoleMemberId(String roleMemberId) {
260             this.roleMemberId = roleMemberId;
261         }
262 
263         public MemberType getType() {
264             return type;
265         }
266 
267         public void setType(MemberType type) {
268             this.type = type;
269         }
270 
271         @Override
272         public Map<String, String> getAttributes() {
273             return this.attributes;
274         }
275 
276         public void setAttributes(Map<String, String> attributes) {
277             this.attributes = attributes;
278         }
279 
280         @Override
281         public Long getVersionNumber() {
282             return versionNumber;
283         }
284 
285         public void setVersionNumber(Long versionNumber) {
286             this.versionNumber = versionNumber;
287         }
288 
289         @Override
290         public DateTime getActiveFromDate() {
291             return activeFromDate;
292         }
293 
294         public void setActiveFromDate(DateTime activeFromDate) {
295             this.activeFromDate = activeFromDate;
296         }
297 
298         @Override
299         public DateTime getActiveToDate() {
300             return activeToDate;
301         }
302 
303         public void setActiveToDate(DateTime activeToDate) {
304             this.activeToDate = activeToDate;
305         }
306 
307         @Override
308         public boolean isActive(DateTime activeAsOfDate) {
309             return InactivatableFromToUtils.isActive(activeFromDate, activeToDate, activeAsOfDate);
310         }
311 
312         @Override
313         public boolean isActive() {
314             return InactivatableFromToUtils.isActive(activeFromDate, activeToDate, null);
315         }
316     }
317 
318     /**
319      * Defines some internal constants used on this class.
320      */
321     static class Constants {
322 
323         final static String ROOT_ELEMENT_NAME = "delegateMember";
324         final static String TYPE_NAME = "DelegateMemberType";
325 
326     }
327 
328     /**
329      * A private class which exposes constants which define the XML element names to use when this object is marshalled to XML.
330      */
331     static class Elements {
332         final static String DELEGATION_MEMBER_ID = "delegationMemberId";
333         final static String ROLE_ID = "roleId";
334         final static String DELEGATION_ID = "delegationId";
335         final static String MEMBER_ID = "memberId";
336         final static String ATTRIBUTES = "attributes";
337         final static String ROLE_MEMBER_ID = "roleMemberId";
338         final static String TYPE_CODE = "typeCode";
339     }
340 
341     public static class Cache {
342         public static final String NAME = KimConstants.Namespaces.KIM_NAMESPACE_2_0 + "/" + DelegateMember.Constants.TYPE_NAME;
343     }
344 }