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