View Javadoc

1   /**
2    * Copyright 2005-2012 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.county;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.criteria.CriteriaLookupService;
20  import org.kuali.rice.core.api.criteria.GenericQueryResults;
21  import org.kuali.rice.core.api.criteria.QueryByCriteria;
22  import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
23  import org.kuali.rice.krad.service.BusinessObjectService;
24  import org.kuali.rice.location.api.county.County;
25  import org.kuali.rice.location.api.county.CountyQueryResults;
26  import org.kuali.rice.location.api.county.CountyService;
27  
28  import java.util.ArrayList;
29  import java.util.Collection;
30  import java.util.Collections;
31  import java.util.HashMap;
32  import java.util.List;
33  import java.util.Map;
34  
35  public class CountyServiceImpl implements CountyService {
36  
37      private BusinessObjectService businessObjectService;
38      private CriteriaLookupService criteriaLookupService;
39  
40      @Override
41      public County getCounty(String countryCode, String stateCode, String code) {
42          if (StringUtils.isBlank(countryCode)) {
43              throw new RiceIllegalArgumentException(("countryCode is null"));
44          }
45  
46          if (StringUtils.isBlank(code)) {
47              throw new RiceIllegalArgumentException(("code is null"));
48          }
49  
50          if (StringUtils.isBlank(stateCode)) {
51              throw new RiceIllegalArgumentException(("stateCode is null"));
52          }
53  
54          final Map<String, Object> map = new HashMap<String, Object>();
55          map.put("countryCode", countryCode);
56          map.put("stateCode", stateCode);
57          map.put("code", code);
58  
59          return CountyBo.to(businessObjectService.findByPrimaryKey(CountyBo.class, Collections.unmodifiableMap(map)));
60      }
61  
62      @Override
63      public List<County> findAllCountiesInCountryAndState(String countryCode, String stateCode) {
64          if (StringUtils.isBlank(countryCode)) {
65              throw new RiceIllegalArgumentException(("countryCode is null"));
66          }
67  
68          if (StringUtils.isBlank(stateCode)) {
69              throw new RiceIllegalArgumentException(("stateCode is null"));
70          }
71  
72          final Map<String, Object> map = new HashMap<String, Object>();
73          map.put("countryCode", countryCode);
74          map.put("stateCode", stateCode);
75          map.put("active", Boolean.TRUE);
76  
77          final Collection<CountyBo> bos = businessObjectService.findMatching(CountyBo.class, Collections.unmodifiableMap(map));
78          if (bos == null) {
79              return Collections.emptyList();
80          }
81  
82          final List<County> toReturn = new ArrayList<County>();
83          for (CountyBo bo : bos) {
84              if (bo != null && bo.isActive()) {
85                  toReturn.add(CountyBo.to(bo));
86              }
87          }
88  
89          return Collections.unmodifiableList(toReturn);
90      }
91  
92      @Override
93      public CountyQueryResults findCounties(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
94          incomingParamCheck(queryByCriteria, "queryByCriteria");
95  
96          GenericQueryResults<CountyBo> results = criteriaLookupService.lookup(CountyBo.class, queryByCriteria);
97  
98          CountyQueryResults.Builder builder = CountyQueryResults.Builder.create();
99          builder.setMoreResultsAvailable(results.isMoreResultsAvailable());
100         builder.setTotalRowCount(results.getTotalRowCount());
101 
102         final List<County.Builder> ims = new ArrayList<County.Builder>();
103         for (CountyBo bo : results.getResults()) {
104             ims.add(County.Builder.create(bo));
105         }
106 
107         builder.setResults(ims);
108         return builder.build();
109     }
110 
111     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
112         this.businessObjectService = businessObjectService;
113     }
114 
115     private void incomingParamCheck(Object object, String name) {
116         if (object == null) {
117             throw new RiceIllegalArgumentException(name + " was null");
118         } else if (object instanceof String
119                 && StringUtils.isBlank((String) object)) {
120             throw new RiceIllegalArgumentException(name + " was blank");
121         }
122     }
123 
124     /**
125      * Sets the criteriaLookupService attribute value.
126      *
127      * @param criteriaLookupService The criteriaLookupService to set.
128      */
129     public void setCriteriaLookupService(final CriteriaLookupService criteriaLookupService) {
130         this.criteriaLookupService = criteriaLookupService;
131     }
132 
133 }