View Javadoc
1   /**
2    * Copyright 2005-2014 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.kuali.rice.kim.api.KimConstants;
35  import org.kuali.rice.kim.api.permission.Permission;
36  import org.kuali.rice.kim.api.permission.PermissionContract;
37  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
38  import org.kuali.rice.kim.api.type.KimType;
39  import org.kuali.rice.kim.api.type.KimTypeAttribute;
40  import org.kuali.rice.kim.api.type.KimTypeInfoService;
41  import org.kuali.rice.kim.impl.common.attribute.KimAttributeDataBo;
42  import org.kuali.rice.kim.impl.role.RolePermissionBo;
43  import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
44  import org.kuali.rice.krad.data.jpa.converters.BooleanYNConverter;
45  import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
46  import org.kuali.rice.krad.service.DataDictionaryService;
47  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
48  import org.springframework.util.AutoPopulatingList;
49  
50  @Entity
51  @Table(name = "KRIM_PERM_T")
52  public class PermissionBo extends PersistableBusinessObjectBase implements PermissionContract {
53  
54      private static final long serialVersionUID = 1L;
55  
56      @PortableSequenceGenerator(name = "KRIM_PERM_ID_S")
57      @GeneratedValue(generator = "KRIM_PERM_ID_S")
58      @Id
59      @Column(name = "PERM_ID")
60      protected String id;
61  
62      @Column(name = "NMSPC_CD")
63      protected String namespaceCode;
64  
65      @Column(name = "NM")
66      protected String name;
67  
68      @Column(name = "DESC_TXT")
69      protected String description;
70  
71      @Column(name = "PERM_TMPL_ID")
72      protected String templateId;
73  
74      @Column(name = "ACTV_IND")
75      @Convert(converter = BooleanYNConverter.class)
76      protected boolean active;
77  
78      @ManyToOne(targetEntity = PermissionTemplateBo.class, cascade = { CascadeType.REFRESH })
79      @JoinColumn(name = "PERM_TMPL_ID", referencedColumnName = "PERM_TMPL_ID", insertable = false, updatable = false)
80      protected PermissionTemplateBo template = new PermissionTemplateBo();
81  
82      @OneToMany(targetEntity = PermissionAttributeBo.class, orphanRemoval = true, cascade = { CascadeType.ALL })
83      @JoinColumn(name = "PERM_ID", referencedColumnName = "PERM_ID")
84      protected List<PermissionAttributeBo> attributeDetails;
85  
86      @Transient
87      protected Map<String, String> attributes;
88  
89      @OneToMany(mappedBy = "permission")
90      @JoinColumn(name = "PERM_ID", referencedColumnName = "PERM_ID", insertable = false, updatable = false)
91      protected List<RolePermissionBo> rolePermissions = new AutoPopulatingList<RolePermissionBo>(RolePermissionBo.class);
92  
93      @Override
94      public Map<String, String> getAttributes() {
95          return attributeDetails != null ? KimAttributeDataBo.toAttributes(attributeDetails) : attributes;
96      }
97  
98      //TODO: rename/fix later - only including this method and attributeDetails field for Role conversion
99      public Map<String, String> getDetails() {
100         return attributeDetails != null ? KimAttributeDataBo.toAttributes(attributeDetails) : attributes;
101     }
102 
103     @Override
104     public String getId() {
105         return id;
106     }
107 
108     public void setId(String id) {
109         this.id = id;
110     }
111 
112     @Override
113     public String getNamespaceCode() {
114         return namespaceCode;
115     }
116 
117     public void setNamespaceCode(String namespaceCode) {
118         this.namespaceCode = namespaceCode;
119     }
120 
121     @Override
122     public String getName() {
123         return name;
124     }
125 
126     public void setName(String name) {
127         this.name = name;
128     }
129 
130     @Override
131     public String getDescription() {
132         return description;
133     }
134 
135     public void setDescription(String description) {
136         this.description = description;
137     }
138 
139     public String getTemplateId() {
140         return templateId;
141     }
142 
143     public void setTemplateId(String templateId) {
144         this.templateId = templateId;
145     }
146 
147     @Override
148     public boolean isActive() {
149         return active;
150     }
151 
152     public void setActive(boolean active) {
153         this.active = active;
154     }
155 
156     public List<PermissionAttributeBo> getAttributeDetails() {
157         return attributeDetails;
158     }
159 
160     public void setAttributeDetails(List<PermissionAttributeBo> attributeDetails) {
161         this.attributeDetails = attributeDetails;
162     }
163 
164     public List<RolePermissionBo> getRolePermissions() {
165         return rolePermissions;
166     }
167 
168     public void setRolePermissions(List<RolePermissionBo> rolePermissions) {
169         this.rolePermissions = rolePermissions;
170     }
171 
172     public void setAttributes(Map<String, String> attributes) {
173         this.attributes = attributes;
174     }
175 
176     /**
177      * Converts a mutable bo to its immutable counterpart
178      * @param bo the mutable business object
179      * @return the immutable object
180      */
181     public static Permission to(PermissionBo bo) {
182         if (bo == null) {
183             return null;
184         }
185         return Permission.Builder.create(bo).build();
186     }
187 
188     /**
189      * Converts a immutable object to its mutable counterpart
190      * @param im immutable object
191      * @return the mutable bo
192      */
193     public static PermissionBo from(Permission im) {
194         if (im == null) {
195             return null;
196         }
197         PermissionBo bo = new PermissionBo();
198         bo.setId(im.getId());
199         bo.setNamespaceCode(im.getNamespaceCode());
200         bo.setName(im.getName());
201         bo.setDescription(im.getDescription());
202         bo.setActive(im.isActive());
203         bo.setTemplateId(im.getTemplate() != null ? im.getTemplate().getId() : null);
204         bo.setTemplate(PermissionTemplateBo.from(im.getTemplate()));
205         bo.setAttributes(im.getAttributes());
206         bo.setVersionNumber(im.getVersionNumber());
207         bo.setObjectId(im.getObjectId());
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         StringBuffer detailObjects = new StringBuffer();
236         Iterator<PermissionAttributeBo> permIter = attributeDetails.iterator();
237         while (permIter.hasNext()) {
238             PermissionAttributeBo bo = permIter.next();
239             detailObjects.append(getKimAttributeLabelFromDD(kimType.getAttributeDefinitionById(bo.getKimAttributeId()))).append(":").append(bo.getAttributeValue());
240             if (permIter.hasNext()) {
241                 detailObjects.append(KimConstants.KimUIConstants.COMMA_SEPARATOR);
242             }
243         }
244         return detailObjects.toString();
245     }
246 
247     private String getKimAttributeLabelFromDD(KimTypeAttribute attribute) {
248         return getDataDictionaryService().getAttributeLabel(attribute.getKimAttribute().getComponentName(), attribute.getKimAttribute().getAttributeName());
249     }
250 
251     private DataDictionaryService getDataDictionaryService() {
252         return KRADServiceLocatorWeb.getDataDictionaryService();
253     }
254 
255     private KimTypeInfoService getTypeInfoService() {
256         return KimApiServiceLocator.getKimTypeInfoService();
257     }
258 }