001/**
002 * Copyright 2005-2014 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 */
016package org.kuali.rice.kim.api.group;
017
018import org.apache.commons.lang.StringUtils;
019import org.joda.time.DateTime;
020import org.kuali.rice.core.api.CoreConstants;
021import org.kuali.rice.core.api.membership.MemberType;
022import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
023import org.kuali.rice.core.api.mo.ModelBuilder;
024import org.kuali.rice.core.api.mo.common.active.InactivatableFromToUtils;
025import org.kuali.rice.core.api.util.jaxb.DateTimeAdapter;
026import org.kuali.rice.kim.api.KimConstants;
027import org.w3c.dom.Element;
028
029import javax.xml.bind.annotation.XmlAccessType;
030import javax.xml.bind.annotation.XmlAccessorType;
031import javax.xml.bind.annotation.XmlAnyElement;
032import javax.xml.bind.annotation.XmlElement;
033import javax.xml.bind.annotation.XmlRootElement;
034import javax.xml.bind.annotation.XmlType;
035import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
036import java.io.Serializable;
037import 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})
052public 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    @Override
108    public String getId() {
109        return id;
110    }
111
112    @Override
113    public String getGroupId() {
114        return groupId;
115    }
116
117    @Override
118    public String getMemberId() {
119        return memberId;
120    }
121
122    @Override
123    public MemberType getType() {
124        return MemberType.fromCode(typeCode);
125    }
126
127    @Override
128    public DateTime getActiveFromDate() {
129        return activeFromDate;
130    }
131
132    @Override
133    public DateTime getActiveToDate() {
134        return activeToDate;
135    }
136
137    @Override
138    public Long getVersionNumber() {
139        return versionNumber;
140    }
141
142    @Override
143    public String getObjectId() {
144        return objectId;
145    }
146
147    @Override
148    public boolean isActive(DateTime activeAsOf) {
149        return InactivatableFromToUtils.isActive(activeFromDate, activeToDate, activeAsOf);
150    }
151
152    @Override
153    public boolean isActive() {
154        return InactivatableFromToUtils.isActive(activeFromDate, activeToDate, null);
155    }
156
157    public static class Builder implements GroupMemberContract, ModelBuilder, Serializable {
158        private String id;
159        private String groupId;
160        private String memberId;
161        private MemberType type;
162        private DateTime activeFromDate;
163        private DateTime activeToDate;
164        private Long versionNumber;
165        private String objectId;
166
167        private Builder(String groupId, String memberId, MemberType type) {
168            setGroupId(groupId);
169            setMemberId(memberId);
170            setType(type);
171        }
172
173        /**
174         * creates a Parameter with the required fields.
175         */
176        public static Builder create(String groupId, String memberId, MemberType type) {
177            return new Builder(groupId, memberId, type);
178        }
179
180        /**
181         * creates a GroupMember from an existing {@link org.kuali.rice.kim.api.group.GroupMemberContract}.
182         */
183        public static Builder create(GroupMemberContract contract) {
184            if (contract == null) {
185                throw new IllegalArgumentException("contract was null");
186            }
187            Builder builder = new Builder(contract.getGroupId(), contract.getMemberId(), contract.getType());
188            builder.setId(contract.getId());
189            builder.setActiveFromDate(contract.getActiveFromDate());
190            builder.setActiveToDate(contract.getActiveToDate());
191            builder.setVersionNumber(contract.getVersionNumber());
192            builder.setObjectId(contract.getObjectId());
193            return builder;
194        }
195
196        @Override
197        public String getId() {
198            return id;
199        }
200
201        public void setId(final String id) {
202            if (StringUtils.isWhitespace(id)) {
203                throw new IllegalArgumentException("id is blank");
204            }
205            this.id = id;
206        }
207
208        @Override
209        public String getGroupId() {
210            return groupId;
211        }
212
213        public void setGroupId(final String groupId) {
214            if (StringUtils.isEmpty(groupId)) {
215                throw new IllegalArgumentException("groupId is empty");
216            }
217            this.groupId = groupId;
218        }
219
220        @Override
221        public String getMemberId() {
222            return memberId;
223        }
224
225        public void setMemberId(final String memberId) {
226            if (StringUtils.isEmpty(memberId)) {
227                throw new IllegalArgumentException("memberId is empty");
228            }
229            this.memberId = memberId;
230        }
231
232        @Override
233        public MemberType getType() {
234            return type;
235        }
236
237        public void setType(final MemberType type) {
238            if (type == null) {
239                throw new IllegalArgumentException("type is null");
240            }
241            this.type = type;
242        }
243
244        @Override
245        public DateTime getActiveFromDate() {
246            return activeFromDate;
247        }
248
249        public void setActiveFromDate(final DateTime activeFromDate) {
250            this.activeFromDate = activeFromDate;
251        }
252
253        @Override
254        public DateTime getActiveToDate() {
255            return activeToDate;
256        }
257
258        public void setActiveToDate(final DateTime activeToDate) {
259            this.activeToDate = activeToDate;
260        }
261
262        @Override
263        public Long getVersionNumber() {
264            return versionNumber;
265        }
266
267        public void setVersionNumber(final Long versionNumber) {
268            this.versionNumber = versionNumber;
269        }
270
271        @Override
272        public String getObjectId() {
273            return objectId;
274        }
275
276        public void setObjectId(final String objectId) {
277            this.objectId = objectId;
278        }
279
280        @Override
281        public boolean isActive(DateTime activeAsOf) {
282            return InactivatableFromToUtils.isActive(activeFromDate, activeToDate, activeAsOf);
283        }
284
285        @Override
286        public boolean isActive() {
287            return InactivatableFromToUtils.isActive(activeFromDate, activeToDate, null);
288        }
289
290        @Override
291        public GroupMember build() {
292            return new GroupMember(this);
293        }
294    }
295
296    /**
297     * Defines some internal constants used on this class.
298     */
299    static class Constants {
300        final static String ROOT_ELEMENT_NAME = "groupMember";
301        final static String TYPE_NAME = "GroupMemberType";
302    }
303
304    /**
305     * A private class which exposes constants which define the XML element names to use
306     * when this object is marshalled to XML.
307     */
308    static class Elements {
309        final static String ID = "id";
310        final static String GROUP_ID = "groupId";
311        final static String MEMBER_ID = "memberId";
312        final static String TYPE_CODE = "typeCode";
313    }
314
315    public static class Cache {
316        public static final String NAME = KimConstants.Namespaces.KIM_NAMESPACE_2_0 + "/" + GroupMember.Constants.TYPE_NAME;
317    }
318}