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.responsibility;
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.Inheritance;
29  import javax.persistence.InheritanceType;
30  import javax.persistence.JoinColumn;
31  import javax.persistence.ManyToOne;
32  import javax.persistence.OneToMany;
33  import javax.persistence.Table;
34  import javax.persistence.Transient;
35  
36  import org.kuali.rice.kim.api.KimConstants;
37  import org.kuali.rice.kim.api.responsibility.Responsibility;
38  import org.kuali.rice.kim.api.responsibility.ResponsibilityContract;
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.RoleResponsibilityBo;
45  import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
46  import org.kuali.rice.krad.data.jpa.RemoveMapping;
47  import org.kuali.rice.krad.data.jpa.RemoveMappings;
48  import org.kuali.rice.krad.data.jpa.converters.BooleanYNConverter;
49  import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
50  import org.kuali.rice.krad.service.DataDictionaryService;
51  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
52  import org.springframework.util.AutoPopulatingList;
53  
54  @Entity
55  @Table(name = "KRIM_RSP_T")
56  @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
57  @RemoveMappings( value = {
58          @RemoveMapping(name = "documentTypeName"),
59          @RemoveMapping(name = "routeNodeName"),
60          @RemoveMapping(name = "actionDetailsAtRoleMemberLevel"),
61          @RemoveMapping(name = "required"),
62          @RemoveMapping(name = "qualifierResolverProvidedIdentifier")})
63  public class ReviewResponsibilityBo extends PersistableBusinessObjectBase implements ResponsibilityContract {
64  
65      private static final long serialVersionUID = 1L;
66  
67      public static final String ACTION_DETAILS_AT_ROLE_MEMBER_LEVEL_FIELD_NAME = "actionDetailsAtRoleMemberLevel";
68  
69      @PortableSequenceGenerator(name = "KRIM_RSP_ID_S")
70      @GeneratedValue(generator = "KRIM_RSP_ID_S")
71      @Id
72      @Column(name = "RSP_ID")
73      private String id;
74  
75      @Column(name = "NMSPC_CD")
76      private String namespaceCode;
77  
78      @Column(name = "NM")
79      private String name;
80  
81      @Column(name = "DESC_TXT")
82      private String description;
83  
84      @Column(name = "RSP_TMPL_ID")
85      private String templateId;
86  
87      @Column(name = "ACTV_IND")
88      @Convert(converter = BooleanYNConverter.class)
89      private boolean active;
90  
91      private String documentTypeName;
92  
93      private String routeNodeName;
94  
95      private boolean actionDetailsAtRoleMemberLevel;
96  
97      private boolean required;
98  
99      private String qualifierResolverProvidedIdentifier;
100 
101     @ManyToOne(targetEntity = ResponsibilityTemplateBo.class, cascade = { CascadeType.REFRESH })
102     @JoinColumn(name = "RSP_TMPL_ID", referencedColumnName = "RSP_TMPL_ID", insertable = false, updatable = false)
103     private ResponsibilityTemplateBo template = new ResponsibilityTemplateBo();
104 
105     @OneToMany(targetEntity = ResponsibilityAttributeBo.class, orphanRemoval = true, cascade = { CascadeType.ALL })
106     @JoinColumn(name = "RSP_ID", referencedColumnName = "RSP_ID")
107     private List<ResponsibilityAttributeBo> attributeDetails = new AutoPopulatingList<ResponsibilityAttributeBo>(ResponsibilityAttributeBo.class);
108 
109     @OneToMany(mappedBy = "kimResponsibility")
110     @JoinColumn(name = "RSP_ID", referencedColumnName = "RSP_ID", insertable = false, updatable = false,nullable = false)
111     private List<RoleResponsibilityBo> roleResponsibilities = new AutoPopulatingList<RoleResponsibilityBo>(RoleResponsibilityBo.class);
112 
113     @Transient
114     private Map<String, String> attributes;
115 
116     @Override
117     public Map<String, String> getAttributes() {
118         return attributeDetails != null ? KimAttributeDataBo.toAttributes(attributeDetails) : attributes;
119     }
120 
121     /**
122      * Converts a mutable bo to its immutable counterpart
123      *
124      * @param bo the mutable business object
125      * @return the immutable object
126      */
127     public static Responsibility to(ResponsibilityContract bo) {
128         if (bo == null) {
129             return null;
130         }
131         return Responsibility.Builder.create(bo).build();
132     }
133 
134     /**
135      * Converts a immutable object to its mutable counterpart
136      *
137      * @param im immutable object
138      * @return the mutable bo
139      */
140     public static ResponsibilityBo from(Responsibility im) {
141         if (im == null) {
142             return null;
143         }
144         ResponsibilityBo bo = new ResponsibilityBo();
145         bo.id = im.getId();
146         bo.namespaceCode = im.getNamespaceCode();
147         bo.name = im.getName();
148         bo.description = im.getDescription();
149         bo.active = im.isActive();
150         bo.templateId = im.getTemplate() != null ? im.getTemplate().getId() : null;
151         bo.template = ResponsibilityTemplateBo.from(im.getTemplate());
152         bo.attributes = im.getAttributes();
153         bo.setVersionNumber(im.getVersionNumber());
154         bo.setObjectId(im.getObjectId());
155         return bo;
156     }
157 
158     public ResponsibilityTemplateBo getTemplate() {
159         return template;
160     }
161 
162     public String getDetailObjectsValues() {
163         StringBuffer detailObjectsToDisplayBuffer = new StringBuffer();
164         Iterator<ResponsibilityAttributeBo> respIter = attributeDetails.iterator();
165         while (respIter.hasNext()) {
166             ResponsibilityAttributeBo respAttributeData = respIter.next();
167             detailObjectsToDisplayBuffer.append(respAttributeData.getAttributeValue());
168             if (respIter.hasNext()) {
169                 detailObjectsToDisplayBuffer.append(KimConstants.KimUIConstants.COMMA_SEPARATOR);
170             }
171         }
172         return detailObjectsToDisplayBuffer.toString();
173     }
174 
175     public String getDetailObjectsToDisplay() {
176         final KimType kimType = getTypeInfoService().getKimType(getTemplate().getKimTypeId());
177         StringBuffer detailObjects = new StringBuffer();
178         Iterator<ResponsibilityAttributeBo> respIter = attributeDetails.iterator();
179         while (respIter.hasNext()) {
180             ResponsibilityAttributeBo bo = respIter.next();
181             detailObjects.append(getKimAttributeLabelFromDD(kimType.getAttributeDefinitionById(bo.getKimAttributeId()))).append(":").append(bo.getAttributeValue());
182             if (respIter.hasNext()) {
183                 detailObjects.append(KimConstants.KimUIConstants.COMMA_SEPARATOR);
184             }
185         }
186         return detailObjects.toString();
187     }
188 
189     private String getKimAttributeLabelFromDD(KimTypeAttribute attribute) {
190         return getDataDictionaryService().getAttributeLabel(attribute.getKimAttribute().getComponentName(), attribute.getKimAttribute().getAttributeName());
191     }
192 
193     private DataDictionaryService getDataDictionaryService() {
194         return KRADServiceLocatorWeb.getDataDictionaryService();
195     }
196 
197     private KimTypeInfoService getTypeInfoService() {
198         return KimApiServiceLocator.getKimTypeInfoService();
199     }
200 
201     @Override
202     public String getId() {
203         return id;
204     }
205 
206     public void setId(String id) {
207         this.id = id;
208     }
209 
210     @Override
211     public String getNamespaceCode() {
212         return namespaceCode;
213     }
214 
215     public void setNamespaceCode(String namespaceCode) {
216         this.namespaceCode = namespaceCode;
217     }
218 
219     @Override
220     public String getName() {
221         return name;
222     }
223 
224     public void setName(String name) {
225         this.name = name;
226     }
227 
228     @Override
229     public String getDescription() {
230         return description;
231     }
232 
233     public void setDescription(String description) {
234         this.description = description;
235     }
236 
237     public String getTemplateId() {
238         return templateId;
239     }
240 
241     public void setTemplateId(String templateId) {
242         this.templateId = templateId;
243     }
244 
245     public boolean getActive() {
246         return active;
247     }
248 
249     @Override
250     public boolean isActive() {
251         return active;
252     }
253 
254     public void setActive(boolean active) {
255         this.active = active;
256     }
257 
258     public void setTemplate(ResponsibilityTemplateBo template) {
259         this.template = template;
260     }
261 
262     public List<ResponsibilityAttributeBo> getAttributeDetails() {
263         return attributeDetails;
264     }
265 
266     public void setAttributeDetails(List<ResponsibilityAttributeBo> attributeDetails) {
267         this.attributeDetails = attributeDetails;
268     }
269 
270     public List<RoleResponsibilityBo> getRoleResponsibilities() {
271         return roleResponsibilities;
272     }
273 
274     public void setRoleResponsibilities(List<RoleResponsibilityBo> roleResponsibilities) {
275         this.roleResponsibilities = roleResponsibilities;
276     }
277 
278     public void setAttributes(Map<String, String> attributes) {
279         this.attributes = attributes;
280     }
281     
282 
283 
284     public ReviewResponsibilityBo() {
285     }
286 
287     public ReviewResponsibilityBo(ResponsibilityContract resp) {
288         loadFromKimResponsibility(resp);
289     }
290 
291     public void loadFromKimResponsibility(final ResponsibilityContract resp) {
292         setId(resp.getId());
293         setNamespaceCode(resp.getNamespaceCode());
294         setName(resp.getName());
295         setDescription(resp.getDescription());
296         setActive(resp.isActive());
297         setTemplateId(resp.getTemplate() != null ? resp.getTemplate().getId() : null);
298         setTemplate( ResponsibilityTemplateBo.from(resp.getTemplate()) );
299         setAttributes(resp.getAttributes());
300         setVersionNumber(resp.getVersionNumber());
301         setObjectId(resp.getObjectId());
302         Map<String, String> respDetails = resp.getAttributes();
303         documentTypeName = respDetails.get(KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME);
304         routeNodeName = respDetails.get(KimConstants.AttributeConstants.ROUTE_NODE_NAME);
305         actionDetailsAtRoleMemberLevel = Boolean.valueOf(respDetails.get(KimConstants.AttributeConstants.ACTION_DETAILS_AT_ROLE_MEMBER_LEVEL));
306         required = Boolean.valueOf(respDetails.get(KimConstants.AttributeConstants.REQUIRED));
307         qualifierResolverProvidedIdentifier = respDetails.get(KimConstants.AttributeConstants.QUALIFIER_RESOLVER_PROVIDED_IDENTIFIER);
308     }
309 
310     public String getDocumentTypeName() {
311         return documentTypeName;
312     }
313 
314     public void setDocumentTypeName(String documentTypeName) {
315         this.documentTypeName = documentTypeName;
316     }
317 
318     public String getRouteNodeName() {
319         return routeNodeName;
320     }
321 
322     public void setRouteNodeName(String routeNodeName) {
323         this.routeNodeName = routeNodeName;
324     }
325 
326     public boolean getActionDetailsAtRoleMemberLevel() {
327         return actionDetailsAtRoleMemberLevel;
328     }
329 
330     public boolean isActionDetailsAtRoleMemberLevel() {
331         return actionDetailsAtRoleMemberLevel;
332     }
333 
334     public void setActionDetailsAtRoleMemberLevel(boolean actionDetailsAtRoleMemberLevel) {
335         this.actionDetailsAtRoleMemberLevel = actionDetailsAtRoleMemberLevel;
336     }
337 
338     public boolean getRequired() {
339         return required;
340     }
341 
342     public boolean isRequired() {
343         return required;
344     }
345 
346     public void setRequired(boolean required) {
347         this.required = required;
348     }
349 
350     public String getQualifierResolverProvidedIdentifier() {
351         return qualifierResolverProvidedIdentifier;
352     }
353 
354     public void setQualifierResolverProvidedIdentifier(String qualifierResolverProvidedIdentifier) {
355         this.qualifierResolverProvidedIdentifier = qualifierResolverProvidedIdentifier;
356     }
357 }