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.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} related business objects.
36   * 
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   * 
39   */
40  public final class NaturalLanguageUsageBoServiceImpl
41      implements NaturalLanguageUsageBoService {
42  
43      private DataObjectService dataObjectService;
44      private KrmsAttributeDefinitionService attributeDefinitionService;
45  
46      /**
47       * Sets the value of DataObjectService to the given value.
48       * 
49       * @param dataObjectService the DataObjectService value to set.
50       * 
51       */
52      public void setDataObjectService(DataObjectService dataObjectService) {
53          this.dataObjectService = dataObjectService;
54      }
55  
56      public void setAttributeDefinitionService(KrmsAttributeDefinitionService attributeDefinitionService) {
57          this.attributeDefinitionService = attributeDefinitionService;
58      }
59  
60      public KrmsAttributeDefinitionService getAttributeDefinitionService() {
61          if (attributeDefinitionService == null) {
62              attributeDefinitionService = KrmsRepositoryServiceLocator.getKrmsAttributeDefinitionService();
63          }
64  
65          return attributeDefinitionService;
66      }
67  
68      @Override
69      public NaturalLanguageUsage createNaturalLanguageUsage(NaturalLanguageUsage naturalLanguageUsage) {
70          incomingParamCheck(naturalLanguageUsage , "naturalLanguageUsage");
71          if (StringUtils.isNotEmpty(naturalLanguageUsage.getId())) {
72              final String naturalLanguageUsageIdKey = naturalLanguageUsage.getId();
73              final NaturalLanguageUsage existing = getNaturalLanguageUsage(naturalLanguageUsageIdKey);
74  
75              if (existing != null) {
76                  throw new IllegalStateException("the NaturalLanguageUsage to create already exists: " + naturalLanguageUsage);
77              }
78          } else {
79              final NaturalLanguageUsage existing =
80                  getNaturalLanguageUsageByName(naturalLanguageUsage.getNamespace(), naturalLanguageUsage.getName());
81  
82              if (existing != null) {
83                  throw new IllegalStateException("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 void updateNaturalLanguageUsage(NaturalLanguageUsage naturalLanguageUsage) {
121         incomingParamCheck(naturalLanguageUsage , "naturalLanguageUsage");
122         final NaturalLanguageUsage existing = getNaturalLanguageUsage(naturalLanguageUsage.getId());
123 
124         if (existing == null) {
125             throw new IllegalStateException("the NaturalLanguageUsage to update does not exists: " + naturalLanguageUsage);
126         }
127 
128         final NaturalLanguageUsage toUpdate;
129 
130         if (!existing.getId().equals(naturalLanguageUsage.getId())) {
131             // if passed in id does not match existing id, correct it
132             final NaturalLanguageUsage.Builder builder = NaturalLanguageUsage.Builder.create(naturalLanguageUsage);
133             builder.setId(existing.getId());
134             toUpdate = builder.build();
135         } else {
136             toUpdate = naturalLanguageUsage;
137         }
138 
139         // copy all updateable fields to bo
140         NaturalLanguageUsageBo boToUpdate = from(toUpdate);
141 
142         // update the rule and create new attributes
143          dataObjectService.save(boToUpdate, PersistenceOption.FLUSH);
144     }
145 
146     @Override
147     public void deleteNaturalLanguageUsage(String naturalLanguageUsageId) {
148         incomingParamCheck(naturalLanguageUsageId , "naturalLanguageUsageId");
149         final NaturalLanguageUsage existing = getNaturalLanguageUsage(naturalLanguageUsageId);
150 
151         if (existing == null) {
152             throw new IllegalStateException("the NaturalLanguageUsage to delete does not exists: " + naturalLanguageUsageId);
153         }
154 
155         dataObjectService.delete(from(existing));
156     }
157 
158     @Override
159     public List<NaturalLanguageUsage> findNaturalLanguageUsagesByName(String name) {
160         if (org.apache.commons.lang.StringUtils.isBlank(name)) {
161             throw new IllegalArgumentException("name is null or blank");
162         }
163 
164         final Map<String, Object> map = new HashMap<String, Object>();
165         map.put("name", name);
166         List<NaturalLanguageUsageBo> bos = findMatching(dataObjectService, NaturalLanguageUsageBo.class, map);
167 
168         return convertBosToImmutables(bos);
169     }
170 
171     @Override
172     public List<NaturalLanguageUsage> findNaturalLanguageUsagesByDescription(String description) {
173         if (org.apache.commons.lang.StringUtils.isBlank(description)) {
174             throw new IllegalArgumentException("description is null or blank");
175         }
176 
177         final Map<String, Object> map = new HashMap<String, Object>();
178         map.put("description", description);
179         List<NaturalLanguageUsageBo> bos = findMatching(dataObjectService, NaturalLanguageUsageBo.class, map);
180 
181         return convertBosToImmutables(bos);
182     }
183 
184     @Override
185     public List<NaturalLanguageUsage> findNaturalLanguageUsagesByNamespace(String namespace) {
186         if (org.apache.commons.lang.StringUtils.isBlank(namespace)) {
187             throw new IllegalArgumentException("namespace is null or blank");
188         }
189 
190         final Map<String, Object> map = new HashMap<String, Object>();
191         map.put("namespace", namespace);
192         List<NaturalLanguageUsageBo> bos = findMatching(dataObjectService, NaturalLanguageUsageBo.class, map);
193 
194         return convertBosToImmutables(bos);
195     }
196 
197     public List<NaturalLanguageUsage> convertBosToImmutables(final Collection<NaturalLanguageUsageBo> naturalLanguageUsageBos) {
198         List<NaturalLanguageUsage> immutables = new LinkedList<NaturalLanguageUsage>();
199 
200         if (naturalLanguageUsageBos != null) {
201             NaturalLanguageUsage immutable = null;
202             for (NaturalLanguageUsageBo bo : naturalLanguageUsageBos ) {
203                 immutable = to(bo);
204                 immutables.add(immutable);
205             }
206         }
207 
208         return Collections.unmodifiableList(immutables);
209     }
210 
211     @Override
212     public NaturalLanguageUsage to(NaturalLanguageUsageBo naturalLanguageUsageBo) {
213         return NaturalLanguageUsageBo.to(naturalLanguageUsageBo);
214     }
215 
216     public NaturalLanguageUsageBo from(NaturalLanguageUsage naturalLanguageUsage) {
217         return NaturalLanguageUsageBo.from(naturalLanguageUsage);
218     }
219 
220     private void incomingParamCheck(Object object, String name) {
221         if (object == null) {
222             throw new IllegalArgumentException(name + " was null");
223         } else if (object instanceof String
224                 && StringUtils.isBlank((String)object)) {
225             throw new IllegalArgumentException(name + " was blank");
226         }
227     }
228 }