View Javadoc

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