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