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