001 /** 002 * Copyright 2005-2014 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.permission; 017 018 import javax.persistence.CascadeType; 019 import javax.persistence.Column; 020 import javax.persistence.Entity; 021 import javax.persistence.FetchType; 022 import javax.persistence.Id; 023 import javax.persistence.JoinColumn; 024 import javax.persistence.OneToMany; 025 import javax.persistence.OneToOne; 026 import javax.persistence.Table; 027 import javax.persistence.Transient; 028 import org.hibernate.annotations.Fetch; 029 import org.hibernate.annotations.FetchMode; 030 import org.hibernate.annotations.Type; 031 import org.kuali.rice.kim.api.KimConstants; 032 import org.kuali.rice.kim.api.permission.Permission; 033 import org.kuali.rice.kim.api.permission.PermissionContract; 034 import org.kuali.rice.kim.api.services.KimApiServiceLocator; 035 import org.kuali.rice.kim.api.type.KimType; 036 import org.kuali.rice.kim.api.type.KimTypeAttribute; 037 import org.kuali.rice.kim.api.type.KimTypeInfoService; 038 import org.kuali.rice.kim.impl.common.attribute.KimAttributeDataBo; 039 import org.kuali.rice.kim.impl.role.RolePermissionBo; 040 import org.kuali.rice.krad.bo.PersistableBusinessObjectBase; 041 import org.kuali.rice.krad.service.DataDictionaryService; 042 import org.kuali.rice.krad.service.KRADServiceLocatorWeb; 043 import org.springframework.util.AutoPopulatingList; 044 045 import java.util.Iterator; 046 import java.util.List; 047 import java.util.Map; 048 049 @Entity 050 @Table(name = "KRIM_PERM_T") 051 public class PermissionBo extends PersistableBusinessObjectBase implements PermissionContract { 052 private static final long serialVersionUID = 1L; 053 054 @Id 055 @Column(name = "PERM_ID") 056 private String id; 057 058 @Column(name = "NMSPC_CD") 059 private String namespaceCode; 060 061 @Column(name = "NM") 062 private String name; 063 064 @Column(name = "DESC_TXT", length = 400) 065 private String description; 066 067 @Column(name = "PERM_TMPL_ID") 068 private String templateId; 069 070 @Column(name = "ACTV_IND") 071 @Type(type = "yes_no") 072 private boolean active; 073 074 @OneToOne(targetEntity = PermissionTemplateBo.class, cascade = {}, fetch = FetchType.EAGER) 075 @JoinColumn(name = "PERM_TMPL_ID", insertable = false, updatable = false) 076 private PermissionTemplateBo template = new PermissionTemplateBo(); 077 078 @OneToMany(targetEntity = PermissionAttributeBo.class, cascade = {CascadeType.ALL}, fetch = FetchType.EAGER, mappedBy = "id") 079 @Fetch(value = FetchMode.SELECT) 080 private List<PermissionAttributeBo> attributeDetails; 081 082 @Transient 083 private Map<String,String> attributes; 084 085 @OneToMany(targetEntity = RolePermissionBo.class, cascade = {CascadeType.ALL}, fetch = FetchType.EAGER, mappedBy = "id") 086 @Fetch(value = FetchMode.SELECT) 087 private List<RolePermissionBo> rolePermissions = new AutoPopulatingList(RolePermissionBo.class); 088 089 090 091 public Map<String,String> getAttributes() { 092 return attributeDetails != null ? KimAttributeDataBo.toAttributes(attributeDetails) : attributes; 093 } 094 095 //TODO: rename/fix later - only including this method and attributeDetails field for Role conversion 096 public Map<String,String> getDetails() { 097 return attributeDetails != null ? KimAttributeDataBo.toAttributes(attributeDetails) : attributes; 098 } 099 100 @Override 101 public String getId() { 102 return id; 103 } 104 105 public void setId(String id) { 106 this.id = id; 107 } 108 109 @Override 110 public String getNamespaceCode() { 111 return namespaceCode; 112 } 113 114 public void setNamespaceCode(String namespaceCode) { 115 this.namespaceCode = namespaceCode; 116 } 117 118 @Override 119 public String getName() { 120 return name; 121 } 122 123 public void setName(String name) { 124 this.name = name; 125 } 126 127 @Override 128 public String getDescription() { 129 return description; 130 } 131 132 public void setDescription(String description) { 133 this.description = description; 134 } 135 136 public String getTemplateId() { 137 return templateId; 138 } 139 140 public void setTemplateId(String templateId) { 141 this.templateId = templateId; 142 } 143 144 @Override 145 public boolean isActive() { 146 return active; 147 } 148 149 public void setActive(boolean active) { 150 this.active = active; 151 } 152 153 public List<PermissionAttributeBo> getAttributeDetails() { 154 return attributeDetails; 155 } 156 157 public void setAttributeDetails(List<PermissionAttributeBo> attributeDetails) { 158 this.attributeDetails = attributeDetails; 159 } 160 161 public List<RolePermissionBo> getRolePermissions() { 162 return rolePermissions; 163 } 164 165 public void setRolePermissions(List<RolePermissionBo> rolePermissions) { 166 this.rolePermissions = rolePermissions; 167 } 168 169 public void setAttributes(Map<String, String> attributes) { 170 this.attributes = attributes; 171 } 172 173 /** 174 * Converts a mutable bo to its immutable counterpart 175 * @param bo the mutable business object 176 * @return the immutable object 177 */ 178 public static Permission to(PermissionBo bo) { 179 if (bo == null) { 180 return null; 181 } 182 183 return Permission.Builder.create(bo).build(); 184 } 185 186 /** 187 * Converts a immutable object to its mutable counterpart 188 * @param im immutable object 189 * @return the mutable bo 190 */ 191 public static PermissionBo from(Permission im) { 192 if (im == null) { 193 return null; 194 } 195 196 PermissionBo bo = new PermissionBo(); 197 bo.setId(im.getId()); 198 bo.setNamespaceCode(im.getNamespaceCode()); 199 bo.setName(im.getName()); 200 bo.setDescription(im.getDescription()); 201 bo.setActive(im.isActive()); 202 bo.setTemplateId(im.getTemplate() != null ? im.getTemplate().getId() : null); 203 bo.setTemplate(PermissionTemplateBo.from(im.getTemplate())); 204 bo.setAttributes(im.getAttributes()); 205 bo.setVersionNumber(im.getVersionNumber()); 206 bo.setObjectId(im.getObjectId()); 207 208 return bo; 209 } 210 211 @Override 212 public PermissionTemplateBo getTemplate() { 213 return template; 214 } 215 216 public void setTemplate(PermissionTemplateBo template) { 217 this.template = template; 218 } 219 220 public String getDetailObjectsValues() { 221 StringBuffer detailObjectsToDisplayBuffer = new StringBuffer(); 222 Iterator<PermissionAttributeBo> permIter = attributeDetails.iterator(); 223 while (permIter.hasNext()) { 224 PermissionAttributeBo permissionAttributeData = permIter.next(); 225 detailObjectsToDisplayBuffer.append(permissionAttributeData.getAttributeValue()); 226 if (permIter.hasNext()) { 227 detailObjectsToDisplayBuffer.append(KimConstants.KimUIConstants.COMMA_SEPARATOR); 228 } 229 } 230 return detailObjectsToDisplayBuffer.toString(); 231 } 232 233 public String getDetailObjectsToDisplay() { 234 final KimType kimType = getTypeInfoService().getKimType( getTemplate().getKimTypeId() ); 235 236 237 StringBuffer detailObjects = new StringBuffer(); 238 Iterator<PermissionAttributeBo> permIter = attributeDetails.iterator(); 239 while (permIter.hasNext()) { 240 PermissionAttributeBo bo = permIter.next(); 241 detailObjects.append(getKimAttributeLabelFromDD(kimType.getAttributeDefinitionById(bo.getKimAttributeId()))) 242 .append(":") 243 .append(bo.getAttributeValue()); 244 if (permIter.hasNext()) { 245 detailObjects.append(KimConstants.KimUIConstants.COMMA_SEPARATOR); 246 } 247 } 248 249 return detailObjects.toString(); 250 } 251 252 private String getKimAttributeLabelFromDD( KimTypeAttribute attribute ){ 253 return getDataDictionaryService().getAttributeLabel(attribute.getKimAttribute().getComponentName(), attribute.getKimAttribute().getAttributeName() ); 254 } 255 256 257 private DataDictionaryService getDataDictionaryService() { 258 return KRADServiceLocatorWeb.getDataDictionaryService(); 259 } 260 261 262 private KimTypeInfoService getTypeInfoService() { 263 return KimApiServiceLocator.getKimTypeInfoService(); 264 } 265 }