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.kuali.rice.core.api.CoreConstants;
024    import org.kuali.rice.core.api.membership.MemberType;
025    import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
026    import org.kuali.rice.core.api.mo.ModelBuilder;
027    import org.kuali.rice.core.api.mo.ModelObjectComplete;
028    import org.kuali.rice.core.api.util.jaxb.MapStringStringAdapter;
029    import org.kuali.rice.kim.api.KimConstants;
030    import org.kuali.rice.kim.api.common.delegate.DelegateType;
031    import org.kuali.rice.kim.api.common.delegate.DelegateTypeContract;
032    import org.w3c.dom.Element;
033    
034    import javax.xml.bind.annotation.XmlAccessType;
035    import javax.xml.bind.annotation.XmlAccessorType;
036    import javax.xml.bind.annotation.XmlAnyElement;
037    import javax.xml.bind.annotation.XmlElement;
038    import javax.xml.bind.annotation.XmlRootElement;
039    import javax.xml.bind.annotation.XmlType;
040    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
041    import java.util.ArrayList;
042    import java.util.Collection;
043    import java.util.Collections;
044    import java.util.List;
045    import java.util.Map;
046    
047    @XmlRootElement(name = RoleMembership.Constants.ROOT_ELEMENT_NAME)
048    @XmlAccessorType(XmlAccessType.NONE)
049    @XmlType(name = RoleMembership.Constants.TYPE_NAME, propOrder = {
050            RoleMembership.Elements.ROLE_ID,
051            RoleMembership.Elements.ROLE_MEMBER_ID,
052            RoleMembership.Elements.EMBEDDED_ROLE_ID,
053            RoleMembership.Elements.MEMBER_ID,
054            RoleMembership.Elements.MEMBER_TYPE_CODE,
055            RoleMembership.Elements.ROLE_SORTING_CODE,
056            RoleMembership.Elements.QUALIFIER,
057            RoleMembership.Elements.DELEGATES,
058            CoreConstants.CommonElements.FUTURE_ELEMENTS
059    })
060    public class RoleMembership extends AbstractDataTransferObject implements RoleMembershipContract {
061        private static final long serialVersionUID = 1L;
062    
063        @XmlElement(name=Elements.ROLE_ID)
064        private final String roleId;
065    
066        @XmlElement(name=Elements.ROLE_MEMBER_ID)
067        private final String roleMemberId;
068    
069        @XmlElement(name=Elements.EMBEDDED_ROLE_ID)
070        private final String embeddedRoleId;
071    
072        @XmlElement(name=Elements.MEMBER_ID, required = true)
073        private final String memberId;
074    
075        @XmlElement(name=Elements.MEMBER_TYPE_CODE, required = true)
076        private final String memberTypeCode;
077    
078        @XmlElement(name=Elements.ROLE_SORTING_CODE)
079        private final String roleSortingCode;
080    
081        @XmlElement(name=Elements.QUALIFIER)
082        @XmlJavaTypeAdapter(value = MapStringStringAdapter.class)
083        private final Map<String, String> qualifier;
084    
085        @XmlElement(name=Elements.DELEGATES)
086        private final List<DelegateType> delegates;
087    
088        @SuppressWarnings("unused")
089        @XmlAnyElement
090        private final Collection<Element> _futureElements = null;
091    
092        /**
093         * Private constructor for JAXB only
094         */
095        @SuppressWarnings("unused")
096        private RoleMembership() {
097            roleId = null;
098            roleMemberId = null;
099            embeddedRoleId = null;
100            memberId = null;
101            memberTypeCode = null;
102            roleSortingCode = null;
103            qualifier = null;
104            delegates = null;
105        }
106    
107        private RoleMembership(Builder b) {
108            roleId = b.getRoleId();
109            roleMemberId = b.getRoleMemberId();
110            embeddedRoleId = b.getEmbeddedRoleId();
111            memberId = b.getMemberId();
112            memberTypeCode = b.getMemberType().getCode();
113            roleSortingCode = b.getRoleSortingCode();
114            qualifier = b.getQualifier();
115    
116            delegates = new ArrayList<DelegateType>();
117            if (!CollectionUtils.isEmpty(b.getDelegates())) {
118                for (DelegateType.Builder delegateBuilder : b.getDelegates()) {
119                    delegates.add(delegateBuilder.build());
120                }
121            }
122        }
123    
124        public String getRoleId() {
125            return roleId;
126        }
127    
128        public String getRoleMemberId() {
129            return roleMemberId;
130        }
131    
132        public String getEmbeddedRoleId() {
133            return embeddedRoleId;
134        }
135    
136        public String getMemberId() {
137            return memberId;
138        }
139    
140        public MemberType getMemberType() {
141            return MemberType.fromCode(memberTypeCode);
142        }
143    
144        public String getRoleSortingCode() {
145            return roleSortingCode;
146        }
147    
148        public Map<String, String> getQualifier() {
149            return qualifier;
150        }
151    
152        public List<DelegateType> getDelegates() {
153            return Collections.unmodifiableList(delegates);
154        }
155    
156    
157        public static final class Builder implements ModelBuilder, RoleMembershipContract, ModelObjectComplete {
158            private String roleId;
159            private String roleMemberId;
160            private String embeddedRoleId;
161            private String memberId;
162            private MemberType memberType;
163            private String roleSortingCode;
164            private Map<String, String> qualifier;
165            private List<DelegateType.Builder> delegates;
166    
167            private Builder() {
168            }
169    
170            public static Builder create(String roleId, String roleMemberId, String memberId, MemberType memberType,
171                                         Map<String, String> qualifier) {
172    
173                Builder b = new Builder();
174                b.setRoleId(roleId);
175                b.setRoleMemberId(roleMemberId);
176                b.setMemberId(memberId);
177                b.setMemberType(memberType);
178                b.setQualifier(qualifier);
179                return b;
180            }
181    
182            public static Builder create(RoleMembershipContract contract) {
183                Builder b = new Builder();
184                b.setRoleId(contract.getRoleId());
185                b.setRoleMemberId(contract.getRoleMemberId());
186                b.setEmbeddedRoleId(contract.getEmbeddedRoleId());
187                b.setMemberId(contract.getMemberId());
188                b.setMemberType(contract.getMemberType());
189                b.setRoleSortingCode(contract.getRoleSortingCode());
190                b.setQualifier(contract.getQualifier());
191    
192                List<DelegateType.Builder> delegateBuilders = new ArrayList<DelegateType.Builder>();
193                if (!CollectionUtils.isEmpty(contract.getDelegates())) {
194                    for (DelegateTypeContract delegateContract : contract.getDelegates()) {
195                        delegateBuilders.add(DelegateType.Builder.create(delegateContract));
196                    }
197                }
198                b.setDelegates(delegateBuilders);
199    
200                return b;
201            }
202    
203            @Override
204            public RoleMembership build() {
205                return new RoleMembership(this);
206            }
207    
208            public String getRoleId() {
209                return this.roleId;
210            }
211    
212            public void setRoleId(String roleId) {
213                this.roleId = roleId;
214            }
215    
216            public Map<String, String> getQualifier() {
217                return this.qualifier;
218            }
219    
220            public void setQualifier(Map<String, String> qualifier) {
221                this.qualifier = qualifier;
222            }
223    
224            public List<DelegateType.Builder> getDelegates() {
225                return this.delegates;
226            }
227    
228            public void setDelegates(List<DelegateType.Builder> delegates) {
229                this.delegates = delegates;
230            }
231    
232            public String getRoleMemberId() {
233                return this.roleMemberId;
234            }
235    
236            public void setRoleMemberId(String roleMemberId) {
237                this.roleMemberId = roleMemberId;
238            }
239    
240            public String getMemberId() {
241                return this.memberId;
242            }
243    
244            public void setMemberId(String memberId) {
245                if (StringUtils.isEmpty(memberId)) {
246                    throw new IllegalArgumentException("memberId cannot be empty or null");
247                }
248                this.memberId = memberId;
249            }
250    
251            public MemberType getMemberType() {
252                return this.memberType;
253            }
254    
255            public void setMemberType(MemberType memberType) {
256                if (memberType == null) {
257                    throw new IllegalArgumentException("memberTypeCode cannot be null");
258                }
259                this.memberType = memberType;
260            }
261    
262            public String getEmbeddedRoleId() {
263                return this.embeddedRoleId;
264            }
265    
266            public void setEmbeddedRoleId(String embeddedRoleId) {
267                this.embeddedRoleId = embeddedRoleId;
268            }
269    
270            public String getRoleSortingCode() {
271                return this.roleSortingCode;
272            }
273    
274            public void setRoleSortingCode(String roleSortingCode) {
275                this.roleSortingCode = roleSortingCode;
276            }
277    
278            @Override
279            public int hashCode() {
280                return HashCodeBuilder.reflectionHashCode(this);
281            }
282    
283            @Override
284            public boolean equals(Object obj) {
285                return EqualsBuilder.reflectionEquals(obj, this);
286            }
287    
288            @Override
289            public String toString() {
290                return ToStringBuilder.reflectionToString(this);
291            }
292    
293    
294        }
295    
296        /**
297         * A private class which exposes constants which define the XML element names to use
298         * when this object is marshalled to XML.
299         */
300        static class Elements {
301            final static String ROLE_ID = "roleId";
302            final static String ROLE_MEMBER_ID = "roleMemberId";
303            final static String EMBEDDED_ROLE_ID = "embeddedRoleId";
304            final static String MEMBER_ID = "memberId";
305            final static String MEMBER_TYPE_CODE = "memberTypeCode";
306            final static String ROLE_SORTING_CODE = "roleSortingCode";
307            final static String QUALIFIER = "qualifier";
308            final static String DELEGATES = "delegates";
309        }
310    
311        /**
312         * Defines some internal constants used on this class.
313         */
314        static class Constants {
315            final static String ROOT_ELEMENT_NAME = "roleMembership";
316            final static String TYPE_NAME = "RoleMembershipType";
317        }
318    
319        public static class Cache {
320            public static final String NAME = KimConstants.Namespaces.KIM_NAMESPACE_2_0 + "/" + RoleMembership.Constants.TYPE_NAME;
321        }
322    }