1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.location.impl.country;
17
18
19 import org.apache.commons.lang.StringUtils;
20 import org.kuali.rice.core.api.criteria.CriteriaLookupService;
21 import org.kuali.rice.core.api.criteria.GenericQueryResults;
22 import org.kuali.rice.core.api.criteria.QueryByCriteria;
23 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
24 import org.kuali.rice.core.api.exception.RiceIllegalStateException;
25 import org.kuali.rice.coreservice.framework.parameter.ParameterService;
26 import org.kuali.rice.kns.service.KNSServiceLocator;
27 import org.kuali.rice.kns.util.KNSConstants;
28 import org.kuali.rice.krad.service.BusinessObjectService;
29 import org.kuali.rice.krad.util.KRADConstants;
30 import org.kuali.rice.krad.util.KRADPropertyConstants;
31 import org.kuali.rice.location.api.campus.Campus;
32 import org.kuali.rice.location.api.campus.CampusQueryResults;
33 import org.kuali.rice.location.api.country.Country;
34 import org.kuali.rice.location.api.country.CountryQueryResults;
35 import org.kuali.rice.location.api.country.CountryService;
36 import org.kuali.rice.location.impl.campus.CampusBo;
37
38 import java.util.ArrayList;
39 import java.util.Collection;
40 import java.util.Collections;
41 import java.util.HashMap;
42 import java.util.List;
43 import java.util.Map;
44
45 public final class CountryServiceImpl implements CountryService {
46
47 private BusinessObjectService businessObjectService;
48 private ParameterService parameterService;
49 private CriteriaLookupService criteriaLookupService;
50
51 @Override
52 public Country getCountry(final String code) {
53 if (StringUtils.isBlank(code)) {
54 throw new RiceIllegalArgumentException("code is blank");
55 }
56
57 CountryBo countryBo = businessObjectService.findByPrimaryKey(CountryBo.class, Collections.singletonMap(
58 KRADPropertyConstants.POSTAL_COUNTRY_CODE, code));
59
60 return CountryBo.to(countryBo);
61 }
62
63 @Override
64 public Country getCountryByAlternateCode(final String alternateCode) {
65 if (StringUtils.isBlank(alternateCode)) {
66 throw new RiceIllegalArgumentException("alt code is blank");
67 }
68
69 Collection<CountryBo> countryList = businessObjectService.findMatching(CountryBo.class, Collections.singletonMap(
70 KRADPropertyConstants.ALTERNATE_POSTAL_COUNTRY_CODE, alternateCode));
71 if (countryList == null || countryList.isEmpty()) {
72 return null;
73 } else if (countryList.size() == 1) {
74 return CountryBo.to(countryList.iterator().next());
75 } else throw new RiceIllegalStateException("Multiple countries found with same alternateCode");
76 }
77
78 @Override
79 public List<Country> findAllCountriesNotRestricted() {
80 List<String> criteriaValues = new ArrayList<String>();
81 criteriaValues.add(null);
82 criteriaValues.add("N");
83
84 final Map<String, Object> map = new HashMap<String, Object>();
85 map.put(KRADPropertyConstants.POSTAL_COUNTRY_RESTRICTED_INDICATOR, criteriaValues);
86 map.put("active", Boolean.TRUE);
87
88 Collection<CountryBo> countryBos = businessObjectService.findMatching(CountryBo.class, Collections.unmodifiableMap(map));
89
90 return convertListOfBosToImmutables(countryBos);
91 }
92
93 @Override
94 public List<Country> findAllCountries() {
95 final Map<String, Object> map = new HashMap<String, Object>();
96 map.put("active", Boolean.TRUE);
97
98 Collection<CountryBo> countryBos = businessObjectService.findMatching(CountryBo.class, Collections.unmodifiableMap(map));
99 return convertListOfBosToImmutables(countryBos);
100 }
101
102 @Override
103 public Country getDefaultCountry() {
104 String defaultCountryCode = parameterService.getParameterValueAsString(KRADConstants.KNS_NAMESPACE,
105 KRADConstants.DetailTypes.ALL_DETAIL_TYPE, KRADConstants.SystemGroupParameterNames.DEFAULT_COUNTRY);
106 if (StringUtils.isBlank(defaultCountryCode)) {
107 return null;
108 }
109 return getCountry(defaultCountryCode);
110 }
111
112 @Override
113 public CountryQueryResults findCountries(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
114 incomingParamCheck(queryByCriteria, "queryByCriteria");
115
116 GenericQueryResults<CountryBo> results = criteriaLookupService.lookup(CountryBo.class, queryByCriteria);
117
118 CountryQueryResults.Builder builder = CountryQueryResults.Builder.create();
119 builder.setMoreResultsAvailable(results.isMoreResultsAvailable());
120 builder.setTotalRowCount(results.getTotalRowCount());
121
122 final List<Country.Builder> ims = new ArrayList<Country.Builder>();
123 for (CountryBo bo : results.getResults()) {
124 ims.add(Country.Builder.create(bo));
125 }
126
127 builder.setResults(ims);
128 return builder.build();
129 }
130
131 public ParameterService getParameterService() {
132 return parameterService;
133 }
134
135 public void setParameterService(ParameterService parameterService) {
136 this.parameterService = parameterService;
137 }
138
139
140
141
142
143
144 public void setBusinessObjectService(final BusinessObjectService businessObjectService) {
145 this.businessObjectService = businessObjectService;
146 }
147
148
149
150
151
152
153
154 List<Country> convertListOfBosToImmutables(final Collection<CountryBo> countryBos) {
155 ArrayList<Country> countries = new ArrayList<Country>();
156 for (CountryBo bo : countryBos) {
157 Country country = CountryBo.to(bo);
158 countries.add(country);
159 }
160 return Collections.unmodifiableList(countries);
161 }
162
163 private void incomingParamCheck(Object object, String name) {
164 if (object == null) {
165 throw new RiceIllegalArgumentException(name + " was null");
166 } else if (object instanceof String
167 && StringUtils.isBlank((String) object)) {
168 throw new RiceIllegalArgumentException(name + " was blank");
169 }
170 }
171
172
173
174
175
176
177 public void setCriteriaLookupService(final CriteriaLookupService criteriaLookupService) {
178 this.criteriaLookupService = criteriaLookupService;
179 }
180 }