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.krms.impl.repository;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.exception.RiceIllegalStateException;
20  import org.kuali.rice.core.api.mo.common.Versioned;
21  import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
22  import org.kuali.rice.krms.api.repository.language.NaturalLanguageTemplate;
23  import org.kuali.rice.krms.api.repository.language.NaturalLanguageTemplateContract;
24  import org.kuali.rice.krms.api.repository.type.KrmsAttributeDefinition;
25  import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
26  import org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService;
27  
28  import javax.persistence.CascadeType;
29  import javax.persistence.Column;
30  import javax.persistence.Entity;
31  import javax.persistence.GeneratedValue;
32  import javax.persistence.Id;
33  import javax.persistence.JoinColumn;
34  import javax.persistence.OneToMany;
35  import javax.persistence.Table;
36  import javax.persistence.Transient;
37  import javax.persistence.Version;
38  import java.io.Serializable;
39  import java.util.Collection;
40  import java.util.Collections;
41  import java.util.HashMap;
42  import java.util.HashSet;
43  import java.util.LinkedList;
44  import java.util.List;
45  import java.util.Map;
46  import java.util.Set;
47  
48  /**
49   * The mutable implementation of the @{link NaturalLanguageTemplateContract} interface, the counterpart to the immutable implementation {@link NaturalLanguageTemplate}.
50   * @author Kuali Rice Team (rice.collab@kuali.org)
51   * 
52   */
53  @Entity
54  @Table(name = "KRMS_NL_TMPL_T")
55  public class NaturalLanguageTemplateBo implements NaturalLanguageTemplateContract, Versioned, Serializable {
56  
57      private static final long serialVersionUID = 1l;
58  
59      @Transient
60      private Map<String, String> attributes;
61  
62      @Column(name = "LANG_CD")
63      private String languageCode;
64  
65      @Column(name = "NL_USAGE_ID")
66      private String naturalLanguageUsageId;
67  
68      @Column(name = "TYP_ID")
69      private String typeId;
70  
71      @Column(name = "TMPL")
72      private String template;
73  
74      @PortableSequenceGenerator(name = "KRMS_NL_TMPL_S")
75      @GeneratedValue(generator = "KRMS_NL_TMPL_S")
76      @Id
77      @Column(name = "NL_TMPL_ID")
78      private String id;
79  
80      @Transient
81      private boolean active;
82  
83      @Column(name = "VER_NBR")
84      @Version
85      private Long versionNumber;
86  
87      @OneToMany(targetEntity = NaturalLanguageTemplateAttributeBo.class, orphanRemoval = true, cascade = { CascadeType.REFRESH, CascadeType.REMOVE, CascadeType.PERSIST })
88      @JoinColumn(name = "NL_TMPL_ID", referencedColumnName = "NL_TMPL_ID", insertable = false, updatable = false)
89      private Set<NaturalLanguageTemplateAttributeBo> attributeBos;
90  
91      private static KrmsAttributeDefinitionService attributeDefinitionService;
92  
93      private static KrmsTypeRepositoryService typeRepositoryService;
94  
95      /**
96       * Default Constructor
97       * 
98       */
99      public NaturalLanguageTemplateBo() {
100     }
101 
102     @Override
103     public String getLanguageCode() {
104         return this.languageCode;
105     }
106 
107     @Override
108     public String getNaturalLanguageUsageId() {
109         return this.naturalLanguageUsageId;
110     }
111 
112     @Override
113     public String getTypeId() {
114         return this.typeId;
115     }
116 
117     @Override
118     public String getTemplate() {
119         return this.template;
120     }
121 
122     @Override
123     public String getId() {
124         return this.id;
125     }
126 
127     @Override
128     public boolean isActive() {
129         return this.active;
130     }
131 
132     @Override
133     public Long getVersionNumber() {
134         return this.versionNumber;
135     }
136 
137     /**
138      * Sets the value of languageCode on this builder to the given value.
139      * 
140      * @param languageCode the languageCode value to set.
141      * 
142      */
143     public void setLanguageCode(String languageCode) {
144         this.languageCode = languageCode;
145     }
146 
147     /**
148      * Sets the value of naturalLanguageUsageId on this builder to the given value.
149      * 
150      * @param naturalLanguageUsageId the naturalLanguageUsageId value to set.
151      * 
152      */
153     public void setNaturalLanguageUsageId(String naturalLanguageUsageId) {
154         this.naturalLanguageUsageId = naturalLanguageUsageId;
155     }
156 
157     /**
158      * Sets the value of typeId on this builder to the given value.
159      * 
160      * @param typeId the typeId value to set.
161      * 
162      */
163     public void setTypeId(String typeId) {
164         this.typeId = typeId;
165     }
166 
167     /**
168      * Sets the value of template on this builder to the given value.
169      * 
170      * @param template the template value to set.
171      * 
172      */
173     public void setTemplate(String template) {
174         this.template = template;
175     }
176 
177     /**
178      * Sets the value of id on this builder to the given value.
179      * 
180      * @param id the id value to set.
181      * 
182      */
183     public void setId(String id) {
184         this.id = id;
185     }
186 
187     /**
188      * Sets the value of active on this builder to the given value.
189      * 
190      * @param active the active value to set.
191      * 
192      */
193     public void setActive(boolean active) {
194         this.active = active;
195     }
196 
197     /**
198      * Sets the value of versionNumber on this builder to the given value.
199      * 
200      * @param versionNumber the versionNumber value to set.
201      * 
202      */
203     public void setVersionNumber(Long versionNumber) {
204         this.versionNumber = versionNumber;
205     }
206 
207     /**
208      * Sets the value of AttributeBos on this builder to the given value.
209      * 
210      * @param attributeBos the AttributeBos value to set.
211      * 
212      */
213     public void setAttributeBos(List<NaturalLanguageTemplateAttributeBo> attributeBos) {
214         this.attributeBos = new HashSet<NaturalLanguageTemplateAttributeBo>(attributeBos);
215     }
216 
217     /**
218      * Sets the value of AttributeBos on this builder to the given value.
219      * 
220      * @param attributeBos the AttributeBos value to set.
221      * 
222      */
223     public void setAttributeBos(Set<NaturalLanguageTemplateAttributeBo> attributeBos) {
224         this.attributeBos = new HashSet<NaturalLanguageTemplateAttributeBo>(attributeBos);
225     }
226 
227     /**
228      * Converts a mutable {@link NaturalLanguageTemplateBo} to its immutable counterpart, {@link NaturalLanguageTemplate}.
229      * @param naturalLanguageTemplateBo the mutable business object.
230      * @return a {@link NaturalLanguageTemplate} the immutable object.
231      * 
232      */
233     public static NaturalLanguageTemplate to(NaturalLanguageTemplateBo naturalLanguageTemplateBo) {
234         if (naturalLanguageTemplateBo == null) {
235             return null;
236         }
237 
238         return NaturalLanguageTemplate.Builder.create(naturalLanguageTemplateBo).build();
239     }
240 
241     /**
242      * Converts a immutable {@link NaturalLanguageTemplate} to its mutable {@link NaturalLanguageTemplateBo} counterpart.
243      * @param naturalLanguageTemplate the immutable object.
244      * @return a {@link NaturalLanguageTemplateBo} the mutable NaturalLanguageTemplateBo.
245      * 
246      */
247     public static org.kuali.rice.krms.impl.repository.NaturalLanguageTemplateBo from(NaturalLanguageTemplate naturalLanguageTemplate) {
248         if (naturalLanguageTemplate == null) {
249             return null;
250         }
251 
252         NaturalLanguageTemplateBo naturalLanguageTemplateBo = new NaturalLanguageTemplateBo();
253         naturalLanguageTemplateBo.setLanguageCode(naturalLanguageTemplate.getLanguageCode());
254         naturalLanguageTemplateBo.setNaturalLanguageUsageId(naturalLanguageTemplate.getNaturalLanguageUsageId());
255         naturalLanguageTemplateBo.setTypeId(naturalLanguageTemplate.getTypeId());
256         naturalLanguageTemplateBo.setTemplate(naturalLanguageTemplate.getTemplate());
257         naturalLanguageTemplateBo.setId(naturalLanguageTemplate.getId());
258         naturalLanguageTemplateBo.setActive(naturalLanguageTemplate.isActive());
259         naturalLanguageTemplateBo.setVersionNumber(naturalLanguageTemplate.getVersionNumber());
260         if (StringUtils.isNotBlank(naturalLanguageTemplate.getId())) {
261            naturalLanguageTemplateBo.setAttributeBos(buildAttributeBoSet(naturalLanguageTemplate));
262         }
263         return naturalLanguageTemplateBo;
264     }
265 
266     @Override
267     public Map<String, String> getAttributes() {
268         if (attributeBos == null) {
269             return Collections.emptyMap();
270         }
271 
272         HashMap<String, String> attributes = new HashMap<String, String>(attributeBos.size());
273 
274         for (NaturalLanguageTemplateAttributeBo attr : attributeBos) {
275             attributes.put(attr.getAttributeDefinition().getName(), attr.getValue());
276         }
277 
278         return attributes;
279     }
280 
281     /**
282      * TODO
283      * 
284      */
285     public void setAttributes(Map<String, String> attributes) {
286         this.attributeBos = new HashSet<NaturalLanguageTemplateAttributeBo>();
287 
288         if (!org.apache.commons.lang.StringUtils.isBlank(this.typeId)) {
289             List<KrmsAttributeDefinition> attributeDefinitions = KrmsRepositoryServiceLocator.getKrmsAttributeDefinitionService().findAttributeDefinitionsByType(this.getTypeId());
290             Map<String, KrmsAttributeDefinition> attributeDefinitionsByName = new HashMap<String, KrmsAttributeDefinition>(attributeDefinitions.size());
291 
292             if (attributeDefinitions != null) for (KrmsAttributeDefinition attributeDefinition : attributeDefinitions) {
293                 attributeDefinitionsByName.put(attributeDefinition.getName(), attributeDefinition);
294             }
295 
296             for (Map.Entry<String, String> attr : attributes.entrySet()) {
297                 KrmsAttributeDefinition attributeDefinition = attributeDefinitionsByName.get(attr.getKey());
298                 NaturalLanguageTemplateAttributeBo attributeBo = new NaturalLanguageTemplateAttributeBo();
299                 attributeBo.setNaturalLanguageTemplateId(this.getId());
300                 attributeBo.setAttributeDefinitionId((attributeDefinition == null) ? null : attributeDefinition.getId());
301                 attributeBo.setValue(attr.getValue());
302                 attributeBo.setAttributeDefinition(KrmsAttributeDefinitionBo.from(attributeDefinition));
303                 attributeBos.add(attributeBo);
304             }
305         }
306     }
307 
308     private static Collection<NaturalLanguageTemplateAttributeBo> buildAttributes(NaturalLanguageTemplate im, Collection<NaturalLanguageTemplateAttributeBo> attributes) {
309         KrmsTypeDefinition krmsTypeDefinition = getTypeRepositoryService().getTypeById(im.getTypeId());
310 
311         // for each entry, build a NaturalLanguageTemplateAttributeBo and add it 
312         if (im.getAttributes() != null) {
313             for (Map.Entry<String, String> entry : im.getAttributes().entrySet()) {
314                 KrmsAttributeDefinition attrDef = getAttributeDefinitionService().getAttributeDefinitionByNameAndNamespace(entry.getKey(), krmsTypeDefinition.getNamespace());
315 
316                 if (attrDef != null) {
317                     NaturalLanguageTemplateAttributeBo attributeBo = new NaturalLanguageTemplateAttributeBo();
318                     attributeBo.setNaturalLanguageTemplateId(im.getId());
319                     attributeBo.setAttributeDefinitionId(attrDef.getId());
320                     attributeBo.setValue(entry.getValue());
321                     attributeBo.setAttributeDefinition(KrmsAttributeDefinitionBo.from(attrDef));
322                     attributes.add(attributeBo);
323                 } else {
324                     throw new RiceIllegalStateException("there is no attribute definition with the name '" + entry.getKey() + "' that is valid for the naturalLanguageTemplate type with id = '" + im.getTypeId() + "'");
325                 }
326             }
327         }
328 
329         return attributes;
330     }
331 
332     private static Set<NaturalLanguageTemplateAttributeBo> buildAttributeBoSet(NaturalLanguageTemplate im) {
333         Set<NaturalLanguageTemplateAttributeBo> attributes = new HashSet<NaturalLanguageTemplateAttributeBo>();
334 
335         return (Set) buildAttributes(im, attributes);
336     }
337 
338     private static List<NaturalLanguageTemplateAttributeBo> buildAttributeBoList(NaturalLanguageTemplate im) {
339         List<NaturalLanguageTemplateAttributeBo> attributes = new LinkedList<NaturalLanguageTemplateAttributeBo>();
340 
341         return (List) buildAttributes(im, attributes);
342     }
343 
344     public static void setAttributeDefinitionService(KrmsAttributeDefinitionService attributeDefinitionService) {
345         NaturalLanguageTemplateBo.attributeDefinitionService = attributeDefinitionService;
346     }
347 
348     public static KrmsTypeRepositoryService getTypeRepositoryService() {
349         if (typeRepositoryService == null) {
350             typeRepositoryService = KrmsRepositoryServiceLocator.getKrmsTypeRepositoryService();
351         }
352 
353         return typeRepositoryService;
354     }
355 
356     public static void setTypeRepositoryService(KrmsTypeRepositoryService typeRepositoryService) {
357         NaturalLanguageTemplateBo.typeRepositoryService = typeRepositoryService;
358     }
359 
360     public static KrmsAttributeDefinitionService getAttributeDefinitionService() {
361         if (attributeDefinitionService == null) {
362             attributeDefinitionService = KrmsRepositoryServiceLocator.getKrmsAttributeDefinitionService();
363         }
364 
365         return attributeDefinitionService;
366     }
367 }