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