1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.location.impl.state;
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.core.api.exception.RiceIllegalStateException;
23 import org.kuali.rice.krad.data.CompoundKey;
24 import org.kuali.rice.krad.data.DataObjectService;
25 import org.kuali.rice.location.api.country.Country;
26 import org.kuali.rice.location.api.country.CountryService;
27 import org.kuali.rice.location.api.services.LocationApiServiceLocator;
28 import org.kuali.rice.location.api.state.State;
29 import org.kuali.rice.location.api.state.StateQueryResults;
30 import org.kuali.rice.location.api.state.StateService;
31 import org.springframework.beans.factory.annotation.Required;
32
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.Map;
38
39 public class StateServiceImpl implements StateService {
40 private CountryService countryService;
41 private DataObjectService dataObjectService;
42
43 @Override
44 public State getState(String countryCode, String code) {
45 if (StringUtils.isBlank(countryCode)) {
46 throw new RiceIllegalArgumentException(("countryCode is null"));
47 }
48
49 if (StringUtils.isBlank(code)) {
50 throw new RiceIllegalArgumentException(("code is null"));
51 }
52
53 final Map<String, Object> map = new HashMap<String, Object>();
54 map.put("countryCode", countryCode);
55 map.put("code", code);
56
57 return StateBo.to(getDataObjectService().find(StateBo.class,new CompoundKey(map)));
58 }
59
60 @Override
61 public List<State> findAllStatesInCountry(String countryCode) {
62 if (StringUtils.isBlank(countryCode)) {
63 throw new RiceIllegalArgumentException(("countryCode is null"));
64 }
65
66 final Map<String, Object> map = new HashMap<String, Object>();
67 map.put("countryCode", countryCode);
68 map.put("active", Boolean.TRUE);
69
70 QueryResults<StateBo> stateBos = getDataObjectService().findMatching(StateBo.class,
71 QueryByCriteria.Builder.andAttributes(map).build());
72
73 if (stateBos == null) {
74 return Collections.emptyList();
75 }
76
77 final List<State> toReturn = new ArrayList<State>();
78 for (StateBo bo : stateBos.getResults()) {
79 if (bo != null && bo.isActive()) {
80 toReturn.add(StateBo.to(bo));
81 }
82 }
83
84 return Collections.unmodifiableList(toReturn);
85 }
86
87 @Override
88 public List<State> findAllStatesInCountryByAltCode(String alternateCode) {
89 if (StringUtils.isBlank(alternateCode)) {
90 throw new RiceIllegalArgumentException(("alternateCode is null"));
91 }
92
93 Country country = getCountryService().getCountryByAlternateCode(alternateCode);
94
95 if(country == null) {
96 throw new RiceIllegalStateException("The alternateCode is not a valid Alternate Postal Country Code. alternateCode: " + alternateCode);
97 }
98
99 final List<State> toReturn = findAllStatesInCountry(country.getCode());
100 return Collections.unmodifiableList(toReturn);
101 }
102
103 @Override
104 public StateQueryResults findStates(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
105 incomingParamCheck(queryByCriteria, "queryByCriteria");
106
107 QueryResults<StateBo> results = getDataObjectService().findMatching(StateBo.class, queryByCriteria);
108
109 StateQueryResults.Builder builder = StateQueryResults.Builder.create();
110 builder.setMoreResultsAvailable(results.isMoreResultsAvailable());
111 builder.setTotalRowCount(results.getTotalRowCount());
112
113 final List<State.Builder> ims = new ArrayList<State.Builder>();
114 for (StateBo bo : results.getResults()) {
115 ims.add(State.Builder.create(bo));
116 }
117
118 builder.setResults(ims);
119 return builder.build();
120 }
121
122 public CountryService getCountryService() {
123 if (countryService == null) {
124 countryService = LocationApiServiceLocator.getCountryService();
125 }
126 return countryService;
127 }
128
129 public void setCountryService(CountryService countryService) {
130 this.countryService = countryService;
131 }
132
133 private void incomingParamCheck(Object object, String name) {
134 if (object == null) {
135 throw new RiceIllegalArgumentException(name + " was null");
136 } else if (object instanceof String
137 && StringUtils.isBlank((String) object)) {
138 throw new RiceIllegalArgumentException(name + " was blank");
139 }
140 }
141
142 public DataObjectService getDataObjectService() {
143 return dataObjectService;
144 }
145
146 @Required
147 public void setDataObjectService(DataObjectService dataObjectService) {
148 this.dataObjectService = dataObjectService;
149 }
150
151 }