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.common.delegate;
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 = DelegateMember.Constants.ROOT_ELEMENT_NAME)
040    @XmlAccessorType(XmlAccessType.NONE)
041    @XmlType(name = DelegateMember.Constants.TYPE_NAME, propOrder = {
042            DelegateMember.Elements.DELEGATION_MEMBER_ID,
043            DelegateMember.Elements.DELEGATION_ID,
044            DelegateMember.Elements.MEMBER_ID,
045            DelegateMember.Elements.ROLE_MEMBER_ID,
046            DelegateMember.Elements.TYPE_CODE,
047            CoreConstants.CommonElements.ACTIVE_FROM_DATE,
048            CoreConstants.CommonElements.ACTIVE_TO_DATE,
049            CoreConstants.CommonElements.VERSION_NUMBER,
050            CoreConstants.CommonElements.FUTURE_ELEMENTS
051    })
052    public final class DelegateMember extends AbstractDataTransferObject
053            implements DelegateMemberContract {
054    
055        @XmlElement(name = Elements.DELEGATION_MEMBER_ID, required = true)
056        private final String delegationMemberId;
057    
058        @XmlElement(name = Elements.DELEGATION_ID, required = false)
059        private final String delegationId;
060    
061        @XmlElement(name = Elements.MEMBER_ID, required = false)
062        private final String memberId;
063    
064        @XmlElement(name = Elements.ROLE_MEMBER_ID, required = false)
065        private final String roleMemberId;
066    
067        @XmlElement(name = Elements.TYPE_CODE, required = false)
068        private final String typeCode;
069    
070        @XmlJavaTypeAdapter(DateTimeAdapter.class)
071        @XmlElement(name = CoreConstants.CommonElements.ACTIVE_FROM_DATE)
072        private final DateTime activeFromDate;
073    
074        @XmlJavaTypeAdapter(DateTimeAdapter.class)
075        @XmlElement(name = CoreConstants.CommonElements.ACTIVE_TO_DATE)
076        private final DateTime activeToDate;
077    
078        @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER)
079        private final Long versionNumber;
080    
081        @SuppressWarnings("unused")
082        @XmlAnyElement
083        private final Collection<Element> _futureElements = null;
084    
085        /**
086         * Private constructor used only by JAXB.
087         */
088        @SuppressWarnings("unused")
089        private DelegateMember() {
090            this.delegationMemberId = null;
091            this.delegationId = null;
092            this.memberId = null;
093            this.roleMemberId = null;
094            this.typeCode = null;
095            this.versionNumber = null;
096            this.activeFromDate = null;
097            this.activeToDate = null;
098        }
099    
100        private DelegateMember(Builder builder) {
101            this.delegationMemberId = builder.getDelegationMemberId();
102            this.delegationId = builder.getDelegationId();
103            this.memberId = builder.getMemberId();
104            this.roleMemberId = builder.getRoleMemberId();
105            this.typeCode = builder.getType().getCode();
106            this.versionNumber = builder.getVersionNumber();
107            this.activeFromDate = builder.getActiveFromDate();
108            this.activeToDate = builder.getActiveToDate();
109        }
110    
111    
112        @Override
113        public String getDelegationMemberId() {
114            return this.delegationMemberId;
115        }
116    
117        @Override
118        public String getDelegationId() {
119            return this.delegationId;
120        }
121    
122        @Override
123        public MemberType getType() {
124            return MemberType.fromCode(this.typeCode);
125        }
126    
127        @Override
128        public String getRoleMemberId() {
129            return this.roleMemberId;
130        }
131    
132        @Override
133        public String getMemberId() {
134            return this.memberId;
135        }
136    
137        @Override
138        public Long getVersionNumber() {
139            return this.versionNumber;
140        }
141    
142        @Override
143        public DateTime getActiveFromDate() {
144            return activeFromDate;
145        }
146    
147        @Override
148        public DateTime getActiveToDate() {
149            return activeToDate;
150        }
151    
152        @Override
153        public boolean isActive(DateTime activeAsOfDate) {
154            return InactivatableFromToUtils.isActive(activeFromDate, activeToDate, activeAsOfDate);
155        }
156    
157        /**
158         * A builder which can be used to construct {@link DelegateMember} instances.  Enforces the constraints of the {@link DelegateMemberContract}.
159         */
160        public static final class Builder implements Serializable, ModelBuilder, DelegateMemberContract {
161    
162            private String delegationMemberId;
163            private String delegationId;
164            private String memberId;
165            private String roleMemberId;
166            private MemberType type;
167            private DateTime activeFromDate;
168            private DateTime activeToDate;
169            private Long versionNumber;
170    
171            private Builder() {
172    
173            }
174    
175            public static Builder create() {
176                return new Builder();
177            }
178    
179            public static Builder create(DelegateMemberContract contract) {
180                if (contract == null) {
181                    throw new IllegalArgumentException("contract was null");
182                }
183                Builder builder = create();
184                builder.setDelegationMemberId(contract.getDelegationMemberId());
185                builder.setDelegationId(contract.getDelegationId());
186                builder.setMemberId(contract.getMemberId());
187                builder.setRoleMemberId(contract.getRoleMemberId());
188                builder.setType(contract.getType());
189                builder.setActiveFromDate(contract.getActiveFromDate());
190                builder.setActiveToDate(contract.getActiveToDate());
191                builder.setVersionNumber(contract.getVersionNumber());
192                return builder;
193            }
194    
195            public DelegateMember build() {
196                if (StringUtils.isEmpty(this.delegationMemberId) || this.versionNumber == null) {
197                    throw new IllegalStateException("Required fields of delegationMemberId and versionNumber set to valid non-null" +
198                            " values before calling build()");
199                }
200                return new DelegateMember(this);
201            }
202    
203            @Override
204            public String getDelegationMemberId() {
205                return this.delegationMemberId;
206            }
207    
208            public void setDelegationMemberId(String delegationMemberId) {
209                if (StringUtils.isEmpty(delegationMemberId)) {
210                    throw new IllegalArgumentException("delegationMemberId is a required field and cannot be null or blank");
211                }
212                this.delegationMemberId = delegationMemberId;
213            }
214    
215            public String getDelegationId() {
216                return delegationId;
217            }
218    
219            public void setDelegationId(String delegationId) {
220                this.delegationId = delegationId;
221            }
222    
223            public String getMemberId() {
224                return memberId;
225            }
226    
227            public void setMemberId(String memberId) {
228                this.memberId = memberId;
229            }
230    
231            public String getRoleMemberId() {
232                return roleMemberId;
233            }
234    
235            public void setRoleMemberId(String roleMemberId) {
236                this.roleMemberId = roleMemberId;
237            }
238    
239            public MemberType getType() {
240                return type;
241            }
242    
243            public void setType(MemberType type) {
244                this.type = type;
245            }
246    
247            @Override
248            public Long getVersionNumber() {
249                return versionNumber;
250            }
251    
252            public void setVersionNumber(Long versionNumber) {
253                if (versionNumber == null) {
254                    throw new IllegalArgumentException("versionNumber is required and must be non-null");
255                }
256                this.versionNumber = versionNumber;
257            }
258    
259            @Override
260            public DateTime getActiveFromDate() {
261                return activeFromDate;
262            }
263    
264            public void setActiveFromDate(DateTime activeFromDate) {
265                this.activeFromDate = activeFromDate;
266            }
267    
268            @Override
269            public DateTime getActiveToDate() {
270                return activeToDate;
271            }
272    
273            public void setActiveToDate(DateTime activeToDate) {
274                this.activeToDate = activeToDate;
275            }
276    
277            @Override
278            public boolean isActive(DateTime activeAsOfDate) {
279                return InactivatableFromToUtils.isActive(activeFromDate, activeToDate, activeAsOfDate);
280            }
281        }
282    
283        /**
284         * Defines some internal constants used on this class.
285         */
286        static class Constants {
287    
288            final static String ROOT_ELEMENT_NAME = "delegateMember";
289            final static String TYPE_NAME = "DelegateMemberType";
290    
291        }
292    
293        /**
294         * A private class which exposes constants which define the XML element names to use when this object is marshalled to XML.
295         */
296        static class Elements {
297            final static String DELEGATION_MEMBER_ID = "delegationMemberId";
298            final static String ROLE_ID = "roleId";
299            final static String DELEGATION_ID = "delegationId";
300            final static String MEMBER_ID = "memberId";
301            final static String ROLE_MEMBER_ID = "roleMemberId";
302            final static String TYPE_CODE = "typeCode";
303        }
304    
305        public static class Cache {
306            public static final String NAME = KimConstants.Namespaces.KIM_NAMESPACE_2_0 + "/" + DelegateMember.Constants.TYPE_NAME;
307        }
308    }