001    /**
002     * Copyright 2005-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.location.impl.service;
017    
018    import java.util.ArrayList;
019    import java.util.Collection;
020    import java.util.Collections;
021    import java.util.List;
022    import java.util.Map;
023    
024    import org.kuali.rice.core.api.criteria.Predicate;
025    import org.kuali.rice.core.api.criteria.PredicateUtils;
026    import org.kuali.rice.core.api.criteria.QueryByCriteria;
027    import org.kuali.rice.krad.bo.ExternalizableBusinessObject;
028    import org.kuali.rice.krad.service.impl.RemoteModuleServiceBase;
029    import org.kuali.rice.location.api.LocationConstants;
030    import org.kuali.rice.location.api.campus.Campus;
031    import org.kuali.rice.location.api.campus.CampusContract;
032    import org.kuali.rice.location.api.campus.CampusService;
033    import org.kuali.rice.location.api.country.Country;
034    import org.kuali.rice.location.api.country.CountryContract;
035    import org.kuali.rice.location.api.country.CountryService;
036    import org.kuali.rice.location.api.county.County;
037    import org.kuali.rice.location.api.county.CountyContract;
038    import org.kuali.rice.location.api.county.CountyService;
039    import org.kuali.rice.location.api.postalcode.PostalCode;
040    import org.kuali.rice.location.api.postalcode.PostalCodeContract;
041    import org.kuali.rice.location.api.postalcode.PostalCodeService;
042    import org.kuali.rice.location.api.services.LocationApiServiceLocator;
043    import org.kuali.rice.location.api.state.State;
044    import org.kuali.rice.location.api.state.StateContract;
045    import org.kuali.rice.location.api.state.StateService;
046    import org.kuali.rice.location.framework.campus.CampusEbo;
047    import org.kuali.rice.location.framework.country.CountryEbo;
048    import org.kuali.rice.location.framework.county.CountyEbo;
049    import org.kuali.rice.location.framework.postalcode.PostalCodeEbo;
050    import org.kuali.rice.location.framework.state.StateEbo;
051    import org.kuali.rice.location.impl.campus.CampusBo;
052    import org.kuali.rice.location.impl.country.CountryBo;
053    import org.kuali.rice.location.impl.county.CountyBo;
054    import org.kuali.rice.location.impl.postalcode.PostalCodeBo;
055    import org.kuali.rice.location.impl.state.StateBo;
056    
057    public class LocationRemoteModuleService extends RemoteModuleServiceBase {
058    
059        private CampusService campusService;
060        private StateService stateService;
061        private CountryService countryService;
062        private CountyService countyService;
063        private PostalCodeService postalCodeService;
064    
065    
066        public <T extends ExternalizableBusinessObject> T getExternalizableBusinessObject(Class<T> businessObjectClass, Map<String, Object> fieldValues) {
067            if(CampusContract.class.isAssignableFrom(businessObjectClass)){
068                if(fieldValues.containsKey(LocationConstants.PrimaryKeyConstants.CODE)){
069                    Campus campus = getCampusService().getCampus((String) fieldValues.get(
070                            LocationConstants.PrimaryKeyConstants.CODE));
071                    return (T) CampusBo.from(campus);
072                }
073            } else if(StateContract.class.isAssignableFrom(businessObjectClass)){
074                if(fieldValues.containsKey(LocationConstants.PrimaryKeyConstants.COUNTRY_CODE)
075                        && fieldValues.containsKey(LocationConstants.PrimaryKeyConstants.CODE)) {
076                    State state = getStateService().getState((String) fieldValues.get(
077                            LocationConstants.PrimaryKeyConstants.COUNTRY_CODE), (String) fieldValues.get(
078                            LocationConstants.PrimaryKeyConstants.CODE));
079                    return (T) StateBo.from(state);
080                }
081            } else if(CountryContract.class.isAssignableFrom(businessObjectClass)){
082                if(fieldValues.containsKey(LocationConstants.PrimaryKeyConstants.CODE)) {
083                    Country country = getCountryService().getCountry((String) fieldValues.get(
084                            LocationConstants.PrimaryKeyConstants.CODE));
085                    return (T) CountryBo.from(country);
086                }
087            } else if (CountyContract.class.isAssignableFrom(businessObjectClass)) {
088                if (fieldValues.containsKey(LocationConstants.PrimaryKeyConstants.CODE)
089                        && fieldValues.containsKey(LocationConstants.PrimaryKeyConstants.COUNTRY_CODE)
090                        && fieldValues.containsKey(LocationConstants.PrimaryKeyConstants.STATE_CODE)) {
091                    County county = getCountyService().getCounty((String) fieldValues.get(
092                            LocationConstants.PrimaryKeyConstants.COUNTRY_CODE), (String) fieldValues.get(
093                            LocationConstants.PrimaryKeyConstants.STATE_CODE), (String) fieldValues.get(
094                            LocationConstants.PrimaryKeyConstants.CODE));
095                    return (T)CountyBo.from(county);
096                }
097            } else if (PostalCodeContract.class.isAssignableFrom(businessObjectClass)) {
098                if (fieldValues.containsKey(LocationConstants.PrimaryKeyConstants.CODE)
099                        && fieldValues.containsKey(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            // otherwise, use the default implementation
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    }