001/**
002 * Copyright 2005-2015 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.common.delegate;
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.core.api.util.jaxb.MapStringStringAdapter;
027import org.kuali.rice.kim.api.KimConstants;
028import org.w3c.dom.Element;
029
030import javax.xml.bind.annotation.XmlAccessType;
031import javax.xml.bind.annotation.XmlAccessorType;
032import javax.xml.bind.annotation.XmlAnyElement;
033import javax.xml.bind.annotation.XmlElement;
034import javax.xml.bind.annotation.XmlRootElement;
035import javax.xml.bind.annotation.XmlType;
036import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
037import java.io.Serializable;
038import java.util.Collection;
039import 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})
055public final class DelegateMember extends AbstractDataTransferObject
056        implements DelegateMemberContract {
057
058    @XmlElement(name = Elements.DELEGATION_MEMBER_ID, required = false)
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        if (builder.getType() == null) {
114            this.typeCode = null;
115        } else {
116            this.typeCode = builder.getType().getCode();
117        }
118        this.versionNumber = builder.getVersionNumber();
119        this.activeFromDate = builder.getActiveFromDate();
120        this.activeToDate = builder.getActiveToDate();
121        this.attributes = builder.getAttributes();
122    }
123
124
125    @Override
126    public String getDelegationMemberId() {
127        return this.delegationMemberId;
128    }
129
130    @Override
131    public String getDelegationId() {
132        return this.delegationId;
133    }
134
135    @Override
136    public MemberType getType() {
137        return MemberType.fromCode(this.typeCode);
138    }
139
140    @Override
141    public String getRoleMemberId() {
142        return this.roleMemberId;
143    }
144
145    @Override
146    public String getMemberId() {
147        return this.memberId;
148    }
149
150    @Override
151    public Long getVersionNumber() {
152        return this.versionNumber;
153    }
154
155    @Override
156    public DateTime getActiveFromDate() {
157        return activeFromDate;
158    }
159
160    @Override
161    public DateTime getActiveToDate() {
162        return activeToDate;
163    }
164
165    /**
166     * @return the attributes
167     */
168    @Override
169    public Map<String, String> getAttributes() {
170        return this.attributes;
171    }
172
173    @Override
174    public boolean isActive(DateTime activeAsOfDate) {
175        return InactivatableFromToUtils.isActive(activeFromDate, activeToDate, activeAsOfDate);
176    }
177
178    @Override
179    public boolean isActive() {
180        return InactivatableFromToUtils.isActive(activeFromDate, activeToDate, null);
181    }
182
183    /**
184     * A builder which can be used to construct {@link DelegateMember} instances.  Enforces the constraints of the {@link DelegateMemberContract}.
185     */
186    public static final class Builder implements Serializable, ModelBuilder, DelegateMemberContract {
187
188        private String delegationMemberId;
189        private String delegationId;
190        private String memberId;
191        private String roleMemberId;
192        private MemberType type;
193        private Map<String, String> attributes;
194        private DateTime activeFromDate;
195        private DateTime activeToDate;
196        private Long versionNumber;
197
198        private Builder() {
199
200        }
201
202        public static Builder create() {
203            return new Builder();
204        }
205
206        public static Builder create(DelegateMemberContract contract) {
207            if (contract == null) {
208                throw new IllegalArgumentException("contract was null");
209            }
210            Builder builder = create();
211            builder.setDelegationMemberId(contract.getDelegationMemberId());
212            builder.setDelegationId(contract.getDelegationId());
213            builder.setMemberId(contract.getMemberId());
214            builder.setRoleMemberId(contract.getRoleMemberId());
215            builder.setAttributes(contract.getAttributes());
216            builder.setType(contract.getType());
217            builder.setActiveFromDate(contract.getActiveFromDate());
218            builder.setActiveToDate(contract.getActiveToDate());
219            builder.setVersionNumber(contract.getVersionNumber());
220            return builder;
221        }
222
223        public DelegateMember build() {
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.isWhitespace(delegationMemberId)) {
234                throw new IllegalArgumentException("delegationMemberId cannot be whitespace");
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            this.versionNumber = versionNumber;
287        }
288
289        @Override
290        public DateTime getActiveFromDate() {
291            return activeFromDate;
292        }
293
294        public void setActiveFromDate(DateTime activeFromDate) {
295            this.activeFromDate = activeFromDate;
296        }
297
298        @Override
299        public DateTime getActiveToDate() {
300            return activeToDate;
301        }
302
303        public void setActiveToDate(DateTime activeToDate) {
304            this.activeToDate = activeToDate;
305        }
306
307        @Override
308        public boolean isActive(DateTime activeAsOfDate) {
309            return InactivatableFromToUtils.isActive(activeFromDate, activeToDate, activeAsOfDate);
310        }
311
312        @Override
313        public boolean isActive() {
314            return InactivatableFromToUtils.isActive(activeFromDate, activeToDate, null);
315        }
316    }
317
318    /**
319     * Defines some internal constants used on this class.
320     */
321    static class Constants {
322
323        final static String ROOT_ELEMENT_NAME = "delegateMember";
324        final static String TYPE_NAME = "DelegateMemberType";
325
326    }
327
328    /**
329     * A private class which exposes constants which define the XML element names to use when this object is marshalled to XML.
330     */
331    static class Elements {
332        final static String DELEGATION_MEMBER_ID = "delegationMemberId";
333        final static String ROLE_ID = "roleId";
334        final static String DELEGATION_ID = "delegationId";
335        final static String MEMBER_ID = "memberId";
336        final static String ATTRIBUTES = "attributes";
337        final static String ROLE_MEMBER_ID = "roleMemberId";
338        final static String TYPE_CODE = "typeCode";
339    }
340
341    public static class Cache {
342        public static final String NAME = KimConstants.Namespaces.KIM_NAMESPACE_2_0 + "/" + DelegateMember.Constants.TYPE_NAME;
343    }
344}