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.ArrayList;
23  import java.util.List;
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.util.jaxb.RiceXmlExportList;
35  import org.kuali.rice.core.util.jaxb.RiceXmlImportList;
36  import org.kuali.rice.core.util.jaxb.RiceXmlListAdditionListener;
37  import org.kuali.rice.core.util.jaxb.RiceXmlListGetterListener;
38  import org.kuali.rice.kim.api.permission.PermissionContract;
39  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
40  
41  /**
42   * Base class representing an unmarshalled &lt;rolePermissions&gt; element.
43   * Refer to the static inner classes for more information about the specific contexts.
44   * 
45   * TODO: Alter the role/permission service APIs so that finding all the permissions assigned to a role is possible; the
46   * current lack of such an API method prevents role permissions from being exported.
47   * 
48   * @author Kuali Rice Team (rice.collab@kuali.org)
49   */
50  @XmlTransient
51  public abstract class RolePermissionsXmlDTO<T extends RolePermissionXmlDTO> implements RiceXmlListAdditionListener<T>, Serializable {
52      
53      private static final long serialVersionUID = 1L;
54  
55      public abstract List<T> getRolePermissions();
56      
57      public abstract void setRolePermissions(List<T> rolePermissions);
58      
59      void beforeUnmarshal(Unmarshaller unmarshaller, Object parent) throws UnmarshalException {
60          setRolePermissions(new RiceXmlImportList<T>(this));
61      }
62      
63      void afterUnmarshal(Unmarshaller unmarshaller, Object parent) throws UnmarshalException {
64          setRolePermissions(null);
65      }
66      
67      // =======================================================================================================
68      
69      /**
70       * This class represents a &lt;rolePermissions&gt; element that is not a child of a &lt;role&gt; element.
71       * 
72       * @author Kuali Rice Team (rice.collab@kuali.org)
73       */
74      @XmlAccessorType(XmlAccessType.FIELD)
75      @XmlType(name="StandaloneRolePermissionsType", propOrder={"rolePermissions"})
76      public static class OutsideOfRole extends RolePermissionsXmlDTO<RolePermissionXmlDTO.OutsideOfRole> {
77          
78          private static final long serialVersionUID = 1L;
79          
80          @XmlElement(name="rolePermission")
81          private List<RolePermissionXmlDTO.OutsideOfRole> rolePermissions;
82          
83          public List<RolePermissionXmlDTO.OutsideOfRole> getRolePermissions() {
84              return rolePermissions;
85          }
86  
87          public void setRolePermissions(List<RolePermissionXmlDTO.OutsideOfRole> rolePermissions) {
88              this.rolePermissions = rolePermissions;
89          }
90          
91          public void newItemAdded(RolePermissionXmlDTO.OutsideOfRole item) {
92              try {
93                  RoleXmlUtil.validateAndPersistNewRolePermission(item);
94              } catch (UnmarshalException e) {
95                  throw new RuntimeException(e);
96              }
97          }
98          
99      }
100     
101     // =======================================================================================================
102     
103     /**
104      * This class represents a &lt;rolePermissions&gt; element that is a child of a &lt;role&gt; element.
105      * 
106      * @author Kuali Rice Team (rice.collab@kuali.org)
107      */
108     @XmlAccessorType(XmlAccessType.FIELD)
109     @XmlType(name="RolePermissionsType", propOrder={"rolePermissions"})
110     public static class WithinRole extends RolePermissionsXmlDTO<RolePermissionXmlDTO.WithinRole>
111             implements RiceXmlListGetterListener<RolePermissionXmlDTO.WithinRole,String> {
112         
113         private static final long serialVersionUID = 1L;
114 
115         @XmlElement(name="rolePermission")
116         private List<RolePermissionXmlDTO.WithinRole> rolePermissions;
117         
118         @XmlTransient
119         private String roleId;
120         
121         public WithinRole() {}
122         
123         public WithinRole(String roleId) {
124             this.roleId = roleId;
125         }
126         
127         public List<RolePermissionXmlDTO.WithinRole> getRolePermissions() {
128             return rolePermissions;
129         }
130 
131         public void setRolePermissions(List<RolePermissionXmlDTO.WithinRole> rolePermissions) {
132             this.rolePermissions = rolePermissions;
133         }
134         
135         public String getRoleId() {
136             return roleId;
137         }
138         
139         @Override
140         void beforeUnmarshal(Unmarshaller unmarshaller, Object parent) throws UnmarshalException {
141             if (parent instanceof RoleXmlDTO) {
142                 // Obtain the role ID from the enclosing role, and persist the role if it has not been persisted yet.
143                 RoleXmlDTO parentRole = (RoleXmlDTO) parent;
144                 if (!parentRole.isAlreadyPersisted()) {
145                     RoleXmlUtil.validateAndPersistNewRole(parentRole);
146                 }
147                 roleId = parentRole.getRoleId();
148             }
149             super.beforeUnmarshal(unmarshaller, parent);
150         }
151         
152         public void newItemAdded(RolePermissionXmlDTO.WithinRole item) {
153             try {
154                 RoleXmlUtil.validateAndPersistNewRolePermission(item);
155             } catch (UnmarshalException e) {
156                 throw new RuntimeException(e);
157             }
158         }
159         
160         void beforeMarshal(Marshaller marshaller) {
161             // TODO: Use new API method once it becomes available!!!!
162             List<String> permissionIds = new ArrayList<String>();// KIMServiceLocator.getPermissionService().getRoleIdsForPermission(permissionId);
163             if (permissionIds != null && !permissionIds.isEmpty()) {
164                 setRolePermissions(new RiceXmlExportList<RolePermissionXmlDTO.WithinRole,String>(permissionIds, this));
165             }
166         }
167         
168         void afterMarshal(Marshaller marshaller) {
169             setRolePermissions(null);
170         }
171 
172         public RolePermissionXmlDTO.WithinRole gettingNextItem(String nextItem, int index) {
173             PermissionContract permission = KimApiServiceLocator.getPermissionService().getPermission(nextItem);
174             if (permission == null) {
175                 throw new RuntimeException("Cannot find permission with ID \"" + nextItem + "\"");
176             }
177             return new RolePermissionXmlDTO.WithinRole(permission, false);
178         }
179     }
180 }