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