View Javadoc

1   /**
2    * Copyright 2005-2012 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      @Transient
90      private KimTypeInfoService kimTypeInfoService;
91      @Transient
92      private DataDictionaryService dataDictionaryService;
93  
94  
95      public Map<String,String> getAttributes() {
96          return attributeDetails != null ? KimAttributeDataBo.toAttributes(attributeDetails) : attributes;
97      }
98  
99      //TODO: rename/fix later - only including this method and attributeDetails field for Role conversion
100     public Map<String,String> getDetails() {
101         return attributeDetails != null ? KimAttributeDataBo.toAttributes(attributeDetails) : attributes;
102     }
103 
104     @Override
105     public String getId() {
106         return id;
107     }
108 
109     public void setId(String id) {
110         this.id = id;
111     }
112 
113     @Override
114     public String getNamespaceCode() {
115         return namespaceCode;
116     }
117 
118     public void setNamespaceCode(String namespaceCode) {
119         this.namespaceCode = namespaceCode;
120     }
121 
122     @Override
123     public String getName() {
124         return name;
125     }
126 
127     public void setName(String name) {
128         this.name = name;
129     }
130 
131     @Override
132     public String getDescription() {
133         return description;
134     }
135 
136     public void setDescription(String description) {
137         this.description = description;
138     }
139 
140     public String getTemplateId() {
141         return templateId;
142     }
143 
144     public void setTemplateId(String templateId) {
145         this.templateId = templateId;
146     }
147 
148     @Override
149     public boolean isActive() {
150         return active;
151     }
152 
153     public void setActive(boolean active) {
154         this.active = active;
155     }
156 
157     public List<PermissionAttributeBo> getAttributeDetails() {
158         return attributeDetails;
159     }
160 
161     public void setAttributeDetails(List<PermissionAttributeBo> attributeDetails) {
162         this.attributeDetails = attributeDetails;
163     }
164 
165     public List<RolePermissionBo> getRolePermissions() {
166         return rolePermissions;
167     }
168 
169     public void setRolePermissions(List<RolePermissionBo> rolePermissions) {
170         this.rolePermissions = rolePermissions;
171     }
172 
173     public KimTypeInfoService getKimTypeInfoService() {
174         return kimTypeInfoService;
175     }
176 
177     public void setKimTypeInfoService(KimTypeInfoService kimTypeInfoService) {
178         this.kimTypeInfoService = kimTypeInfoService;
179     }
180 
181     public void setAttributes(Map<String, String> attributes) {
182         this.attributes = attributes;
183     }
184 
185     /**
186      * Converts a mutable bo to its immutable counterpart
187      * @param bo the mutable business object
188      * @return the immutable object
189      */
190     public static Permission to(PermissionBo bo) {
191         if (bo == null) {
192             return null;
193         }
194 
195         return Permission.Builder.create(bo).build();
196     }
197 
198     /**
199      * Converts a immutable object to its mutable counterpart
200      * @param im immutable object
201      * @return the mutable bo
202      */
203     public static PermissionBo from(Permission im) {
204         if (im == null) {
205             return null;
206         }
207 
208         PermissionBo bo = new PermissionBo();
209         bo.setId(im.getId());
210         bo.setNamespaceCode(im.getNamespaceCode());
211         bo.setName(im.getName());
212         bo.setDescription(im.getDescription());
213         bo.setActive(im.isActive());
214         bo.setTemplateId(im.getTemplate() != null ? im.getTemplate().getId() : null);
215         bo.setTemplate(PermissionTemplateBo.from(im.getTemplate()));
216         bo.setAttributes(im.getAttributes());
217         bo.setVersionNumber(im.getVersionNumber());
218         bo.setObjectId(im.getObjectId());
219 
220         return bo;
221     }
222 
223     @Override
224     public PermissionTemplateBo getTemplate() {
225         return template;
226     }
227 
228     public void setTemplate(PermissionTemplateBo template) {
229         this.template = template;
230     }
231 
232     public String getDetailObjectsValues() {
233         StringBuffer detailObjectsToDisplayBuffer = new StringBuffer();
234         Iterator<PermissionAttributeBo> permIter = attributeDetails.iterator();
235         while (permIter.hasNext()) {
236             PermissionAttributeBo permissionAttributeData = permIter.next();
237             detailObjectsToDisplayBuffer.append(permissionAttributeData.getAttributeValue());
238             if (permIter.hasNext()) {
239                 detailObjectsToDisplayBuffer.append(KimConstants.KimUIConstants.COMMA_SEPARATOR);
240             }
241         }
242         return detailObjectsToDisplayBuffer.toString();
243     }
244 
245     public String getDetailObjectsToDisplay() {
246         final KimType kimType = getTypeInfoService().getKimType( getTemplate().getKimTypeId() );
247 
248 
249         StringBuffer detailObjects = new StringBuffer();
250         Iterator<PermissionAttributeBo> permIter = attributeDetails.iterator();
251         while (permIter.hasNext()) {
252             PermissionAttributeBo bo = permIter.next();
253             detailObjects.append(getKimAttributeLabelFromDD(kimType.getAttributeDefinitionById(bo.getKimAttributeId())))
254                     .append(":")
255                     .append(bo.getAttributeValue());
256             if (permIter.hasNext()) {
257                 detailObjects.append(",");
258             }
259         }
260         /*return attributeDetails.collect {
261             getKimAttributeLabelFromDD(kimType.getAttributeDefinitionById(it.kimAttributeId)) + ":" + it.attributeValue
262         }.join(",")*/
263         return detailObjects.toString();
264     }
265 
266     private String getKimAttributeLabelFromDD( KimTypeAttribute attribute ){
267         return getDataDictionaryService().getAttributeLabel(attribute.getKimAttribute().getComponentName(), attribute.getKimAttribute().getAttributeName() );
268     }
269 
270 
271     private DataDictionaryService getDataDictionaryService() {
272         if(dataDictionaryService == null){
273             dataDictionaryService = KRADServiceLocatorWeb.getDataDictionaryService();
274         }
275         return dataDictionaryService;
276     }
277 
278 
279     private KimTypeInfoService getTypeInfoService() {
280         if(kimTypeInfoService == null){
281             kimTypeInfoService = KimApiServiceLocator.getKimTypeInfoService();
282         }
283         return kimTypeInfoService;
284     }
285 }