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; 022import java.util.ArrayList; 023import java.util.List; 024 025import javax.xml.bind.Marshaller; 026import javax.xml.bind.UnmarshalException; 027import javax.xml.bind.Unmarshaller; 028import javax.xml.bind.annotation.XmlAccessType; 029import javax.xml.bind.annotation.XmlAccessorType; 030import javax.xml.bind.annotation.XmlElement; 031import javax.xml.bind.annotation.XmlTransient; 032import javax.xml.bind.annotation.XmlType; 033 034import org.kuali.rice.core.util.jaxb.RiceXmlExportList; 035import org.kuali.rice.core.util.jaxb.RiceXmlImportList; 036import org.kuali.rice.core.util.jaxb.RiceXmlListAdditionListener; 037import org.kuali.rice.core.util.jaxb.RiceXmlListGetterListener; 038import org.kuali.rice.kim.api.permission.PermissionContract; 039import org.kuali.rice.kim.api.services.KimApiServiceLocator; 040 041/** 042 * Base class representing an unmarshalled <rolePermissions> element. 043 * Refer to the static inner classes for more information about the specific contexts. 044 * 045 * TODO: Alter the role/permission service APIs so that finding all the permissions assigned to a role is possible; the 046 * current lack of such an API method prevents role permissions from being exported. 047 * 048 * @author Kuali Rice Team (rice.collab@kuali.org) 049 */ 050@XmlTransient 051public abstract class RolePermissionsXmlDTO<T extends RolePermissionXmlDTO> implements RiceXmlListAdditionListener<T>, Serializable { 052 053 private static final long serialVersionUID = 1L; 054 055 public abstract List<T> getRolePermissions(); 056 057 public abstract void setRolePermissions(List<T> rolePermissions); 058 059 void beforeUnmarshal(Unmarshaller unmarshaller, Object parent) throws UnmarshalException { 060 setRolePermissions(new RiceXmlImportList<T>(this)); 061 } 062 063 void afterUnmarshal(Unmarshaller unmarshaller, Object parent) throws UnmarshalException { 064 setRolePermissions(null); 065 } 066 067 // ======================================================================================================= 068 069 /** 070 * This class represents a <rolePermissions> element that is not a child of a <role> element. 071 * 072 * @author Kuali Rice Team (rice.collab@kuali.org) 073 */ 074 @XmlAccessorType(XmlAccessType.FIELD) 075 @XmlType(name="StandaloneRolePermissionsType", propOrder={"rolePermissions"}) 076 public static class OutsideOfRole extends RolePermissionsXmlDTO<RolePermissionXmlDTO.OutsideOfRole> { 077 078 private static final long serialVersionUID = 1L; 079 080 @XmlElement(name="rolePermission") 081 private List<RolePermissionXmlDTO.OutsideOfRole> rolePermissions; 082 083 public List<RolePermissionXmlDTO.OutsideOfRole> getRolePermissions() { 084 return rolePermissions; 085 } 086 087 public void setRolePermissions(List<RolePermissionXmlDTO.OutsideOfRole> rolePermissions) { 088 this.rolePermissions = rolePermissions; 089 } 090 091 public void newItemAdded(RolePermissionXmlDTO.OutsideOfRole item) { 092 try { 093 RoleXmlUtil.validateAndPersistNewRolePermission(item); 094 } catch (UnmarshalException e) { 095 throw new RuntimeException(e); 096 } 097 } 098 099 } 100 101 // ======================================================================================================= 102 103 /** 104 * This class represents a <rolePermissions> element that is a child of a <role> element. 105 * 106 * @author Kuali Rice Team (rice.collab@kuali.org) 107 */ 108 @XmlAccessorType(XmlAccessType.FIELD) 109 @XmlType(name="RolePermissionsType", propOrder={"rolePermissions"}) 110 public static class WithinRole extends RolePermissionsXmlDTO<RolePermissionXmlDTO.WithinRole> 111 implements RiceXmlListGetterListener<RolePermissionXmlDTO.WithinRole,String> { 112 113 private static final long serialVersionUID = 1L; 114 115 @XmlElement(name="rolePermission") 116 private List<RolePermissionXmlDTO.WithinRole> rolePermissions; 117 118 @XmlTransient 119 private String roleId; 120 121 public WithinRole() {} 122 123 public WithinRole(String roleId) { 124 this.roleId = roleId; 125 } 126 127 public List<RolePermissionXmlDTO.WithinRole> getRolePermissions() { 128 return rolePermissions; 129 } 130 131 public void setRolePermissions(List<RolePermissionXmlDTO.WithinRole> rolePermissions) { 132 this.rolePermissions = rolePermissions; 133 } 134 135 public String getRoleId() { 136 return roleId; 137 } 138 139 @Override 140 void beforeUnmarshal(Unmarshaller unmarshaller, Object parent) throws UnmarshalException { 141 if (parent instanceof RoleXmlDTO) { 142 // Obtain the role ID from the enclosing role, and persist the role if it has not been persisted yet. 143 RoleXmlDTO parentRole = (RoleXmlDTO) parent; 144 if (!parentRole.isAlreadyPersisted()) { 145 RoleXmlUtil.validateAndPersistNewRole(parentRole); 146 } 147 roleId = parentRole.getRoleId(); 148 } 149 super.beforeUnmarshal(unmarshaller, parent); 150 } 151 152 public void newItemAdded(RolePermissionXmlDTO.WithinRole item) { 153 try { 154 RoleXmlUtil.validateAndPersistNewRolePermission(item); 155 } catch (UnmarshalException e) { 156 throw new RuntimeException(e); 157 } 158 } 159 160 void beforeMarshal(Marshaller marshaller) { 161 // TODO: Use new API method once it becomes available!!!! 162 List<String> permissionIds = new ArrayList<String>();// KIMServiceLocator.getPermissionService().getRoleIdsForPermission(permissionId); 163 if (permissionIds != null && !permissionIds.isEmpty()) { 164 setRolePermissions(new RiceXmlExportList<RolePermissionXmlDTO.WithinRole,String>(permissionIds, this)); 165 } 166 } 167 168 void afterMarshal(Marshaller marshaller) { 169 setRolePermissions(null); 170 } 171 172 public RolePermissionXmlDTO.WithinRole gettingNextItem(String nextItem, int index) { 173 PermissionContract permission = KimApiServiceLocator.getPermissionService().getPermission(nextItem); 174 if (permission == null) { 175 throw new RuntimeException("Cannot find permission with ID \"" + nextItem + "\""); 176 } 177 return new RolePermissionXmlDTO.WithinRole(permission, false); 178 } 179 } 180}