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 java.util.Iterator;
19  import java.util.List;
20  import java.util.Map;
21  
22  import javax.persistence.CascadeType;
23  import javax.persistence.Column;
24  import javax.persistence.Convert;
25  import javax.persistence.Entity;
26  import javax.persistence.GeneratedValue;
27  import javax.persistence.Id;
28  import javax.persistence.JoinColumn;
29  import javax.persistence.ManyToOne;
30  import javax.persistence.OneToMany;
31  import javax.persistence.Table;
32  import javax.persistence.Transient;
33  
34  import org.eclipse.persistence.annotations.JoinFetch;
35  import org.eclipse.persistence.annotations.JoinFetchType;
36  import org.kuali.rice.kim.api.KimConstants;
37  import org.kuali.rice.kim.api.permission.Permission;
38  import org.kuali.rice.kim.api.permission.PermissionContract;
39  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
40  import org.kuali.rice.kim.api.type.KimType;
41  import org.kuali.rice.kim.api.type.KimTypeAttribute;
42  import org.kuali.rice.kim.api.type.KimTypeInfoService;
43  import org.kuali.rice.kim.impl.common.attribute.KimAttributeDataBo;
44  import org.kuali.rice.kim.impl.role.RolePermissionBo;
45  import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
46  import org.kuali.rice.krad.data.jpa.converters.BooleanYNConverter;
47  import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
48  import org.kuali.rice.krad.service.DataDictionaryService;
49  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
50  import org.springframework.util.AutoPopulatingList;
51  
52  @Entity
53  @Table(name = "KRIM_PERM_T")
54  public class PermissionBo extends PersistableBusinessObjectBase implements PermissionContract {
55  
56      private static final long serialVersionUID = 1L;
57  
58      @PortableSequenceGenerator(name = "KRIM_PERM_ID_S")
59      @GeneratedValue(generator = "KRIM_PERM_ID_S")
60      @Id
61      @Column(name = "PERM_ID")
62      protected String id;
63  
64      @Column(name = "NMSPC_CD")
65      protected String namespaceCode;
66  
67      @Column(name = "NM")
68      protected String name;
69  
70      @Column(name = "DESC_TXT")
71      protected String description;
72  
73      @Column(name = "PERM_TMPL_ID")
74      protected String templateId;
75  
76      @Column(name = "ACTV_IND")
77      @Convert(converter = BooleanYNConverter.class)
78      protected boolean active;
79  
80      @JoinFetch(value= JoinFetchType.OUTER)
81      @ManyToOne(targetEntity = PermissionTemplateBo.class, cascade = { CascadeType.REFRESH })
82      @JoinColumn(name = "PERM_TMPL_ID", referencedColumnName = "PERM_TMPL_ID", insertable = false, updatable = false)
83      protected PermissionTemplateBo template = new PermissionTemplateBo();
84  
85      @JoinFetch(value= JoinFetchType.OUTER)
86      @OneToMany(targetEntity = PermissionAttributeBo.class, orphanRemoval = true, cascade = { CascadeType.ALL })
87      @JoinColumn(name = "PERM_ID", referencedColumnName = "PERM_ID")
88      protected List<PermissionAttributeBo> attributeDetails;
89  
90      @Transient
91      protected Map<String, String> attributes;
92  
93      @JoinFetch(value= JoinFetchType.OUTER)
94      @OneToMany(mappedBy = "permission")
95      @JoinColumn(name = "PERM_ID", referencedColumnName = "PERM_ID", insertable = false, updatable = false)
96      protected List<RolePermissionBo> rolePermissions = new AutoPopulatingList<RolePermissionBo>(RolePermissionBo.class);
97  
98      @Override
99      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 }