001 /** 002 * Copyright 2005-2014 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.TermRepositoryService; 030 import org.kuali.rice.krms.api.repository.term.TermResolverDefinition; 031 import org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition; 032 import org.kuali.rice.krms.impl.repository.ContextValidTermBo; 033 import org.kuali.rice.krms.impl.repository.TermSpecificationBo; 034 import org.springframework.util.CollectionUtils; 035 036 /** 037 * Implementation of {@link TermBoService} 038 * 039 * @author Kuali Rice Team (rice.collab@kuali.org) 040 * 041 */ 042 public class TermBoServiceImpl implements TermBoService, TermRepositoryService { 043 044 private BusinessObjectService businessObjectService; 045 046 /** 047 * @param businessObjectService the businessObjectService to set 048 */ 049 public void setBusinessObjectService(BusinessObjectService businessObjectService) { 050 this.businessObjectService = businessObjectService; 051 } 052 053 /** 054 * @see org.kuali.rice.krms.impl.repository.TermBoService#getTermSpecificationById(java.lang.String) 055 */ 056 @Override 057 public TermSpecificationDefinition getTermSpecificationById(String id) { 058 TermSpecificationBo termSpecificationBo = 059 businessObjectService.findBySinglePrimaryKey(TermSpecificationBo.class, id); 060 return TermSpecificationDefinition.Builder.create(termSpecificationBo).build(); 061 } 062 063 /** 064 * @see org.kuali.rice.krms.impl.repository.TermBoService#createTermSpecification(org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition) 065 */ 066 @Override 067 public TermSpecificationDefinition createTermSpecification(TermSpecificationDefinition termSpec) { 068 if (!StringUtils.isBlank(termSpec.getId())) { 069 throw new RiceIllegalArgumentException("for creation, TermSpecification.id must be null"); 070 } 071 072 TermSpecificationBo termSpecBo = TermSpecificationBo.from(termSpec); 073 074 businessObjectService.save(termSpecBo); 075 076 return TermSpecificationBo.to(termSpecBo); 077 } 078 079 /** 080 * @see org.kuali.rice.krms.impl.repository.TermBoService#createTerm(org.kuali.rice.krms.api.repository.term.TermDefinition) 081 */ 082 @Override 083 public TermDefinition createTerm(TermDefinition termDef) { 084 if (!StringUtils.isBlank(termDef.getId())) { 085 throw new RiceIllegalArgumentException("for creation, TermDefinition.id must be null"); 086 } 087 088 TermBo termBo = TermBo.from(termDef); 089 090 businessObjectService.save(termBo); 091 092 return TermBo.to(termBo); 093 } 094 095 /** 096 * @see org.kuali.rice.krms.impl.repository.TermBoService#createTermResolver(org.kuali.rice.krms.api.repository.term.TermResolverDefinition) 097 */ 098 @Override 099 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 }