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.location.impl.state; 017 018 019 import org.apache.commons.lang.StringUtils; 020 import org.kuali.rice.core.api.criteria.CriteriaLookupService; 021 import org.kuali.rice.core.api.criteria.GenericQueryResults; 022 import org.kuali.rice.core.api.criteria.QueryByCriteria; 023 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException; 024 import org.kuali.rice.core.api.exception.RiceIllegalStateException; 025 import org.kuali.rice.krad.service.BusinessObjectService; 026 import org.kuali.rice.location.api.country.Country; 027 import org.kuali.rice.location.api.country.CountryService; 028 import org.kuali.rice.location.api.services.LocationApiServiceLocator; 029 import org.kuali.rice.location.api.state.State; 030 import org.kuali.rice.location.api.state.StateQueryResults; 031 import org.kuali.rice.location.api.state.StateService; 032 033 import java.util.ArrayList; 034 import java.util.Collection; 035 import java.util.Collections; 036 import java.util.HashMap; 037 import java.util.List; 038 import java.util.Map; 039 040 public class StateServiceImpl implements StateService { 041 042 private BusinessObjectService businessObjectService; 043 private CountryService countryService; 044 private CriteriaLookupService criteriaLookupService; 045 046 @Override 047 public State getState(String countryCode, String code) { 048 if (StringUtils.isBlank(countryCode)) { 049 throw new RiceIllegalArgumentException(("countryCode is null")); 050 } 051 052 if (StringUtils.isBlank(code)) { 053 throw new RiceIllegalArgumentException(("code is null")); 054 } 055 056 final Map<String, Object> map = new HashMap<String, Object>(); 057 map.put("countryCode", countryCode); 058 map.put("code", code); 059 060 return StateBo.to(businessObjectService.findByPrimaryKey(StateBo.class, Collections.unmodifiableMap(map))); 061 } 062 063 @Override 064 public List<State> findAllStatesInCountry(String countryCode) { 065 if (StringUtils.isBlank(countryCode)) { 066 throw new RiceIllegalArgumentException(("countryCode is null")); 067 } 068 069 final Map<String, Object> map = new HashMap<String, Object>(); 070 map.put("countryCode", countryCode); 071 map.put("active", Boolean.TRUE); 072 073 final Collection<StateBo> bos = businessObjectService.findMatching(StateBo.class, Collections.unmodifiableMap(map)); 074 if (bos == null) { 075 return Collections.emptyList(); 076 } 077 078 final List<State> toReturn = new ArrayList<State>(); 079 for (StateBo bo : bos) { 080 if (bo != null && bo.isActive()) { 081 toReturn.add(StateBo.to(bo)); 082 } 083 } 084 085 return Collections.unmodifiableList(toReturn); 086 } 087 088 @Override 089 public List<State> findAllStatesInCountryByAltCode(String alternateCode) { 090 if (StringUtils.isBlank(alternateCode)) { 091 throw new RiceIllegalArgumentException(("alternateCode is null")); 092 } 093 094 Country country = getCountryService().getCountryByAlternateCode(alternateCode); 095 096 if(country == null) { 097 throw new RiceIllegalStateException("The alternateCode is not a valid Alternate Postal Country Code. alternateCode: " + alternateCode); 098 } 099 100 final List<State> toReturn = findAllStatesInCountry(country.getCode()); 101 return Collections.unmodifiableList(toReturn); 102 } 103 104 @Override 105 public StateQueryResults findStates(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException { 106 incomingParamCheck(queryByCriteria, "queryByCriteria"); 107 108 GenericQueryResults<StateBo> results = criteriaLookupService.lookup(StateBo.class, queryByCriteria); 109 110 StateQueryResults.Builder builder = StateQueryResults.Builder.create(); 111 builder.setMoreResultsAvailable(results.isMoreResultsAvailable()); 112 builder.setTotalRowCount(results.getTotalRowCount()); 113 114 final List<State.Builder> ims = new ArrayList<State.Builder>(); 115 for (StateBo bo : results.getResults()) { 116 ims.add(State.Builder.create(bo)); 117 } 118 119 builder.setResults(ims); 120 return builder.build(); 121 } 122 123 public CountryService getCountryService() { 124 if (countryService == null) { 125 countryService = LocationApiServiceLocator.getCountryService(); 126 } 127 return countryService; 128 } 129 130 public void setCountryService(CountryService countryService) { 131 this.countryService = countryService; 132 } 133 134 public void setBusinessObjectService(BusinessObjectService businessObjectService) { 135 this.businessObjectService = businessObjectService; 136 } 137 138 private void incomingParamCheck(Object object, String name) { 139 if (object == null) { 140 throw new RiceIllegalArgumentException(name + " was null"); 141 } else if (object instanceof String 142 && StringUtils.isBlank((String) object)) { 143 throw new RiceIllegalArgumentException(name + " was blank"); 144 } 145 } 146 147 /** 148 * Sets the criteriaLookupService attribute value. 149 * 150 * @param criteriaLookupService The criteriaLookupService to set. 151 */ 152 public void setCriteriaLookupService(final CriteriaLookupService criteriaLookupService) { 153 this.criteriaLookupService = criteriaLookupService; 154 } 155 }