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.RiceIllegalArgumentException;
20  import org.kuali.rice.core.api.exception.RiceIllegalStateException;
21  import org.kuali.rice.krad.data.DataObjectService;
22  import org.kuali.rice.krad.data.PersistenceOption;
23  import org.kuali.rice.krms.api.repository.language.NaturalLanguageTemplate;
24  import org.kuali.rice.krms.api.repository.language.NaturalLanguageTemplaterContract;
25  import org.kuali.rice.krms.api.repository.type.KrmsAttributeDefinition;
26  import org.kuali.rice.krms.impl.repository.language.SimpleNaturalLanguageTemplater;
27  import org.kuali.rice.krms.impl.util.KrmsImplConstants;
28  
29  import java.util.Collection;
30  import java.util.Collections;
31  import java.util.HashMap;
32  import java.util.HashSet;
33  import java.util.LinkedList;
34  import java.util.List;
35  import java.util.Map;
36  import java.util.Set;
37  
38  import static org.kuali.rice.krms.impl.repository.BusinessObjectServiceMigrationUtils.deleteMatching;
39  import static org.kuali.rice.krms.impl.repository.BusinessObjectServiceMigrationUtils.findMatching;
40  
41  /**
42   * Implementation of the @{link NaturalLanguageTemplateBoService} interface for accessing  {@link NaturalLanguageTemplateBo} related business objects.
43   * 
44   * @author Kuali Rice Team (rice.collab@kuali.org)
45   * 
46   */
47  public final class NaturalLanguageTemplateBoServiceImpl implements NaturalLanguageTemplateBoService {
48  
49      private DataObjectService dataObjectService;
50      private KrmsAttributeDefinitionService attributeDefinitionService;
51      private NaturalLanguageTemplaterContract naturalLanguageTemplater = new SimpleNaturalLanguageTemplater ();
52  
53      /**
54       * Sets the value of DataObjectService to the given value.
55       * 
56       * @param dataObjectService the DataObjectService value to set.
57       * 
58       */
59      public void setDataObjectService(DataObjectService dataObjectService) {
60          this.dataObjectService = dataObjectService;
61      }
62  
63      public void setAttributeDefinitionService(KrmsAttributeDefinitionService attributeDefinitionService) {
64          this.attributeDefinitionService = attributeDefinitionService;
65      }
66  
67      public KrmsAttributeDefinitionService getAttributeDefinitionService() {
68          if (attributeDefinitionService == null) {
69              attributeDefinitionService = KrmsRepositoryServiceLocator.getKrmsAttributeDefinitionService();
70          }
71  
72          return attributeDefinitionService;
73      }
74  
75      public NaturalLanguageTemplaterContract getNaturalLanguageTemplater() {
76          return naturalLanguageTemplater;
77      }
78  
79      public void setNaturalLanguageTemplater(NaturalLanguageTemplaterContract naturalLanguageTemplater) {
80          this.naturalLanguageTemplater = naturalLanguageTemplater;
81      }
82  
83      @Override
84      public NaturalLanguageTemplate createNaturalLanguageTemplate(NaturalLanguageTemplate naturalLanguageTemplate) {
85          incomingParamCheck(naturalLanguageTemplate , "naturalLanguageTemplate");
86          final String naturalLanguageTemplateIdKey = naturalLanguageTemplate.getId();
87          final NaturalLanguageTemplate existing = getNaturalLanguageTemplate(naturalLanguageTemplateIdKey);
88  
89          if (existing != null) {
90              throw new IllegalStateException("the NaturalLanguageTemplate to create already exists: " + naturalLanguageTemplate);
91          }
92  
93          NaturalLanguageTemplateBo bo = dataObjectService.save(from(naturalLanguageTemplate), PersistenceOption.FLUSH);
94  
95          return NaturalLanguageTemplateBo.to(bo);
96      }
97  
98      @Override
99      public NaturalLanguageTemplate getNaturalLanguageTemplate(String naturalLanguageTemplateId) {
100         incomingParamCheck(naturalLanguageTemplateId , "naturalLanguageTemplateId");
101         NaturalLanguageTemplateBo bo = dataObjectService.find(NaturalLanguageTemplateBo.class, naturalLanguageTemplateId);
102 
103         return NaturalLanguageTemplateBo.to(bo);
104     }
105 
106     @Override
107     public void updateNaturalLanguageTemplate(NaturalLanguageTemplate naturalLanguageTemplate) {
108         incomingParamCheck(naturalLanguageTemplate , "naturalLanguageTemplate");
109         final NaturalLanguageTemplate existing = getNaturalLanguageTemplate(naturalLanguageTemplate.getId());
110 
111         if (existing == null) {
112             throw new IllegalStateException("the NaturalLanguageTemplate to update does not exists: " + naturalLanguageTemplate);
113         }
114 
115         final NaturalLanguageTemplate toUpdate;
116 
117         if (!existing.getId().equals(naturalLanguageTemplate.getId())){
118             // if passed in id does not match existing id, correct it
119             final NaturalLanguageTemplate.Builder builder = NaturalLanguageTemplate.Builder.create(naturalLanguageTemplate);
120             builder.setId(existing.getId());
121             toUpdate = builder.build();
122         } else {
123             toUpdate = naturalLanguageTemplate;
124         }
125 
126         // copy all updateable fields to bo
127         NaturalLanguageTemplateBo boToUpdate = from(toUpdate);
128 
129         // delete any old, existing attributes
130         Map<String,String> fields = new HashMap<String,String>(1);
131         fields.put(KrmsImplConstants.PropertyNames.NaturalLanguageTemplate.NATURAL_LANGUAGE_TEMPLATE_ID, toUpdate.getId()); // TODO verify PropertyNames.NaturalLanguageTemplate.NATURAL_LANGUAGE_TEMPLATE_ID
132         deleteMatching(dataObjectService, NaturalLanguageTemplateAttributeBo.class, fields);
133 
134         // update the rule and create new attributes
135          dataObjectService.save(boToUpdate, PersistenceOption.FLUSH);
136     }
137 
138     @Override
139     public void deleteNaturalLanguageTemplate(String naturalLanguageTemplateId) {
140         incomingParamCheck(naturalLanguageTemplateId , "naturalLanguageTemplateId");
141         final NaturalLanguageTemplate existing = getNaturalLanguageTemplate(naturalLanguageTemplateId);
142 
143         if (existing == null) {
144             throw new IllegalStateException("the NaturalLanguageTemplate to delete does not exists: " + naturalLanguageTemplateId);
145         }
146 
147         // delete any existing attributes
148         Map<String,String> fields = new HashMap<String,String>(1);
149         fields.put(KrmsImplConstants.PropertyNames.NaturalLanguageTemplate.NATURAL_LANGUAGE_TEMPLATE_ID,existing.getId());
150         deleteMatching(dataObjectService, NaturalLanguageTemplateAttributeBo.class, fields);
151 
152         dataObjectService.delete(from(existing));
153     }
154 
155     @Override
156     public List<NaturalLanguageTemplate> findNaturalLanguageTemplatesByAttributes(Map attributes) {
157         if (attributes == null) {
158             throw new IllegalArgumentException("attributes is null");
159         }
160 
161         final Map<String, Object> map = new HashMap<String, Object>();
162         map.put("attributes", attributes);
163         List<NaturalLanguageTemplateBo> bos = findMatching(dataObjectService, NaturalLanguageTemplateBo.class, map);
164 
165         return convertBosToImmutables(bos);
166     }
167 
168     @Override
169     public List<NaturalLanguageTemplate> findNaturalLanguageTemplatesByLanguageCode(String languageCode) {
170         if (org.apache.commons.lang.StringUtils.isBlank(languageCode)) {
171             throw new IllegalArgumentException("languageCode is null or blank");
172         }
173 
174         final Map<String, Object> map = new HashMap<String, Object>();
175         map.put("languageCode", languageCode);
176         List<NaturalLanguageTemplateBo> bos = findMatching(dataObjectService, NaturalLanguageTemplateBo.class, map);
177 
178         return convertBosToImmutables(bos);
179     }
180 
181     @Override
182     public NaturalLanguageTemplate findNaturalLanguageTemplateByLanguageCodeTypeIdAndNluId(String languageCode,
183             String typeId,
184             String naturalLanguageUsageId) {
185         if (org.apache.commons.lang.StringUtils.isBlank(languageCode)) {
186             throw new IllegalArgumentException("languageCode is null or blank");
187         }
188 
189         final Map<String, Object> map = new HashMap<String, Object>();
190         map.put("languageCode", languageCode);
191         map.put("naturalLanguageUsageId", naturalLanguageUsageId);
192         map.put("typeId", typeId);
193         List<NaturalLanguageTemplateBo> bos = findMatching(dataObjectService, NaturalLanguageTemplateBo.class, map);
194 
195         if (bos.isEmpty()) {
196             return null;
197         }
198 
199         if (bos.size() > 1) {
200             throw new RiceIllegalArgumentException (languageCode + typeId +  naturalLanguageUsageId + " is supposed to be unique");
201         }
202 
203         return convertBosToImmutables(bos).get(0);
204     }
205 
206     @Override
207     public List<NaturalLanguageTemplate> findNaturalLanguageTemplatesByNaturalLanguageUsage(String naturalLanguageUsageId) {
208         if (org.apache.commons.lang.StringUtils.isBlank(naturalLanguageUsageId)) {
209             throw new IllegalArgumentException("naturalLanguageUsageId is null or blank");
210         }
211 
212         final Map<String, Object> map = new HashMap<String, Object>();
213         map.put("naturalLanguageUsageId", naturalLanguageUsageId);
214         List<NaturalLanguageTemplateBo> bos = findMatching(dataObjectService, NaturalLanguageTemplateBo.class, map);
215 
216         return convertBosToImmutables(bos);
217     }
218 
219     @Override
220     public List<NaturalLanguageTemplate> findNaturalLanguageTemplatesByType(String typeId) {
221         if (org.apache.commons.lang.StringUtils.isBlank(typeId)) {
222             throw new IllegalArgumentException("typeId is null or blank");
223         }
224 
225         final Map<String, Object> map = new HashMap<String, Object>();
226         map.put("typeId", typeId);
227         List<NaturalLanguageTemplateBo> bos = findMatching(dataObjectService, NaturalLanguageTemplateBo.class, map);
228 
229         return convertBosToImmutables(bos);
230     }
231 
232     @Override
233     public List<NaturalLanguageTemplate> findNaturalLanguageTemplatesByTemplate(String template) {
234         if (org.apache.commons.lang.StringUtils.isBlank(template)) {
235             throw new IllegalArgumentException("template is null or blank");
236         }
237 
238         final Map<String, Object> map = new HashMap<String, Object>();
239         map.put("template", template);
240         List<NaturalLanguageTemplateBo> bos = findMatching(dataObjectService, NaturalLanguageTemplateBo.class, map);
241 
242         return convertBosToImmutables(bos);
243     }
244 
245     public List<NaturalLanguageTemplate> convertBosToImmutables(final Collection<NaturalLanguageTemplateBo> naturalLanguageTemplateBos) {
246         List<NaturalLanguageTemplate> immutables = new LinkedList<NaturalLanguageTemplate>();
247 
248         if (naturalLanguageTemplateBos != null) {
249             NaturalLanguageTemplate immutable = null;
250             for (NaturalLanguageTemplateBo bo : naturalLanguageTemplateBos ) {
251                 immutable = to(bo);
252                 immutables.add(immutable);
253             }
254         }
255 
256         return Collections.unmodifiableList(immutables);
257     }
258 
259     @Override
260     public NaturalLanguageTemplate to(NaturalLanguageTemplateBo naturalLanguageTemplateBo) {
261         return NaturalLanguageTemplateBo.to(naturalLanguageTemplateBo);
262     }
263 
264     public NaturalLanguageTemplateBo from(NaturalLanguageTemplate naturalLanguageTemplate) {
265         return NaturalLanguageTemplateBo.from(naturalLanguageTemplate);
266     }
267 
268     private Collection<NaturalLanguageTemplateAttributeBo> buildAttributes(NaturalLanguageTemplate im, Collection<NaturalLanguageTemplateAttributeBo> attributes) {
269         // build a map from attribute name to definition
270         Map<String, KrmsAttributeDefinition> attributeDefinitionMap = new HashMap<String, KrmsAttributeDefinition>();
271 
272         List<KrmsAttributeDefinition> attributeDefinitions = getAttributeDefinitionService().findAttributeDefinitionsByType(im.getTypeId());
273 
274         for (KrmsAttributeDefinition attributeDefinition : attributeDefinitions) {
275             attributeDefinitionMap.put(attributeDefinition.getName(), attributeDefinition);
276         }
277 
278         // for each entry, build a NaturalLanguageTemplateAttributeBo and add it
279         if (im.getAttributes() != null) {
280             for (Map.Entry<String,String> entry  : im.getAttributes().entrySet()) {
281                 KrmsAttributeDefinition attrDef = attributeDefinitionMap.get(entry.getKey());
282 
283                 if (attrDef != null) {
284                     NaturalLanguageTemplateAttributeBo attributeBo = new NaturalLanguageTemplateAttributeBo();
285                     attributeBo.setNaturalLanguageTemplateId( im.getId() );
286                     attributeBo.setAttributeDefinitionId(attrDef.getId());
287                     attributeBo.setValue(entry.getValue());
288                     attributeBo.setAttributeDefinition(KrmsAttributeDefinitionBo.from(attrDef));
289                     attributes.add(attributeBo);
290                 } else {
291                     throw new RiceIllegalStateException("there is no attribute definition with the name '" +
292                                  entry.getKey() + "' that is valid for the naturalLanguageTemplate type with id = '" + im.getTypeId() +"'");
293                 }
294             }
295         }
296 
297         return attributes;
298     }
299 
300     private Set<NaturalLanguageTemplateAttributeBo> buildAttributeBoSet(NaturalLanguageTemplate im) {
301         Set<NaturalLanguageTemplateAttributeBo> attributes = new HashSet<NaturalLanguageTemplateAttributeBo>();
302 
303         return (Set)buildAttributes(im, attributes);
304     }
305 
306     private List<NaturalLanguageTemplateAttributeBo> buildAttributeBoList(NaturalLanguageTemplate im) {
307         List<NaturalLanguageTemplateAttributeBo> attributes = new LinkedList<NaturalLanguageTemplateAttributeBo>();
308 
309         return (List)buildAttributes(im, attributes);
310     }
311 
312     private void incomingParamCheck(Object object, String name) {
313         if (object == null) {
314             throw new IllegalArgumentException(name + " was null");
315         } else if (object instanceof String
316                 && StringUtils.isBlank((String)object)) {
317             throw new IllegalArgumentException(name + " was blank");
318         }
319     }
320 }