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.krad.service.BusinessObjectService;
21  import org.kuali.rice.krms.api.repository.language.NaturalLanguageUsage;
22  import java.util.Collection;
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.LinkedList;
26  import java.util.List;
27  import java.util.Map;
28  
29  /**
30   * Implementation of the @{link NaturalLanguageUsageBoService} interface for accessing  {@link NaturalLanguageUsageBo} related business objects.
31   * 
32   * @author Kuali Rice Team (rice.collab@kuali.org)
33   * 
34   */
35  public final class NaturalLanguageUsageBoServiceImpl
36      implements NaturalLanguageUsageBoService
37  {
38  
39      private BusinessObjectService businessObjectService;
40      private KrmsAttributeDefinitionService attributeDefinitionService;
41  
42      /**
43       * Sets the value of BusinessObjectService to the given value.
44       * 
45       * @param businessObjectService the BusinessObjectService value to set.
46       * 
47       */
48      public void setBusinessObjectService(BusinessObjectService businessObjectService) {
49          this.businessObjectService = businessObjectService;
50      }
51  
52      public void setAttributeDefinitionService(KrmsAttributeDefinitionService attributeDefinitionService) {
53          this.attributeDefinitionService = attributeDefinitionService;
54      }
55  
56      public KrmsAttributeDefinitionService getAttributeDefinitionService() {
57          if (attributeDefinitionService == null) {
58              attributeDefinitionService = KrmsRepositoryServiceLocator.getKrmsAttributeDefinitionService();
59          }
60          return attributeDefinitionService;
61      }
62  
63      @Override
64      public NaturalLanguageUsage createNaturalLanguageUsage(NaturalLanguageUsage naturalLanguageUsage) {
65          incomingParamCheck(naturalLanguageUsage , "naturalLanguageUsage");
66          final String naturalLanguageUsageIdKey = naturalLanguageUsage.getId();
67          final NaturalLanguageUsage existing = getNaturalLanguageUsage(naturalLanguageUsageIdKey);
68          if (existing != null){ throw new IllegalStateException("the NaturalLanguageUsage to create already exists: " + naturalLanguageUsage);	}
69          NaturalLanguageUsageBo bo = (NaturalLanguageUsageBo)businessObjectService.save(from(naturalLanguageUsage));
70          return NaturalLanguageUsageBo.to(bo);
71      }
72  
73      @Override
74      public NaturalLanguageUsage getNaturalLanguageUsage(String naturalLanguageUsageId) {
75          incomingParamCheck(naturalLanguageUsageId , "naturalLanguageUsageId");
76          NaturalLanguageUsageBo bo = businessObjectService.findBySinglePrimaryKey(NaturalLanguageUsageBo.class, naturalLanguageUsageId);
77          return NaturalLanguageUsageBo.to(bo);
78      }
79  
80      @Override
81      public NaturalLanguageUsage getNaturalLanguageUsageByName(String namespace, String name) {
82          if (StringUtils.isBlank(namespace)) {
83              throw new RiceIllegalArgumentException("namespace was a null or blank value");
84          }
85          if (StringUtils.isBlank(name)) {
86              throw new RiceIllegalArgumentException("name was a null or blank value");
87          }
88          final Map<String, Object> map = new HashMap<String, Object>();
89          map.put("namespace", namespace);
90          map.put("name", name);
91  
92          NaturalLanguageUsageBo usageBo = businessObjectService.findByPrimaryKey(NaturalLanguageUsageBo.class, Collections.unmodifiableMap(map));
93          return NaturalLanguageUsageBo.to(usageBo);
94      }
95  
96      @Override
97      public void updateNaturalLanguageUsage(NaturalLanguageUsage naturalLanguageUsage) {
98          incomingParamCheck(naturalLanguageUsage , "naturalLanguageUsage");
99          final NaturalLanguageUsage existing = getNaturalLanguageUsage(naturalLanguageUsage.getId());
100         if (existing == null){ throw new IllegalStateException("the NaturalLanguageUsage to update does not exists: " + naturalLanguageUsage);}
101         final NaturalLanguageUsage toUpdate;
102         if (!existing.getId().equals(naturalLanguageUsage.getId())){
103             // if passed in id does not match existing id, correct it
104             final NaturalLanguageUsage.Builder builder = NaturalLanguageUsage.Builder.create(naturalLanguageUsage);
105             builder.setId(existing.getId());
106             toUpdate = builder.build();
107         } else {
108             toUpdate = naturalLanguageUsage;
109         }
110 
111         // copy all updateable fields to bo
112         NaturalLanguageUsageBo boToUpdate = from(toUpdate);
113 
114         // update the rule and create new attributes
115          businessObjectService.save(boToUpdate);
116     }
117 
118     @Override
119     public void deleteNaturalLanguageUsage(String naturalLanguageUsageId) {
120         incomingParamCheck(naturalLanguageUsageId , "naturalLanguageUsageId");
121         final NaturalLanguageUsage existing = getNaturalLanguageUsage(naturalLanguageUsageId);
122         if (existing == null){ throw new IllegalStateException("the NaturalLanguageUsage to delete does not exists: " + naturalLanguageUsageId);}
123         businessObjectService.delete(from(existing));
124     }
125 
126     @Override
127     public List<NaturalLanguageUsage> findNaturalLanguageUsagesByName(String name) {
128         if (org.apache.commons.lang.StringUtils.isBlank(name)) {
129             throw new IllegalArgumentException("name is null or blank");
130         }
131         final Map<String, Object> map = new HashMap<String, Object>();
132         map.put("name", name);
133         List<NaturalLanguageUsageBo> bos = (List<NaturalLanguageUsageBo>) businessObjectService.findMatching(NaturalLanguageUsageBo.class, map);
134         return convertBosToImmutables(bos);
135     }
136 
137     @Override
138     public List<NaturalLanguageUsage> findNaturalLanguageUsagesByDescription(String description) {
139         if (org.apache.commons.lang.StringUtils.isBlank(description)) {
140             throw new IllegalArgumentException("description is null or blank");
141         }
142         final Map<String, Object> map = new HashMap<String, Object>();
143         map.put("description", description);
144         List<NaturalLanguageUsageBo> bos = (List<NaturalLanguageUsageBo>) businessObjectService.findMatching(NaturalLanguageUsageBo.class, map);
145         return convertBosToImmutables(bos);
146     }
147 
148     @Override
149     public List<NaturalLanguageUsage> findNaturalLanguageUsagesByNamespace(String namespace) {
150         if (org.apache.commons.lang.StringUtils.isBlank(namespace)) {
151             throw new IllegalArgumentException("namespace is null or blank");
152         }
153         final Map<String, Object> map = new HashMap<String, Object>();
154         map.put("namespace", namespace);
155         List<NaturalLanguageUsageBo> bos = (List<NaturalLanguageUsageBo>) businessObjectService.findMatching(NaturalLanguageUsageBo.class, map);
156         return convertBosToImmutables(bos);
157     }
158 
159     public List<NaturalLanguageUsage> convertBosToImmutables(final Collection<NaturalLanguageUsageBo> naturalLanguageUsageBos) {
160         List<NaturalLanguageUsage> immutables = new LinkedList<NaturalLanguageUsage>();
161         if (naturalLanguageUsageBos != null) {
162             NaturalLanguageUsage immutable = null;
163             for (NaturalLanguageUsageBo bo : naturalLanguageUsageBos ) {
164                 immutable = to(bo);
165                 immutables.add(immutable);
166             }
167         }
168         return Collections.unmodifiableList(immutables);
169     }
170 
171     @Override
172     public NaturalLanguageUsage to(NaturalLanguageUsageBo naturalLanguageUsageBo) {
173         return NaturalLanguageUsageBo.to(naturalLanguageUsageBo);
174     }
175 
176     public NaturalLanguageUsageBo from(NaturalLanguageUsage naturalLanguageUsage) {
177         return NaturalLanguageUsageBo.from(naturalLanguageUsage);
178     }
179 
180     private void incomingParamCheck(Object object, String name) {
181         if (object == null) {
182             throw new IllegalArgumentException(name + " was null");
183         } else if (object instanceof String
184                 && StringUtils.isBlank((String)object)) {
185             throw new IllegalArgumentException(name + " was blank");
186         }
187     }
188 
189 }