1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.location.service.impl;
17
18 import org.kuali.rice.core.api.criteria.Predicate;
19 import org.kuali.rice.core.api.criteria.QueryByCriteria;
20 import org.kuali.rice.krad.bo.ExternalizableBusinessObject;
21 import org.kuali.rice.krad.service.KRADServiceLocator;
22 import org.kuali.rice.krad.service.impl.ModuleServiceBase;
23 import org.kuali.rice.krad.util.KRADConstants;
24 import org.kuali.rice.krad.util.UrlFactory;
25 import org.kuali.rice.location.api.LocationConstants;
26 import org.kuali.rice.location.api.campus.Campus;
27 import org.kuali.rice.location.api.campus.CampusService;
28 import org.kuali.rice.location.api.country.Country;
29 import org.kuali.rice.location.api.country.CountryService;
30 import org.kuali.rice.location.api.county.County;
31 import org.kuali.rice.location.api.county.CountyService;
32 import org.kuali.rice.location.api.postalcode.PostalCode;
33 import org.kuali.rice.location.api.postalcode.PostalCodeService;
34 import org.kuali.rice.location.api.services.LocationApiServiceLocator;
35 import org.kuali.rice.location.api.state.State;
36 import org.kuali.rice.location.api.state.StateService;
37 import org.kuali.rice.location.framework.campus.CampusEbo;
38 import org.kuali.rice.location.framework.country.CountryEbo;
39 import org.kuali.rice.location.framework.county.CountyEbo;
40 import org.kuali.rice.location.framework.postalcode.PostalCodeEbo;
41 import org.kuali.rice.location.framework.state.StateEbo;
42 import org.kuali.rice.location.impl.campus.CampusBo;
43 import org.kuali.rice.location.impl.country.CountryBo;
44 import org.kuali.rice.location.impl.county.CountyBo;
45 import org.kuali.rice.location.impl.postalcode.PostalCodeBo;
46 import org.kuali.rice.location.impl.state.StateBo;
47
48 import java.util.ArrayList;
49 import java.util.Collection;
50 import java.util.HashMap;
51 import java.util.HashSet;
52 import java.util.List;
53 import java.util.Map;
54 import java.util.Properties;
55 import java.util.Set;
56
57 import static org.kuali.rice.core.api.criteria.PredicateFactory.equal;
58
59 public class LocationModuleService extends ModuleServiceBase {
60
61 private CampusService campusService;
62 private StateService stateService;
63 private CountryService countryService;
64 private CountyService countyService;
65 private PostalCodeService postalCodeService;
66
67
68 public <T extends ExternalizableBusinessObject> T getExternalizableBusinessObject(Class<T> businessObjectClass, Map<String, Object> fieldValues) {
69 if(CampusEbo.class.isAssignableFrom(businessObjectClass)){
70 if(fieldValues.containsKey(LocationConstants.PrimaryKeyConstants.CODE)){
71 Campus campus = getCampusService().getCampus((String) fieldValues.get(
72 LocationConstants.PrimaryKeyConstants.CODE));
73 return (T) CampusEbo.from(campus);
74 }
75 } else if(StateEbo.class.isAssignableFrom(businessObjectClass)){
76 if(fieldValues.containsKey(LocationConstants.PrimaryKeyConstants.COUNTRY_CODE)
77 && fieldValues.containsKey(LocationConstants.PrimaryKeyConstants.STATE_CODE)) {
78 State state = getStateService().getState((String) fieldValues.get(
79 LocationConstants.PrimaryKeyConstants.COUNTRY_CODE), (String) fieldValues.get(
80 LocationConstants.PrimaryKeyConstants.STATE_CODE));
81 return (T) StateEbo.from(state);
82 }
83 } else if(CountryEbo.class.isAssignableFrom(businessObjectClass)){
84 if(fieldValues.containsKey(LocationConstants.PrimaryKeyConstants.CODE)) {
85 Country country = getCountryService().getCountry((String) fieldValues.get(
86 LocationConstants.PrimaryKeyConstants.CODE));
87 return (T) CountryEbo.from(country);
88 }
89 } else if (CountyEbo.class.isAssignableFrom(businessObjectClass)) {
90 if (fieldValues.containsKey(LocationConstants.PrimaryKeyConstants.CODE)
91 && fieldValues.containsKey(LocationConstants.PrimaryKeyConstants.COUNTRY_CODE)
92 && fieldValues.containsKey(LocationConstants.PrimaryKeyConstants.STATE_CODE)) {
93 County county = getCountyService().getCounty((String) fieldValues.get(
94 LocationConstants.PrimaryKeyConstants.COUNTRY_CODE), (String) fieldValues.get(
95 LocationConstants.PrimaryKeyConstants.STATE_CODE), (String) fieldValues.get(
96 LocationConstants.PrimaryKeyConstants.CODE));
97 return (T)CountyEbo.from(county);
98 }
99 } else if (PostalCodeEbo.class.isAssignableFrom(businessObjectClass)) {
100 if (fieldValues.containsKey(LocationConstants.PrimaryKeyConstants.CODE)
101 && fieldValues.containsKey(LocationConstants.PrimaryKeyConstants.COUNTRY_CODE)) {
102 PostalCode postalCode = getPostalCodeService().getPostalCode((String) fieldValues.get(
103 LocationConstants.PrimaryKeyConstants.COUNTRY_CODE), (String) fieldValues.get(
104 LocationConstants.PrimaryKeyConstants.CODE));
105 return (T)PostalCodeEbo.from(postalCode);
106 }
107 }
108
109 return super.getExternalizableBusinessObject( businessObjectClass, fieldValues );
110 }
111
112
113
114
115
116
117 @SuppressWarnings("unchecked")
118 @Override
119 public <T extends ExternalizableBusinessObject> List<T> getExternalizableBusinessObjectsList(
120 Class<T> externalizableBusinessObjectClass, Map<String, Object> fieldValues) {
121
122 if ( StateEbo.class.isAssignableFrom( externalizableBusinessObjectClass ) ) {
123 Collection<StateBo> states = getBusinessObjectService().findMatching(StateBo.class, fieldValues);
124 List<StateEbo> stateEbos = new ArrayList<StateEbo>(states.size());
125 for (StateBo state : states) {
126 stateEbos.add(StateEbo.from(State.Builder.create(state).build()));
127 }
128 return (List<T>)stateEbos;
129 } else if ( CampusEbo.class.isAssignableFrom( externalizableBusinessObjectClass ) ) {
130 Collection<CampusBo> campuses = getBusinessObjectService().findMatching(CampusBo.class, fieldValues);
131 List<CampusEbo> campusEbos = new ArrayList<CampusEbo>(campuses.size());
132 for (CampusBo campus : campuses) {
133 campusEbos.add(CampusEbo.from(Campus.Builder.create(campus).build()));
134 }
135 return (List<T>)campusEbos;
136 } else if ( CountryEbo.class.isAssignableFrom( externalizableBusinessObjectClass ) ) {
137 Collection<CountryBo> countries = getBusinessObjectService().findMatching(CountryBo.class, fieldValues);
138 List<CountryEbo> countryEbos = new ArrayList<CountryEbo>(countries.size());
139 for (CountryBo country : countries) {
140 countryEbos.add(CountryEbo.from(Country.Builder.create(country).build()));
141 }
142 return (List<T>)countryEbos;
143 } else if ( CountyEbo.class.isAssignableFrom( externalizableBusinessObjectClass ) ) {
144 Collection<CountyBo> counties = getBusinessObjectService().findMatching(CountyBo.class, fieldValues);
145 List<CountyEbo> countyEbos = new ArrayList<CountyEbo>(counties.size());
146 for (CountyBo county : counties) {
147 countyEbos.add(CountyEbo.from(County.Builder.create(county).build()));
148 }
149 return (List<T>)countyEbos;
150 } else if ( PostalCodeEbo.class.isAssignableFrom( externalizableBusinessObjectClass ) ) {
151 Collection<PostalCodeBo> postalCodes = getBusinessObjectService().findMatching(PostalCodeBo.class, fieldValues);
152 List<PostalCodeEbo> postalCodeEbos = new ArrayList<PostalCodeEbo>(postalCodes.size());
153 for (PostalCodeBo postalCode : postalCodes) {
154 postalCodeEbos.add(PostalCodeEbo.from(PostalCode.Builder.create(postalCode).build()));
155 }
156 return (List<T>)postalCodeEbos;
157 }
158
159 return super.getExternalizableBusinessObjectsList( externalizableBusinessObjectClass, fieldValues );
160 }
161
162
163
164
165 @SuppressWarnings("unchecked")
166 @Override
167 public <T extends ExternalizableBusinessObject> List<T> getExternalizableBusinessObjectsListForLookup(
168 Class<T> externalizableBusinessObjectClass, Map<String, Object> fieldValues, boolean unbounded) {
169
170 Map<String, String> searchCriteria = new HashMap<String, String>();
171 for (Map.Entry<String, Object> fieldValue : fieldValues.entrySet()) {
172 if (fieldValue.getValue() != null) {
173 searchCriteria.put(fieldValue.getKey(), fieldValue.getValue().toString());
174 }
175 else {
176 searchCriteria.put(fieldValue.getKey(), null);
177 }
178 }
179
180 if ( StateEbo.class.isAssignableFrom( externalizableBusinessObjectClass ) ) {
181 Collection<StateBo> states = getLookupService().findCollectionBySearchHelper(StateBo.class, searchCriteria,
182 unbounded);
183 List<StateEbo> stateEbos = new ArrayList<StateEbo>(states.size());
184 for (StateBo state : states) {
185 stateEbos.add(StateEbo.from(State.Builder.create(state).build()));
186 }
187 return (List<T>)stateEbos;
188 } else if ( CampusEbo.class.isAssignableFrom( externalizableBusinessObjectClass ) ) {
189 Collection<CampusBo> campuses = getLookupService().findCollectionBySearchHelper(CampusBo.class,
190 searchCriteria, unbounded);
191 List<CampusEbo> campusEbos = new ArrayList<CampusEbo>(campuses.size());
192 for (CampusBo campus : campuses) {
193 campusEbos.add(CampusEbo.from(Campus.Builder.create(campus).build()));
194 }
195 return (List<T>)campusEbos;
196 } else if ( CountryEbo.class.isAssignableFrom( externalizableBusinessObjectClass ) ) {
197 Collection<CountryBo> countries = getLookupService().findCollectionBySearchHelper(CountryBo.class,
198 searchCriteria, unbounded);
199 List<CountryEbo> countryEbos = new ArrayList<CountryEbo>(countries.size());
200 for (CountryBo country : countries) {
201 countryEbos.add(CountryEbo.from(Country.Builder.create(country).build()));
202 }
203 return (List<T>)countryEbos;
204 } else if ( CountyEbo.class.isAssignableFrom( externalizableBusinessObjectClass ) ) {
205 Collection<CountyBo> counties = getLookupService().findCollectionBySearchHelper(CountyBo.class,
206 searchCriteria, unbounded);
207 List<CountyEbo> countyEbos = new ArrayList<CountyEbo>(counties.size());
208 for (CountyBo county : counties) {
209 countyEbos.add(CountyEbo.from(County.Builder.create(county).build()));
210 }
211 return (List<T>)countyEbos;
212 } else if ( PostalCodeEbo.class.isAssignableFrom( externalizableBusinessObjectClass ) ) {
213 Collection<PostalCodeBo> postalCodes = getLookupService().findCollectionBySearchHelper(PostalCodeBo.class,
214 searchCriteria, unbounded);
215 List<PostalCodeEbo> postalCodeEbos = new ArrayList<PostalCodeEbo>(postalCodes.size());
216 for (PostalCodeBo postalCode : postalCodes) {
217 postalCodeEbos.add(PostalCodeEbo.from(PostalCode.Builder.create(postalCode).build()));
218 }
219 return (List<T>)postalCodeEbos;
220 }
221
222 return super.getExternalizableBusinessObjectsListForLookup(externalizableBusinessObjectClass, fieldValues, unbounded);
223 }
224
225 @Override
226 protected String getInquiryUrl(Class inquiryBusinessObjectClass) {
227 String riceBaseUrl = KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString(
228 KRADConstants.KUALI_RICE_URL_KEY);
229 String inquiryUrl = riceBaseUrl;
230 if (!inquiryUrl.endsWith("/")) {
231 inquiryUrl = inquiryUrl + "/";
232 }
233 return inquiryUrl + KRADConstants.INQUIRY_ACTION;
234 }
235
236
237
238
239
240
241
242 @Deprecated
243 @Override
244 public String getExternalizableBusinessObjectLookupUrl(Class inquiryBusinessObjectClass,
245 Map<String, String> parameters) {
246 Properties urlParameters = new Properties();
247
248 String riceBaseUrl = KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString(
249 KRADConstants.KUALI_RICE_URL_KEY);
250 String lookupUrl = riceBaseUrl;
251 if (!lookupUrl.endsWith("/")) {
252 lookupUrl = lookupUrl + "/";
253 }
254 if (parameters.containsKey(KRADConstants.MULTIPLE_VALUE)) {
255 lookupUrl = lookupUrl + KRADConstants.MULTIPLE_VALUE_LOOKUP_ACTION;
256 } else {
257 lookupUrl = lookupUrl + KRADConstants.LOOKUP_ACTION;
258 }
259 for (String paramName : parameters.keySet()) {
260 urlParameters.put(paramName, parameters.get(paramName));
261 }
262
263 Class clazz = getExternalizableBusinessObjectImplementation(inquiryBusinessObjectClass);
264
265 urlParameters.put(KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, clazz == null ? "" : clazz.getName());
266
267 return UrlFactory.parameterizeUrl(lookupUrl, urlParameters);
268 }
269
270
271 protected CampusService getCampusService() {
272 if (campusService == null) {
273 campusService = LocationApiServiceLocator.getCampusService();
274 }
275 return campusService;
276 }
277
278 protected StateService getStateService() {
279 if (stateService == null) {
280 stateService = LocationApiServiceLocator.getStateService();
281 }
282 return stateService;
283 }
284
285 protected CountryService getCountryService() {
286 if (countryService == null) {
287 countryService = LocationApiServiceLocator.getCountryService();
288 }
289 return countryService;
290 }
291
292 protected CountyService getCountyService() {
293 if (countyService == null) {
294 countyService = LocationApiServiceLocator.getCountyService();
295 }
296 return countyService;
297 }
298
299 protected PostalCodeService getPostalCodeService() {
300 if (postalCodeService == null) {
301 postalCodeService = LocationApiServiceLocator.getPostalCodeService();
302 }
303 return postalCodeService;
304 }
305
306 private QueryByCriteria toQuery(Map<String,?> fieldValues) {
307 Set<Predicate> preds = new HashSet<Predicate>();
308 for (Map.Entry<String, ?> entry : fieldValues.entrySet()) {
309 preds.add(equal(entry.getKey(), entry.getValue()));
310 }
311 Predicate[] predicates = new Predicate[0];
312 predicates = preds.toArray(predicates);
313 return QueryByCriteria.Builder.fromPredicates(predicates);
314 }
315 }