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 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.TermRepositoryService;
30  import org.kuali.rice.krms.api.repository.term.TermResolverDefinition;
31  import org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition;
32  import org.kuali.rice.krms.impl.repository.ContextValidTermBo;
33  import org.kuali.rice.krms.impl.repository.TermSpecificationBo;
34  import org.springframework.util.CollectionUtils;
35  
36  /**
37   * Implementation of {@link TermBoService}
38   * 
39   * @author Kuali Rice Team (rice.collab@kuali.org)
40   *
41   */
42  public class TermBoServiceImpl implements TermBoService, TermRepositoryService {
43  	
44  	private BusinessObjectService businessObjectService;
45  
46  	/**
47  	 * @param businessObjectService the businessObjectService to set
48  	 */
49  	public void setBusinessObjectService(BusinessObjectService businessObjectService) {
50  		this.businessObjectService = businessObjectService;
51  	}
52  	
53  	/**
54  	 * @see org.kuali.rice.krms.impl.repository.TermBoService#getTermSpecificationById(java.lang.String)
55  	 */
56  	@Override
57  	public TermSpecificationDefinition getTermSpecificationById(String id) {
58  		TermSpecificationBo termSpecificationBo = 
59  			businessObjectService.findBySinglePrimaryKey(TermSpecificationBo.class, id);
60  		return TermSpecificationDefinition.Builder.create(termSpecificationBo).build();
61  	}
62  	
63  	/**
64  	 * @see org.kuali.rice.krms.impl.repository.TermBoService#createTermSpecification(org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition)
65  	 */
66  	@Override
67  	public TermSpecificationDefinition createTermSpecification(TermSpecificationDefinition termSpec) {
68  		if (!StringUtils.isBlank(termSpec.getId())) {
69  			throw new RiceIllegalArgumentException("for creation, TermSpecification.id must be null");
70  		}
71  		
72  		TermSpecificationBo termSpecBo = TermSpecificationBo.from(termSpec);
73  		
74  		businessObjectService.save(termSpecBo);
75  		
76  		return TermSpecificationBo.to(termSpecBo);
77  	}
78  	
79  	/**
80  	 * @see org.kuali.rice.krms.impl.repository.TermBoService#createTerm(org.kuali.rice.krms.api.repository.term.TermDefinition)
81  	 */
82  	@Override
83  	public TermDefinition createTerm(TermDefinition termDef) {
84  		if (!StringUtils.isBlank(termDef.getId())) {
85  			throw new RiceIllegalArgumentException("for creation, TermDefinition.id must be null");
86  		}
87  		
88  		TermBo termBo = TermBo.from(termDef);
89  		
90  		businessObjectService.save(termBo);
91  		
92  		return TermBo.to(termBo);
93  	}
94  	
95  	/**
96  	 * @see org.kuali.rice.krms.impl.repository.TermBoService#createTermResolver(org.kuali.rice.krms.api.repository.term.TermResolverDefinition)
97  	 */
98  	@Override
99  	public TermResolverDefinition createTermResolver(TermResolverDefinition termResolver) {
100 		if (!StringUtils.isBlank(termResolver.getId())) {
101 			throw new RiceIllegalArgumentException("for creation, TermResolverDefinition.id must be null");
102 		}
103 		
104 		TermResolverBo termResolverBo = TermResolverBo.from(termResolver);
105 		
106 		termResolverBo = (TermResolverBo)businessObjectService.save(termResolverBo);
107 		
108 		return TermResolverBo.to(termResolverBo);
109 	}
110 	
111 	/**
112 	 * @see org.kuali.rice.krms.impl.repository.TermBoService#getTerm(java.lang.String)
113 	 */
114 	@Override
115 	public TermDefinition getTerm(String id) {
116 		TermDefinition result = null;
117 		
118 		if (StringUtils.isBlank(id)) {
119 			throw new RiceIllegalArgumentException("id must not be blank or null");
120 		}
121 		TermBo termBo = businessObjectService.findBySinglePrimaryKey(TermBo.class, id);
122 		
123 		if (termBo != null) {
124 			result= TermBo.to(termBo);
125 		}
126 		
127 		return result;
128 	}
129 	
130 	/**
131 	 * @see org.kuali.rice.krms.impl.repository.TermBoService#getTermResolverById(java.lang.String)
132 	 */
133 	@Override
134 	public TermResolverDefinition getTermResolverById(String id) {
135 		TermResolverDefinition result = null;
136 		
137 		if (StringUtils.isBlank(id)) {
138 			throw new RiceIllegalArgumentException("id must not be blank or null");
139 		}
140 		TermResolverBo termResolverBo = businessObjectService.findBySinglePrimaryKey(TermResolverBo.class, id);
141 		
142 		if (termResolverBo != null) {
143 			result = TermResolverBo.to(termResolverBo);
144 		}
145 		
146 		return result;
147 	}
148 
149     @Override
150     public List<TermResolverDefinition> findTermResolversByOutputId(String id, String namespace) {
151         List<TermResolverDefinition> results = null;
152 
153 		if (StringUtils.isBlank(id)) {
154 			throw new RiceIllegalArgumentException("id must not be blank or null");
155 		}
156         if (StringUtils.isBlank(namespace)) {
157 			throw new RiceIllegalArgumentException("namespace must not be blank or null");
158 		}
159         Map<String, String> criteria = new HashMap<String, String>(2);
160 
161         criteria.put("outputId", id);
162         criteria.put("namespace", namespace);
163 
164 		Collection<TermResolverBo> termResolverBos = businessObjectService.findMatching(TermResolverBo.class, criteria);
165 
166 		if (!CollectionUtils.isEmpty(termResolverBos)) {
167 			results = new ArrayList<TermResolverDefinition>(termResolverBos.size());
168 
169             for (TermResolverBo termResolverBo : termResolverBos) {
170                 results.add(TermResolverBo.to(termResolverBo));
171             }
172 		} else {
173             results = Collections.emptyList();
174         }
175 
176 		return results;
177     }
178 
179     @Override
180     public List<TermResolverDefinition> findTermResolversByNamespace(String namespace) {
181         List<TermResolverDefinition> results = null;
182 
183         if (StringUtils.isBlank(namespace)) {
184             throw new RiceIllegalArgumentException("namespace must not be blank or null");
185         }
186 
187         Map fieldValues = new HashMap();
188         fieldValues.put("namespace", namespace);
189 
190         Collection<TermResolverBo> termResolverBos = businessObjectService.findMatching(TermResolverBo.class, fieldValues);
191 
192         if (!CollectionUtils.isEmpty(termResolverBos)) {
193             results = new ArrayList<TermResolverDefinition>(termResolverBos.size());
194 
195             for (TermResolverBo termResolverBo : termResolverBos) if (termResolverBo != null) {
196                 results.add(TermResolverBo.to(termResolverBo));
197             }
198         } else {
199             results = Collections.emptyList();
200         }
201 
202         return results;
203     }
204     
205     @Override
206     public List<TermSpecificationDefinition> findAllTermSpecificationsByContextId(String contextId){
207         List<TermSpecificationDefinition> results = null;
208         
209         if (StringUtils.isBlank(contextId)){
210             throw new RiceIllegalArgumentException("contextId must not be blank or null");        
211         }
212         
213         Collection<ContextValidTermBo> contextValidTerms = 
214                     businessObjectService.findMatching(ContextValidTermBo.class, 
215                                     Collections.singletonMap("contextId", contextId));
216         
217         if (!CollectionUtils.isEmpty(contextValidTerms)) {
218             results = new ArrayList<TermSpecificationDefinition>(contextValidTerms.size());
219             for (ContextValidTermBo validTerm : contextValidTerms) {
220                 results.add(TermSpecificationBo.to(validTerm.getTermSpecification()));
221             }        
222         } else {
223             results = Collections.emptyList();
224         }
225 
226         return results;
227     }
228 }