View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kim.impl.permission;
17  
18  import javax.persistence.CascadeType;
19  import javax.persistence.Column;
20  import javax.persistence.Entity;
21  import javax.persistence.FetchType;
22  import javax.persistence.Id;
23  import javax.persistence.JoinColumn;
24  import javax.persistence.OneToMany;
25  import javax.persistence.OneToOne;
26  import javax.persistence.Table;
27  import javax.persistence.Transient;
28  import org.hibernate.annotations.Fetch;
29  import org.hibernate.annotations.FetchMode;
30  import org.hibernate.annotations.Type;
31  import org.kuali.rice.kim.api.KimConstants;
32  import org.kuali.rice.kim.api.permission.Permission;
33  import org.kuali.rice.kim.api.permission.PermissionContract;
34  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
35  import org.kuali.rice.kim.api.type.KimType;
36  import org.kuali.rice.kim.api.type.KimTypeAttribute;
37  import org.kuali.rice.kim.api.type.KimTypeInfoService;
38  import org.kuali.rice.kim.impl.common.attribute.KimAttributeDataBo;
39  import org.kuali.rice.kim.impl.role.RolePermissionBo;
40  import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
41  import org.kuali.rice.krad.service.DataDictionaryService;
42  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
43  import org.springframework.util.AutoPopulatingList;
44  
45  import java.util.Iterator;
46  import java.util.List;
47  import java.util.Map;
48  
49  @Entity
50  @Table(name = "KRIM_PERM_T")
51  public class PermissionBo extends PersistableBusinessObjectBase implements PermissionContract {
52      private static final long serialVersionUID = 1L;
53  
54      @Id
55      @Column(name = "PERM_ID")
56      private String id;
57  
58      @Column(name = "NMSPC_CD")
59      private String namespaceCode;
60  
61      @Column(name = "NM")
62      private String name;
63  
64      @Column(name = "DESC_TXT", length = 400)
65      private String description;
66  
67      @Column(name = "PERM_TMPL_ID")
68      private String templateId;
69  
70      @Column(name = "ACTV_IND")
71      @Type(type = "yes_no")
72      private boolean active;
73  
74      @OneToOne(targetEntity = PermissionTemplateBo.class, cascade = {}, fetch = FetchType.EAGER)
75      @JoinColumn(name = "PERM_TMPL_ID", insertable = false, updatable = false)
76      private PermissionTemplateBo template = new PermissionTemplateBo();
77  
78      @OneToMany(targetEntity = PermissionAttributeBo.class, cascade = {CascadeType.ALL}, fetch = FetchType.EAGER, mappedBy = "id")
79      @Fetch(value = FetchMode.SELECT)
80      private List<PermissionAttributeBo> attributeDetails;
81  
82      @Transient
83      private Map<String,String> attributes;
84  
85      @OneToMany(targetEntity = RolePermissionBo.class, cascade = {CascadeType.ALL}, fetch = FetchType.EAGER, mappedBy = "id")
86      @Fetch(value = FetchMode.SELECT)
87      private List<RolePermissionBo> rolePermissions = new AutoPopulatingList(RolePermissionBo.class);
88  
89  
90  
91      public Map<String,String> getAttributes() {
92          return attributeDetails != null ? KimAttributeDataBo.toAttributes(attributeDetails) : attributes;
93      }
94  
95      //TODO: rename/fix later - only including this method and attributeDetails field for Role conversion
96      public Map<String,String> getDetails() {
97          return attributeDetails != null ? KimAttributeDataBo.toAttributes(attributeDetails) : attributes;
98      }
99  
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 }