1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
39
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
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
104
105
106
107
108 public void setLanguageCode(String languageCode) {
109 this.languageCode = languageCode;
110 }
111
112
113
114
115
116
117
118 public void setNaturalLanguageUsageId(String naturalLanguageUsageId) {
119 this.naturalLanguageUsageId = naturalLanguageUsageId;
120 }
121
122
123
124
125
126
127
128 public void setTypeId(String typeId) {
129 this.typeId = typeId;
130 }
131
132
133
134
135
136
137
138 public void setTemplate(String template) {
139 this.template = template;
140 }
141
142
143
144
145
146
147
148 public void setId(String id) {
149 this.id = id;
150 }
151
152
153
154
155
156
157
158 public void setActive(boolean active) {
159 this.active = active;
160 }
161
162
163
164
165
166
167
168 public void setVersionNumber(Long versionNumber) {
169 this.versionNumber = versionNumber;
170 }
171
172
173
174
175
176
177
178 public void setAttributeBos(List<NaturalLanguageTemplateAttributeBo> attributeBos) {
179 this.attributeBos = new HashSet<NaturalLanguageTemplateAttributeBo>(attributeBos);
180 }
181
182
183
184
185
186
187
188 public void setAttributeBos(Set<NaturalLanguageTemplateAttributeBo> attributeBos) {
189 this.attributeBos = new HashSet<NaturalLanguageTemplateAttributeBo>(attributeBos);
190 }
191
192
193
194
195
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
205
206
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
220 naturalLanguageTemplateBo.setAttributeBos(buildAttributeBoSet(naturalLanguageTemplate));
221
222 return naturalLanguageTemplateBo;
223 }
224
225
226
227
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
240
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
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
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;
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 }