View Javadoc

1   /**
2    * Copyright 2005-2012 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 java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.Collections;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.apache.commons.lang.StringUtils;
26  import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
27  import org.kuali.rice.krad.service.BusinessObjectService;
28  import org.kuali.rice.krms.api.repository.term.TermDefinition;
29  import org.kuali.rice.krms.api.repository.term.TermResolverDefinition;
30  import org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition;
31  import org.kuali.rice.krms.impl.util.KrmsImplConstants;
32  import org.springframework.util.CollectionUtils;
33  
34  /**
35   * Implementation of {@link TermBoService}
36   * 
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   *
39   */
40  public class TermBoServiceImpl implements TermBoService {
41  	
42  	private BusinessObjectService businessObjectService;
43  
44  	/**
45  	 * @param businessObjectService the businessObjectService to set
46  	 */
47  	public void setBusinessObjectService(BusinessObjectService businessObjectService) {
48  		this.businessObjectService = businessObjectService;
49  	}
50  	
51  	/**
52  	 * @see org.kuali.rice.krms.impl.repository.TermBoService#getTermSpecificationById(java.lang.String)
53  	 */
54  	@Override
55  	public TermSpecificationDefinition getTermSpecificationById(String id) {
56  		TermSpecificationBo termSpecificationBo = 
57  			businessObjectService.findBySinglePrimaryKey(TermSpecificationBo.class, id);
58  		return TermSpecificationDefinition.Builder.create(termSpecificationBo).build();
59  	}
60  	
61  	/**
62  	 * @see org.kuali.rice.krms.impl.repository.TermBoService#createTermSpecification(org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition)
63  	 */
64  	@Override
65  	public TermSpecificationDefinition createTermSpecification(TermSpecificationDefinition termSpec) {
66  		if (!StringUtils.isBlank(termSpec.getId())) {
67  			throw new RiceIllegalArgumentException("for creation, TermSpecification.id must be null");
68  		}
69  		
70  		TermSpecificationBo termSpecBo = TermSpecificationBo.from(termSpec);
71  		
72  		termSpecBo = businessObjectService.save(termSpecBo);
73  
74          // save relations to the contexts on the BO
75          if (!CollectionUtils.isEmpty(termSpec.getContextIds())) for (String contextId : termSpec.getContextIds()) {
76              ContextValidTermBo contextValidTerm = new ContextValidTermBo();
77              contextValidTerm.setContextId(contextId);
78              contextValidTerm.setTermSpecificationId(termSpecBo.getId());
79              businessObjectService.save(contextValidTerm);
80          }
81  		
82  		return TermSpecificationBo.to(termSpecBo);
83  	}
84  
85      @Override
86      public void updateTermSpecification(TermSpecificationDefinition termSpec) throws RiceIllegalArgumentException {
87          if (termSpec == null) {
88              throw new IllegalArgumentException("term specification is null");
89          }
90  
91          // must already exist to be able to update
92          final String termSpecificationId = termSpec.getId();
93          final TermSpecificationBo existing = businessObjectService.findBySinglePrimaryKey(TermSpecificationBo.class, termSpecificationId);
94          if (existing == null) {
95              throw new IllegalStateException("the term specification does not exist: " + termSpec);
96          }
97          final TermSpecificationDefinition toUpdate;
98          if (!existing.getId().equals(termSpec.getId())) {
99              // if passed in id does not match existing id, correct it
100             final TermSpecificationDefinition.Builder builder = TermSpecificationDefinition.Builder.create(termSpec);
101             builder.setId(existing.getId());
102             toUpdate = builder.build();
103         } else {
104             toUpdate = termSpec;
105         }
106 
107         // copy all updateable fields to bo
108         TermSpecificationBo boToUpdate = TermSpecificationBo.from(toUpdate);
109 
110 //        // delete any old, existing attributes DOES NOT HAVE ANY
111 //        Map<String, String> fields = new HashMap<String, String>(1);
112 //        fields.put(KrmsImplConstants.PropertyNames.TermSpecification.TERM_SPECIFICATION_ID, toUpdate.getId());
113 //        businessObjectService.deleteMatching(TermSpecificationAttributeBo.class, fields);
114 
115         // update the rule and create new attributes
116         businessObjectService.save(boToUpdate);
117 
118     }
119 
120     @Override
121     public void deleteTermSpecification(String id) throws RiceIllegalArgumentException {
122         if (id == null) {
123             throw new RiceIllegalArgumentException("agendaId is null");
124         }
125         final TermSpecificationBo existing = businessObjectService.findBySinglePrimaryKey(TermSpecificationBo.class, id);
126         if (existing == null) {
127             throw new IllegalStateException("the TermSpecification to delete does not exists: " + id);
128         }
129         businessObjectService.delete(existing);
130     }
131 	  
132         
133 	/**
134 	 * @see org.kuali.rice.krms.impl.repository.TermBoService#createTerm(org.kuali.rice.krms.api.repository.term.TermDefinition)
135 	 */
136 	@Override
137 	public TermDefinition createTerm(TermDefinition termDef) {
138 		if (!StringUtils.isBlank(termDef.getId())) {
139 			throw new RiceIllegalArgumentException("for creation, TermDefinition.id must be null");
140 		}
141 		
142 		TermBo termBo = TermBo.from(termDef);
143 		
144 		businessObjectService.save(termBo);
145 		
146 		return TermBo.to(termBo);
147 	}
148 	
149             @Override
150     public void updateTerm (TermDefinition term) throws RiceIllegalArgumentException {
151         if (term == null) {
152             throw new IllegalArgumentException("term is null");
153         }
154 
155         // must already exist to be able to update
156         final String termId = term.getId();
157         final TermBo existing = businessObjectService.findBySinglePrimaryKey(TermBo.class, termId);
158         if (existing == null) {
159             throw new IllegalStateException("the term resolver does not exist: " + term);
160         }
161         final TermDefinition toUpdate;
162         if (!existing.getId().equals(term.getId())) {
163             // if passed in id does not match existing id, correct it
164             final TermDefinition.Builder builder = TermDefinition.Builder.create(term);
165             builder.setId(existing.getId());
166             toUpdate = builder.build();
167         } else {
168             toUpdate = term;
169         }
170 
171         // copy all updateable fields to bo
172         TermBo boToUpdate = TermBo.from(toUpdate);
173 
174         // delete any old, existing parameters
175         Map<String, String> fields = new HashMap<String, String>(1);
176         fields.put(KrmsImplConstants.PropertyNames.Term.TERM_ID, toUpdate.getId());
177         businessObjectService.deleteMatching(TermParameterBo.class, fields);
178 
179         // update the rule and create new attributes
180         businessObjectService.save(boToUpdate);
181     }
182 
183     @Override
184     public void deleteTerm(String id) throws RiceIllegalArgumentException {
185         if (id == null) {
186             throw new RiceIllegalArgumentException("termId is null");
187         }
188         TermBo existing =
189                 businessObjectService.findBySinglePrimaryKey(TermBo.class, id);
190         if (existing == null) {
191             throw new IllegalStateException("the term to delete does not exists: " + id);
192         }
193         businessObjectService.delete(existing);
194     }   
195     
196         
197         
198         
199         
200 	/**
201 	 * @see org.kuali.rice.krms.impl.repository.TermBoService#createTermResolver(org.kuali.rice.krms.api.repository.term.TermResolverDefinition)
202 	 */
203 	@Override
204 	public TermResolverDefinition createTermResolver(TermResolverDefinition termResolver) {
205 		if (!StringUtils.isBlank(termResolver.getId())) {
206 			throw new RiceIllegalArgumentException("for creation, TermResolverDefinition.id must be null");
207 		}
208 		
209 		TermResolverBo termResolverBo = TermResolverBo.from(termResolver);
210 		
211 		termResolverBo = (TermResolverBo)businessObjectService.save(termResolverBo);
212 		
213 		return TermResolverBo.to(termResolverBo);
214 	}
215 
216     @Override
217     public void updateTermResolver(TermResolverDefinition termResolver) throws RiceIllegalArgumentException {
218         if (termResolver == null) {
219             throw new IllegalArgumentException("term resolver is null");
220         }
221 
222         // must already exist to be able to update
223         final String termResolverId = termResolver.getId();
224         final TermResolverBo existing = businessObjectService.findBySinglePrimaryKey(TermResolverBo.class, termResolverId);
225         if (existing == null) {
226             throw new IllegalStateException("the term resolver does not exist: " + termResolver);
227         }
228         final TermResolverDefinition toUpdate;
229         if (!existing.getId().equals(termResolver.getId())) {
230             // if passed in id does not match existing id, correct it
231             final TermResolverDefinition.Builder builder = TermResolverDefinition.Builder.create(termResolver);
232             builder.setId(existing.getId());
233             toUpdate = builder.build();
234         } else {
235             toUpdate = termResolver;
236         }
237 
238         // copy all updateable fields to bo
239         TermResolverBo boToUpdate = TermResolverBo.from(toUpdate);
240 
241         // delete any old, existing attributes
242         Map<String, String> fields = new HashMap<String, String>(1);
243         fields.put(KrmsImplConstants.PropertyNames.TermResolver.TERM_RESOLVER_ID, toUpdate.getId());
244         businessObjectService.deleteMatching(TermResolverAttributeBo.class, fields);
245 
246         // update the rule and create new attributes
247         businessObjectService.save(boToUpdate);
248     }
249 
250     @Override
251     public void deleteTermResolver(String id) throws RiceIllegalArgumentException {
252         if (id == null) {
253             throw new RiceIllegalArgumentException("agendaId is null");
254         }
255         TermSpecificationBo existing =
256                 businessObjectService.findBySinglePrimaryKey(TermSpecificationBo.class, id);
257         if (existing == null) {
258             throw new IllegalStateException("the TermResolver to delete does not exists: " + id);
259         }
260         businessObjectService.delete(existing);
261     }   
262     
263 	/**
264 	 * @see org.kuali.rice.krms.impl.repository.TermBoService#getTerm(java.lang.String)
265 	 */
266 	@Override
267 	public TermDefinition getTerm(String id) {
268 		TermDefinition result = null;
269 		
270 		if (StringUtils.isBlank(id)) {
271 			throw new RiceIllegalArgumentException("id must not be blank or null");
272 		}
273 		TermBo termBo = businessObjectService.findBySinglePrimaryKey(TermBo.class, id);
274 		
275 		if (termBo != null) {
276 			result= TermBo.to(termBo);
277 		}
278 		
279 		return result;
280 	}
281 	
282 	/**
283 	 * @see org.kuali.rice.krms.impl.repository.TermBoService#getTermResolverById(java.lang.String)
284 	 */
285 	@Override
286 	public TermResolverDefinition getTermResolverById(String id) {
287 		TermResolverDefinition result = null;
288 		
289 		if (StringUtils.isBlank(id)) {
290 			throw new RiceIllegalArgumentException("id must not be blank or null");
291 		}
292 		TermResolverBo termResolverBo = businessObjectService.findBySinglePrimaryKey(TermResolverBo.class, id);
293 		
294 		if (termResolverBo != null) {
295 			result = TermResolverBo.to(termResolverBo);
296 		}
297 		
298 		return result;
299 	}
300 
301     @Override
302     public List<TermResolverDefinition> findTermResolversByOutputId(String id, String namespace) {
303         List<TermResolverDefinition> results = null;
304 
305 		if (StringUtils.isBlank(id)) {
306 			throw new RiceIllegalArgumentException("id must not be blank or null");
307 		}
308         if (StringUtils.isBlank(namespace)) {
309 			throw new RiceIllegalArgumentException("namespace must not be blank or null");
310 		}
311         Map<String, String> criteria = new HashMap<String, String>(2);
312 
313         criteria.put("outputId", id);
314         criteria.put("namespace", namespace);
315 
316 		Collection<TermResolverBo> termResolverBos = businessObjectService.findMatching(TermResolverBo.class, criteria);
317 
318 		if (!CollectionUtils.isEmpty(termResolverBos)) {
319 			results = new ArrayList<TermResolverDefinition>(termResolverBos.size());
320 
321             for (TermResolverBo termResolverBo : termResolverBos) {
322                 results.add(TermResolverBo.to(termResolverBo));
323             }
324 		} else {
325             results = Collections.emptyList();
326         }
327 
328 		return results;
329     }
330 
331     @Override
332     public List<TermResolverDefinition> findTermResolversByNamespace(String namespace) {
333         List<TermResolverDefinition> results = null;
334 
335         if (StringUtils.isBlank(namespace)) {
336             throw new RiceIllegalArgumentException("namespace must not be blank or null");
337         }
338 
339         Map fieldValues = new HashMap();
340         fieldValues.put("namespace", namespace);
341 
342         Collection<TermResolverBo> termResolverBos = businessObjectService.findMatching(TermResolverBo.class, fieldValues);
343 
344         if (!CollectionUtils.isEmpty(termResolverBos)) {
345             results = new ArrayList<TermResolverDefinition>(termResolverBos.size());
346 
347             for (TermResolverBo termResolverBo : termResolverBos) if (termResolverBo != null) {
348                 results.add(TermResolverBo.to(termResolverBo));
349             }
350         } else {
351             results = Collections.emptyList();
352         }
353 
354         return results;
355     }
356 
357     @Override
358     public TermResolverDefinition getTermResolverByNameAndNamespace(String name, String namespace)
359             throws RiceIllegalArgumentException {
360         if (StringUtils.isBlank(name)) {
361             throw new IllegalArgumentException("name is null or blank");
362         }
363         if (StringUtils.isBlank(namespace)) {
364             throw new IllegalArgumentException("namespace is null or blank");
365         }
366         final Map<String, Object> map = new HashMap<String, Object>();
367         map.put("name", name);
368         map.put("namespace", namespace);
369         TermResolverBo bo = businessObjectService.findByPrimaryKey(TermResolverBo.class, map);
370         return TermResolverBo.to(bo);
371     }
372 
373     @Override
374     public TermSpecificationDefinition getTermSpecificationByNameAndNamespace(String name, String namespace) 
375             throws RiceIllegalArgumentException {
376         if (StringUtils.isBlank(name)) {
377             throw new IllegalArgumentException("name is null or blank");
378         }
379         if (StringUtils.isBlank(namespace)) {
380             throw new IllegalArgumentException("namespace is null or blank");
381         }
382         final Map<String, Object> map = new HashMap<String, Object>();
383         map.put("name", name);
384         map.put("namespace", namespace);
385         TermSpecificationBo bo = businessObjectService.findByPrimaryKey(TermSpecificationBo.class, map);
386         return TermSpecificationBo.to(bo);
387     }
388     
389     
390     
391 }