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  
23  import javax.xml.bind.Unmarshaller;
24  import javax.xml.bind.annotation.XmlAccessType;
25  import javax.xml.bind.annotation.XmlAccessorType;
26  import javax.xml.bind.annotation.XmlElement;
27  import javax.xml.bind.annotation.XmlTransient;
28  import javax.xml.bind.annotation.XmlType;
29  import javax.xml.bind.annotation.adapters.NormalizedStringAdapter;
30  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
31  
32  import org.kuali.rice.core.util.jaxb.NameAndNamespacePair;
33  import org.kuali.rice.core.util.jaxb.NameAndNamespacePairValidatingAdapter;
34  import org.kuali.rice.kim.api.permission.PermissionContract;
35  import org.kuali.rice.kim.api.role.RoleContract;
36  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
37  
38  
39  /**
40   * Base class representing an unmarshalled &lt;rolePermission&gt; element.
41   * Refer to the static inner classes for more information about the specific contexts.
42   * 
43   * @author Kuali Rice Team (rice.collab@kuali.org)
44   */
45  @XmlTransient
46  public abstract class RolePermissionXmlDTO implements Serializable {
47  
48      private static final long serialVersionUID = 1L;
49      
50      @XmlElement(name="permissionId")
51      @XmlJavaTypeAdapter(NormalizedStringAdapter.class)
52      private String permissionId;
53      
54      @XmlElement(name="permissionName")
55      @XmlJavaTypeAdapter(NameAndNamespacePairValidatingAdapter.class)
56      private NameAndNamespacePair permissionNameAndNamespace;
57  
58      /**
59       * Constructs an empty RolePermissionXmlDTO instance.
60       */
61      public RolePermissionXmlDTO() {}
62      
63      /**
64       * Constructs a RolePermissionXmlDTO that gets populated from the given KIM permission.
65       * 
66       * @param permission The permission that this DTO should obtain its data from.
67       * @param populateIds If true, the permission ID will get populated; otherwise, it will remain null.
68       */
69      public RolePermissionXmlDTO(PermissionContract permission, boolean populateIds) {
70          if (permission == null) {
71              throw new IllegalArgumentException("Cannot construct a role permission with a null permission");
72          }
73          if (populateIds) {
74              this.permissionId = permission.getId();
75          }
76          this.permissionNameAndNamespace = new NameAndNamespacePair(permission.getNamespaceCode(), permission.getName());
77      }
78  
79      /**
80       * @return the permissionId
81       */
82      public String getPermissionId() {
83          return this.permissionId;
84      }
85  
86      /**
87       * @param permissionId the permissionId to set
88       */
89      public void setPermissionId(String permissionId) {
90          this.permissionId = permissionId;
91      }
92  
93      /**
94       * @return the permissionNameAndNamespace
95       */
96      public NameAndNamespacePair getPermissionNameAndNamespace() {
97          return this.permissionNameAndNamespace;
98      }
99  
100     /**
101      * @param permissionNameAndNamespace the permissionNameAndNamespace to set
102      */
103     public void setPermissionNameAndNamespace(NameAndNamespacePair permissionNameAndNamespace) {
104         this.permissionNameAndNamespace = permissionNameAndNamespace;
105     }
106 
107     /**
108      * Retrieves the permission name from the permission-name-and-namespace combo.
109      * 
110      * @return The name of the permission assigned to the role, or null if the permission-name-and-namespace combo is null.
111      */
112     public String getPermissionName() {
113         return (permissionNameAndNamespace != null) ? permissionNameAndNamespace.getName() : null;
114     }
115 
116     /**
117      * Retrieves the permission namespace code from the permission-name-and-namespace combo.
118      * 
119      * @return The namespace code of the permission assigned to the role, or null if the permission-name-and-namespace combo is null.
120      */
121     public String getPermissionNamespaceCode() {
122         return (permissionNameAndNamespace != null) ? permissionNameAndNamespace.getNamespaceCode() : null;
123     }
124     
125     /**
126      * Retrieves the ID of the role that the permission is assigned to.
127      * Subclasses are responsible for implementing this method so that it does so.
128      * 
129      * @return The role ID of the role that the permission is assigned to.
130      */
131     public abstract String getRoleId();
132     
133     // =======================================================================================================
134     
135     /**
136      * This class represents a &lt;rolePermission&gt; element that is not a descendant of a &lt;role&gt; element.
137      * 
138      * @author Kuali Rice Team (rice.collab@kuali.org)
139      */
140     @XmlAccessorType(XmlAccessType.FIELD)
141     @XmlType(name="StandaloneRolePermissionType", propOrder={
142             "roleId", "roleNameAndNamespace", "permissionId", "permissionNameAndNamespace"
143     })
144     public static class OutsideOfRole extends RolePermissionXmlDTO {
145         
146         private static final long serialVersionUID = 1L;
147 
148         @XmlElement(name="roleId")
149         @XmlJavaTypeAdapter(NormalizedStringAdapter.class)
150         private String roleId;
151         
152         @XmlElement(name="roleName")
153         @XmlJavaTypeAdapter(NameAndNamespacePairValidatingAdapter.class)
154         private NameAndNamespacePair roleNameAndNamespace;
155         
156         public OutsideOfRole() {
157             super();
158         }
159         
160         public OutsideOfRole(PermissionContract permission, String roleId, boolean populateIds) {
161             super(permission, populateIds);
162             if (populateIds) {
163                 this.roleId = roleId;
164             }
165             RoleContract tempRole = KimApiServiceLocator.getRoleService().getRole(roleId);
166             if (tempRole == null) {
167                 throw new IllegalArgumentException("Cannot find role with ID \"" + roleId + "\"");
168             }
169             this.roleNameAndNamespace = new NameAndNamespacePair(tempRole.getNamespaceCode(), tempRole.getName());
170         }
171 
172         /**
173          * @see org.kuali.rice.kim.impl.jaxb.RolePermissionXmlDTO#getRoleId()
174          */
175         @Override
176         public String getRoleId() {
177             return this.roleId;
178         }
179 
180         /**
181          * @param roleId the roleId to set
182          */
183         public void setRoleId(String roleId) {
184             this.roleId = roleId;
185         }
186 
187         /**
188          * @return the roleNameAndNamespace
189          */
190         public NameAndNamespacePair getRoleNameAndNamespace() {
191             return this.roleNameAndNamespace;
192         }
193 
194         /**
195          * @param roleNameAndNamespace the roleNameAndNamespace to set
196          */
197         public void setRoleNameAndNamespace(NameAndNamespacePair roleNameAndNamespace) {
198             this.roleNameAndNamespace = roleNameAndNamespace;
199         }
200         
201         /**
202          * Retrieves the role name from the role-name-and-namespace combo.
203          * 
204          * @return The name of the role that is assigned to the permission, or null if the role-name-and-namespace combo is null.
205          */
206         public String getRoleName() {
207             return (roleNameAndNamespace != null) ? roleNameAndNamespace.getName() : null;
208         }
209 
210         /**
211          * Retrieves the role namespace code from the role-name-and-namespace combo.
212          * 
213          * @return The namespace code of the role that is assigned to the permission, or null if the role-name-and-namespace combo is null.
214          */
215         public String getRoleNamespaceCode() {
216             return (roleNameAndNamespace != null) ? roleNameAndNamespace.getNamespaceCode() : null;
217         }
218     }
219     
220     // =======================================================================================================
221     
222     /**
223      * This class represents a &lt;rolePermission&gt; element that is a descendant of a &lt;role&gt; element.
224      * 
225      * @author Kuali Rice Team (rice.collab@kuali.org)
226      */
227     @XmlAccessorType(XmlAccessType.FIELD)
228     @XmlType(name="RolePermissionType", propOrder={
229             "permissionId", "permissionNameAndNamespace"
230     })
231     public static class WithinRole extends RolePermissionXmlDTO {
232         
233         private static final long serialVersionUID = 1L;
234         
235         @XmlTransient
236         private String roleId;
237         
238         public WithinRole() {
239             super();
240         }
241         
242         public WithinRole(PermissionContract permission, boolean populateIds) {
243             super(permission, populateIds);
244         }
245         
246         void beforeUnmarshal(Unmarshaller unmarshaller, Object parent) {
247             if (parent instanceof RolePermissionsXmlDTO) {
248                 this.roleId = ((RolePermissionXmlDTO)parent).getRoleId();
249             }
250         }
251 
252         /**
253          * @see org.kuali.rice.kim.impl.jaxb.RolePermissionXmlDTO#getRoleId()
254          */
255         @Override
256         public String getRoleId() {
257             return this.roleId;
258         }
259     }
260 }