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.TermRepositoryService;
30  import org.kuali.rice.krms.api.repository.term.TermResolverDefinition;
31  import org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition;
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, TermRepositoryService {
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  		businessObjectService.save(termSpecBo);
73  		
74  		return TermSpecificationBo.to(termSpecBo);
75  	}
76  	
77  	/**
78  	 * @see org.kuali.rice.krms.impl.repository.TermBoService#createTerm(org.kuali.rice.krms.api.repository.term.TermDefinition)
79  	 */
80  	@Override
81  	public TermDefinition createTerm(TermDefinition termDef) {
82  		if (!StringUtils.isBlank(termDef.getId())) {
83  			throw new RiceIllegalArgumentException("for creation, TermDefinition.id must be null");
84  		}
85  		
86  		TermBo termBo = TermBo.from(termDef);
87  		
88  		businessObjectService.save(termBo);
89  		
90  		return TermBo.to(termBo);
91  	}
92  	
93  	/**
94  	 * @see org.kuali.rice.krms.impl.repository.TermBoService#createTermResolver(org.kuali.rice.krms.api.repository.term.TermResolverDefinition)
95  	 */
96  	@Override
97  	public TermResolverDefinition createTermResolver(TermResolverDefinition termResolver) {
98  		if (!StringUtils.isBlank(termResolver.getId())) {
99  			throw new RiceIllegalArgumentException("for creation, TermResolverDefinition.id must be null");
100 		}
101 		
102 		TermResolverBo termResolverBo = TermResolverBo.from(termResolver);
103 		
104 		termResolverBo = (TermResolverBo)businessObjectService.save(termResolverBo);
105 		
106 		return TermResolverBo.to(termResolverBo);
107 	}
108 	
109 	/**
110 	 * @see org.kuali.rice.krms.impl.repository.TermBoService#getTerm(java.lang.String)
111 	 */
112 	@Override
113 	public TermDefinition getTerm(String id) {
114 		TermDefinition result = null;
115 		
116 		if (StringUtils.isBlank(id)) {
117 			throw new RiceIllegalArgumentException("id must not be blank or null");
118 		}
119 		TermBo termBo = businessObjectService.findBySinglePrimaryKey(TermBo.class, id);
120 		
121 		if (termBo != null) {
122 			result= TermBo.to(termBo);
123 		}
124 		
125 		return result;
126 	}
127 	
128 	/**
129 	 * @see org.kuali.rice.krms.impl.repository.TermBoService#getTermResolverById(java.lang.String)
130 	 */
131 	@Override
132 	public TermResolverDefinition getTermResolverById(String id) {
133 		TermResolverDefinition result = null;
134 		
135 		if (StringUtils.isBlank(id)) {
136 			throw new RiceIllegalArgumentException("id must not be blank or null");
137 		}
138 		TermResolverBo termResolverBo = businessObjectService.findBySinglePrimaryKey(TermResolverBo.class, id);
139 		
140 		if (termResolverBo != null) {
141 			result = TermResolverBo.to(termResolverBo);
142 		}
143 		
144 		return result;
145 	}
146 
147     @Override
148     public List<TermResolverDefinition> findTermResolversByOutputId(String id, String namespace) {
149         List<TermResolverDefinition> results = null;
150 
151 		if (StringUtils.isBlank(id)) {
152 			throw new RiceIllegalArgumentException("id must not be blank or null");
153 		}
154         if (StringUtils.isBlank(namespace)) {
155 			throw new RiceIllegalArgumentException("namespace must not be blank or null");
156 		}
157         Map<String, String> criteria = new HashMap<String, String>(2);
158 
159         criteria.put("outputId", id);
160         criteria.put("namespace", namespace);
161 
162 		Collection<TermResolverBo> termResolverBos = businessObjectService.findMatching(TermResolverBo.class, criteria);
163 
164 		if (!CollectionUtils.isEmpty(termResolverBos)) {
165 			results = new ArrayList<TermResolverDefinition>(termResolverBos.size());
166 
167             for (TermResolverBo termResolverBo : termResolverBos) {
168                 results.add(TermResolverBo.to(termResolverBo));
169             }
170 		} else {
171             results = Collections.emptyList();
172         }
173 
174 		return results;
175     }
176 
177     @Override
178     public List<TermResolverDefinition> findTermResolversByNamespace(String namespace) {
179         List<TermResolverDefinition> results = null;
180 
181         if (StringUtils.isBlank(namespace)) {
182             throw new RiceIllegalArgumentException("namespace must not be blank or null");
183         }
184 
185         Map fieldValues = new HashMap();
186         fieldValues.put("namespace", namespace);
187 
188         Collection<TermResolverBo> termResolverBos = businessObjectService.findMatching(TermResolverBo.class, fieldValues);
189 
190         if (!CollectionUtils.isEmpty(termResolverBos)) {
191             results = new ArrayList<TermResolverDefinition>(termResolverBos.size());
192 
193             for (TermResolverBo termResolverBo : termResolverBos) if (termResolverBo != null) {
194                 results.add(TermResolverBo.to(termResolverBo));
195             }
196         } else {
197             results = Collections.emptyList();
198         }
199 
200         return results;
201     }
202 }