View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  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          if (StringUtils.isBlank(countryCode)) {
44              throw new RiceIllegalArgumentException(("countryCode is null"));
45          }
46  
47          if (StringUtils.isBlank(code)) {
48              throw new RiceIllegalArgumentException(("code is null"));
49          }
50  
51          final Map<String, Object> map = new HashMap<String, Object>();
52          map.put("countryCode", countryCode);
53          map.put("code", code);
54  
55          return StateBo.to(businessObjectService.findByPrimaryKey(StateBo.class, Collections.unmodifiableMap(map)));
56      }
57  
58      @Override
59      public List<State> findAllStatesInCountry(String countryCode) {
60          if (StringUtils.isBlank(countryCode)) {
61              throw new RiceIllegalArgumentException(("countryCode is null"));
62          }
63  
64          final Map<String, Object> map = new HashMap<String, Object>();
65          map.put("countryCode", countryCode);
66          map.put("active", Boolean.TRUE);
67  
68          final Collection<StateBo> bos = businessObjectService.findMatching(StateBo.class, Collections.unmodifiableMap(map));
69          if (bos == null) {
70              return Collections.emptyList();
71          }
72  
73          final List<State> toReturn = new ArrayList<State>();
74          for (StateBo bo : bos) {
75              if (bo != null && bo.isActive()) {
76                  toReturn.add(StateBo.to(bo));
77              }
78          }
79  
80          return Collections.unmodifiableList(toReturn);
81      }
82      
83      @Override
84      public List<State> findAllStatesInCountryByAltCode(String alternateCode) {
85          if (StringUtils.isBlank(alternateCode)) {
86              throw new RiceIllegalArgumentException(("alternateCode is null"));
87          }
88          
89          Country country = getCountryService().getCountryByAlternateCode(alternateCode);
90          
91          if(country == null) {
92              throw new RiceIllegalStateException("The alternateCode is not a valid Alternate Postal Country Code.  alternateCode: " + alternateCode);
93          }
94          
95          final List<State> toReturn = findAllStatesInCountry(country.getCode());
96          return Collections.unmodifiableList(toReturn);
97      }
98  
99      public CountryService getCountryService() {
100         if (countryService == null) {
101             countryService = LocationApiServiceLocator.getCountryService();
102         }
103         return countryService;
104     }   
105 
106     public void setCountryService(CountryService countryService) {
107         this.countryService = countryService;
108     }
109     
110     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
111         this.businessObjectService = businessObjectService;
112     }
113 }