001 /** 002 * Copyright 2005-2012 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.krms.impl.repository; 017 018 import java.util.ArrayList; 019 import java.util.Collection; 020 import java.util.Collections; 021 import java.util.HashMap; 022 import java.util.List; 023 import java.util.Map; 024 025 import org.apache.commons.lang.StringUtils; 026 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException; 027 import org.kuali.rice.krad.service.BusinessObjectService; 028 import org.kuali.rice.krms.api.repository.term.TermDefinition; 029 import org.kuali.rice.krms.api.repository.term.TermResolverDefinition; 030 import org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition; 031 import org.kuali.rice.krms.impl.util.KrmsImplConstants; 032 import org.springframework.util.CollectionUtils; 033 034 /** 035 * Implementation of {@link TermBoService} 036 * 037 * @author Kuali Rice Team (rice.collab@kuali.org) 038 * 039 */ 040 public class TermBoServiceImpl implements TermBoService { 041 042 private BusinessObjectService businessObjectService; 043 044 /** 045 * @param businessObjectService the businessObjectService to set 046 */ 047 public void setBusinessObjectService(BusinessObjectService businessObjectService) { 048 this.businessObjectService = businessObjectService; 049 } 050 051 /** 052 * @see org.kuali.rice.krms.impl.repository.TermBoService#getTermSpecificationById(java.lang.String) 053 */ 054 @Override 055 public TermSpecificationDefinition getTermSpecificationById(String id) { 056 TermSpecificationBo termSpecificationBo = 057 businessObjectService.findBySinglePrimaryKey(TermSpecificationBo.class, id); 058 return TermSpecificationDefinition.Builder.create(termSpecificationBo).build(); 059 } 060 061 /** 062 * @see org.kuali.rice.krms.impl.repository.TermBoService#createTermSpecification(org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition) 063 */ 064 @Override 065 public TermSpecificationDefinition createTermSpecification(TermSpecificationDefinition termSpec) { 066 if (!StringUtils.isBlank(termSpec.getId())) { 067 throw new RiceIllegalArgumentException("for creation, TermSpecification.id must be null"); 068 } 069 070 TermSpecificationBo termSpecBo = TermSpecificationBo.from(termSpec); 071 072 termSpecBo = businessObjectService.save(termSpecBo); 073 074 // save relations to the contexts on the BO 075 if (!CollectionUtils.isEmpty(termSpec.getContextIds())) for (String contextId : termSpec.getContextIds()) { 076 ContextValidTermBo contextValidTerm = new ContextValidTermBo(); 077 contextValidTerm.setContextId(contextId); 078 contextValidTerm.setTermSpecificationId(termSpecBo.getId()); 079 businessObjectService.save(contextValidTerm); 080 } 081 082 return TermSpecificationBo.to(termSpecBo); 083 } 084 085 @Override 086 public void updateTermSpecification(TermSpecificationDefinition termSpec) throws RiceIllegalArgumentException { 087 if (termSpec == null) { 088 throw new IllegalArgumentException("term specification is null"); 089 } 090 091 // must already exist to be able to update 092 final String termSpecificationId = termSpec.getId(); 093 final TermSpecificationBo existing = businessObjectService.findBySinglePrimaryKey(TermSpecificationBo.class, termSpecificationId); 094 if (existing == null) { 095 throw new IllegalStateException("the term specification does not exist: " + termSpec); 096 } 097 final TermSpecificationDefinition toUpdate; 098 if (!existing.getId().equals(termSpec.getId())) { 099 // 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 }