001/*
002 * The Kuali Financial System, a comprehensive financial management system for higher education.
003 * 
004 * Copyright 2005-2014 The Kuali Foundation
005 * 
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU Affero General Public License as
008 * published by the Free Software Foundation, either version 3 of the
009 * License, or (at your option) any later version.
010 * 
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU Affero General Public License for more details.
015 * 
016 * You should have received a copy of the GNU Affero General Public License
017 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package org.kuali.rice.kim.impl.jaxb;
020
021import java.io.Serializable;
022
023import javax.xml.bind.Unmarshaller;
024import javax.xml.bind.annotation.XmlAccessType;
025import javax.xml.bind.annotation.XmlAccessorType;
026import javax.xml.bind.annotation.XmlElement;
027import javax.xml.bind.annotation.XmlTransient;
028import javax.xml.bind.annotation.XmlType;
029import javax.xml.bind.annotation.adapters.NormalizedStringAdapter;
030import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
031
032import org.kuali.rice.core.util.jaxb.NameAndNamespacePair;
033import org.kuali.rice.core.util.jaxb.NameAndNamespacePairValidatingAdapter;
034import org.kuali.rice.kim.api.permission.PermissionContract;
035import org.kuali.rice.kim.api.role.RoleContract;
036import org.kuali.rice.kim.api.services.KimApiServiceLocator;
037
038
039/**
040 * Base class representing an unmarshalled &lt;rolePermission&gt; element.
041 * Refer to the static inner classes for more information about the specific contexts.
042 * 
043 * @author Kuali Rice Team (rice.collab@kuali.org)
044 */
045@XmlTransient
046public abstract class RolePermissionXmlDTO implements Serializable {
047
048    private static final long serialVersionUID = 1L;
049    
050    @XmlElement(name="permissionId")
051    @XmlJavaTypeAdapter(NormalizedStringAdapter.class)
052    private String permissionId;
053    
054    @XmlElement(name="permissionName")
055    @XmlJavaTypeAdapter(NameAndNamespacePairValidatingAdapter.class)
056    private NameAndNamespacePair permissionNameAndNamespace;
057
058    /**
059     * Constructs an empty RolePermissionXmlDTO instance.
060     */
061    public RolePermissionXmlDTO() {}
062    
063    /**
064     * Constructs a RolePermissionXmlDTO that gets populated from the given KIM permission.
065     * 
066     * @param permission The permission that this DTO should obtain its data from.
067     * @param populateIds If true, the permission ID will get populated; otherwise, it will remain null.
068     */
069    public RolePermissionXmlDTO(PermissionContract permission, boolean populateIds) {
070        if (permission == null) {
071            throw new IllegalArgumentException("Cannot construct a role permission with a null permission");
072        }
073        if (populateIds) {
074            this.permissionId = permission.getId();
075        }
076        this.permissionNameAndNamespace = new NameAndNamespacePair(permission.getNamespaceCode(), permission.getName());
077    }
078
079    /**
080     * @return the permissionId
081     */
082    public String getPermissionId() {
083        return this.permissionId;
084    }
085
086    /**
087     * @param permissionId the permissionId to set
088     */
089    public void setPermissionId(String permissionId) {
090        this.permissionId = permissionId;
091    }
092
093    /**
094     * @return the permissionNameAndNamespace
095     */
096    public NameAndNamespacePair getPermissionNameAndNamespace() {
097        return this.permissionNameAndNamespace;
098    }
099
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}