View Javadoc
1   /**
2    * Copyright 2005-2015 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.impl.service;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.Collections;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.kuali.rice.core.api.criteria.Predicate;
25  import org.kuali.rice.core.api.criteria.PredicateUtils;
26  import org.kuali.rice.core.api.criteria.QueryByCriteria;
27  import org.kuali.rice.krad.bo.ExternalizableBusinessObject;
28  import org.kuali.rice.krad.service.impl.RemoteModuleServiceBase;
29  import org.kuali.rice.location.api.LocationConstants;
30  import org.kuali.rice.location.api.campus.Campus;
31  import org.kuali.rice.location.api.campus.CampusContract;
32  import org.kuali.rice.location.api.campus.CampusService;
33  import org.kuali.rice.location.api.country.Country;
34  import org.kuali.rice.location.api.country.CountryContract;
35  import org.kuali.rice.location.api.country.CountryService;
36  import org.kuali.rice.location.api.county.County;
37  import org.kuali.rice.location.api.county.CountyContract;
38  import org.kuali.rice.location.api.county.CountyService;
39  import org.kuali.rice.location.api.postalcode.PostalCode;
40  import org.kuali.rice.location.api.postalcode.PostalCodeContract;
41  import org.kuali.rice.location.api.postalcode.PostalCodeService;
42  import org.kuali.rice.location.api.services.LocationApiServiceLocator;
43  import org.kuali.rice.location.api.state.State;
44  import org.kuali.rice.location.api.state.StateContract;
45  import org.kuali.rice.location.api.state.StateService;
46  import org.kuali.rice.location.framework.campus.CampusEbo;
47  import org.kuali.rice.location.framework.country.CountryEbo;
48  import org.kuali.rice.location.framework.county.CountyEbo;
49  import org.kuali.rice.location.framework.postalcode.PostalCodeEbo;
50  import org.kuali.rice.location.framework.state.StateEbo;
51  import org.kuali.rice.location.impl.campus.CampusBo;
52  import org.kuali.rice.location.impl.country.CountryBo;
53  import org.kuali.rice.location.impl.county.CountyBo;
54  import org.kuali.rice.location.impl.postalcode.PostalCodeBo;
55  import org.kuali.rice.location.impl.state.StateBo;
56  
57  public class LocationRemoteModuleService extends RemoteModuleServiceBase {
58  
59      private CampusService campusService;
60      private StateService stateService;
61      private CountryService countryService;
62      private CountyService countyService;
63      private PostalCodeService postalCodeService;
64  
65  
66      public <T extends ExternalizableBusinessObject> T getExternalizableBusinessObject(Class<T> businessObjectClass, Map<String, Object> fieldValues) {
67          if(CampusContract.class.isAssignableFrom(businessObjectClass)){
68              if(isNonBlankValueForKey(fieldValues, LocationConstants.PrimaryKeyConstants.CODE)){
69                  Campus campus = getCampusService().getCampus((String) fieldValues.get(
70                          LocationConstants.PrimaryKeyConstants.CODE));
71                  return (T) CampusBo.from(campus);
72              }
73          } else if(StateContract.class.isAssignableFrom(businessObjectClass)){
74              if(isNonBlankValueForKey(fieldValues, LocationConstants.PrimaryKeyConstants.COUNTRY_CODE)
75                      && isNonBlankValueForKey(fieldValues, LocationConstants.PrimaryKeyConstants.CODE)) {
76                  State state = getStateService().getState((String) fieldValues.get(
77                          LocationConstants.PrimaryKeyConstants.COUNTRY_CODE), (String) fieldValues.get(
78                          LocationConstants.PrimaryKeyConstants.CODE));
79                  return (T) StateBo.from(state);
80              }
81          } else if(CountryContract.class.isAssignableFrom(businessObjectClass)){
82              if(isNonBlankValueForKey(fieldValues, LocationConstants.PrimaryKeyConstants.CODE)) {
83                  Country country = getCountryService().getCountry((String) fieldValues.get(
84                          LocationConstants.PrimaryKeyConstants.CODE));
85                  return (T) CountryBo.from(country);
86              }
87          } else if (CountyContract.class.isAssignableFrom(businessObjectClass)) {
88              if (isNonBlankValueForKey(fieldValues, LocationConstants.PrimaryKeyConstants.CODE)
89                      && isNonBlankValueForKey(fieldValues, LocationConstants.PrimaryKeyConstants.COUNTRY_CODE)
90                      && isNonBlankValueForKey(fieldValues, LocationConstants.PrimaryKeyConstants.STATE_CODE)) {
91                  County county = getCountyService().getCounty((String) fieldValues.get(
92                          LocationConstants.PrimaryKeyConstants.COUNTRY_CODE), (String) fieldValues.get(
93                          LocationConstants.PrimaryKeyConstants.STATE_CODE), (String) fieldValues.get(
94                          LocationConstants.PrimaryKeyConstants.CODE));
95                  return (T)CountyBo.from(county);
96              }
97          } else if (PostalCodeContract.class.isAssignableFrom(businessObjectClass)) {
98              if (isNonBlankValueForKey(fieldValues, LocationConstants.PrimaryKeyConstants.CODE)
99                      && isNonBlankValueForKey(fieldValues, LocationConstants.PrimaryKeyConstants.COUNTRY_CODE)) {
100                 PostalCode postalCode = getPostalCodeService().getPostalCode((String) fieldValues.get(
101                         LocationConstants.PrimaryKeyConstants.COUNTRY_CODE), (String) fieldValues.get(
102                         LocationConstants.PrimaryKeyConstants.CODE));
103                 return (T)PostalCodeBo.from(postalCode);
104             }
105         }
106         return null;
107     }
108 
109     @Override
110     public <T extends ExternalizableBusinessObject> List<T> getExternalizableBusinessObjectsList(
111             Class<T> businessObjectClass, Map<String, Object> fieldValues) {
112         //convert fieldValues to Query
113         QueryByCriteria.Builder queryBuilder = QueryByCriteria.Builder.create();
114         Predicate predicate = PredicateUtils.convertObjectMapToPredicate(fieldValues);
115         queryBuilder.setPredicates(predicate);
116 
117         return this.queryForEbos(businessObjectClass, queryBuilder.build());
118     }
119 
120     @Override
121     public boolean isExternalizable(Class boClass) {
122         if(CampusContract.class.isAssignableFrom(boClass)){
123             return true;
124         } else if(StateContract.class.isAssignableFrom(boClass)){
125             return true;
126         } else if(CountryContract.class.isAssignableFrom(boClass)){
127             return true;
128         } else if (CountyContract.class.isAssignableFrom(boClass)) {
129             return true;
130         } else if (PostalCodeContract.class.isAssignableFrom(boClass)) {
131             return true;
132         }
133         return false;
134     }
135 
136     @Override
137     public boolean isExternalizableBusinessObjectLookupable(Class boClass) {
138         return isExternalizable(boClass);
139     }
140 
141     @Override
142     public boolean isExternalizableBusinessObjectInquirable(Class boClass) {
143         return isExternalizable(boClass);
144     }
145 
146     @Override
147     public List<String> listPrimaryKeyFieldNames(Class boClass) {
148 
149         //TODO:  I strongly dislike hard-coding these values, but have this here because the OJB stuff
150         //TODO: isn't available when loaded in REMOTE mode...  Need to find a better way....
151         List<String> primaryKeys = new ArrayList<String>();
152         primaryKeys.add("code");
153         if(StateContract.class.isAssignableFrom(boClass)
154                 || PostalCodeContract.class.isAssignableFrom(boClass)){
155             primaryKeys.add("countryCode");
156         }
157         return primaryKeys;
158     }
159 
160     public <T extends ExternalizableBusinessObject> List<T> queryForEbos(
161             Class<T> businessObjectClass, QueryByCriteria query) {
162 
163         if ( StateContract.class.isAssignableFrom( businessObjectClass ) ) {
164             Collection<State> states = getStateService().findStates(query).getResults();
165             List<StateEbo> stateEbos = new ArrayList<StateEbo>(states.size());
166             for (State state : states) {
167                 stateEbos.add(StateBo.from(state));
168             }
169             return (List<T>)stateEbos;
170         } else if ( CampusContract.class.isAssignableFrom( businessObjectClass ) ) {
171             Collection<Campus> campuses = getCampusService().findCampuses(query).getResults();
172             List<CampusEbo> campusEbos = new ArrayList<CampusEbo>(campuses.size());
173             for (Campus campus : campuses) {
174                 campusEbos.add(CampusBo.from(campus));
175             }
176             return (List<T>)campusEbos;
177         } else if ( CountryContract.class.isAssignableFrom( businessObjectClass ) ) {
178             Collection<Country> countries = getCountryService().findCountries(query).getResults();
179             List<CountryEbo> countryEbos = new ArrayList<CountryEbo>(countries.size());
180             for (Country country : countries) {
181                 countryEbos.add(CountryBo.from(country));
182             }
183             return (List<T>)countryEbos;
184         } else if ( CountyContract.class.isAssignableFrom( businessObjectClass ) ) {
185             Collection<County> counties = getCountyService().findCounties(query).getResults();
186             List<CountyEbo> countyEbos = new ArrayList<CountyEbo>(counties.size());
187             for (County county : counties) {
188                 countyEbos.add(CountyBo.from(county));
189             }
190             return (List<T>)countyEbos;
191         } else if ( PostalCodeContract.class.isAssignableFrom( businessObjectClass ) ) {
192             Collection<PostalCode> postalCodes = getPostalCodeService().findPostalCodes(query).getResults();
193             List<PostalCodeEbo> postalCodeEbos = new ArrayList<PostalCodeEbo>(postalCodes.size());
194             for (PostalCode postalCode : postalCodes) {
195                 postalCodeEbos.add(PostalCodeBo.from(postalCode));
196             }
197             return (List<T>)postalCodeEbos;
198         }
199         // Not responsible for other types -- so return empty list?
200         return Collections.emptyList();
201 
202     }
203     protected CampusService getCampusService() {
204         if (campusService == null) {
205             campusService = LocationApiServiceLocator.getCampusService();
206         }
207         return campusService;
208     }
209 
210     protected StateService getStateService() {
211         if (stateService == null) {
212             stateService = LocationApiServiceLocator.getStateService();
213         }
214         return stateService;
215     }
216 
217     protected CountryService getCountryService() {
218         if (countryService == null) {
219             countryService = LocationApiServiceLocator.getCountryService();
220         }
221         return countryService;
222     }
223 
224     protected CountyService getCountyService() {
225         if (countyService == null) {
226             countyService = LocationApiServiceLocator.getCountyService();
227         }
228         return countyService;
229     }
230 
231     protected PostalCodeService getPostalCodeService() {
232         if (postalCodeService == null) {
233             postalCodeService = LocationApiServiceLocator.getPostalCodeService();
234         }
235         return postalCodeService;
236     }
237 }