View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kim.impl.jaxb;
17  
18  import java.io.Serializable;
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.HashSet;
22  import java.util.List;
23  import java.util.Set;
24  
25  import javax.xml.bind.Marshaller;
26  import javax.xml.bind.UnmarshalException;
27  import javax.xml.bind.Unmarshaller;
28  import javax.xml.bind.annotation.XmlAccessType;
29  import javax.xml.bind.annotation.XmlAccessorType;
30  import javax.xml.bind.annotation.XmlElement;
31  import javax.xml.bind.annotation.XmlTransient;
32  import javax.xml.bind.annotation.XmlType;
33  
34  import org.kuali.rice.core.api.criteria.QueryByCriteria;
35  import org.kuali.rice.core.util.jaxb.RiceXmlExportList;
36  import org.kuali.rice.core.util.jaxb.RiceXmlImportList;
37  import org.kuali.rice.core.util.jaxb.RiceXmlListAdditionListener;
38  import org.kuali.rice.core.util.jaxb.RiceXmlListGetterListener;
39  import org.kuali.rice.kim.api.role.RoleMember;
40  import org.kuali.rice.kim.api.role.RoleMemberContract;
41  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
42  
43  import static org.kuali.rice.core.api.criteria.PredicateFactory.equal;
44  
45  /**
46   * Base class representing an unmarshalled <roleMembers> element.
47   * Refer to the static inner classes for more information about the specific contexts.
48   * 
49   * @author Kuali Rice Team (rice.collab@kuali.org)
50   */
51  @XmlTransient
52  public abstract class RoleMembersXmlDTO<T extends RoleMemberXmlDTO> implements RiceXmlListAdditionListener<T>, Serializable {
53  
54      private static final long serialVersionUID = 1L;
55  
56      public abstract List<T> getRoleMembers();
57      
58      public abstract void setRoleMembers(List<T> roleMembers);
59      
60      void beforeUnmarshal(Unmarshaller unmarshaller, Object parent) throws UnmarshalException {
61          setRoleMembers(new RiceXmlImportList<T>(this));
62      }
63      
64      void afterUnmarshal(Unmarshaller unmarshaller, Object parent) throws UnmarshalException {
65          setRoleMembers(null);
66      }
67      
68      // =======================================================================================================
69      
70      /**
71       * This class represents a &lt;roleMembers&gt; element that is not a child of a &lt;role&gt; element.
72       * 
73       * @author Kuali Rice Team (rice.collab@kuali.org)
74       */
75      @XmlAccessorType(XmlAccessType.FIELD)
76      @XmlType(name="StandaloneRoleMembersType", propOrder={"roleMembers"})
77      public static class OutsideOfRole extends RoleMembersXmlDTO<RoleMemberXmlDTO.OutsideOfRole> {
78  
79          private static final long serialVersionUID = 1L;
80          
81          @XmlElement(name="roleMember")
82          private List<RoleMemberXmlDTO.OutsideOfRole> roleMembers;
83  
84          /**
85           * @see org.kuali.rice.kim.impl.jaxb.RoleMembersXmlDTO#getRoleMembers()
86           */
87          @Override
88          public List<RoleMemberXmlDTO.OutsideOfRole> getRoleMembers() {
89              return this.roleMembers;
90          }
91  
92          /**
93           * @see org.kuali.rice.kim.impl.jaxb.RoleMembersXmlDTO#setRoleMembers(java.util.List)
94           */
95          @Override
96          public void setRoleMembers(List<RoleMemberXmlDTO.OutsideOfRole> roleMembers) {
97              this.roleMembers = roleMembers;
98          }
99  
100         /**
101          * @see org.kuali.rice.core.util.jaxb.RiceXmlListAdditionListener#newItemAdded(java.lang.Object)
102          */
103         @Override
104         public void newItemAdded(RoleMemberXmlDTO.OutsideOfRole item) {
105             try {
106                 RoleXmlUtil.validateAndPersistNewRoleMember(item);
107             } catch (UnmarshalException e) {
108                 throw new RuntimeException(e);
109             }
110         }
111     }
112     
113     // =======================================================================================================
114     
115     /**
116      * This class represents a &lt;roleMembers&gt; element that is a child of a &lt;role&gt; element.
117      * 
118      * @author Kuali Rice Team (rice.collab@kuali.org)
119      */
120     @XmlAccessorType(XmlAccessType.FIELD)
121     @XmlType(name="RoleMembersType", propOrder={"roleMembers"})
122     public static class WithinRole extends RoleMembersXmlDTO<RoleMemberXmlDTO.WithinRole>
123             implements RiceXmlListGetterListener<RoleMemberXmlDTO.WithinRole,String> {
124 
125         private static final long serialVersionUID = 1L;
126         
127         @XmlElement(name="roleMember")
128         private List<RoleMemberXmlDTO.WithinRole> roleMembers;
129 
130         @XmlTransient
131         private String roleId;
132         
133         @XmlTransient
134         private Set<String> existingRoleMemberIds;
135         
136         public WithinRole() {}
137         
138         public WithinRole(String roleId) {
139             this.roleId = roleId;
140         }
141         
142         /**
143          * @see org.kuali.rice.kim.impl.jaxb.RoleMembersXmlDTO#getRoleMembers()
144          */
145         @Override
146         public List<org.kuali.rice.kim.impl.jaxb.RoleMemberXmlDTO.WithinRole> getRoleMembers() {
147             return this.roleMembers;
148         }
149 
150         /**
151          * @see org.kuali.rice.kim.impl.jaxb.RoleMembersXmlDTO#setRoleMembers(java.util.List)
152          */
153         @Override
154         public void setRoleMembers(List<org.kuali.rice.kim.impl.jaxb.RoleMemberXmlDTO.WithinRole> roleMembers) {
155             this.roleMembers = roleMembers;
156         }
157         
158         /**
159          * @return the roleId
160          */
161         public String getRoleId() {
162             return this.roleId;
163         }
164 
165         /**
166          * @see org.kuali.rice.kim.impl.jaxb.RoleMembersXmlDTO#beforeUnmarshal(javax.xml.bind.Unmarshaller, java.lang.Object)
167          */
168         @Override
169         void beforeUnmarshal(Unmarshaller unmarshaller, Object parent) throws UnmarshalException {
170             if (parent instanceof RoleXmlDTO) {
171                 // Obtain the role ID from the enclosing role, and persist the role if it has not been persisted yet.
172                 RoleXmlDTO parentRole = (RoleXmlDTO) parent;
173                 if (!parentRole.isAlreadyPersisted()) {
174                     RoleXmlUtil.validateAndPersistNewRole(parentRole);
175                 }
176                 roleId = parentRole.getRoleId();
177             }
178             existingRoleMemberIds = new HashSet<String>();
179             super.beforeUnmarshal(unmarshaller, parent);
180         }
181 
182         /**
183          * This overridden method ...
184          * 
185          * @see org.kuali.rice.kim.impl.jaxb.RoleMembersXmlDTO#afterUnmarshal(javax.xml.bind.Unmarshaller, java.lang.Object)
186          */
187         @Override
188         void afterUnmarshal(Unmarshaller unmarshaller, Object parent) throws UnmarshalException {
189             super.afterUnmarshal(unmarshaller, parent);
190             if (parent instanceof RoleXmlDTO) {
191                 ((RoleXmlDTO)parent).setExistingRoleMemberIds(existingRoleMemberIds);
192             }
193             existingRoleMemberIds = null;
194         }
195 
196         /**
197          * @see org.kuali.rice.core.util.jaxb.RiceXmlListAdditionListener#newItemAdded(java.lang.Object)
198          */
199         @Override
200         public void newItemAdded(org.kuali.rice.kim.impl.jaxb.RoleMemberXmlDTO.WithinRole item) {
201             // Persist the role member and add it to the set of role members that should not be removed from the role.
202             try {
203                 existingRoleMemberIds.add(RoleXmlUtil.validateAndPersistNewRoleMember(item));
204             } catch (UnmarshalException e) {
205                 throw new RuntimeException(e);
206             }
207         }
208         
209         void beforeMarshal(Marshaller marshaller) {
210             List<RoleMember> tempMembers = KimApiServiceLocator.getRoleService().findRoleMembers(
211                     QueryByCriteria.Builder.fromPredicates(equal("roleId", roleId))).getResults();
212             if (tempMembers != null && !tempMembers.isEmpty()) {
213                 List<String> roleMemberIds = new ArrayList<String>();
214                 
215                 for (RoleMemberContract tempMember : tempMembers) {
216                     if (tempMember.isActive(null)) {
217                         roleMemberIds.add(tempMember.getRoleMemberId());
218                     }
219                 }
220                 
221                 if (!roleMemberIds.isEmpty()) {
222                     setRoleMembers(new RiceXmlExportList<RoleMemberXmlDTO.WithinRole,String>(roleMemberIds, this));
223                 }
224             }
225         }
226         
227         void afterMarshal(Marshaller marshaller) {
228             setRoleMembers(null);
229         }
230 
231         /**
232          * @see org.kuali.rice.core.util.jaxb.RiceXmlListGetterListener#gettingNextItem(java.lang.Object, int)
233          */
234         @Override
235         public RoleMemberXmlDTO.WithinRole gettingNextItem(String nextItem, int index) {
236             return new RoleMemberXmlDTO.WithinRole(KimApiServiceLocator.getRoleService().findRoleMembers(QueryByCriteria.Builder.fromPredicates(equal("roleMemberId", nextItem))).getResults().get(0), false);
237         }
238     }
239 }