001/** 002 * Copyright 2005-2016 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 */ 016package org.kuali.rice.location.impl.postalcode; 017 018import org.apache.commons.lang.StringUtils; 019import org.kuali.rice.core.api.criteria.CriteriaLookupService; 020import org.kuali.rice.core.api.criteria.GenericQueryResults; 021import org.kuali.rice.core.api.criteria.QueryByCriteria; 022import org.kuali.rice.core.api.exception.RiceIllegalArgumentException; 023import org.kuali.rice.krad.service.BusinessObjectService; 024import org.kuali.rice.location.api.postalcode.PostalCode; 025import org.kuali.rice.location.api.postalcode.PostalCodeQueryResults; 026import org.kuali.rice.location.api.postalcode.PostalCodeService; 027 028import java.util.ArrayList; 029import java.util.Collection; 030import java.util.Collections; 031import java.util.HashMap; 032import java.util.List; 033import java.util.Map; 034 035public class PostalCodeServiceImpl implements PostalCodeService { 036 037 private BusinessObjectService businessObjectService; 038 private CriteriaLookupService criteriaLookupService; 039 040 @Override 041 public PostalCode getPostalCode(String countryCode, String code) { 042 if (StringUtils.isBlank(countryCode)) { 043 throw new RiceIllegalArgumentException(("countryCode is null")); 044 } 045 046 if (StringUtils.isBlank(code)) { 047 throw new RiceIllegalArgumentException(("code is null")); 048 } 049 050 final Map<String, Object> map = new HashMap<String, Object>(); 051 map.put("countryCode", countryCode); 052 map.put("code", code); 053 054 return PostalCodeBo.to(businessObjectService.findByPrimaryKey(PostalCodeBo.class, Collections.unmodifiableMap(map))); 055 } 056 057 @Override 058 public List<PostalCode> findAllPostalCodesInCountry(String countryCode) { 059 if (StringUtils.isBlank(countryCode)) { 060 throw new RiceIllegalArgumentException(("countryCode is null")); 061 } 062 063 final Map<String, Object> map = new HashMap<String, Object>(); 064 map.put("countryCode", countryCode); 065 map.put("active", Boolean.TRUE); 066 067 final Collection<PostalCodeBo> bos = businessObjectService.findMatching(PostalCodeBo.class, Collections.unmodifiableMap(map)); 068 if (bos == null) { 069 return Collections.emptyList(); 070 } 071 072 final List<PostalCode> toReturn = new ArrayList<PostalCode>(); 073 for (PostalCodeBo bo : bos) { 074 if (bo != null && bo.isActive()) { 075 toReturn.add(PostalCodeBo.to(bo)); 076 } 077 } 078 079 return Collections.unmodifiableList(toReturn); 080 } 081 082 @Override 083 public PostalCodeQueryResults findPostalCodes(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException { 084 incomingParamCheck(queryByCriteria, "queryByCriteria"); 085 086 GenericQueryResults<PostalCodeBo> results = criteriaLookupService.lookup(PostalCodeBo.class, queryByCriteria); 087 088 PostalCodeQueryResults.Builder builder = PostalCodeQueryResults.Builder.create(); 089 builder.setMoreResultsAvailable(results.isMoreResultsAvailable()); 090 builder.setTotalRowCount(results.getTotalRowCount()); 091 092 final List<PostalCode.Builder> ims = new ArrayList<PostalCode.Builder>(); 093 for (PostalCodeBo bo : results.getResults()) { 094 ims.add(PostalCode.Builder.create(bo)); 095 } 096 097 builder.setResults(ims); 098 return builder.build(); 099 } 100 101 public void setBusinessObjectService(BusinessObjectService businessObjectService) { 102 this.businessObjectService = businessObjectService; 103 } 104 105 private void incomingParamCheck(Object object, String name) { 106 if (object == null) { 107 throw new RiceIllegalArgumentException(name + " was null"); 108 } else if (object instanceof String 109 && StringUtils.isBlank((String) object)) { 110 throw new RiceIllegalArgumentException(name + " was blank"); 111 } 112 } 113 114 /** 115 * Sets the criteriaLookupService attribute value. 116 * 117 * @param criteriaLookupService The criteriaLookupService to set. 118 */ 119 public void setCriteriaLookupService(final CriteriaLookupService criteriaLookupService) { 120 this.criteriaLookupService = criteriaLookupService; 121 } 122}