View Javadoc
1   /**
2    * Copyright 2005-2016 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.QueryByCriteria;
20  import org.kuali.rice.core.api.criteria.QueryResults;
21  import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
22  import org.kuali.rice.krad.data.CompoundKey;
23  import org.kuali.rice.krad.data.DataObjectService;
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  import org.springframework.beans.factory.annotation.Required;
28  
29  import java.util.ArrayList;
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      private DataObjectService dataObjectService;
37  
38      @Override
39      public County getCounty(String countryCode, String stateCode, String code) {
40          if (StringUtils.isBlank(countryCode)) {
41              throw new RiceIllegalArgumentException(("countryCode is null"));
42          }
43  
44          if (StringUtils.isBlank(code)) {
45              throw new RiceIllegalArgumentException(("code is null"));
46          }
47  
48          if (StringUtils.isBlank(stateCode)) {
49              throw new RiceIllegalArgumentException(("stateCode is null"));
50          }
51  
52          final Map<String, Object> map = new HashMap<String, Object>();
53          map.put("countryCode", countryCode);
54          map.put("stateCode", stateCode);
55          map.put("code", code);
56  
57          return CountyBo.to(getDataObjectService().find(CountyBo.class, new CompoundKey(map)));
58      }
59  
60      @Override
61      public List<County> findAllCountiesInCountryAndState(String countryCode, String stateCode) {
62          if (StringUtils.isBlank(countryCode)) {
63              throw new RiceIllegalArgumentException(("countryCode is null"));
64          }
65  
66          if (StringUtils.isBlank(stateCode)) {
67              throw new RiceIllegalArgumentException(("stateCode is null"));
68          }
69  
70          final Map<String, Object> map = new HashMap<String, Object>();
71          map.put("countryCode", countryCode);
72          map.put("stateCode", stateCode);
73          map.put("active", Boolean.TRUE);
74  
75          QueryResults<CountyBo> countyBos = getDataObjectService().findMatching(CountyBo.class,
76                  QueryByCriteria.Builder.andAttributes(map).build());
77  
78          if (countyBos == null) {
79              return Collections.emptyList();
80          }
81  
82          final List<County> toReturn = new ArrayList<County>();
83  
84          List<CountyBo> countyBoList = countyBos.getResults();
85  
86  
87          for(CountyBo countyBo : countyBoList){
88              if(countyBo != null && countyBo.isActive()){
89                  toReturn.add(CountyBo.to(countyBo));
90              }
91          }
92  
93  //        for (CountyBo bo : countyBoList) {
94  //            if (bo != null && bo.isActive()) {
95  //                toReturn.add(CountyBo.to(bo));
96  //            }
97  //        }
98  
99          return Collections.unmodifiableList(toReturn);
100     }
101 
102     @Override
103     public CountyQueryResults findCounties(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
104         incomingParamCheck(queryByCriteria, "queryByCriteria");
105 
106         QueryResults<CountyBo> results = getDataObjectService().findMatching(CountyBo.class, queryByCriteria);
107 
108         CountyQueryResults.Builder builder = CountyQueryResults.Builder.create();
109         builder.setMoreResultsAvailable(results.isMoreResultsAvailable());
110         builder.setTotalRowCount(results.getTotalRowCount());
111 
112         final List<County.Builder> ims = new ArrayList<County.Builder>();
113         for (CountyBo bo : results.getResults()) {
114             ims.add(County.Builder.create(bo));
115         }
116 
117         builder.setResults(ims);
118         return builder.build();
119     }
120 
121     private void incomingParamCheck(Object object, String name) {
122         if (object == null) {
123             throw new RiceIllegalArgumentException(name + " was null");
124         } else if (object instanceof String
125                 && StringUtils.isBlank((String) object)) {
126             throw new RiceIllegalArgumentException(name + " was blank");
127         }
128     }
129 
130     public DataObjectService getDataObjectService() {
131         return dataObjectService;
132     }
133 
134     @Required
135     public void setDataObjectService(DataObjectService dataObjectService) {
136         this.dataObjectService = dataObjectService;
137     }
138 
139 }