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