001    /**
002     * Copyright 2005-2011 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.kim.impl.jaxb;
017    
018    import java.io.Serializable;
019    
020    import javax.xml.bind.Unmarshaller;
021    import javax.xml.bind.annotation.XmlAccessType;
022    import javax.xml.bind.annotation.XmlAccessorType;
023    import javax.xml.bind.annotation.XmlElement;
024    import javax.xml.bind.annotation.XmlTransient;
025    import javax.xml.bind.annotation.XmlType;
026    import javax.xml.bind.annotation.adapters.NormalizedStringAdapter;
027    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
028    
029    import org.kuali.rice.core.util.jaxb.NameAndNamespacePair;
030    import org.kuali.rice.core.util.jaxb.NameAndNamespacePairValidatingAdapter;
031    import org.kuali.rice.kim.api.permission.PermissionContract;
032    import org.kuali.rice.kim.api.role.RoleContract;
033    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
034    
035    
036    /**
037     * Base class representing an unmarshalled <rolePermission> element.
038     * Refer to the static inner classes for more information about the specific contexts.
039     * 
040     * @author Kuali Rice Team (rice.collab@kuali.org)
041     */
042    @XmlTransient
043    public abstract class RolePermissionXmlDTO implements Serializable {
044    
045        private static final long serialVersionUID = 1L;
046        
047        @XmlElement(name="permissionId")
048        @XmlJavaTypeAdapter(NormalizedStringAdapter.class)
049        private String permissionId;
050        
051        @XmlElement(name="permissionName")
052        @XmlJavaTypeAdapter(NameAndNamespacePairValidatingAdapter.class)
053        private NameAndNamespacePair permissionNameAndNamespace;
054    
055        /**
056         * Constructs an empty RolePermissionXmlDTO instance.
057         */
058        public RolePermissionXmlDTO() {}
059        
060        /**
061         * Constructs a RolePermissionXmlDTO that gets populated from the given KIM permission.
062         * 
063         * @param permission The permission that this DTO should obtain its data from.
064         * @param populateIds If true, the permission ID will get populated; otherwise, it will remain null.
065         */
066        public RolePermissionXmlDTO(PermissionContract permission, boolean populateIds) {
067            if (permission == null) {
068                throw new IllegalArgumentException("Cannot construct a role permission with a null permission");
069            }
070            if (populateIds) {
071                this.permissionId = permission.getId();
072            }
073            this.permissionNameAndNamespace = new NameAndNamespacePair(permission.getNamespaceCode(), permission.getName());
074        }
075    
076        /**
077         * @return the permissionId
078         */
079        public String getPermissionId() {
080            return this.permissionId;
081        }
082    
083        /**
084         * @param permissionId the permissionId to set
085         */
086        public void setPermissionId(String permissionId) {
087            this.permissionId = permissionId;
088        }
089    
090        /**
091         * @return the permissionNameAndNamespace
092         */
093        public NameAndNamespacePair getPermissionNameAndNamespace() {
094            return this.permissionNameAndNamespace;
095        }
096    
097        /**
098         * @param permissionNameAndNamespace the permissionNameAndNamespace to set
099         */
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    }