View Javadoc

1   /**
2    * Copyright 2005-2012 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.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.impl.ModuleServiceBase;
22  import org.kuali.rice.location.api.LocationConstants;
23  import org.kuali.rice.location.api.campus.Campus;
24  import org.kuali.rice.location.api.campus.CampusService;
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.county.County;
28  import org.kuali.rice.location.api.county.CountyService;
29  import org.kuali.rice.location.api.postalcode.PostalCode;
30  import org.kuali.rice.location.api.postalcode.PostalCodeService;
31  import org.kuali.rice.location.api.services.LocationApiServiceLocator;
32  import org.kuali.rice.location.api.state.State;
33  import org.kuali.rice.location.api.state.StateService;
34  import org.kuali.rice.location.framework.campus.CampusEbo;
35  import org.kuali.rice.location.framework.country.CountryEbo;
36  import org.kuali.rice.location.framework.county.CountyEbo;
37  import org.kuali.rice.location.framework.postalcode.PostalCodeEbo;
38  import org.kuali.rice.location.framework.state.StateEbo;
39  import org.kuali.rice.location.impl.campus.CampusBo;
40  import org.kuali.rice.location.impl.country.CountryBo;
41  import org.kuali.rice.location.impl.county.CountyBo;
42  import org.kuali.rice.location.impl.postalcode.PostalCodeBo;
43  import org.kuali.rice.location.impl.state.StateBo;
44  
45  import java.util.ArrayList;
46  import java.util.Collection;
47  import java.util.HashMap;
48  import java.util.HashSet;
49  import java.util.List;
50  import java.util.Map;
51  import java.util.Set;
52  
53  import static org.kuali.rice.core.api.criteria.PredicateFactory.equal;
54  
55  public class LocationModuleService extends ModuleServiceBase {
56  
57      private CampusService campusService;
58      private StateService stateService;
59      private CountryService countryService;
60      private CountyService countyService;
61      private PostalCodeService postalCodeService;
62  
63  
64      public <T extends ExternalizableBusinessObject> T getExternalizableBusinessObject(Class<T> businessObjectClass, Map<String, Object> fieldValues) {
65          if(CampusEbo.class.isAssignableFrom(businessObjectClass)){
66              if(fieldValues.containsKey(LocationConstants.PrimaryKeyConstants.CODE)){
67                  Campus campus = getCampusService().getCampus((String) fieldValues.get(
68                          LocationConstants.PrimaryKeyConstants.CODE));
69                  return (T) CampusEbo.from(campus);
70              }
71          } else if(StateEbo.class.isAssignableFrom(businessObjectClass)){
72              if(fieldValues.containsKey(LocationConstants.PrimaryKeyConstants.COUNTRY_CODE)
73                      && fieldValues.containsKey(LocationConstants.PrimaryKeyConstants.STATE_CODE)) {
74                  State state = getStateService().getState((String) fieldValues.get(
75                          LocationConstants.PrimaryKeyConstants.COUNTRY_CODE), (String) fieldValues.get(
76                          LocationConstants.PrimaryKeyConstants.STATE_CODE));
77                  return (T) StateEbo.from(state);
78              }
79          } else if(CountryEbo.class.isAssignableFrom(businessObjectClass)){
80              if(fieldValues.containsKey(LocationConstants.PrimaryKeyConstants.CODE)) {
81                  Country country = getCountryService().getCountry((String) fieldValues.get(
82                          LocationConstants.PrimaryKeyConstants.CODE));
83                  return (T) CountryEbo.from(country);
84              }
85          } else if (CountyEbo.class.isAssignableFrom(businessObjectClass)) {
86              if (fieldValues.containsKey(LocationConstants.PrimaryKeyConstants.CODE)
87                      && fieldValues.containsKey(LocationConstants.PrimaryKeyConstants.COUNTRY_CODE)
88                      && fieldValues.containsKey(LocationConstants.PrimaryKeyConstants.STATE_CODE)) {
89                  County county = getCountyService().getCounty((String) fieldValues.get(
90                          LocationConstants.PrimaryKeyConstants.COUNTRY_CODE), (String) fieldValues.get(
91                          LocationConstants.PrimaryKeyConstants.STATE_CODE), (String) fieldValues.get(
92                          LocationConstants.PrimaryKeyConstants.CODE));
93                  return (T)CountyEbo.from(county);
94              }
95          } else if (PostalCodeEbo.class.isAssignableFrom(businessObjectClass)) {
96              if (fieldValues.containsKey(LocationConstants.PrimaryKeyConstants.CODE)
97                      && fieldValues.containsKey(LocationConstants.PrimaryKeyConstants.COUNTRY_CODE)) {
98                  PostalCode postalCode = getPostalCodeService().getPostalCode((String) fieldValues.get(
99                          LocationConstants.PrimaryKeyConstants.COUNTRY_CODE), (String) fieldValues.get(
100                         LocationConstants.PrimaryKeyConstants.CODE));
101                 return (T)PostalCodeEbo.from(postalCode);
102             }
103         }
104         // otherwise, use the default implementation
105         return super.getExternalizableBusinessObject( businessObjectClass, fieldValues );
106     }
107 
108     /**
109 	 * This overridden method ...
110 	 *
111 	 * @see org.kuali.rice.krad.service.impl.ModuleServiceBase#getExternalizableBusinessObjectsList(java.lang.Class, java.util.Map)
112 	 */
113 	@SuppressWarnings("unchecked")
114 	@Override
115 	public <T extends ExternalizableBusinessObject> List<T> getExternalizableBusinessObjectsList(
116 			Class<T> externalizableBusinessObjectClass, Map<String, Object> fieldValues) {
117 
118 		if ( StateEbo.class.isAssignableFrom( externalizableBusinessObjectClass ) ) {
119             Collection<StateBo> states = getBusinessObjectService().findMatching(StateBo.class, fieldValues);
120             List<StateEbo> stateEbos = new ArrayList<StateEbo>(states.size());
121             for (StateBo state : states) {
122                 stateEbos.add(StateEbo.from(State.Builder.create(state).build()));
123             }
124             return (List<T>)stateEbos;
125 		} else if ( CampusEbo.class.isAssignableFrom( externalizableBusinessObjectClass ) ) {
126             Collection<CampusBo> campuses = getBusinessObjectService().findMatching(CampusBo.class, fieldValues);
127             List<CampusEbo> campusEbos = new ArrayList<CampusEbo>(campuses.size());
128             for (CampusBo campus : campuses) {
129                 campusEbos.add(CampusEbo.from(Campus.Builder.create(campus).build()));
130             }
131             return (List<T>)campusEbos;
132 		} else if ( CountryEbo.class.isAssignableFrom( externalizableBusinessObjectClass ) ) {
133             Collection<CountryBo> countries = getBusinessObjectService().findMatching(CountryBo.class, fieldValues);
134             List<CountryEbo> countryEbos = new ArrayList<CountryEbo>(countries.size());
135             for (CountryBo country : countries) {
136                 countryEbos.add(CountryEbo.from(Country.Builder.create(country).build()));
137             }
138             return (List<T>)countryEbos;
139 		} else if ( CountyEbo.class.isAssignableFrom( externalizableBusinessObjectClass ) ) {
140             Collection<CountyBo> counties = getBusinessObjectService().findMatching(CountyBo.class, fieldValues);
141             List<CountyEbo> countyEbos = new ArrayList<CountyEbo>(counties.size());
142             for (CountyBo county : counties) {
143                 countyEbos.add(CountyEbo.from(County.Builder.create(county).build()));
144             }
145             return (List<T>)countyEbos;
146 		} else if ( PostalCodeEbo.class.isAssignableFrom( externalizableBusinessObjectClass ) ) {
147             Collection<PostalCodeBo> postalCodes = getBusinessObjectService().findMatching(PostalCodeBo.class, fieldValues);
148             List<PostalCodeEbo> postalCodeEbos = new ArrayList<PostalCodeEbo>(postalCodes.size());
149             for (PostalCodeBo postalCode : postalCodes) {
150                 postalCodeEbos.add(PostalCodeEbo.from(PostalCode.Builder.create(postalCode).build()));
151             }
152             return (List<T>)postalCodeEbos;
153 		}
154 		// otherwise, use the default implementation
155 		return super.getExternalizableBusinessObjectsList( externalizableBusinessObjectClass, fieldValues );
156 	}
157 
158 	/***
159 	 * @see org.kuali.rice.krad.service.ModuleService#getExternalizableBusinessObjectsListForLookup(java.lang.Class, java.util.Map, boolean)
160 	 */
161 	@SuppressWarnings("unchecked")
162 	@Override
163 	public <T extends ExternalizableBusinessObject> List<T> getExternalizableBusinessObjectsListForLookup(
164 			Class<T> externalizableBusinessObjectClass, Map<String, Object> fieldValues, boolean unbounded) {
165 
166         Map<String, String> searchCriteria = new HashMap<String, String>();
167 			for (Map.Entry<String, Object> fieldValue : fieldValues.entrySet()) {
168 				if (fieldValue.getValue() != null) {
169 					searchCriteria.put(fieldValue.getKey(), fieldValue.getValue().toString());
170 				}
171 				else {
172 					searchCriteria.put(fieldValue.getKey(), null);
173 				}
174 			}
175 		// for Person objects (which are not real PersistableBOs) pull them through the person service
176 		if ( StateEbo.class.isAssignableFrom( externalizableBusinessObjectClass ) ) {
177             Collection<StateBo> states = getLookupService().findCollectionBySearchHelper(StateBo.class, searchCriteria,
178                     unbounded);
179             List<StateEbo> stateEbos = new ArrayList<StateEbo>(states.size());
180             for (StateBo state : states) {
181                 stateEbos.add(StateEbo.from(State.Builder.create(state).build()));
182             }
183             return (List<T>)stateEbos;
184 		} else if ( CampusEbo.class.isAssignableFrom( externalizableBusinessObjectClass ) ) {
185             Collection<CampusBo> campuses = getLookupService().findCollectionBySearchHelper(CampusBo.class,
186                     searchCriteria, unbounded);
187             List<CampusEbo> campusEbos = new ArrayList<CampusEbo>(campuses.size());
188             for (CampusBo campus : campuses) {
189                 campusEbos.add(CampusEbo.from(Campus.Builder.create(campus).build()));
190             }
191             return (List<T>)campusEbos;
192 		} else if ( CountryEbo.class.isAssignableFrom( externalizableBusinessObjectClass ) ) {
193             Collection<CountryBo> countries = getLookupService().findCollectionBySearchHelper(CountryBo.class,
194                     searchCriteria, unbounded);
195             List<CountryEbo> countryEbos = new ArrayList<CountryEbo>(countries.size());
196             for (CountryBo country : countries) {
197                 countryEbos.add(CountryEbo.from(Country.Builder.create(country).build()));
198             }
199             return (List<T>)countryEbos;
200 		} else if ( CountyEbo.class.isAssignableFrom( externalizableBusinessObjectClass ) ) {
201             Collection<CountyBo> counties = getLookupService().findCollectionBySearchHelper(CountyBo.class,
202                     searchCriteria, unbounded);
203             List<CountyEbo> countyEbos = new ArrayList<CountyEbo>(counties.size());
204             for (CountyBo county : counties) {
205                 countyEbos.add(CountyEbo.from(County.Builder.create(county).build()));
206             }
207             return (List<T>)countyEbos;
208 		} else if ( PostalCodeEbo.class.isAssignableFrom( externalizableBusinessObjectClass ) ) {
209             Collection<PostalCodeBo> postalCodes = getLookupService().findCollectionBySearchHelper(PostalCodeBo.class,
210                     searchCriteria, unbounded);
211             List<PostalCodeEbo> postalCodeEbos = new ArrayList<PostalCodeEbo>(postalCodes.size());
212             for (PostalCodeBo postalCode : postalCodes) {
213                 postalCodeEbos.add(PostalCodeEbo.from(PostalCode.Builder.create(postalCode).build()));
214             }
215             return (List<T>)postalCodeEbos;
216 		}
217 		// otherwise, use the default implementation
218 		return super.getExternalizableBusinessObjectsListForLookup(externalizableBusinessObjectClass, fieldValues, unbounded);
219 	}
220 
221 
222     protected CampusService getCampusService() {
223         if (campusService == null) {
224             campusService = LocationApiServiceLocator.getCampusService();
225         }
226         return campusService;
227     }
228 
229     protected StateService getStateService() {
230         if (stateService == null) {
231             stateService = LocationApiServiceLocator.getStateService();
232         }
233         return stateService;
234     }
235 
236     protected CountryService getCountryService() {
237         if (countryService == null) {
238             countryService = LocationApiServiceLocator.getCountryService();
239         }
240         return countryService;
241     }
242 
243     protected CountyService getCountyService() {
244         if (countyService == null) {
245             countyService = LocationApiServiceLocator.getCountyService();
246         }
247         return countyService;
248     }
249 
250     protected PostalCodeService getPostalCodeService() {
251         if (postalCodeService == null) {
252             postalCodeService = LocationApiServiceLocator.getPostalCodeService();
253         }
254         return postalCodeService;
255     }
256 
257     private QueryByCriteria toQuery(Map<String,?> fieldValues) {
258         Set<Predicate> preds = new HashSet<Predicate>();
259         for (Map.Entry<String, ?> entry : fieldValues.entrySet()) {
260             preds.add(equal(entry.getKey(), entry.getValue()));
261         }
262         Predicate[] predicates = new Predicate[0];
263         predicates = preds.toArray(predicates);
264         return QueryByCriteria.Builder.fromPredicates(predicates);
265     }
266 }