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.role;
017    
018    import org.apache.commons.collections.CollectionUtils;
019    import org.apache.commons.lang.StringUtils;
020    import org.apache.commons.lang.builder.EqualsBuilder;
021    import org.apache.commons.lang.builder.HashCodeBuilder;
022    import org.apache.commons.lang.builder.ToStringBuilder;
023    import org.joda.time.DateTime;
024    import org.kuali.rice.core.api.CoreConstants;
025    import org.kuali.rice.core.api.membership.MemberType;
026    import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
027    import org.kuali.rice.core.api.mo.ModelBuilder;
028    import org.kuali.rice.core.api.mo.ModelObjectComplete;
029    import org.kuali.rice.core.api.mo.common.active.InactivatableFromToUtils;
030    import org.kuali.rice.core.api.util.jaxb.DateTimeAdapter;
031    import org.kuali.rice.core.api.util.jaxb.MapStringStringAdapter;
032    import org.kuali.rice.kim.api.KimConstants;
033    import org.w3c.dom.Element;
034    
035    import javax.xml.bind.annotation.XmlAccessType;
036    import javax.xml.bind.annotation.XmlAccessorType;
037    import javax.xml.bind.annotation.XmlAnyElement;
038    import javax.xml.bind.annotation.XmlElement;
039    import javax.xml.bind.annotation.XmlElementWrapper;
040    import javax.xml.bind.annotation.XmlRootElement;
041    import javax.xml.bind.annotation.XmlType;
042    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
043    import java.util.ArrayList;
044    import java.util.Collection;
045    import java.util.List;
046    import java.util.Map;
047    
048    @XmlRootElement(name = RoleMember.Constants.ROOT_ELEMENT_NAME)
049    @XmlAccessorType(XmlAccessType.NONE)
050    @XmlType(name = RoleMember.Constants.TYPE_NAME, propOrder = {
051            RoleMember.Elements.ID,
052            RoleMember.Elements.ROLE_ID,
053            RoleMember.Elements.ATTRIBUTES,
054            RoleMember.Elements.ROLE_RESPONSIBILITY_ACTIONS,
055            RoleMember.Elements.MEMBER_ID,
056            RoleMember.Elements.TYPE_CODE,
057            RoleMember.Elements.MEMBER_NAME,
058            RoleMember.Elements.MEMBER_NAMESPACE_CODE,
059            CoreConstants.CommonElements.ACTIVE_FROM_DATE,
060            CoreConstants.CommonElements.ACTIVE_TO_DATE,
061            CoreConstants.CommonElements.VERSION_NUMBER,
062            CoreConstants.CommonElements.OBJECT_ID,
063            CoreConstants.CommonElements.FUTURE_ELEMENTS
064    })
065    public class RoleMember extends AbstractDataTransferObject implements RoleMemberContract {
066    
067        private static final long serialVersionUID = 1L;
068    
069        @XmlElement(name = Elements.ID, required = false)
070        private final String id;
071    
072        @XmlElement(name = Elements.ROLE_ID, required = true)
073        private final String roleId;
074    
075        @XmlElement(name = Elements.ATTRIBUTES, required = false)
076        @XmlJavaTypeAdapter(value = MapStringStringAdapter.class)
077        private final Map<String, String> attributes;
078    
079        @XmlElementWrapper(name = Elements.ROLE_RESPONSIBILITY_ACTIONS, required = false)
080        @XmlElement(name = Elements.ROLE_RESPONSIBILITY_ACTION, required = false)
081        private final List<RoleResponsibilityAction> roleResponsibilityActions;
082    
083        @XmlElement(name = Elements.MEMBER_ID, required = true)
084        private final String memberId;
085    
086        @XmlElement(name = Elements.TYPE_CODE, required = true)
087        private final String typeCode;
088    
089        @XmlElement(name = Elements.MEMBER_NAME, required = true)
090        private final String memberName;
091    
092         @XmlElement(name = Elements.MEMBER_NAMESPACE_CODE, required = true)
093        private final String memberNamespaceCode;
094    
095        @XmlJavaTypeAdapter(DateTimeAdapter.class)
096        @XmlElement(name = CoreConstants.CommonElements.ACTIVE_FROM_DATE, required = false)
097        private final DateTime activeFromDate;
098    
099        @XmlJavaTypeAdapter(DateTimeAdapter.class)
100        @XmlElement(name = CoreConstants.CommonElements.ACTIVE_TO_DATE, required = false)
101        private final DateTime activeToDate;
102    
103        @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
104        private final Long versionNumber;
105    
106        @XmlElement(name = CoreConstants.CommonElements.OBJECT_ID, required = false)
107        private final String objectId;
108    
109        @SuppressWarnings("unused")
110        @XmlAnyElement
111        private final Collection<Element> _futureElements = null;
112    
113        /**
114         * Private constructor for JAXB
115         */
116        @SuppressWarnings("unused")
117        private RoleMember() {
118            id = null;
119            roleId = null;
120            attributes = null;
121            roleResponsibilityActions = null;
122            memberId = null;
123            typeCode = null;
124            memberName = null;
125            memberNamespaceCode = null;
126            activeFromDate = null;
127            activeToDate = null;
128            this.versionNumber = null;
129            this.objectId = null;
130        }
131    
132        private RoleMember(Builder b) {
133            id = b.getId();
134            roleId = b.getRoleId();
135            attributes = b.getAttributes();
136    
137            List<RoleResponsibilityAction> roleResponsibilityActions = new ArrayList<RoleResponsibilityAction>();
138            if (!CollectionUtils.isEmpty(b.getRoleRspActions())) {
139                for (RoleResponsibilityAction.Builder rraBuilder : b.getRoleRspActions()) {
140                    roleResponsibilityActions.add(rraBuilder.build());
141                }
142            }
143            this.roleResponsibilityActions = roleResponsibilityActions;
144    
145            memberId = b.getMemberId();
146            typeCode = b.getType().getCode();
147            memberName = b.getMemberName();
148            memberNamespaceCode = b.getMemberNamespaceCode();
149            activeFromDate = b.getActiveFromDate();
150            activeToDate = b.getActiveToDate();
151            versionNumber = b.getVersionNumber();
152            objectId = b.getObjectId();
153        }
154    
155        @Override
156        public String getMemberId() {
157            return this.memberId;
158        }
159    
160        @Override
161        public MemberType getType() {
162            return MemberType.fromCode(this.typeCode);
163        }
164    
165        @Override
166        public String getId() {
167            return this.id;
168        }
169    
170        @Override
171        public String getRoleId() {
172            return this.roleId;
173        }
174    
175        /**
176         * @return the attributes
177         */
178        @Override
179        public Map<String, String> getAttributes() {
180            return this.attributes;
181        }
182    
183        /**
184         * @return the roleResponsibilityActions
185         */
186        @Override
187        public List<RoleResponsibilityAction> getRoleRspActions() {
188            return this.roleResponsibilityActions;
189        }
190    
191        @Override
192        public DateTime getActiveFromDate() {
193            return activeFromDate;
194        }
195    
196        @Override
197        public DateTime getActiveToDate() {
198            return activeToDate;
199        }
200    
201        @Override
202        public boolean isActive(DateTime activeAsOfDate) {
203            return InactivatableFromToUtils.isActive(activeFromDate, activeToDate, activeAsOfDate);
204        }
205    
206        @Override
207        public boolean isActive() {
208            return InactivatableFromToUtils.isActive(activeFromDate, activeToDate, null);
209        }
210    
211        @Override
212        public Long getVersionNumber() {
213            return versionNumber;
214        }
215    
216        @Override
217        public String getObjectId() {
218            return objectId;
219        }
220    
221        @Override
222        public String getMemberName() {
223                return memberName;
224            }
225    
226        @Override
227         public String getMemberNamespaceCode() {
228                return memberNamespaceCode;
229            }
230    
231    
232        public static final class Builder implements ModelBuilder, RoleMemberContract, ModelObjectComplete {
233    
234            private String id;
235            private String roleId;
236            private Map<String, String> attributes;
237            private List<RoleResponsibilityAction.Builder> roleRspActions;
238            private String memberId;
239            private MemberType type;
240            private DateTime activeFromDate;
241            private DateTime activeToDate;
242            private Long versionNumber;
243            private String objectId;
244            private String memberName;
245            private String memberNamespaceCode;
246    
247            private Builder(String roleId, String memberId, MemberType type) {
248                setRoleId(roleId);
249                setMemberId(memberId);
250                setType(type);
251            }
252    
253            public static Builder create(String roleId, String id, String memberId,
254                                         MemberType memberType, DateTime activeFromDate, DateTime activeToDate, Map<String, String> attributes,String memberName,String memberNamespaceCode) {
255                Builder b = new Builder(roleId, memberId, memberType);
256                b.setId(id);
257                b.setActiveFromDate(activeFromDate);
258                b.setActiveToDate(activeToDate);
259                b.setAttributes(attributes);
260                b.setMemberName(memberName);
261                b.setMemberNamespaceCode(memberNamespaceCode);
262                return b;
263            }
264    
265            public static Builder create(RoleMemberContract contract) {
266                Builder b = new Builder(contract.getRoleId(), contract.getMemberId(), contract.getType());
267                b.setId(contract.getId());
268                b.setAttributes(contract.getAttributes());
269    
270                List<RoleResponsibilityAction.Builder> rraBuilders = new ArrayList<RoleResponsibilityAction.Builder>();
271                if (!CollectionUtils.isEmpty(contract.getRoleRspActions())) {
272                    for (RoleResponsibilityActionContract rrac : contract.getRoleRspActions()) {
273                        rraBuilders.add(RoleResponsibilityAction.Builder.create(rrac));
274                    }
275                }
276                b.setRoleRspActions(rraBuilders);
277    
278                b.setActiveFromDate(contract.getActiveFromDate());
279                b.setActiveToDate(contract.getActiveToDate());
280                b.setMemberName(contract.getMemberName());
281                b.setMemberNamespaceCode(contract.getMemberNamespaceCode());
282                b.setVersionNumber(contract.getVersionNumber());
283                b.setObjectId(contract.getObjectId());
284                return b;
285            }
286    
287            @Override
288            public RoleMember build() {
289                return new RoleMember(this);
290            }
291    
292            @Override
293            public String getId() {
294                return id;
295            }
296    
297            public void setId(String id) {
298                this.id = id;
299            }
300    
301            @Override
302            public String getRoleId() {
303                return roleId;
304            }
305    
306            public void setRoleId(String roleId) {
307                if (StringUtils.isEmpty(roleId)) {
308                    throw new IllegalArgumentException("roleId is empty");
309                }
310                this.roleId = roleId;
311            }
312    
313            @Override
314            public Map<String, String> getAttributes() {
315                return attributes;
316            }
317    
318            public void setAttributes(Map<String, String> attributes) {
319                this.attributes = attributes;
320            }
321    
322            @Override
323            public List<RoleResponsibilityAction.Builder> getRoleRspActions() {
324                return roleRspActions;
325            }
326    
327            public void setRoleRspActions(List<RoleResponsibilityAction.Builder> roleRspActions) {
328                this.roleRspActions = roleRspActions;
329            }
330    
331            @Override
332            public String getMemberId() {
333                return memberId;
334            }
335    
336            public void setMemberId(String memberId) {
337                if (StringUtils.isBlank(memberId)) {
338                    throw new IllegalArgumentException("memberId may not be null");
339                }
340                this.memberId = memberId;
341            }
342    
343            @Override
344            public MemberType getType() {
345                return type;
346            }
347    
348            @Override
349            public String getMemberName() {
350                return memberName;
351            }
352    
353             public void setMemberName(String memberName){
354                 this.memberName = memberName;
355             }
356            @Override
357            public String getMemberNamespaceCode() {
358                return memberNamespaceCode;
359            }
360    
361            public void setMemberNamespaceCode(String memberNamespaceCode){
362                 this.memberNamespaceCode = memberNamespaceCode;
363            }
364            public void setType(final MemberType type) {
365                if (type == null) {
366                    throw new IllegalArgumentException("type is null");
367                }
368                this.type = type;
369            }
370    
371            @Override
372            public DateTime getActiveFromDate() {
373                return activeFromDate;
374            }
375    
376            public void setActiveFromDate(DateTime activeFromDate) {
377                this.activeFromDate = activeFromDate;
378            }
379    
380            @Override
381            public DateTime getActiveToDate() {
382                return activeToDate;
383            }
384    
385            public void setActiveToDate(DateTime activeToDate) {
386                this.activeToDate = activeToDate;
387            }
388    
389            @Override
390            public boolean isActive(DateTime activeAsOfDate) {
391                return InactivatableFromToUtils.isActive(activeFromDate, activeToDate, activeAsOfDate);
392            }
393    
394            @Override
395            public boolean isActive() {
396                return InactivatableFromToUtils.isActive(activeFromDate, activeToDate, null);
397            }
398    
399            @Override
400            public Long getVersionNumber() {
401                return versionNumber;
402            }
403    
404            public void setVersionNumber(final Long versionNumber) {
405                this.versionNumber = versionNumber;
406            }
407    
408            @Override
409            public String getObjectId() {
410                return objectId;
411            }
412    
413            public void setObjectId(final String objectId) {
414                this.objectId = objectId;
415            }
416    
417            @Override
418            public int hashCode() {
419                return HashCodeBuilder.reflectionHashCode(this);
420            }
421    
422            @Override
423            public boolean equals(Object obj) {
424                return EqualsBuilder.reflectionEquals(obj, this);
425            }
426    
427            @Override
428            public String toString() {
429                return ToStringBuilder.reflectionToString(this);
430            }
431        }
432    
433        /**
434         * A private class which exposes constants which define the XML element names to use
435         * when this object is marshalled to XML.
436         */
437        static class Elements {
438            final static String ID = "id";
439            final static String ROLE_ID = "roleId";
440            final static String ATTRIBUTES = "attributes";
441            final static String ROLE_RESPONSIBILITY_ACTIONS = "roleResponsibilityActions";
442            final static String ROLE_RESPONSIBILITY_ACTION = "roleResponsibilityAction";
443            final static String MEMBER_ID = "memberId";
444            final static String TYPE_CODE = "typeCode";
445            final static String MEMBER_NAME = "memberName";
446            final static String MEMBER_NAMESPACE_CODE = "memberNamespaceCode";
447        }
448    
449        /**
450         * Defines some internal constants used on this class.
451         */
452        static class Constants {
453            final static String ROOT_ELEMENT_NAME = "roleMember";
454            final static String TYPE_NAME = "RoleMemberType";
455        }
456    
457        public static class Cache {
458            public static final String NAME = KimConstants.Namespaces.KIM_NAMESPACE_2_0 + "/" + RoleMember.Constants.TYPE_NAME;
459        }
460    }