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.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 | 0 | 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 | 0 | if(CampusEbo.class.isAssignableFrom(businessObjectClass)){ |
72 | 0 | if(fieldValues.containsKey(LocationConstants.PrimaryKeyConstants.CODE)){ |
73 | 0 | Campus campus = getCampusService().getCampus((String) fieldValues.get( |
74 | |
LocationConstants.PrimaryKeyConstants.CODE)); |
75 | 0 | return (T) CampusEbo.from(campus); |
76 | |
} |
77 | 0 | } else if(StateEbo.class.isAssignableFrom(businessObjectClass)){ |
78 | 0 | if(fieldValues.containsKey(LocationConstants.PrimaryKeyConstants.COUNTRY_CODE) |
79 | |
&& fieldValues.containsKey(LocationConstants.PrimaryKeyConstants.STATE_CODE)) { |
80 | 0 | State state = getStateService().getState((String) fieldValues.get( |
81 | |
LocationConstants.PrimaryKeyConstants.COUNTRY_CODE), (String) fieldValues.get( |
82 | |
LocationConstants.PrimaryKeyConstants.STATE_CODE)); |
83 | 0 | return (T) StateEbo.from(state); |
84 | |
} |
85 | 0 | } else if(CountryEbo.class.isAssignableFrom(businessObjectClass)){ |
86 | 0 | if(fieldValues.containsKey(LocationConstants.PrimaryKeyConstants.CODE)) { |
87 | 0 | Country country = getCountryService().getCountry((String) fieldValues.get( |
88 | |
LocationConstants.PrimaryKeyConstants.CODE)); |
89 | 0 | return (T) CountryEbo.from(country); |
90 | |
} |
91 | 0 | } else if (CountyEbo.class.isAssignableFrom(businessObjectClass)) { |
92 | 0 | if (fieldValues.containsKey(LocationConstants.PrimaryKeyConstants.CODE) |
93 | |
&& fieldValues.containsKey(LocationConstants.PrimaryKeyConstants.COUNTRY_CODE) |
94 | |
&& fieldValues.containsKey(LocationConstants.PrimaryKeyConstants.STATE_CODE)) { |
95 | 0 | 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 | 0 | return (T)CountyEbo.from(county); |
100 | |
} |
101 | 0 | } else if (PostalCodeEbo.class.isAssignableFrom(businessObjectClass)) { |
102 | 0 | if (fieldValues.containsKey(LocationConstants.PrimaryKeyConstants.CODE) |
103 | |
&& fieldValues.containsKey(LocationConstants.PrimaryKeyConstants.COUNTRY_CODE)) { |
104 | 0 | PostalCode postalCode = getPostalCodeService().getPostalCode((String) fieldValues.get( |
105 | |
LocationConstants.PrimaryKeyConstants.COUNTRY_CODE), (String) fieldValues.get( |
106 | |
LocationConstants.PrimaryKeyConstants.CODE)); |
107 | 0 | return (T)PostalCodeEbo.from(postalCode); |
108 | |
} |
109 | |
} |
110 | |
|
111 | 0 | return super.getExternalizableBusinessObject( businessObjectClass, fieldValues ); |
112 | |
} |
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
@SuppressWarnings("unchecked") |
120 | |
@Override |
121 | |
public <T extends ExternalizableBusinessObject> List<T> getExternalizableBusinessObjectsList( |
122 | |
Class<T> externalizableBusinessObjectClass, Map<String, Object> fieldValues) { |
123 | |
|
124 | 0 | if ( StateEbo.class.isAssignableFrom( externalizableBusinessObjectClass ) ) { |
125 | 0 | Collection<StateBo> states = getBusinessObjectService().findMatching(StateBo.class, fieldValues); |
126 | 0 | List<StateEbo> stateEbos = new ArrayList<StateEbo>(states.size()); |
127 | 0 | for (StateBo state : states) { |
128 | 0 | stateEbos.add(StateEbo.from(State.Builder.create(state).build())); |
129 | |
} |
130 | 0 | return (List<T>)stateEbos; |
131 | 0 | } else if ( CampusEbo.class.isAssignableFrom( externalizableBusinessObjectClass ) ) { |
132 | 0 | Collection<CampusBo> campuses = getBusinessObjectService().findMatching(CampusBo.class, fieldValues); |
133 | 0 | List<CampusEbo> campusEbos = new ArrayList<CampusEbo>(campuses.size()); |
134 | 0 | for (CampusBo campus : campuses) { |
135 | 0 | campusEbos.add(CampusEbo.from(Campus.Builder.create(campus).build())); |
136 | |
} |
137 | 0 | return (List<T>)campusEbos; |
138 | 0 | } else if ( CountryEbo.class.isAssignableFrom( externalizableBusinessObjectClass ) ) { |
139 | 0 | Collection<CountryBo> countries = getBusinessObjectService().findMatching(CountryBo.class, fieldValues); |
140 | 0 | List<CountryEbo> countryEbos = new ArrayList<CountryEbo>(countries.size()); |
141 | 0 | for (CountryBo country : countries) { |
142 | 0 | countryEbos.add(CountryEbo.from(Country.Builder.create(country).build())); |
143 | |
} |
144 | 0 | return (List<T>)countryEbos; |
145 | 0 | } else if ( CountyEbo.class.isAssignableFrom( externalizableBusinessObjectClass ) ) { |
146 | 0 | Collection<CountyBo> counties = getBusinessObjectService().findMatching(CountyBo.class, fieldValues); |
147 | 0 | List<CountyEbo> countyEbos = new ArrayList<CountyEbo>(counties.size()); |
148 | 0 | for (CountyBo county : counties) { |
149 | 0 | countyEbos.add(CountyEbo.from(County.Builder.create(county).build())); |
150 | |
} |
151 | 0 | return (List<T>)countyEbos; |
152 | 0 | } else if ( PostalCodeEbo.class.isAssignableFrom( externalizableBusinessObjectClass ) ) { |
153 | 0 | Collection<PostalCodeBo> postalCodes = getBusinessObjectService().findMatching(PostalCodeBo.class, fieldValues); |
154 | 0 | List<PostalCodeEbo> postalCodeEbos = new ArrayList<PostalCodeEbo>(postalCodes.size()); |
155 | 0 | for (PostalCodeBo postalCode : postalCodes) { |
156 | 0 | postalCodeEbos.add(PostalCodeEbo.from(PostalCode.Builder.create(postalCode).build())); |
157 | |
} |
158 | 0 | return (List<T>)postalCodeEbos; |
159 | |
} |
160 | |
|
161 | 0 | return super.getExternalizableBusinessObjectsList( externalizableBusinessObjectClass, fieldValues ); |
162 | |
} |
163 | |
|
164 | |
|
165 | |
|
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 | 0 | Map<String, String> searchCriteria = new HashMap<String, String>(); |
173 | 0 | for (Map.Entry<String, Object> fieldValue : fieldValues.entrySet()) { |
174 | 0 | if (fieldValue.getValue() != null) { |
175 | 0 | searchCriteria.put(fieldValue.getKey(), fieldValue.getValue().toString()); |
176 | |
} |
177 | |
else { |
178 | 0 | searchCriteria.put(fieldValue.getKey(), null); |
179 | |
} |
180 | |
} |
181 | |
|
182 | 0 | if ( StateEbo.class.isAssignableFrom( externalizableBusinessObjectClass ) ) { |
183 | 0 | Collection<StateBo> states = getLookupService().findCollectionBySearchHelper(StateBo.class, searchCriteria, |
184 | |
unbounded); |
185 | 0 | List<StateEbo> stateEbos = new ArrayList<StateEbo>(states.size()); |
186 | 0 | for (StateBo state : states) { |
187 | 0 | stateEbos.add(StateEbo.from(State.Builder.create(state).build())); |
188 | |
} |
189 | 0 | return (List<T>)stateEbos; |
190 | 0 | } else if ( CampusEbo.class.isAssignableFrom( externalizableBusinessObjectClass ) ) { |
191 | 0 | Collection<CampusBo> campuses = getLookupService().findCollectionBySearchHelper(CampusBo.class, |
192 | |
searchCriteria, unbounded); |
193 | 0 | List<CampusEbo> campusEbos = new ArrayList<CampusEbo>(campuses.size()); |
194 | 0 | for (CampusBo campus : campuses) { |
195 | 0 | campusEbos.add(CampusEbo.from(Campus.Builder.create(campus).build())); |
196 | |
} |
197 | 0 | return (List<T>)campusEbos; |
198 | 0 | } else if ( CountryEbo.class.isAssignableFrom( externalizableBusinessObjectClass ) ) { |
199 | 0 | Collection<CountryBo> countries = getLookupService().findCollectionBySearchHelper(CountryBo.class, |
200 | |
searchCriteria, unbounded); |
201 | 0 | List<CountryEbo> countryEbos = new ArrayList<CountryEbo>(countries.size()); |
202 | 0 | for (CountryBo country : countries) { |
203 | 0 | countryEbos.add(CountryEbo.from(Country.Builder.create(country).build())); |
204 | |
} |
205 | 0 | return (List<T>)countryEbos; |
206 | 0 | } else if ( CountyEbo.class.isAssignableFrom( externalizableBusinessObjectClass ) ) { |
207 | 0 | Collection<CountyBo> counties = getLookupService().findCollectionBySearchHelper(CountyBo.class, |
208 | |
searchCriteria, unbounded); |
209 | 0 | List<CountyEbo> countyEbos = new ArrayList<CountyEbo>(counties.size()); |
210 | 0 | for (CountyBo county : counties) { |
211 | 0 | countyEbos.add(CountyEbo.from(County.Builder.create(county).build())); |
212 | |
} |
213 | 0 | return (List<T>)countyEbos; |
214 | 0 | } else if ( PostalCodeEbo.class.isAssignableFrom( externalizableBusinessObjectClass ) ) { |
215 | 0 | Collection<PostalCodeBo> postalCodes = getLookupService().findCollectionBySearchHelper(PostalCodeBo.class, |
216 | |
searchCriteria, unbounded); |
217 | 0 | List<PostalCodeEbo> postalCodeEbos = new ArrayList<PostalCodeEbo>(postalCodes.size()); |
218 | 0 | for (PostalCodeBo postalCode : postalCodes) { |
219 | 0 | postalCodeEbos.add(PostalCodeEbo.from(PostalCode.Builder.create(postalCode).build())); |
220 | |
} |
221 | 0 | return (List<T>)postalCodeEbos; |
222 | |
} |
223 | |
|
224 | 0 | return super.getExternalizableBusinessObjectsListForLookup(externalizableBusinessObjectClass, fieldValues, unbounded); |
225 | |
} |
226 | |
|
227 | |
|
228 | |
protected CampusService getCampusService() { |
229 | 0 | if (campusService == null) { |
230 | 0 | campusService = LocationApiServiceLocator.getCampusService(); |
231 | |
} |
232 | 0 | return campusService; |
233 | |
} |
234 | |
|
235 | |
protected StateService getStateService() { |
236 | 0 | if (stateService == null) { |
237 | 0 | stateService = LocationApiServiceLocator.getStateService(); |
238 | |
} |
239 | 0 | return stateService; |
240 | |
} |
241 | |
|
242 | |
protected CountryService getCountryService() { |
243 | 0 | if (countryService == null) { |
244 | 0 | countryService = LocationApiServiceLocator.getCountryService(); |
245 | |
} |
246 | 0 | return countryService; |
247 | |
} |
248 | |
|
249 | |
protected CountyService getCountyService() { |
250 | 0 | if (countyService == null) { |
251 | 0 | countyService = LocationApiServiceLocator.getCountyService(); |
252 | |
} |
253 | 0 | return countyService; |
254 | |
} |
255 | |
|
256 | |
protected PostalCodeService getPostalCodeService() { |
257 | 0 | if (postalCodeService == null) { |
258 | 0 | postalCodeService = LocationApiServiceLocator.getPostalCodeService(); |
259 | |
} |
260 | 0 | return postalCodeService; |
261 | |
} |
262 | |
|
263 | |
private QueryByCriteria toQuery(Map<String,?> fieldValues) { |
264 | 0 | Set<Predicate> preds = new HashSet<Predicate>(); |
265 | 0 | for (Map.Entry<String, ?> entry : fieldValues.entrySet()) { |
266 | 0 | preds.add(equal(entry.getKey(), entry.getValue())); |
267 | |
} |
268 | 0 | Predicate[] predicates = new Predicate[0]; |
269 | 0 | predicates = preds.toArray(predicates); |
270 | 0 | return QueryByCriteria.Builder.fromPredicates(predicates); |
271 | |
} |
272 | |
} |