1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
116
117
118
119 public void setCriteriaLookupService(final CriteriaLookupService criteriaLookupService) {
120 this.criteriaLookupService = criteriaLookupService;
121 }
122 }