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.group;
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.kim.api.KimConstants;
27  import org.w3c.dom.Element;
28  
29  import javax.xml.bind.annotation.XmlAccessType;
30  import javax.xml.bind.annotation.XmlAccessorType;
31  import javax.xml.bind.annotation.XmlAnyElement;
32  import javax.xml.bind.annotation.XmlElement;
33  import javax.xml.bind.annotation.XmlRootElement;
34  import javax.xml.bind.annotation.XmlType;
35  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
36  import java.io.Serializable;
37  import java.util.Collection;
38  
39  @XmlRootElement(name = GroupMember.Constants.ROOT_ELEMENT_NAME)
40  @XmlAccessorType(XmlAccessType.NONE)
41  @XmlType(name = GroupMember.Constants.TYPE_NAME, propOrder = {
42          GroupMember.Elements.ID,
43          GroupMember.Elements.GROUP_ID,
44          GroupMember.Elements.MEMBER_ID,
45          GroupMember.Elements.TYPE_CODE,
46          CoreConstants.CommonElements.ACTIVE_FROM_DATE,
47          CoreConstants.CommonElements.ACTIVE_TO_DATE,
48          CoreConstants.CommonElements.VERSION_NUMBER,
49          CoreConstants.CommonElements.OBJECT_ID,
50          CoreConstants.CommonElements.FUTURE_ELEMENTS
51  })
52  public class GroupMember extends AbstractDataTransferObject implements GroupMemberContract  {
53  
54      @XmlElement(name = Elements.ID, required = false)
55      private final String id;
56  
57      @XmlElement(name = Elements.GROUP_ID, required = true)
58      private final String groupId;
59  
60      @XmlElement(name = Elements.MEMBER_ID, required = true)
61      private final String memberId;
62  
63      @XmlElement(name = Elements.TYPE_CODE, required = true)
64      private final String typeCode;
65  
66      @XmlElement(name = CoreConstants.CommonElements.ACTIVE_FROM_DATE, required = false)
67      @XmlJavaTypeAdapter(DateTimeAdapter.class)
68      private final DateTime activeFromDate;
69  
70      @XmlElement(name = CoreConstants.CommonElements.ACTIVE_TO_DATE, required = false)
71      @XmlJavaTypeAdapter(DateTimeAdapter.class)
72  	private final DateTime activeToDate;
73  
74      @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
75      private final Long versionNumber;
76  
77      @XmlElement(name = CoreConstants.CommonElements.OBJECT_ID, required = false)
78      private final String objectId;
79  
80      @SuppressWarnings("unused")
81      @XmlAnyElement
82      private final Collection<Element> _futureElements = null;
83  
84      private GroupMember() {
85          this.id = null;
86          this.groupId = null;
87          this.memberId = null;
88          this.typeCode = null;
89          this.versionNumber = null;
90          this.objectId = null;
91          this.activeFromDate = null;
92          this.activeToDate = null;
93      }
94  
95  
96      public GroupMember(Builder builder) {
97          this.id = builder.getId();
98          this.groupId = builder.getGroupId();
99          this.memberId = builder.getMemberId();
100         this.typeCode = builder.getType().getCode();
101         this.versionNumber = builder.getVersionNumber();
102         this.objectId = builder.getObjectId();
103         this.activeFromDate = builder.getActiveFromDate();
104         this.activeToDate = builder.getActiveToDate();
105     }
106 
107     public String getId() {
108         return id;
109     }
110 
111     public String getGroupId() {
112         return groupId;
113     }
114 
115     public String getMemberId() {
116         return memberId;
117     }
118 
119     public MemberType getType() {
120         return MemberType.fromCode(typeCode);
121     }
122 
123     public DateTime getActiveFromDate() {
124         return activeFromDate;
125     }
126 
127     public DateTime getActiveToDate() {
128         return activeToDate;
129     }
130 
131     public Long getVersionNumber() {
132         return versionNumber;
133     }
134 
135     public String getObjectId() {
136         return objectId;
137     }
138 
139     @Override
140     public boolean isActive(DateTime activeAsOf) {
141         return InactivatableFromToUtils.isActive(activeFromDate, activeToDate, activeAsOf);
142     }
143 
144     public static class Builder implements GroupMemberContract, ModelBuilder, Serializable {
145         private String id;
146         private String groupId;
147         private String memberId;
148         private MemberType type;
149         private DateTime activeFromDate;
150         private DateTime activeToDate;
151         private Long versionNumber;
152         private String objectId;
153 
154         private Builder(String groupId, String memberId, MemberType type) {
155             setGroupId(groupId);
156             setMemberId(memberId);
157             setType(type);
158         }
159 
160         /**
161          * creates a Parameter with the required fields.
162          */
163         public static Builder create(String groupId, String memberId, MemberType type) {
164             return new Builder(groupId, memberId, type);
165         }
166 
167         /**
168          * creates a GroupMember from an existing {@link org.kuali.rice.kim.api.group.GroupMemberContract}.
169          */
170         public static Builder create(GroupMemberContract contract) {
171             if (contract == null) {
172                 throw new IllegalArgumentException("contract was null");
173             }
174             Builder builder = new Builder(contract.getGroupId(), contract.getMemberId(), contract.getType());
175             builder.setId(contract.getId());
176             builder.setActiveFromDate(contract.getActiveFromDate());
177             builder.setActiveToDate(contract.getActiveToDate());
178             builder.setVersionNumber(contract.getVersionNumber());
179             builder.setObjectId(contract.getObjectId());
180             return builder;
181         }
182 
183         @Override
184         public String getId() {
185             return id;
186         }
187 
188         public void setId(final String id) {
189             if (StringUtils.isWhitespace(id)) {
190                 throw new IllegalArgumentException("id is blank");
191             }
192             this.id = id;
193         }
194 
195         @Override
196         public String getGroupId() {
197             return groupId;
198         }
199 
200         public void setGroupId(final String groupId) {
201             if (StringUtils.isEmpty(groupId)) {
202                 throw new IllegalArgumentException("groupId is empty");
203             }
204             this.groupId = groupId;
205         }
206 
207         @Override
208         public String getMemberId() {
209             return memberId;
210         }
211 
212         public void setMemberId(final String memberId) {
213             if (StringUtils.isEmpty(memberId)) {
214                 throw new IllegalArgumentException("memberId is empty");
215             }
216             this.memberId = memberId;
217         }
218 
219         @Override
220         public MemberType getType() {
221             return type;
222         }
223 
224         public void setType(final MemberType type) {
225             if (type == null) {
226                 throw new IllegalArgumentException("type is null");
227             }
228             this.type = type;
229         }
230 
231         @Override
232         public DateTime getActiveFromDate() {
233             return activeFromDate;
234         }
235 
236         public void setActiveFromDate(final DateTime activeFromDate) {
237             this.activeFromDate = activeFromDate;
238         }
239 
240         @Override
241         public DateTime getActiveToDate() {
242             return activeToDate;
243         }
244 
245         public void setActiveToDate(final DateTime activeToDate) {
246             this.activeToDate = activeToDate;
247         }
248 
249         @Override
250         public Long getVersionNumber() {
251             return versionNumber;
252         }
253 
254         public void setVersionNumber(final Long versionNumber) {
255             this.versionNumber = versionNumber;
256         }
257 
258         @Override
259         public String getObjectId() {
260             return objectId;
261         }
262 
263         public void setObjectId(final String objectId) {
264             this.objectId = objectId;
265         }
266 
267         @Override
268         public boolean isActive(DateTime activeAsOf) {
269             return InactivatableFromToUtils.isActive(activeFromDate, activeToDate, activeAsOf);
270         }
271 
272         @Override
273         public GroupMember build() {
274             return new GroupMember(this);
275         }
276     }
277 
278     /**
279      * Defines some internal constants used on this class.
280      */
281     static class Constants {
282         final static String ROOT_ELEMENT_NAME = "groupMember";
283         final static String TYPE_NAME = "GroupMemberType";
284     }
285 
286     /**
287      * A private class which exposes constants which define the XML element names to use
288      * when this object is marshalled to XML.
289      */
290     static class Elements {
291         final static String ID = "id";
292         final static String GROUP_ID = "groupId";
293         final static String MEMBER_ID = "memberId";
294         final static String TYPE_CODE = "typeCode";
295     }
296 
297     public static class Cache {
298         public static final String NAME = KimConstants.Namespaces.KIM_NAMESPACE_2_0 + "/" + GroupMember.Constants.TYPE_NAME;
299     }
300 }