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.DataObjectBase;
46  import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
47  import org.kuali.rice.krad.data.jpa.converters.BooleanYNConverter;
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_RSP_T")
54  @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
55  public class ResponsibilityBo extends DataObjectBase implements ResponsibilityContract {
56  
57      private static final long serialVersionUID = 1L;
58  
59      @PortableSequenceGenerator(name = "KRIM_RSP_ID_S")
60      @GeneratedValue(generator = "KRIM_RSP_ID_S")
61      @Id
62      @Column(name = "RSP_ID")
63      String id;
64  
65      @Column(name = "NMSPC_CD")
66      String namespaceCode;
67  
68      @Column(name = "NM")
69      String name;
70  
71      @Column(name = "DESC_TXT")
72      String description;
73  
74      @Column(name = "RSP_TMPL_ID")
75      String templateId;
76  
77      @Column(name = "ACTV_IND")
78      @Convert(converter = BooleanYNConverter.class)
79      boolean active;
80  
81      @ManyToOne(targetEntity = ResponsibilityTemplateBo.class, cascade = { CascadeType.REFRESH })
82      @JoinColumn(name = "RSP_TMPL_ID", referencedColumnName = "RSP_TMPL_ID", insertable = false, updatable = false)
83      ResponsibilityTemplateBo template = new ResponsibilityTemplateBo();
84  
85      @OneToMany(targetEntity = ResponsibilityAttributeBo.class, orphanRemoval = true, cascade = { CascadeType.ALL })
86      @JoinColumn(name = "RSP_ID", referencedColumnName = "RSP_ID")
87      List<ResponsibilityAttributeBo> attributeDetails = new AutoPopulatingList<ResponsibilityAttributeBo>(ResponsibilityAttributeBo.class);
88  
89      @OneToMany(mappedBy = "kimResponsibility")
90      @JoinColumn(name = "RSP_ID", referencedColumnName = "RSP_ID", insertable = false, updatable = false)
91      List<RoleResponsibilityBo> roleResponsibilities = new AutoPopulatingList<RoleResponsibilityBo>(RoleResponsibilityBo.class);
92  
93      @Transient
94      Map<String, String> attributes;
95  
96      @Override
97      public Map<String, String> getAttributes() {
98          return attributeDetails != null ? KimAttributeDataBo.toAttributes(attributeDetails) : attributes;
99      }
100 
101     /**
102      * Converts a mutable bo to its immutable counterpart
103      *
104      * @param bo the mutable business object
105      * @return the immutable object
106      */
107     public static Responsibility to(ResponsibilityContract bo) {
108         if (bo == null) {
109             return null;
110         }
111         return Responsibility.Builder.create(bo).build();
112     }
113 
114     /**
115      * Converts a immutable object to its mutable counterpart
116      *
117      * @param im immutable object
118      * @return the mutable bo
119      */
120     public static ResponsibilityBo from(Responsibility im) {
121         if (im == null) {
122             return null;
123         }
124         ResponsibilityBo bo = new ResponsibilityBo();
125         bo.id = im.getId();
126         bo.namespaceCode = im.getNamespaceCode();
127         bo.name = im.getName();
128         bo.description = im.getDescription();
129         bo.active = im.isActive();
130         bo.templateId = im.getTemplate() != null ? im.getTemplate().getId() : null;
131         bo.template = ResponsibilityTemplateBo.from(im.getTemplate());
132         bo.attributes = im.getAttributes();
133         bo.setVersionNumber(im.getVersionNumber());
134         bo.setObjectId(im.getObjectId());
135         return bo;
136     }
137 
138     @Override
139     public ResponsibilityTemplateBo getTemplate() {
140         return template;
141     }
142 
143     public String getDetailObjectsValues() {
144         StringBuffer detailObjectsToDisplayBuffer = new StringBuffer();
145         Iterator<ResponsibilityAttributeBo> respIter = attributeDetails.iterator();
146         while (respIter.hasNext()) {
147             ResponsibilityAttributeBo respAttributeData = respIter.next();
148             detailObjectsToDisplayBuffer.append(respAttributeData.getAttributeValue());
149             if (respIter.hasNext()) {
150                 detailObjectsToDisplayBuffer.append(KimConstants.KimUIConstants.COMMA_SEPARATOR);
151             }
152         }
153         return detailObjectsToDisplayBuffer.toString();
154     }
155 
156     public String getDetailObjectsToDisplay() {
157         final KimType kimType = getTypeInfoService().getKimType(getTemplate().getKimTypeId());
158         StringBuffer detailObjects = new StringBuffer();
159         Iterator<ResponsibilityAttributeBo> respIter = attributeDetails.iterator();
160         while (respIter.hasNext()) {
161             ResponsibilityAttributeBo bo = respIter.next();
162             detailObjects.append(getKimAttributeLabelFromDD(kimType.getAttributeDefinitionById(bo.getKimAttributeId()))).append(":").append(bo.getAttributeValue());
163             if (respIter.hasNext()) {
164                 detailObjects.append(KimConstants.KimUIConstants.COMMA_SEPARATOR);
165             }
166         }
167         return detailObjects.toString();
168     }
169 
170     private String getKimAttributeLabelFromDD(KimTypeAttribute attribute) {
171         return getDataDictionaryService().getAttributeLabel(attribute.getKimAttribute().getComponentName(), attribute.getKimAttribute().getAttributeName());
172     }
173 
174     private DataDictionaryService getDataDictionaryService() {
175         return KRADServiceLocatorWeb.getDataDictionaryService();
176     }
177 
178     private KimTypeInfoService getTypeInfoService() {
179         return KimApiServiceLocator.getKimTypeInfoService();
180     }
181 
182     @Override
183     public String getId() {
184         return id;
185     }
186 
187     public void setId(String id) {
188         this.id = id;
189     }
190 
191     @Override
192     public String getNamespaceCode() {
193         return namespaceCode;
194     }
195 
196     public void setNamespaceCode(String namespaceCode) {
197         this.namespaceCode = namespaceCode;
198     }
199 
200     @Override
201     public String getName() {
202         return name;
203     }
204 
205     public void setName(String name) {
206         this.name = name;
207     }
208 
209     @Override
210     public String getDescription() {
211         return description;
212     }
213 
214     public void setDescription(String description) {
215         this.description = description;
216     }
217 
218     public String getTemplateId() {
219         return templateId;
220     }
221 
222     public void setTemplateId(String templateId) {
223         this.templateId = templateId;
224     }
225 
226     public boolean getActive() {
227         return active;
228     }
229 
230     @Override
231     public boolean isActive() {
232         return active;
233     }
234 
235     public void setActive(boolean active) {
236         this.active = active;
237     }
238 
239     public void setTemplate(ResponsibilityTemplateBo template) {
240         this.template = template;
241     }
242 
243     public List<ResponsibilityAttributeBo> getAttributeDetails() {
244         return attributeDetails;
245     }
246 
247     public void setAttributeDetails(List<ResponsibilityAttributeBo> attributeDetails) {
248         this.attributeDetails = attributeDetails;
249     }
250 
251     public List<RoleResponsibilityBo> getRoleResponsibilities() {
252         return roleResponsibilities;
253     }
254 
255     public void setRoleResponsibilities(List<RoleResponsibilityBo> roleResponsibilities) {
256         this.roleResponsibilities = roleResponsibilities;
257     }
258 
259     public void setAttributes(Map<String, String> attributes) {
260         this.attributes = attributes;
261     }
262 }