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