View Javadoc

1   /**
2    * Copyright 2005-2013 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.criteria.CriteriaLookupService;
21  import org.kuali.rice.core.api.criteria.GenericQueryResults;
22  import org.kuali.rice.core.api.criteria.QueryByCriteria;
23  import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
24  import org.kuali.rice.core.api.exception.RiceIllegalStateException;
25  import org.kuali.rice.krad.service.BusinessObjectService;
26  import org.kuali.rice.location.api.country.Country;
27  import org.kuali.rice.location.api.country.CountryService;
28  import org.kuali.rice.location.api.services.LocationApiServiceLocator;
29  import org.kuali.rice.location.api.state.State;
30  import org.kuali.rice.location.api.state.StateQueryResults;
31  import org.kuali.rice.location.api.state.StateService;
32  
33  import java.util.ArrayList;
34  import java.util.Collection;
35  import java.util.Collections;
36  import java.util.HashMap;
37  import java.util.List;
38  import java.util.Map;
39  
40  public class StateServiceImpl implements StateService {
41  
42      private BusinessObjectService businessObjectService;
43      private CountryService countryService;
44      private CriteriaLookupService criteriaLookupService;
45  
46      @Override
47      public State getState(String countryCode, String code) {
48          if (StringUtils.isBlank(countryCode)) {
49              throw new RiceIllegalArgumentException(("countryCode is null"));
50          }
51  
52          if (StringUtils.isBlank(code)) {
53              throw new RiceIllegalArgumentException(("code is null"));
54          }
55  
56          final Map<String, Object> map = new HashMap<String, Object>();
57          map.put("countryCode", countryCode);
58          map.put("code", code);
59  
60          return StateBo.to(businessObjectService.findByPrimaryKey(StateBo.class, Collections.unmodifiableMap(map)));
61      }
62  
63      @Override
64      public List<State> findAllStatesInCountry(String countryCode) {
65          if (StringUtils.isBlank(countryCode)) {
66              throw new RiceIllegalArgumentException(("countryCode is null"));
67          }
68  
69          final Map<String, Object> map = new HashMap<String, Object>();
70          map.put("countryCode", countryCode);
71          map.put("active", Boolean.TRUE);
72  
73          final Collection<StateBo> bos = businessObjectService.findMatching(StateBo.class, Collections.unmodifiableMap(map));
74          if (bos == null) {
75              return Collections.emptyList();
76          }
77  
78          final List<State> toReturn = new ArrayList<State>();
79          for (StateBo bo : bos) {
80              if (bo != null && bo.isActive()) {
81                  toReturn.add(StateBo.to(bo));
82              }
83          }
84  
85          return Collections.unmodifiableList(toReturn);
86      }
87      
88      @Override
89      public List<State> findAllStatesInCountryByAltCode(String alternateCode) {
90          if (StringUtils.isBlank(alternateCode)) {
91              throw new RiceIllegalArgumentException(("alternateCode is null"));
92          }
93          
94          Country country = getCountryService().getCountryByAlternateCode(alternateCode);
95          
96          if(country == null) {
97              throw new RiceIllegalStateException("The alternateCode is not a valid Alternate Postal Country Code.  alternateCode: " + alternateCode);
98          }
99          
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 }