|  1 |     | 
     | 
  |  2 |     | 
     | 
  |  3 |     | 
     | 
  |  4 |     | 
     | 
  |  5 |     | 
     | 
  |  6 |     | 
     | 
  |  7 |     | 
     | 
  |  8 |     | 
     | 
  |  9 |     | 
     | 
  |  10 |     | 
     | 
  |  11 |     | 
     | 
  |  12 |     | 
     | 
  |  13 |     | 
     | 
  |  14 |     | 
     | 
  |  15 |     | 
     | 
  |  16 |     | 
   package org.kuali.rice.location.impl.state;  | 
  |  17 |     | 
     | 
  |  18 |     | 
     | 
  |  19 |     | 
   import org.apache.commons.lang.StringUtils;  | 
  |  20 |     | 
   import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;  | 
  |  21 |     | 
   import org.kuali.rice.core.api.exception.RiceIllegalStateException;  | 
  |  22 |     | 
   import org.kuali.rice.krad.service.BusinessObjectService;  | 
  |  23 |     | 
   import org.kuali.rice.location.api.country.Country;  | 
  |  24 |     | 
   import org.kuali.rice.location.api.country.CountryService;  | 
  |  25 |     | 
   import org.kuali.rice.location.api.services.LocationApiServiceLocator;  | 
  |  26 |     | 
   import org.kuali.rice.location.api.state.State;  | 
  |  27 |     | 
   import org.kuali.rice.location.api.state.StateService;  | 
  |  28 |     | 
     | 
  |  29 |     | 
   import java.util.ArrayList;  | 
  |  30 |     | 
   import java.util.Collection;  | 
  |  31 |     | 
   import java.util.Collections;  | 
  |  32 |     | 
   import java.util.HashMap;  | 
  |  33 |     | 
   import java.util.List;  | 
  |  34 |     | 
   import java.util.Map;  | 
  |  35 |     | 
     | 
  |  36 |    10 |    public class StateServiceImpl implements StateService { | 
  |  37 |     | 
     | 
  |  38 |     | 
       private BusinessObjectService businessObjectService;  | 
  |  39 |     | 
       private CountryService countryService;  | 
  |  40 |     | 
     | 
  |  41 |     | 
       @Override  | 
  |  42 |     | 
       public State getState(String countryCode, String code) { | 
  |  43 |    4 |            if (StringUtils.isBlank(countryCode)) { | 
  |  44 |    1 |                throw new RiceIllegalArgumentException(("countryCode is null")); | 
  |  45 |     | 
           }  | 
  |  46 |     | 
     | 
  |  47 |    3 |            if (StringUtils.isBlank(code)) { | 
  |  48 |    1 |                throw new RiceIllegalArgumentException(("code is null")); | 
  |  49 |     | 
           }  | 
  |  50 |     | 
     | 
  |  51 |    2 |            final Map<String, Object> map = new HashMap<String, Object>();  | 
  |  52 |    2 |            map.put("countryCode", countryCode); | 
  |  53 |    2 |            map.put("code", code); | 
  |  54 |     | 
     | 
  |  55 |    2 |            return StateBo.to(businessObjectService.findByPrimaryKey(StateBo.class, Collections.unmodifiableMap(map)));  | 
  |  56 |     | 
       }  | 
  |  57 |     | 
     | 
  |  58 |     | 
       @Override  | 
  |  59 |     | 
       public List<State> findAllStatesInCountry(String countryCode) { | 
  |  60 |    4 |            if (StringUtils.isBlank(countryCode)) { | 
  |  61 |    1 |                throw new RiceIllegalArgumentException(("countryCode is null")); | 
  |  62 |     | 
           }  | 
  |  63 |     | 
     | 
  |  64 |    3 |            final Map<String, Object> map = new HashMap<String, Object>();  | 
  |  65 |    3 |            map.put("countryCode", countryCode); | 
  |  66 |    3 |            map.put("active", Boolean.TRUE); | 
  |  67 |     | 
     | 
  |  68 |    3 |            final Collection<StateBo> bos = businessObjectService.findMatching(StateBo.class, Collections.unmodifiableMap(map));  | 
  |  69 |    3 |            if (bos == null) { | 
  |  70 |    1 |                return Collections.emptyList();  | 
  |  71 |     | 
           }  | 
  |  72 |     | 
     | 
  |  73 |    2 |            final List<State> toReturn = new ArrayList<State>();  | 
  |  74 |    2 |            for (StateBo bo : bos) { | 
  |  75 |    4 |                if (bo != null && bo.isActive()) { | 
  |  76 |    4 |                    toReturn.add(StateBo.to(bo));  | 
  |  77 |     | 
               }  | 
  |  78 |     | 
           }  | 
  |  79 |     | 
     | 
  |  80 |    2 |            return Collections.unmodifiableList(toReturn);  | 
  |  81 |     | 
       }  | 
  |  82 |     | 
         | 
  |  83 |     | 
       @Override  | 
  |  84 |     | 
       public List<State> findAllStatesInCountryByAltCode(String alternateCode) { | 
  |  85 |    3 |            if (StringUtils.isBlank(alternateCode)) { | 
  |  86 |    1 |                throw new RiceIllegalArgumentException(("alternateCode is null")); | 
  |  87 |     | 
           }  | 
  |  88 |     | 
             | 
  |  89 |    2 |            Country country = getCountryService().getCountryByAlternateCode(alternateCode);  | 
  |  90 |     | 
             | 
  |  91 |    2 |            if(country == null) { | 
  |  92 |    1 |                throw new RiceIllegalStateException("The alternateCode is not a valid Alternate Postal Country Code.  alternateCode: " + alternateCode); | 
  |  93 |     | 
           }  | 
  |  94 |     | 
             | 
  |  95 |    1 |            final List<State> toReturn = findAllStatesInCountry(country.getCode());  | 
  |  96 |    1 |            return Collections.unmodifiableList(toReturn);  | 
  |  97 |     | 
       }  | 
  |  98 |     | 
     | 
  |  99 |     | 
       public CountryService getCountryService() { | 
  |  100 |    2 |            if (countryService == null) { | 
  |  101 |    0 |                countryService = LocationApiServiceLocator.getCountryService();  | 
  |  102 |     | 
           }  | 
  |  103 |    2 |            return countryService;  | 
  |  104 |     | 
       }     | 
  |  105 |     | 
     | 
  |  106 |     | 
       public void setCountryService(CountryService countryService) { | 
  |  107 |    3 |            this.countryService = countryService;  | 
  |  108 |    3 |        }  | 
  |  109 |     | 
         | 
  |  110 |     | 
       public void setBusinessObjectService(BusinessObjectService businessObjectService) { | 
  |  111 |    10 |            this.businessObjectService = businessObjectService;  | 
  |  112 |    10 |        }  | 
  |  113 |     | 
   }  |