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