001    /**
002     * Copyright 2005-2011 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.kim.api.group;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.joda.time.DateTime;
020    import org.kuali.rice.core.api.CoreConstants;
021    import org.kuali.rice.core.api.membership.MemberType;
022    import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
023    import org.kuali.rice.core.api.mo.ModelBuilder;
024    import org.kuali.rice.core.api.mo.common.active.InactivatableFromToUtils;
025    import org.kuali.rice.core.api.util.jaxb.DateTimeAdapter;
026    import org.kuali.rice.kim.api.KimConstants;
027    import org.w3c.dom.Element;
028    
029    import javax.xml.bind.annotation.XmlAccessType;
030    import javax.xml.bind.annotation.XmlAccessorType;
031    import javax.xml.bind.annotation.XmlAnyElement;
032    import javax.xml.bind.annotation.XmlElement;
033    import javax.xml.bind.annotation.XmlRootElement;
034    import javax.xml.bind.annotation.XmlType;
035    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
036    import java.io.Serializable;
037    import java.util.Collection;
038    
039    @XmlRootElement(name = GroupMember.Constants.ROOT_ELEMENT_NAME)
040    @XmlAccessorType(XmlAccessType.NONE)
041    @XmlType(name = GroupMember.Constants.TYPE_NAME, propOrder = {
042            GroupMember.Elements.ID,
043            GroupMember.Elements.GROUP_ID,
044            GroupMember.Elements.MEMBER_ID,
045            GroupMember.Elements.TYPE_CODE,
046            CoreConstants.CommonElements.ACTIVE_FROM_DATE,
047            CoreConstants.CommonElements.ACTIVE_TO_DATE,
048            CoreConstants.CommonElements.VERSION_NUMBER,
049            CoreConstants.CommonElements.OBJECT_ID,
050            CoreConstants.CommonElements.FUTURE_ELEMENTS
051    })
052    public class GroupMember extends AbstractDataTransferObject implements GroupMemberContract  {
053    
054        @XmlElement(name = Elements.ID, required = false)
055        private final String id;
056    
057        @XmlElement(name = Elements.GROUP_ID, required = true)
058        private final String groupId;
059    
060        @XmlElement(name = Elements.MEMBER_ID, required = true)
061        private final String memberId;
062    
063        @XmlElement(name = Elements.TYPE_CODE, required = true)
064        private final String typeCode;
065    
066        @XmlElement(name = CoreConstants.CommonElements.ACTIVE_FROM_DATE, required = false)
067        @XmlJavaTypeAdapter(DateTimeAdapter.class)
068        private final DateTime activeFromDate;
069    
070        @XmlElement(name = CoreConstants.CommonElements.ACTIVE_TO_DATE, required = false)
071        @XmlJavaTypeAdapter(DateTimeAdapter.class)
072            private final DateTime activeToDate;
073    
074        @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
075        private final Long versionNumber;
076    
077        @XmlElement(name = CoreConstants.CommonElements.OBJECT_ID, required = false)
078        private final String objectId;
079    
080        @SuppressWarnings("unused")
081        @XmlAnyElement
082        private final Collection<Element> _futureElements = null;
083    
084        private GroupMember() {
085            this.id = null;
086            this.groupId = null;
087            this.memberId = null;
088            this.typeCode = null;
089            this.versionNumber = null;
090            this.objectId = null;
091            this.activeFromDate = null;
092            this.activeToDate = null;
093        }
094    
095    
096        public GroupMember(Builder builder) {
097            this.id = builder.getId();
098            this.groupId = builder.getGroupId();
099            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    }