View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.rice.kim.impl.jaxb;
20  
21  import java.io.Serializable;
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.XmlType;
32  
33  import org.kuali.rice.core.util.jaxb.RiceXmlExportList;
34  import org.kuali.rice.core.util.jaxb.RiceXmlImportList;
35  import org.kuali.rice.core.util.jaxb.RiceXmlListAdditionListener;
36  import org.kuali.rice.core.util.jaxb.RiceXmlListGetterListener;
37  import org.kuali.rice.kim.api.role.RoleContract;
38  
39  /**
40   * This class represents a &lt;roles&gt; element. 
41   * 
42   * @author Kuali Rice Team (rice.collab@kuali.org)
43   */
44  @XmlAccessorType(XmlAccessType.FIELD)
45  @XmlType(name="RolesType", propOrder={"roles"})
46  public class RolesXmlDTO implements RiceXmlListAdditionListener<RoleXmlDTO>, RiceXmlListGetterListener<RoleXmlDTO,Object>, Serializable {
47      
48      private static final long serialVersionUID = 1L;
49      
50      @XmlElement(name="role")
51      private List<RoleXmlDTO> roles;
52      
53      public RolesXmlDTO() {}
54      
55      public RolesXmlDTO(List<? extends Object> rolesToExport) {
56          this.roles = new RiceXmlExportList<RoleXmlDTO,Object>(rolesToExport, this);
57      }
58      
59      public List<RoleXmlDTO> getRoles() {
60          return roles;
61      }
62  
63      public void setRoles(List<RoleXmlDTO> roles) {
64          this.roles = roles;
65      }
66  
67      void beforeUnmarshal(Unmarshaller unmarshaller, Object parent) {
68          roles = new RiceXmlImportList<RoleXmlDTO>(this);
69      }
70      
71      void afterUnmarshal(Unmarshaller unmarshaller, Object parent) {
72          roles = null;
73      }
74      
75      /**
76       * @see org.kuali.rice.core.util.jaxb.RiceXmlListAdditionListener#newItemAdded(java.lang.Object)
77       */
78      public void newItemAdded(RoleXmlDTO item) {
79          // Persist the role if it has not already been persisted yet.
80          if (!item.isAlreadyPersisted()) {
81              try {
82                  RoleXmlUtil.validateAndPersistNewRole(item);
83              } catch (UnmarshalException e) {
84                  throw new RuntimeException(e);
85              }
86          }
87          
88          // If a "roleMembers" element was present, remove any existing roles that do not match the new ones.
89          Set<String> existingRoleMemberIds = item.getExistingRoleMemberIds();
90          if (existingRoleMemberIds != null) {
91              RoleXmlUtil.removeRoleMembers(item.getRoleId(), existingRoleMemberIds);
92          }
93          item.setExistingRoleMemberIds(null);
94      }
95  
96      void afterMarshal(Marshaller marshaller) {
97          roles = null;
98      }
99      
100     /**
101      * @see org.kuali.rice.core.util.jaxb.RiceXmlListGetterListener#gettingNextItem(java.lang.Object, int)
102      */
103     public RoleXmlDTO gettingNextItem(Object nextItem, int index) {
104         if (!(nextItem instanceof RoleContract)) {
105             throw new IllegalStateException("Object for exportation should have been a role");
106         }
107         RoleContract role = ((RoleContract)nextItem);
108         return new RoleXmlDTO(role, new RoleMembersXmlDTO.WithinRole(role.getId()), new RolePermissionsXmlDTO.WithinRole(role.getId()));
109     }
110 }