001    /**
002     * Copyright 2005-2012 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.core.api.util.jaxb.MapStringStringAdapter;
027    import org.kuali.rice.kim.api.KimConstants;
028    import org.w3c.dom.Element;
029    
030    import javax.xml.bind.annotation.XmlAccessType;
031    import javax.xml.bind.annotation.XmlAccessorType;
032    import javax.xml.bind.annotation.XmlAnyElement;
033    import javax.xml.bind.annotation.XmlElement;
034    import javax.xml.bind.annotation.XmlRootElement;
035    import javax.xml.bind.annotation.XmlType;
036    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
037    import java.io.Serializable;
038    import java.util.Collection;
039    import java.util.Map;
040    
041    @XmlRootElement(name = DelegateMember.Constants.ROOT_ELEMENT_NAME)
042    @XmlAccessorType(XmlAccessType.NONE)
043    @XmlType(name = DelegateMember.Constants.TYPE_NAME, propOrder = {
044            DelegateMember.Elements.DELEGATION_MEMBER_ID,
045            DelegateMember.Elements.DELEGATION_ID,
046            DelegateMember.Elements.MEMBER_ID,
047            DelegateMember.Elements.ROLE_MEMBER_ID,
048            DelegateMember.Elements.TYPE_CODE,
049            DelegateMember.Elements.ATTRIBUTES,
050            CoreConstants.CommonElements.ACTIVE_FROM_DATE,
051            CoreConstants.CommonElements.ACTIVE_TO_DATE,
052            CoreConstants.CommonElements.VERSION_NUMBER,
053            CoreConstants.CommonElements.FUTURE_ELEMENTS
054    })
055    public final class DelegateMember extends AbstractDataTransferObject
056            implements DelegateMemberContract {
057    
058        @XmlElement(name = Elements.DELEGATION_MEMBER_ID, required = true)
059        private final String delegationMemberId;
060    
061        @XmlElement(name = Elements.DELEGATION_ID, required = false)
062        private final String delegationId;
063    
064        @XmlElement(name = Elements.MEMBER_ID, required = false)
065        private final String memberId;
066    
067        @XmlElement(name = Elements.ROLE_MEMBER_ID, required = false)
068        private final String roleMemberId;
069    
070        @XmlElement(name = Elements.TYPE_CODE, required = false)
071        private final String typeCode;
072    
073        @XmlElement(name = Elements.ATTRIBUTES, required = false)
074        @XmlJavaTypeAdapter(value = MapStringStringAdapter.class)
075        private final Map<String, String> attributes;
076    
077        @XmlJavaTypeAdapter(DateTimeAdapter.class)
078        @XmlElement(name = CoreConstants.CommonElements.ACTIVE_FROM_DATE)
079        private final DateTime activeFromDate;
080    
081        @XmlJavaTypeAdapter(DateTimeAdapter.class)
082        @XmlElement(name = CoreConstants.CommonElements.ACTIVE_TO_DATE)
083        private final DateTime activeToDate;
084    
085        @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER)
086        private final Long versionNumber;
087    
088        @SuppressWarnings("unused")
089        @XmlAnyElement
090        private final Collection<Element> _futureElements = null;
091    
092        /**
093         * Private constructor used only by JAXB.
094         */
095        @SuppressWarnings("unused")
096        private DelegateMember() {
097            this.delegationMemberId = null;
098            this.delegationId = null;
099            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            this.typeCode = builder.getType().getCode();
114            this.versionNumber = builder.getVersionNumber();
115            this.activeFromDate = builder.getActiveFromDate();
116            this.activeToDate = builder.getActiveToDate();
117            this.attributes = builder.getAttributes();
118        }
119    
120    
121        @Override
122        public String getDelegationMemberId() {
123            return this.delegationMemberId;
124        }
125    
126        @Override
127        public String getDelegationId() {
128            return this.delegationId;
129        }
130    
131        @Override
132        public MemberType getType() {
133            return MemberType.fromCode(this.typeCode);
134        }
135    
136        @Override
137        public String getRoleMemberId() {
138            return this.roleMemberId;
139        }
140    
141        @Override
142        public String getMemberId() {
143            return this.memberId;
144        }
145    
146        @Override
147        public Long getVersionNumber() {
148            return this.versionNumber;
149        }
150    
151        @Override
152        public DateTime getActiveFromDate() {
153            return activeFromDate;
154        }
155    
156        @Override
157        public DateTime getActiveToDate() {
158            return activeToDate;
159        }
160    
161        /**
162         * @return the attributes
163         */
164        @Override
165        public Map<String, String> getAttributes() {
166            return this.attributes;
167        }
168    
169        @Override
170        public boolean isActive(DateTime activeAsOfDate) {
171            return InactivatableFromToUtils.isActive(activeFromDate, activeToDate, activeAsOfDate);
172        }
173    
174        @Override
175        public boolean isActive() {
176            return InactivatableFromToUtils.isActive(activeFromDate, activeToDate, null);
177        }
178    
179        /**
180         * A builder which can be used to construct {@link DelegateMember} instances.  Enforces the constraints of the {@link DelegateMemberContract}.
181         */
182        public static final class Builder implements Serializable, ModelBuilder, DelegateMemberContract {
183    
184            private String delegationMemberId;
185            private String delegationId;
186            private String memberId;
187            private String roleMemberId;
188            private MemberType type;
189            private Map<String, String> attributes;
190            private DateTime activeFromDate;
191            private DateTime activeToDate;
192            private Long versionNumber;
193    
194            private Builder() {
195    
196            }
197    
198            public static Builder create() {
199                return new Builder();
200            }
201    
202            public static Builder create(DelegateMemberContract contract) {
203                if (contract == null) {
204                    throw new IllegalArgumentException("contract was null");
205                }
206                Builder builder = create();
207                builder.setDelegationMemberId(contract.getDelegationMemberId());
208                builder.setDelegationId(contract.getDelegationId());
209                builder.setMemberId(contract.getMemberId());
210                builder.setRoleMemberId(contract.getRoleMemberId());
211                builder.setAttributes(contract.getAttributes());
212                builder.setType(contract.getType());
213                builder.setActiveFromDate(contract.getActiveFromDate());
214                builder.setActiveToDate(contract.getActiveToDate());
215                builder.setVersionNumber(contract.getVersionNumber());
216                return builder;
217            }
218    
219            public DelegateMember build() {
220                if (StringUtils.isEmpty(this.delegationMemberId) || this.versionNumber == null) {
221                    throw new IllegalStateException("Required fields of delegationMemberId and versionNumber set to valid non-null" +
222                            " values before calling build()");
223                }
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.isEmpty(delegationMemberId)) {
234                    throw new IllegalArgumentException("delegationMemberId is a required field and cannot be null or blank");
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                if (versionNumber == null) {
287                    throw new IllegalArgumentException("versionNumber is required and must be non-null");
288                }
289                this.versionNumber = versionNumber;
290            }
291    
292            @Override
293            public DateTime getActiveFromDate() {
294                return activeFromDate;
295            }
296    
297            public void setActiveFromDate(DateTime activeFromDate) {
298                this.activeFromDate = activeFromDate;
299            }
300    
301            @Override
302            public DateTime getActiveToDate() {
303                return activeToDate;
304            }
305    
306            public void setActiveToDate(DateTime activeToDate) {
307                this.activeToDate = activeToDate;
308            }
309    
310            @Override
311            public boolean isActive(DateTime activeAsOfDate) {
312                return InactivatableFromToUtils.isActive(activeFromDate, activeToDate, activeAsOfDate);
313            }
314    
315            @Override
316            public boolean isActive() {
317                return InactivatableFromToUtils.isActive(activeFromDate, activeToDate, null);
318            }
319        }
320    
321        /**
322         * Defines some internal constants used on this class.
323         */
324        static class Constants {
325    
326            final static String ROOT_ELEMENT_NAME = "delegateMember";
327            final static String TYPE_NAME = "DelegateMemberType";
328    
329        }
330    
331        /**
332         * A private class which exposes constants which define the XML element names to use when this object is marshalled to XML.
333         */
334        static class Elements {
335            final static String DELEGATION_MEMBER_ID = "delegationMemberId";
336            final static String ROLE_ID = "roleId";
337            final static String DELEGATION_ID = "delegationId";
338            final static String MEMBER_ID = "memberId";
339            final static String ATTRIBUTES = "attributes";
340            final static String ROLE_MEMBER_ID = "roleMemberId";
341            final static String TYPE_CODE = "typeCode";
342        }
343    
344        public static class Cache {
345            public static final String NAME = KimConstants.Namespaces.KIM_NAMESPACE_2_0 + "/" + DelegateMember.Constants.TYPE_NAME;
346        }
347    }