View Javadoc

1   /**
2    * Copyright 2005-2012 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.List;
20  import java.util.Set;
21  
22  import javax.xml.bind.Marshaller;
23  import javax.xml.bind.UnmarshalException;
24  import javax.xml.bind.Unmarshaller;
25  import javax.xml.bind.annotation.XmlAccessType;
26  import javax.xml.bind.annotation.XmlAccessorType;
27  import javax.xml.bind.annotation.XmlElement;
28  import javax.xml.bind.annotation.XmlType;
29  
30  import org.kuali.rice.core.util.jaxb.RiceXmlExportList;
31  import org.kuali.rice.core.util.jaxb.RiceXmlImportList;
32  import org.kuali.rice.core.util.jaxb.RiceXmlListAdditionListener;
33  import org.kuali.rice.core.util.jaxb.RiceXmlListGetterListener;
34  import org.kuali.rice.kim.api.role.RoleContract;
35  
36  /**
37   * This class represents a <roles> element. 
38   * 
39   * @author Kuali Rice Team (rice.collab@kuali.org)
40   */
41  @XmlAccessorType(XmlAccessType.FIELD)
42  @XmlType(name="RolesType", propOrder={"roles"})
43  public class RolesXmlDTO implements RiceXmlListAdditionListener<RoleXmlDTO>, RiceXmlListGetterListener<RoleXmlDTO,Object>, Serializable {
44      
45      private static final long serialVersionUID = 1L;
46      
47      @XmlElement(name="role")
48      private List<RoleXmlDTO> roles;
49      
50      public RolesXmlDTO() {}
51      
52      public RolesXmlDTO(List<? extends Object> rolesToExport) {
53          this.roles = new RiceXmlExportList<RoleXmlDTO,Object>(rolesToExport, this);
54      }
55      
56      public List<RoleXmlDTO> getRoles() {
57          return roles;
58      }
59  
60      public void setRoles(List<RoleXmlDTO> roles) {
61          this.roles = roles;
62      }
63  
64      void beforeUnmarshal(Unmarshaller unmarshaller, Object parent) {
65          roles = new RiceXmlImportList<RoleXmlDTO>(this);
66      }
67      
68      void afterUnmarshal(Unmarshaller unmarshaller, Object parent) {
69          roles = null;
70      }
71      
72      /**
73       * @see org.kuali.rice.core.util.jaxb.RiceXmlListAdditionListener#newItemAdded(java.lang.Object)
74       */
75      public void newItemAdded(RoleXmlDTO item) {
76          // Persist the role if it has not already been persisted yet.
77          if (!item.isAlreadyPersisted()) {
78              try {
79                  RoleXmlUtil.validateAndPersistNewRole(item);
80              } catch (UnmarshalException e) {
81                  throw new RuntimeException(e);
82              }
83          }
84          
85          // If a "roleMembers" element was present, remove any existing roles that do not match the new ones.
86          Set<String> existingRoleMemberIds = item.getExistingRoleMemberIds();
87          if (existingRoleMemberIds != null) {
88              RoleXmlUtil.removeRoleMembers(item.getRoleId(), existingRoleMemberIds);
89          }
90          item.setExistingRoleMemberIds(null);
91      }
92  
93      void afterMarshal(Marshaller marshaller) {
94          roles = null;
95      }
96      
97      /**
98       * @see org.kuali.rice.core.util.jaxb.RiceXmlListGetterListener#gettingNextItem(java.lang.Object, int)
99       */
100     public RoleXmlDTO gettingNextItem(Object nextItem, int index) {
101         if (!(nextItem instanceof RoleContract)) {
102             throw new IllegalStateException("Object for exportation should have been a role");
103         }
104         RoleContract role = ((RoleContract)nextItem);
105         return new RoleXmlDTO(role, new RoleMembersXmlDTO.WithinRole(role.getId()), new RolePermissionsXmlDTO.WithinRole(role.getId()));
106     }
107 }