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