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.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.XmlRootElement;
040    import javax.xml.bind.annotation.XmlType;
041    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
042    import java.util.ArrayList;
043    import java.util.Collection;
044    import java.util.List;
045    import java.util.Map;
046    
047    @XmlRootElement(name = RoleMember.Constants.ROOT_ELEMENT_NAME)
048    @XmlAccessorType(XmlAccessType.NONE)
049    @XmlType(name = RoleMember.Constants.TYPE_NAME, propOrder = {
050            RoleMember.Elements.ROLE_MEMBER_ID,
051            RoleMember.Elements.ROLE_ID,
052            RoleMember.Elements.ATTRIBUTES,
053            RoleMember.Elements.ROLE_RESPONSIBILITY_ACTIONS,
054            RoleMember.Elements.MEMBER_ID,
055            RoleMember.Elements.MEMBER_TYPE_CODE,
056            CoreConstants.CommonElements.ACTIVE_FROM_DATE,
057            CoreConstants.CommonElements.ACTIVE_TO_DATE,
058            CoreConstants.CommonElements.FUTURE_ELEMENTS
059    })
060    public class RoleMember extends AbstractDataTransferObject implements RoleMemberContract {
061    
062        private static final long serialVersionUID = 1L;
063    
064        @XmlElement(name = Elements.ROLE_MEMBER_ID)
065        private final String roleMemberId;
066    
067        @XmlElement(name = Elements.ROLE_ID)
068        private final String roleId;
069    
070        @XmlElement(name = Elements.ATTRIBUTES, required = false)
071        @XmlJavaTypeAdapter(value = MapStringStringAdapter.class)
072        private final Map<String, String> attributes;
073    
074        @XmlElement(name = Elements.ROLE_RESPONSIBILITY_ACTIONS)
075        private final List<RoleResponsibilityAction> roleResponsibilityActions;
076    
077        @XmlElement(name = Elements.MEMBER_ID)
078        private final String memberId;
079    
080        @XmlElement(name = Elements.MEMBER_TYPE_CODE)
081        private final String memberTypeCode;
082    
083        @XmlJavaTypeAdapter(DateTimeAdapter.class)
084        @XmlElement(name = CoreConstants.CommonElements.ACTIVE_FROM_DATE)
085        private final DateTime activeFromDate;
086    
087        @XmlJavaTypeAdapter(DateTimeAdapter.class)
088        @XmlElement(name = CoreConstants.CommonElements.ACTIVE_TO_DATE)
089        private final DateTime activeToDate;
090    
091        @SuppressWarnings("unused")
092        @XmlAnyElement
093        private final Collection<Element> _futureElements = null;
094    
095        /**
096         * Private constructor for JAXB
097         */
098        @SuppressWarnings("unused")
099        private RoleMember() {
100            roleMemberId = null;
101            roleId = null;
102            attributes = null;
103            roleResponsibilityActions = null;
104            memberId = null;
105            memberTypeCode = null;
106            activeFromDate = null;
107            activeToDate = null;
108        }
109    
110        private RoleMember(Builder b) {
111            roleMemberId = b.getRoleMemberId();
112            roleId = b.getRoleId();
113            attributes = b.getAttributes();
114    
115            List<RoleResponsibilityAction> roleResponsibilityActions = new ArrayList<RoleResponsibilityAction>();
116            if (!CollectionUtils.isEmpty(b.getRoleRspActions())) {
117                for (RoleResponsibilityAction.Builder rraBuilder : b.getRoleRspActions()) {
118                    roleResponsibilityActions.add(rraBuilder.build());
119                }
120            }
121            this.roleResponsibilityActions = roleResponsibilityActions;
122    
123            memberId = b.getMemberId();
124            memberTypeCode = b.getMemberType().getCode();
125            activeFromDate = b.getActiveFromDate();
126            activeToDate = b.getActiveToDate();
127        }
128    
129    
130        public String getMemberId() {
131            return this.memberId;
132        }
133    
134        public MemberType getMemberType() {
135            return MemberType.fromCode(this.memberTypeCode);
136        }
137    
138    
139        public String getRoleMemberId() {
140            return this.roleMemberId;
141        }
142    
143        public String getRoleId() {
144            return this.roleId;
145        }
146    
147        /**
148         * @return the attributes
149         */
150        public Map<String, String> getAttributes() {
151            return this.attributes;
152        }
153    
154        /**
155         * @return the roleResponsibilityActions
156         */
157        public List<RoleResponsibilityAction> getRoleRspActions() {
158            return this.roleResponsibilityActions;
159        }
160    
161        public DateTime getActiveFromDate() {
162            return activeFromDate;
163        }
164    
165        public DateTime getActiveToDate() {
166            return activeToDate;
167        }
168    
169        @Override
170        public boolean isActive(DateTime activeAsOfDate) {
171            return InactivatableFromToUtils.isActive(activeFromDate, activeToDate, activeAsOfDate);
172        }
173    
174        public static final class Builder implements ModelBuilder, RoleMemberContract, ModelObjectComplete {
175    
176            private String roleMemberId;
177            private String roleId;
178            private Map<String, String> attributes;
179            private List<RoleResponsibilityAction.Builder> roleRspActions;
180            private String memberId;
181            private MemberType memberType;
182            private DateTime activeFromDate;
183            private DateTime activeToDate;
184    
185            public static Builder create(String roleId, String roleMemberId, String memberId,
186                                         MemberType memberType, DateTime activeFromDate, DateTime activeToDate, Map<String, String> attributes) {
187                Builder b = new Builder();
188                b.setRoleId(roleId);
189                b.setRoleMemberId(roleMemberId);
190                b.setMemberId(memberId);
191                b.setMemberType(memberType);
192                b.setActiveFromDate(activeFromDate);
193                b.setActiveToDate(activeToDate);
194                b.setAttributes(attributes);
195                return b;
196            }
197    
198            public static Builder create(RoleMemberContract contract) {
199                Builder b = new Builder();
200                b.setRoleMemberId(contract.getRoleMemberId());
201                b.setRoleId(contract.getRoleId());
202                b.setAttributes(contract.getAttributes());
203    
204                List<RoleResponsibilityAction.Builder> rraBuilders = new ArrayList<RoleResponsibilityAction.Builder>();
205                if (!CollectionUtils.isEmpty(contract.getRoleRspActions())) {
206                    for (RoleResponsibilityActionContract rrac : contract.getRoleRspActions()) {
207                        rraBuilders.add(RoleResponsibilityAction.Builder.create(rrac));
208                    }
209                }
210                b.setRoleRspActions(rraBuilders);
211    
212                b.setMemberId(contract.getMemberId());
213                b.setMemberType(contract.getMemberType());
214                b.setActiveFromDate(contract.getActiveFromDate());
215                b.setActiveToDate(contract.getActiveToDate());
216                return b;
217            }
218    
219            public RoleMember build() {
220                return new RoleMember(this);
221            }
222    
223            public String getRoleMemberId() {
224                return roleMemberId;
225            }
226    
227            public void setRoleMemberId(String roleMemberId) {
228                this.roleMemberId = roleMemberId;
229            }
230    
231            public String getRoleId() {
232                return roleId;
233            }
234    
235            public void setRoleId(String roleId) {
236                this.roleId = roleId;
237            }
238    
239            public Map<String, String> getAttributes() {
240                return attributes;
241            }
242    
243            public void setAttributes(Map<String, String> attributes) {
244                this.attributes = attributes;
245            }
246    
247            public List<RoleResponsibilityAction.Builder> getRoleRspActions() {
248                return roleRspActions;
249            }
250    
251            public void setRoleRspActions(List<RoleResponsibilityAction.Builder> roleRspActions) {
252                this.roleRspActions = roleRspActions;
253            }
254    
255            public String getMemberId() {
256                return memberId;
257            }
258    
259            public void setMemberId(String memberId) {
260                if (StringUtils.isBlank(memberId)) {
261                    throw new IllegalArgumentException("memberId may not be null");
262                }
263                this.memberId = memberId;
264            }
265    
266            public MemberType getMemberType() {
267                return memberType;
268            }
269    
270            public void setMemberType(MemberType memberType) {
271                if (memberType == null) {
272                    throw new IllegalArgumentException("memberType may not be null");
273                }
274                this.memberType = memberType;
275            }
276    
277            public DateTime getActiveFromDate() {
278                return activeFromDate;
279            }
280    
281            public void setActiveFromDate(DateTime activeFromDate) {
282                this.activeFromDate = activeFromDate;
283            }
284    
285            public DateTime getActiveToDate() {
286                return activeToDate;
287            }
288    
289            public void setActiveToDate(DateTime activeToDate) {
290                this.activeToDate = activeToDate;
291            }
292    
293            @Override
294            public boolean isActive(DateTime activeAsOfDate) {
295                return InactivatableFromToUtils.isActive(activeFromDate, activeToDate, activeAsOfDate);
296            }
297    
298            @Override
299            public int hashCode() {
300                return HashCodeBuilder.reflectionHashCode(this);
301            }
302    
303            @Override
304            public boolean equals(Object obj) {
305                return EqualsBuilder.reflectionEquals(obj, this);
306            }
307    
308            @Override
309            public String toString() {
310                return ToStringBuilder.reflectionToString(this);
311            }
312        }
313    
314        /**
315         * A private class which exposes constants which define the XML element names to use
316         * when this object is marshalled to XML.
317         */
318        static class Elements {
319            final static String ROLE_MEMBER_ID = "roleMemberId";
320            final static String ROLE_ID = "roleId";
321            final static String ATTRIBUTES = "attributes";
322            final static String ROLE_RESPONSIBILITY_ACTIONS = "roleResponsibilityActions";
323            final static String MEMBER_ID = "memberId";
324            final static String MEMBER_TYPE_CODE = "memberTypeCode";
325        }
326    
327        /**
328         * Defines some internal constants used on this class.
329         */
330        static class Constants {
331            final static String ROOT_ELEMENT_NAME = "roleMember";
332            final static String TYPE_NAME = "RoleMemberType";
333        }
334    
335        public static class Cache {
336            public static final String NAME = KimConstants.Namespaces.KIM_NAMESPACE_2_0 + "/" + RoleMember.Constants.TYPE_NAME;
337        }
338    }