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