001 /*
002 * Copyright 2006-2011 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
017 package org.kuali.rice.shareddata.impl.state;
018
019
020 import org.apache.commons.lang.StringUtils;
021 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
022 import org.kuali.rice.core.api.exception.RiceIllegalStateException;
023 import org.kuali.rice.krad.service.BusinessObjectService;
024 import org.kuali.rice.shareddata.api.country.Country;
025 import org.kuali.rice.shareddata.api.country.CountryService;
026 import org.kuali.rice.shareddata.api.services.SharedDataApiServiceLocator;
027 import org.kuali.rice.shareddata.api.state.State;
028 import org.kuali.rice.shareddata.api.state.StateService;
029
030 import java.util.ArrayList;
031 import java.util.Collection;
032 import java.util.Collections;
033 import java.util.HashMap;
034 import java.util.List;
035 import java.util.Map;
036
037 public class StateServiceImpl implements StateService {
038
039 private BusinessObjectService businessObjectService;
040 private CountryService countryService;
041
042 @Override
043 public State getState(String countryCode, String code) {
044 if (StringUtils.isBlank(countryCode)) {
045 throw new RiceIllegalArgumentException(("countryCode is null"));
046 }
047
048 if (StringUtils.isBlank(code)) {
049 throw new RiceIllegalArgumentException(("code is null"));
050 }
051
052 final Map<String, Object> map = new HashMap<String, Object>();
053 map.put("countryCode", countryCode);
054 map.put("code", code);
055
056 return StateBo.to(businessObjectService.findByPrimaryKey(StateBo.class, Collections.unmodifiableMap(map)));
057 }
058
059 @Override
060 public List<State> findAllStatesInCountry(String countryCode) {
061 if (StringUtils.isBlank(countryCode)) {
062 throw new RiceIllegalArgumentException(("countryCode is null"));
063 }
064
065 final Map<String, Object> map = new HashMap<String, Object>();
066 map.put("countryCode", countryCode);
067 map.put("active", Boolean.TRUE);
068
069 final Collection<StateBo> bos = businessObjectService.findMatching(StateBo.class, Collections.unmodifiableMap(map));
070 if (bos == null) {
071 return Collections.emptyList();
072 }
073
074 final List<State> toReturn = new ArrayList<State>();
075 for (StateBo bo : bos) {
076 if (bo != null && bo.isActive()) {
077 toReturn.add(StateBo.to(bo));
078 }
079 }
080
081 return Collections.unmodifiableList(toReturn);
082 }
083
084 @Override
085 public List<State> findAllStatesInCountryByAltCode(String alternateCode) {
086 if (StringUtils.isBlank(alternateCode)) {
087 throw new RiceIllegalArgumentException(("alternateCode is null"));
088 }
089
090 Country country = getCountryService().getCountryByAlternateCode(alternateCode);
091
092 if(country == null) {
093 throw new RiceIllegalStateException("The alternateCode is not a valid Alternate Postal Country Code. alternateCode: " + alternateCode);
094 }
095
096 final List<State> toReturn = findAllStatesInCountry(country.getCode());
097 return Collections.unmodifiableList(toReturn);
098 }
099
100 public CountryService getCountryService() {
101 if (countryService == null) {
102 countryService = SharedDataApiServiceLocator.getCountryService();
103 }
104 return countryService;
105 }
106
107 public void setCountryService(CountryService countryService) {
108 this.countryService = countryService;
109 }
110
111 public void setBusinessObjectService(BusinessObjectService businessObjectService) {
112 this.businessObjectService = businessObjectService;
113 }
114 }