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.postalcode;
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.postalcode.PostalCode;
25  import org.kuali.rice.location.api.postalcode.PostalCodeQueryResults;
26  import org.kuali.rice.location.api.postalcode.PostalCodeService;
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 PostalCodeServiceImpl implements PostalCodeService {
36  
37      private BusinessObjectService businessObjectService;
38      private CriteriaLookupService criteriaLookupService;
39  
40      @Override
41      public PostalCode getPostalCode(String countryCode, 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          final Map<String, Object> map = new HashMap<String, Object>();
51          map.put("countryCode", countryCode);
52          map.put("code", code);
53  
54          return PostalCodeBo.to(businessObjectService.findByPrimaryKey(PostalCodeBo.class, Collections.unmodifiableMap(map)));
55      }
56  
57      @Override
58      public List<PostalCode> findAllPostalCodesInCountry(String countryCode) {
59          if (StringUtils.isBlank(countryCode)) {
60              throw new RiceIllegalArgumentException(("countryCode is null"));
61          }
62  
63          final Map<String, Object> map = new HashMap<String, Object>();
64          map.put("countryCode", countryCode);
65          map.put("active", Boolean.TRUE);
66  
67          final Collection<PostalCodeBo> bos = businessObjectService.findMatching(PostalCodeBo.class, Collections.unmodifiableMap(map));
68          if (bos == null) {
69              return Collections.emptyList();
70          }
71  
72          final List<PostalCode> toReturn = new ArrayList<PostalCode>();
73          for (PostalCodeBo bo : bos) {
74              if (bo != null && bo.isActive()) {
75                  toReturn.add(PostalCodeBo.to(bo));
76              }
77          }
78  
79          return Collections.unmodifiableList(toReturn);
80      }
81  
82      @Override
83      public PostalCodeQueryResults findPostalCodes(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
84          incomingParamCheck(queryByCriteria, "queryByCriteria");
85  
86          GenericQueryResults<PostalCodeBo> results = criteriaLookupService.lookup(PostalCodeBo.class, queryByCriteria);
87  
88          PostalCodeQueryResults.Builder builder = PostalCodeQueryResults.Builder.create();
89          builder.setMoreResultsAvailable(results.isMoreResultsAvailable());
90          builder.setTotalRowCount(results.getTotalRowCount());
91  
92          final List<PostalCode.Builder> ims = new ArrayList<PostalCode.Builder>();
93          for (PostalCodeBo bo : results.getResults()) {
94              ims.add(PostalCode.Builder.create(bo));
95          }
96  
97          builder.setResults(ims);
98          return builder.build();
99      }
100 
101     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
102         this.businessObjectService = businessObjectService;
103     }
104 
105     private void incomingParamCheck(Object object, String name) {
106         if (object == null) {
107             throw new RiceIllegalArgumentException(name + " was null");
108         } else if (object instanceof String
109                 && StringUtils.isBlank((String) object)) {
110             throw new RiceIllegalArgumentException(name + " was blank");
111         }
112     }
113 
114     /**
115      * Sets the criteriaLookupService attribute value.
116      *
117      * @param criteriaLookupService The criteriaLookupService to set.
118      */
119     public void setCriteriaLookupService(final CriteriaLookupService criteriaLookupService) {
120         this.criteriaLookupService = criteriaLookupService;
121     }
122 }