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.krad.service.BusinessObjectService;
21 import org.kuali.rice.krms.api.repository.language.NaturalLanguageTemplate;
22 import org.kuali.rice.krms.api.repository.language.NaturalLanguageTemplaterContract;
23 import org.kuali.rice.krms.api.repository.type.KrmsAttributeDefinition;
24 import org.kuali.rice.krms.impl.util.KrmsImplConstants;
25
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.HashSet;
30 import java.util.LinkedList;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Set;
34 import org.kuali.rice.krms.impl.repository.language.SimpleNaturalLanguageTemplater;
35
36
37
38
39
40
41
42 public final class NaturalLanguageTemplateBoServiceImpl
43 implements NaturalLanguageTemplateBoService
44 {
45
46 private BusinessObjectService businessObjectService;
47 private KrmsAttributeDefinitionService attributeDefinitionService;
48 private NaturalLanguageTemplaterContract naturalLanguageTemplater = new SimpleNaturalLanguageTemplater ();
49
50
51
52
53
54
55
56 public void setBusinessObjectService(BusinessObjectService businessObjectService) {
57 this.businessObjectService = businessObjectService;
58 }
59
60 public void setAttributeDefinitionService(KrmsAttributeDefinitionService attributeDefinitionService) {
61 this.attributeDefinitionService = attributeDefinitionService;
62 }
63
64 public KrmsAttributeDefinitionService getAttributeDefinitionService() {
65 if (attributeDefinitionService == null) {
66 attributeDefinitionService = KrmsRepositoryServiceLocator.getKrmsAttributeDefinitionService();
67 }
68 return attributeDefinitionService;
69 }
70
71 public NaturalLanguageTemplaterContract getNaturalLanguageTemplater() {
72 return naturalLanguageTemplater;
73 }
74
75 public void setNaturalLanguageTemplater(NaturalLanguageTemplaterContract naturalLanguageTemplater) {
76 this.naturalLanguageTemplater = naturalLanguageTemplater;
77 }
78
79 @Override
80 public NaturalLanguageTemplate createNaturalLanguageTemplate(NaturalLanguageTemplate naturalLanguageTemplate) {
81 incomingParamCheck(naturalLanguageTemplate , "naturalLanguageTemplate");
82 final String naturalLanguageTemplateIdKey = naturalLanguageTemplate.getId();
83 final NaturalLanguageTemplate existing = getNaturalLanguageTemplate(naturalLanguageTemplateIdKey);
84 if (existing != null){ throw new IllegalStateException("the NaturalLanguageTemplate to create already exists: " + naturalLanguageTemplate); }
85 NaturalLanguageTemplateBo bo = (NaturalLanguageTemplateBo)businessObjectService.save(from(naturalLanguageTemplate));
86 return NaturalLanguageTemplateBo.to(bo);
87 }
88
89 @Override
90 public NaturalLanguageTemplate getNaturalLanguageTemplate(String naturalLanguageTemplateId) {
91 incomingParamCheck(naturalLanguageTemplateId , "naturalLanguageTemplateId");
92 NaturalLanguageTemplateBo bo = businessObjectService.findBySinglePrimaryKey(NaturalLanguageTemplateBo.class, naturalLanguageTemplateId);
93 return NaturalLanguageTemplateBo.to(bo);
94 }
95
96 @Override
97 public void updateNaturalLanguageTemplate(NaturalLanguageTemplate naturalLanguageTemplate) {
98 incomingParamCheck(naturalLanguageTemplate , "naturalLanguageTemplate");
99 final NaturalLanguageTemplate existing = getNaturalLanguageTemplate(naturalLanguageTemplate.getId());
100 if (existing == null){ throw new IllegalStateException("the NaturalLanguageTemplate to update does not exists: " + naturalLanguageTemplate);}
101 final NaturalLanguageTemplate toUpdate;
102 if (!existing.getId().equals(naturalLanguageTemplate.getId())){
103
104 final NaturalLanguageTemplate.Builder builder = NaturalLanguageTemplate.Builder.create(naturalLanguageTemplate);
105 builder.setId(existing.getId());
106 toUpdate = builder.build();
107 } else {
108 toUpdate = naturalLanguageTemplate;
109 }
110
111
112 NaturalLanguageTemplateBo boToUpdate = from(toUpdate);
113
114
115 Map<String,String> fields = new HashMap<String,String>(1);
116 fields.put(KrmsImplConstants.PropertyNames.NaturalLanguageTemplate.NATURAL_LANGUAGE_TEMPLATE_ID, toUpdate.getId());
117 businessObjectService.deleteMatching(NaturalLanguageTemplateAttributeBo.class, fields);
118
119
120 businessObjectService.save(boToUpdate);
121 }
122
123 @Override
124 public void deleteNaturalLanguageTemplate(String naturalLanguageTemplateId) {
125 incomingParamCheck(naturalLanguageTemplateId , "naturalLanguageTemplateId");
126 final NaturalLanguageTemplate existing = getNaturalLanguageTemplate(naturalLanguageTemplateId);
127 if (existing == null){ throw new IllegalStateException("the NaturalLanguageTemplate to delete does not exists: " + naturalLanguageTemplateId);}
128 businessObjectService.delete(from(existing));
129 }
130
131 @Override
132 public List<NaturalLanguageTemplate> findNaturalLanguageTemplatesByAttributes(Map attributes) {
133 if (attributes == null) {
134 throw new IllegalArgumentException("attributes is null");
135 }
136 final Map<String, Object> map = new HashMap<String, Object>();
137 map.put("attributes", attributes);
138 List<NaturalLanguageTemplateBo> bos = (List<NaturalLanguageTemplateBo>) businessObjectService.findMatching(NaturalLanguageTemplateBo.class, map);
139 return convertBosToImmutables(bos);
140 }
141
142 @Override
143 public List<NaturalLanguageTemplate> findNaturalLanguageTemplatesByLanguageCode(String languageCode) {
144 if (org.apache.commons.lang.StringUtils.isBlank(languageCode)) {
145 throw new IllegalArgumentException("languageCode is null or blank");
146 }
147 final Map<String, Object> map = new HashMap<String, Object>();
148 map.put("languageCode", languageCode);
149 List<NaturalLanguageTemplateBo> bos = (List<NaturalLanguageTemplateBo>) businessObjectService.findMatching(NaturalLanguageTemplateBo.class, map);
150 return convertBosToImmutables(bos);
151 }
152
153 @Override
154 public NaturalLanguageTemplate findNaturalLanguageTemplateByLanguageCodeTypeIdAndNluId(String languageCode, String typeId, String naturalLanguageUsageId) {
155 if (org.apache.commons.lang.StringUtils.isBlank(languageCode)) {
156 throw new IllegalArgumentException("languageCode is null or blank");
157 }
158 final Map<String, Object> map = new HashMap<String, Object>();
159 map.put("languageCode", languageCode);
160 map.put("naturalLanguageUsageId", naturalLanguageUsageId);
161 map.put("typeId", typeId);
162 List<NaturalLanguageTemplateBo> bos = (List<NaturalLanguageTemplateBo>) businessObjectService.findMatching(NaturalLanguageTemplateBo.class, map);
163 return convertBosToImmutables(bos).get(0);
164 }
165
166 @Override
167 public List<NaturalLanguageTemplate> findNaturalLanguageTemplatesByNaturalLanguageUsage(String naturalLanguageUsageId) {
168 if (org.apache.commons.lang.StringUtils.isBlank(naturalLanguageUsageId)) {
169 throw new IllegalArgumentException("naturalLanguageUsageId is null or blank");
170 }
171 final Map<String, Object> map = new HashMap<String, Object>();
172 map.put("naturalLanguageUsageId", naturalLanguageUsageId);
173 List<NaturalLanguageTemplateBo> bos = (List<NaturalLanguageTemplateBo>) businessObjectService.findMatching(NaturalLanguageTemplateBo.class, map);
174 return convertBosToImmutables(bos);
175 }
176
177 @Override
178 public List<NaturalLanguageTemplate> findNaturalLanguageTemplatesByType(String typeId) {
179 if (org.apache.commons.lang.StringUtils.isBlank(typeId)) {
180 throw new IllegalArgumentException("typeId is null or blank");
181 }
182 final Map<String, Object> map = new HashMap<String, Object>();
183 map.put("typeId", typeId);
184 List<NaturalLanguageTemplateBo> bos = (List<NaturalLanguageTemplateBo>) businessObjectService.findMatching(NaturalLanguageTemplateBo.class, map);
185 return convertBosToImmutables(bos);
186 }
187
188 @Override
189 public List<NaturalLanguageTemplate> findNaturalLanguageTemplatesByTemplate(String template) {
190 if (org.apache.commons.lang.StringUtils.isBlank(template)) {
191 throw new IllegalArgumentException("template is null or blank");
192 }
193 final Map<String, Object> map = new HashMap<String, Object>();
194 map.put("template", template);
195 List<NaturalLanguageTemplateBo> bos = (List<NaturalLanguageTemplateBo>) businessObjectService.findMatching(NaturalLanguageTemplateBo.class, map);
196 return convertBosToImmutables(bos);
197 }
198
199 public List<NaturalLanguageTemplate> convertBosToImmutables(final Collection<NaturalLanguageTemplateBo> naturalLanguageTemplateBos) {
200 List<NaturalLanguageTemplate> immutables = new LinkedList<NaturalLanguageTemplate>();
201 if (naturalLanguageTemplateBos != null) {
202 NaturalLanguageTemplate immutable = null;
203 for (NaturalLanguageTemplateBo bo : naturalLanguageTemplateBos ) {
204 immutable = to(bo);
205 immutables.add(immutable);
206 }
207 }
208 return Collections.unmodifiableList(immutables);
209 }
210
211 @Override
212 public NaturalLanguageTemplate to(NaturalLanguageTemplateBo naturalLanguageTemplateBo) {
213 return NaturalLanguageTemplateBo.to(naturalLanguageTemplateBo);
214 }
215
216 public NaturalLanguageTemplateBo from(NaturalLanguageTemplate naturalLanguageTemplate) {
217 return NaturalLanguageTemplateBo.from(naturalLanguageTemplate);
218 }
219
220 private Collection<NaturalLanguageTemplateAttributeBo> buildAttributes(NaturalLanguageTemplate im, Collection<NaturalLanguageTemplateAttributeBo> attributes) {
221
222 Map<String, KrmsAttributeDefinition> attributeDefinitionMap = new HashMap<String, KrmsAttributeDefinition>();
223
224 List<KrmsAttributeDefinition> attributeDefinitions = getAttributeDefinitionService().findAttributeDefinitionsByType(im.getTypeId());
225
226 for (KrmsAttributeDefinition attributeDefinition : attributeDefinitions) {
227 attributeDefinitionMap.put(attributeDefinition.getName(), attributeDefinition);
228 }
229
230
231 if (im.getAttributes() != null) {
232 for (Map.Entry<String,String> entry : im.getAttributes().entrySet()) {
233 KrmsAttributeDefinition attrDef = attributeDefinitionMap.get(entry.getKey());
234
235 if (attrDef != null) {
236 NaturalLanguageTemplateAttributeBo attributeBo = new NaturalLanguageTemplateAttributeBo();
237 attributeBo.setNaturalLanguageTemplateId( im.getId() );
238 attributeBo.setAttributeDefinitionId(attrDef.getId());
239 attributeBo.setValue(entry.getValue());
240 attributeBo.setAttributeDefinition(KrmsAttributeDefinitionBo.from(attrDef));
241 attributes.add(attributeBo);
242 } else {
243 throw new RiceIllegalStateException("there is no attribute definition with the name '" +
244 entry.getKey() + "' that is valid for the naturalLanguageTemplate type with id = '" + im.getTypeId() +"'");
245 }
246 }
247 }
248 return attributes;
249 }
250
251 private Set<NaturalLanguageTemplateAttributeBo> buildAttributeBoSet(NaturalLanguageTemplate im) {
252 Set<NaturalLanguageTemplateAttributeBo> attributes = new HashSet<NaturalLanguageTemplateAttributeBo>();
253 return (Set)buildAttributes(im, attributes);
254 }
255
256 private List<NaturalLanguageTemplateAttributeBo> buildAttributeBoList(NaturalLanguageTemplate im) {
257 List<NaturalLanguageTemplateAttributeBo> attributes = new LinkedList<NaturalLanguageTemplateAttributeBo>();
258 return (List)buildAttributes(im, attributes);
259 }
260
261 private void incomingParamCheck(Object object, String name) {
262 if (object == null) {
263 throw new IllegalArgumentException(name + " was null");
264 } else if (object instanceof String
265 && StringUtils.isBlank((String)object)) {
266 throw new IllegalArgumentException(name + " was blank");
267 }
268 }
269
270 }