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.io.IOException;
19  import java.io.ObjectOutputStream;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Map;
23  
24  import javax.persistence.CascadeType;
25  import javax.persistence.Column;
26  import javax.persistence.Convert;
27  import javax.persistence.Entity;
28  import javax.persistence.GeneratedValue;
29  import javax.persistence.Id;
30  import javax.persistence.Inheritance;
31  import javax.persistence.InheritanceType;
32  import javax.persistence.JoinColumn;
33  import javax.persistence.ManyToOne;
34  import javax.persistence.OneToMany;
35  import javax.persistence.Table;
36  import javax.persistence.Transient;
37  
38  import org.kuali.rice.kim.api.KimConstants;
39  import org.kuali.rice.kim.api.responsibility.Responsibility;
40  import org.kuali.rice.kim.api.responsibility.ResponsibilityContract;
41  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
42  import org.kuali.rice.kim.api.type.KimType;
43  import org.kuali.rice.kim.api.type.KimTypeAttribute;
44  import org.kuali.rice.kim.api.type.KimTypeInfoService;
45  import org.kuali.rice.kim.impl.common.attribute.KimAttributeDataBo;
46  import org.kuali.rice.kim.impl.role.RoleResponsibilityBo;
47  import org.kuali.rice.krad.bo.DataObjectBase;
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  public class ResponsibilityBo extends DataObjectBase implements ResponsibilityContract {
58  
59      private static final long serialVersionUID = 1L;
60  
61      @PortableSequenceGenerator(name = "KRIM_RSP_ID_S")
62      @GeneratedValue(generator = "KRIM_RSP_ID_S")
63      @Id
64      @Column(name = "RSP_ID")
65      String id;
66  
67      @Column(name = "NMSPC_CD")
68      String namespaceCode;
69  
70      @Column(name = "NM")
71      String name;
72  
73      @Column(name = "DESC_TXT")
74      String description;
75  
76      @Column(name = "RSP_TMPL_ID")
77      String templateId;
78  
79      @Column(name = "ACTV_IND")
80      @Convert(converter = BooleanYNConverter.class)
81      boolean active;
82  
83      @ManyToOne(targetEntity = ResponsibilityTemplateBo.class, cascade = { CascadeType.REFRESH })
84      @JoinColumn(name = "RSP_TMPL_ID", referencedColumnName = "RSP_TMPL_ID", insertable = false, updatable = false)
85      ResponsibilityTemplateBo template = new ResponsibilityTemplateBo();
86  
87      @OneToMany(targetEntity = ResponsibilityAttributeBo.class, orphanRemoval = true, cascade = { CascadeType.ALL })
88      @JoinColumn(name = "RSP_ID", referencedColumnName = "RSP_ID")
89      List<ResponsibilityAttributeBo> attributeDetails = new AutoPopulatingList<ResponsibilityAttributeBo>(ResponsibilityAttributeBo.class);
90  
91      @OneToMany(mappedBy = "kimResponsibility")
92      @JoinColumn(name = "RSP_ID", referencedColumnName = "RSP_ID", insertable = false, updatable = false)
93      List<RoleResponsibilityBo> roleResponsibilities = new AutoPopulatingList<RoleResponsibilityBo>(RoleResponsibilityBo.class);
94  
95      @Transient
96      Map<String, String> attributes;
97  
98      @Override
99      public Map<String, String> getAttributes() {
100         return attributeDetails != null ? KimAttributeDataBo.toAttributes(attributeDetails) : attributes;
101     }
102 
103     /**
104      * Converts a mutable bo to its immutable counterpart
105      *
106      * @param bo the mutable business object
107      * @return the immutable object
108      */
109     public static Responsibility to(ResponsibilityContract bo) {
110         if (bo == null) {
111             return null;
112         }
113         return Responsibility.Builder.create(bo).build();
114     }
115 
116     /**
117      * Converts a immutable object to its mutable counterpart
118      *
119      * @param im immutable object
120      * @return the mutable bo
121      */
122     public static ResponsibilityBo from(Responsibility im) {
123         if (im == null) {
124             return null;
125         }
126         ResponsibilityBo bo = new ResponsibilityBo();
127         bo.id = im.getId();
128         bo.namespaceCode = im.getNamespaceCode();
129         bo.name = im.getName();
130         bo.description = im.getDescription();
131         bo.active = im.isActive();
132         bo.templateId = im.getTemplate() != null ? im.getTemplate().getId() : null;
133         bo.template = ResponsibilityTemplateBo.from(im.getTemplate());
134         bo.attributes = im.getAttributes();
135         bo.setVersionNumber(im.getVersionNumber());
136         bo.setObjectId(im.getObjectId());
137         return bo;
138     }
139 
140     @Override
141     public ResponsibilityTemplateBo getTemplate() {
142         return template;
143     }
144 
145     public String getDetailObjectsValues() {
146         StringBuffer detailObjectsToDisplayBuffer = new StringBuffer();
147         Iterator<ResponsibilityAttributeBo> respIter = attributeDetails.iterator();
148         while (respIter.hasNext()) {
149             ResponsibilityAttributeBo respAttributeData = respIter.next();
150             detailObjectsToDisplayBuffer.append(respAttributeData.getAttributeValue());
151             if (respIter.hasNext()) {
152                 detailObjectsToDisplayBuffer.append(KimConstants.KimUIConstants.COMMA_SEPARATOR);
153             }
154         }
155         return detailObjectsToDisplayBuffer.toString();
156     }
157 
158     public String getDetailObjectsToDisplay() {
159         final KimType kimType = getTypeInfoService().getKimType(getTemplate().getKimTypeId());
160         StringBuffer detailObjects = new StringBuffer();
161         Iterator<ResponsibilityAttributeBo> respIter = attributeDetails.iterator();
162         while (respIter.hasNext()) {
163             ResponsibilityAttributeBo bo = respIter.next();
164             detailObjects.append(getKimAttributeLabelFromDD(kimType.getAttributeDefinitionById(bo.getKimAttributeId()))).append(":").append(bo.getAttributeValue());
165             if (respIter.hasNext()) {
166                 detailObjects.append(KimConstants.KimUIConstants.COMMA_SEPARATOR);
167             }
168         }
169         return detailObjects.toString();
170     }
171 
172     private String getKimAttributeLabelFromDD(KimTypeAttribute attribute) {
173         return getDataDictionaryService().getAttributeLabel(attribute.getKimAttribute().getComponentName(), attribute.getKimAttribute().getAttributeName());
174     }
175 
176     private DataDictionaryService getDataDictionaryService() {
177         return KRADServiceLocatorWeb.getDataDictionaryService();
178     }
179 
180     private KimTypeInfoService getTypeInfoService() {
181         return KimApiServiceLocator.getKimTypeInfoService();
182     }
183 
184     /*
185     This is being done because there is a  major issue with lazy relationships, in ensuring that the relationship is
186     still available after the object has been detached, or serialized. For most JPA providers, after serialization
187     any lazy relationship that was not instantiated will be broken, and either throw an error when accessed,
188     or return null.
189      */
190 
191     private void writeObject(ObjectOutputStream stream) throws IOException, ClassNotFoundException {
192         attributeDetails.size();
193         roleResponsibilities.size();
194         stream.defaultWriteObject();
195     }
196 
197     @Override
198     public String getId() {
199         return id;
200     }
201 
202     public void setId(String id) {
203         this.id = id;
204     }
205 
206     @Override
207     public String getNamespaceCode() {
208         return namespaceCode;
209     }
210 
211     public void setNamespaceCode(String namespaceCode) {
212         this.namespaceCode = namespaceCode;
213     }
214 
215     @Override
216     public String getName() {
217         return name;
218     }
219 
220     public void setName(String name) {
221         this.name = name;
222     }
223 
224     @Override
225     public String getDescription() {
226         return description;
227     }
228 
229     public void setDescription(String description) {
230         this.description = description;
231     }
232 
233     public String getTemplateId() {
234         return templateId;
235     }
236 
237     public void setTemplateId(String templateId) {
238         this.templateId = templateId;
239     }
240 
241     public boolean getActive() {
242         return active;
243     }
244 
245     @Override
246     public boolean isActive() {
247         return active;
248     }
249 
250     public void setActive(boolean active) {
251         this.active = active;
252     }
253 
254     public void setTemplate(ResponsibilityTemplateBo template) {
255         this.template = template;
256     }
257 
258     public List<ResponsibilityAttributeBo> getAttributeDetails() {
259         return attributeDetails;
260     }
261 
262     public void setAttributeDetails(List<ResponsibilityAttributeBo> attributeDetails) {
263         this.attributeDetails = attributeDetails;
264     }
265 
266     public List<RoleResponsibilityBo> getRoleResponsibilities() {
267         return roleResponsibilities;
268     }
269 
270     public void setRoleResponsibilities(List<RoleResponsibilityBo> roleResponsibilities) {
271         this.roleResponsibilities = roleResponsibilities;
272     }
273 
274     public void setAttributes(Map<String, String> attributes) {
275         this.attributes = attributes;
276     }
277 }