View Javadoc

1   /**
2    * Copyright 2005-2013 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.collections.CollectionUtils;
19  import org.apache.commons.lang.StringUtils;
20  import org.kuali.rice.core.api.exception.RiceIllegalStateException;
21  import org.kuali.rice.krad.service.BusinessObjectService;
22  import org.kuali.rice.krms.api.repository.language.NaturalLanguageTemplate;
23  import org.kuali.rice.krms.api.repository.language.NaturalLanguageTemplaterContract;
24  import org.kuali.rice.krms.api.repository.type.KrmsAttributeDefinition;
25  import org.kuali.rice.krms.impl.util.KrmsImplConstants;
26  
27  import java.util.Collection;
28  import java.util.Collections;
29  import java.util.HashMap;
30  import java.util.HashSet;
31  import java.util.LinkedList;
32  import java.util.List;
33  import java.util.Map;
34  import java.util.Set;
35  
36  /**
37   * Implementation of the @{link NaturalLanguageTemplateBoService} interface for accessing  {@link NaturalLanguageTemplateBo} related business objects.
38   * 
39   * @author Kuali Rice Team (rice.collab@kuali.org)
40   * 
41   */
42  public final class NaturalLanguageTemplateBoServiceImpl
43      implements NaturalLanguageTemplateBoService
44  {
45  
46      private BusinessObjectService businessObjectService;
47      private KrmsAttributeDefinitionService attributeDefinitionService;
48      private NaturalLanguageTemplaterContract naturalLanguageTemplater;
49  
50      /**
51       * Sets the value of BusinessObjectService to the given value.
52       * 
53       * @param businessObjectService the BusinessObjectService value to set.
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             // if passed in id does not match existing id, correct it
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         // copy all updateable fields to bo
112         NaturalLanguageTemplateBo boToUpdate = from(toUpdate);
113 
114         // delete any old, existing attributes
115         Map<String,String> fields = new HashMap<String,String>(1);
116         fields.put(KrmsImplConstants.PropertyNames.NaturalLanguageTemplate.NATURAL_LANGUAGE_TEMPLATE_ID, toUpdate.getId()); // TODO verify PropertyNames.NaturalLanguageTemplate.NATURAL_LANGUAGE_TEMPLATE_ID
117         businessObjectService.deleteMatching(NaturalLanguageTemplateAttributeBo.class, fields);
118 
119         // update the rule and create new attributes
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         List<NaturalLanguageTemplate> immutables = convertBosToImmutables(bos);
164         return CollectionUtils.isEmpty(immutables) ? null : immutables.get(0);
165     }
166 
167     @Override
168     public List<NaturalLanguageTemplate> findNaturalLanguageTemplatesByNaturalLanguageUsage(String naturalLanguageUsageId) {
169         if (org.apache.commons.lang.StringUtils.isBlank(naturalLanguageUsageId)) {
170             throw new IllegalArgumentException("naturalLanguageUsageId is null or blank");
171         }
172         final Map<String, Object> map = new HashMap<String, Object>();
173         map.put("naturalLanguageUsageId", naturalLanguageUsageId);
174         List<NaturalLanguageTemplateBo> bos = (List<NaturalLanguageTemplateBo>) businessObjectService.findMatching(NaturalLanguageTemplateBo.class, map);
175         return convertBosToImmutables(bos);
176     }
177 
178     @Override
179     public List<NaturalLanguageTemplate> findNaturalLanguageTemplatesByType(String typeId) {
180         if (org.apache.commons.lang.StringUtils.isBlank(typeId)) {
181             throw new IllegalArgumentException("typeId is null or blank");
182         }
183         final Map<String, Object> map = new HashMap<String, Object>();
184         map.put("typeId", typeId);
185         List<NaturalLanguageTemplateBo> bos = (List<NaturalLanguageTemplateBo>) businessObjectService.findMatching(NaturalLanguageTemplateBo.class, map);
186         return convertBosToImmutables(bos);
187     }
188 
189     @Override
190     public List<NaturalLanguageTemplate> findNaturalLanguageTemplatesByTemplate(String template) {
191         if (org.apache.commons.lang.StringUtils.isBlank(template)) {
192             throw new IllegalArgumentException("template is null or blank");
193         }
194         final Map<String, Object> map = new HashMap<String, Object>();
195         map.put("template", template);
196         List<NaturalLanguageTemplateBo> bos = (List<NaturalLanguageTemplateBo>) businessObjectService.findMatching(NaturalLanguageTemplateBo.class, map);
197         return convertBosToImmutables(bos);
198     }
199 
200     public List<NaturalLanguageTemplate> convertBosToImmutables(final Collection<NaturalLanguageTemplateBo> naturalLanguageTemplateBos) {
201         List<NaturalLanguageTemplate> immutables = new LinkedList<NaturalLanguageTemplate>();
202         if (naturalLanguageTemplateBos != null) {
203             NaturalLanguageTemplate immutable = null;
204             for (NaturalLanguageTemplateBo bo : naturalLanguageTemplateBos ) {
205                 immutable = to(bo);
206                 immutables.add(immutable);
207             }
208         }
209         return Collections.unmodifiableList(immutables);
210     }
211 
212     @Override
213     public NaturalLanguageTemplate to(NaturalLanguageTemplateBo naturalLanguageTemplateBo) {
214         return NaturalLanguageTemplateBo.to(naturalLanguageTemplateBo);
215     }
216 
217     public NaturalLanguageTemplateBo from(NaturalLanguageTemplate naturalLanguageTemplate) {
218         return NaturalLanguageTemplateBo.from(naturalLanguageTemplate);
219     }
220 
221     @Override
222     public String template(NaturalLanguageTemplate naturalLanguageTemplate) {
223         return naturalLanguageTemplater.template(naturalLanguageTemplate);
224     }
225 
226     private Collection<NaturalLanguageTemplateAttributeBo> buildAttributes(NaturalLanguageTemplate im, Collection<NaturalLanguageTemplateAttributeBo> attributes) {
227         // build a map from attribute name to definition
228         Map<String, KrmsAttributeDefinition> attributeDefinitionMap = new HashMap<String, KrmsAttributeDefinition>();
229 
230         List<KrmsAttributeDefinition> attributeDefinitions = getAttributeDefinitionService().findAttributeDefinitionsByType(im.getTypeId());
231 
232         for (KrmsAttributeDefinition attributeDefinition : attributeDefinitions) {
233             attributeDefinitionMap.put(attributeDefinition.getName(), attributeDefinition);
234         }
235 
236         // for each entry, build a NaturalLanguageTemplateAttributeBo and add it
237         if (im.getAttributes() != null) {
238             for (Map.Entry<String,String> entry  : im.getAttributes().entrySet()) {
239                 KrmsAttributeDefinition attrDef = attributeDefinitionMap.get(entry.getKey());
240 
241                 if (attrDef != null) {
242                     NaturalLanguageTemplateAttributeBo attributeBo = new NaturalLanguageTemplateAttributeBo();
243                     attributeBo.setNaturalLanguageTemplateId( im.getId() );
244                     attributeBo.setAttributeDefinitionId(attrDef.getId());
245                     attributeBo.setValue(entry.getValue());
246                     attributeBo.setAttributeDefinition(KrmsAttributeDefinitionBo.from(attrDef));
247                     attributes.add(attributeBo);
248                 } else {
249                     throw new RiceIllegalStateException("there is no attribute definition with the name '" +
250                                  entry.getKey() + "' that is valid for the naturalLanguageTemplate type with id = '" + im.getTypeId() +"'");
251                 }
252             }
253         }
254         return attributes;
255     }
256 
257     private Set<NaturalLanguageTemplateAttributeBo> buildAttributeBoSet(NaturalLanguageTemplate im) {
258         Set<NaturalLanguageTemplateAttributeBo> attributes = new HashSet<NaturalLanguageTemplateAttributeBo>();
259         return (Set)buildAttributes(im, attributes);
260     }
261 
262     private List<NaturalLanguageTemplateAttributeBo> buildAttributeBoList(NaturalLanguageTemplate im) {
263         List<NaturalLanguageTemplateAttributeBo> attributes = new LinkedList<NaturalLanguageTemplateAttributeBo>();
264         return (List)buildAttributes(im, attributes);
265     }
266 
267     private void incomingParamCheck(Object object, String name) {
268         if (object == null) {
269             throw new IllegalArgumentException(name + " was null");
270         } else if (object instanceof String
271                 && StringUtils.isBlank((String)object)) {
272             throw new IllegalArgumentException(name + " was blank");
273         }
274     }
275 
276 }