1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.contract.model.util;
17
18 import org.kuali.student.contract.exception.DictionaryExecutionException;
19 import org.kuali.student.contract.model.*;
20
21 import java.util.ArrayList;
22 import java.util.LinkedHashSet;
23 import java.util.List;
24 import java.util.Set;
25
26
27
28
29
30
31 public class ModelFinder
32 {
33
34 private SearchModel searchModel;
35 private ServiceContractModel serviceContractModel;
36 private DictionaryModel dictionaryModel;
37
38 public ModelFinder (DictionaryModel model)
39 {
40 this.dictionaryModel = model;
41 this.searchModel = model;
42 this.serviceContractModel = model;
43 }
44
45 public ModelFinder (SearchModel model)
46 {
47 this.searchModel = model;
48 }
49
50 public ModelFinder (ServiceContractModel model)
51 {
52 this.serviceContractModel = model;
53 }
54
55 public XmlType findXmlType (String xmlTypeName)
56 {
57 for (XmlType xmlType : serviceContractModel.getXmlTypes ())
58 {
59 if (xmlTypeName.equalsIgnoreCase (xmlType.getName ()))
60 {
61 return xmlType;
62 }
63 }
64 return null;
65 }
66
67 public List<State> findStates (String xmlObject)
68 {
69 List<State> list = new ArrayList ();
70 for (State state : dictionaryModel.getStates ())
71 {
72 if (state.getXmlObject ().equalsIgnoreCase (xmlObject))
73 {
74 list.add (state);
75 }
76 }
77 return list;
78 }
79
80 public List<Type> findTypes (String xmlObject)
81 {
82 List<Type> list = new ArrayList ();
83 for (Type type : dictionaryModel.getTypes ())
84 {
85 if (type.getXmlObject ().equalsIgnoreCase (xmlObject))
86 {
87 list.add (type);
88 }
89 }
90 return list;
91 }
92
93 public Type findType (String xmlObject, String typeName)
94 {
95 if (typeName.equalsIgnoreCase (State.DEFAULT))
96 {
97 return this.getDefaultType ();
98 }
99 for (Type type : dictionaryModel.getTypes ())
100 {
101 if (type.getXmlObject ().equalsIgnoreCase (xmlObject))
102 {
103 if (type.getName ().equalsIgnoreCase (typeName))
104 {
105 return type;
106 }
107 }
108 }
109 return null;
110 }
111
112 public Type findType (String typeKey)
113 {
114 for (Type type : dictionaryModel.getTypes ())
115 {
116 if (type.getTypeKey ().equalsIgnoreCase (typeKey))
117 {
118 return type;
119 }
120 }
121 return null;
122 }
123
124 public State findState (String xmlObject, String stateName)
125 {
126 if (stateName.equalsIgnoreCase (State.DEFAULT))
127 {
128 return this.getDefaultState ();
129 }
130 for (State state : dictionaryModel.getStates ())
131 {
132 if (state.getXmlObject ().equalsIgnoreCase (xmlObject))
133 {
134 if (state.getName ().equalsIgnoreCase (stateName))
135 {
136 return state;
137 }
138 }
139 }
140 return null;
141 }
142
143 public State findState (String stateKey)
144 {
145 for (State state : dictionaryModel.getStates ())
146 {
147 if (state.getStateKey ().equalsIgnoreCase (stateKey))
148 {
149 return state;
150 }
151 }
152 return null;
153 }
154
155
156 public Dictionary findRoot (Dictionary dict)
157 {
158 if (dict.getParent () == null)
159 {
160 return dict;
161 }
162 return findRoot (dict.getParent ());
163 }
164
165
166
167
168
169 public Dictionary findDictionaryEntry (String dictId)
170 {
171 for (Dictionary d : dictionaryModel.getDictionary ())
172 {
173 if (d.getId ().equalsIgnoreCase (dictId))
174 {
175 return d;
176 }
177 }
178 return null;
179 }
180
181
182
183
184
185 public List<Dictionary> findChildDictionaryEntries (Dictionary parent)
186 {
187 List<Dictionary> list = new ArrayList ();
188 for (Dictionary d : dictionaryModel.getDictionary ())
189 {
190 if (d.getParent () == null)
191 {
192 continue;
193 }
194 if (d.getParent ().equals (parent))
195 {
196 list.add (d);
197 }
198 }
199 return list;
200 }
201
202
203
204
205
206 public List<Dictionary> findDefaultDictionary ()
207 {
208 List<Dictionary> list = new ArrayList (dictionaryModel.getDictionary ().size ());
209 for (Dictionary d : dictionaryModel.getDictionary ())
210 {
211 if (d.getState ().equalsIgnoreCase (State.DEFAULT))
212 {
213 list.add (d);
214 }
215 }
216 return list;
217 }
218
219 public List<Dictionary> findDictionaryEntriees (String xmlTypeName)
220 {
221 List<Dictionary> list = new ArrayList ();
222 for (Dictionary dict : dictionaryModel.getDictionary ())
223 {
224 if (dict.getXmlObject ().equalsIgnoreCase (xmlTypeName))
225 {
226 list.add (dict);
227 }
228 }
229 return list;
230 }
231
232 public List<Dictionary> findDefaultDictionaryEntriees (String xmlTypeName)
233 {
234 List<Dictionary> list = new ArrayList ();
235 for (Dictionary dict : this.findDefaultDictionary ())
236 {
237 if (dict.getXmlObject ().equalsIgnoreCase (xmlTypeName))
238 {
239 list.add (dict);
240 }
241 }
242 return list;
243 }
244
245
246
247
248
249 public List<Dictionary> findStateOverrideDictionary ()
250 {
251 List<Dictionary> list = new ArrayList (dictionaryModel.getDictionary ().size ());
252 for (Dictionary d : dictionaryModel.getDictionary ())
253 {
254 if ( ! d.getState ().equalsIgnoreCase (State.DEFAULT))
255 {
256 list.add (d);
257 }
258 }
259 return list;
260 }
261
262
263
264
265
266
267
268 public Set<Type> expandType (Type type)
269 {
270 Set<Type> types = new LinkedHashSet ();
271 if ( ! type.getStatus ().equalsIgnoreCase (Type.GROUPING))
272 {
273 types.add (type);
274 return types;
275 }
276 String pattern = type.getTypeKey ();
277 GroupTypeStatePatternMatcher matcher =
278 new GroupTypeStatePatternMatcher (pattern);
279 for (Type t : dictionaryModel.getTypes ())
280 {
281
282 if (t == type)
283 {
284
285 continue;
286 }
287 if ( ! t.getInclude ())
288 {
289 continue;
290 }
291
292
293 if (type.getXmlObject ().equalsIgnoreCase (t.getXmlObject ()))
294 {
295 if (matcher.matches (t.getTypeKey ()))
296 {
297 if (t.getStatus ().equalsIgnoreCase (Type.GROUPING))
298 {
299
300 types.addAll (expandType (t));
301 }
302 else
303 {
304 types.add (t);
305 }
306 }
307 }
308 }
309 return types;
310 }
311
312
313
314
315
316
317
318 public Set<State> expandState (State state)
319 {
320 Set<State> states = new LinkedHashSet ();
321 if ( ! state.getStatus ().equalsIgnoreCase (State.GROUPING))
322 {
323 states.add (state);
324 return states;
325 }
326 String pattern = state.getStateKey ();
327 GroupTypeStatePatternMatcher matcher =
328 new GroupTypeStatePatternMatcher (pattern);
329 for (State s : dictionaryModel.getStates ())
330 {
331
332 if (s == state)
333 {
334
335 continue;
336 }
337 if ( ! s.getInclude ())
338 {
339 continue;
340 }
341
342
343 if (state.getXmlObject ().equalsIgnoreCase (s.getXmlObject ()))
344 {
345 if (matcher.matches (s.getStateKey ()))
346 {
347 if (s.getStatus ().equalsIgnoreCase (Type.GROUPING))
348 {
349
350 states.addAll (expandState (s));
351 }
352 else
353 {
354 states.add (s);
355 }
356 }
357 }
358 }
359 return states;
360 }
361
362 public Constraint findConstraint (String id)
363 {
364 for (Constraint cons : dictionaryModel.getConstraints ())
365 {
366 if (cons.getId ().equalsIgnoreCase (id))
367 {
368 return cons;
369 }
370 }
371 return null;
372 }
373
374 public Field findField (String id)
375 {
376 for (Field field : dictionaryModel.getFields ())
377 {
378 if (field.getId ().equalsIgnoreCase (id))
379 {
380 return field;
381 }
382 }
383 return null;
384 }
385
386 public Field findField (String xmlTypeName, String shortName)
387 {
388 return findField (xmlTypeName + "." + shortName);
389 }
390
391 public Field findField (Dictionary dict)
392 {
393 if (dict.isDynamic ())
394 {
395 return findField (dict.getXmlObject (), "attributes");
396 }
397 return findField (dict.getXmlObject (), dict.getShortName ());
398 }
399
400 public List<Field> findFields (String xmlTypeName)
401 {
402 List<Field> list = new ArrayList ();
403 for (Field field : dictionaryModel.getFields ())
404 {
405 if (field.getXmlObject ().equalsIgnoreCase (xmlTypeName))
406 {
407 list.add (field);
408 }
409 }
410 return list;
411 }
412
413 public Service findService (String key)
414 {
415 for (Service serv : serviceContractModel.getServices ())
416 {
417 if (serv.getKey ().equalsIgnoreCase (key))
418 {
419 return serv;
420 }
421 }
422 return null;
423 }
424
425 public Project findProject (String key)
426 {
427 for (Project proj : dictionaryModel.getProjects ())
428 {
429 if (proj.getKey ().equalsIgnoreCase (key))
430 {
431 return proj;
432 }
433 }
434 return null;
435 }
436
437 public SearchType findSearchType (String key)
438 {
439 for (SearchType st : searchModel.getSearchTypes ())
440 {
441 if (st.getKey ().equalsIgnoreCase (key))
442 {
443 return st;
444 }
445 }
446 return null;
447 }
448
449 public ServiceMethod findServiceMethod (String service, String name)
450 {
451 for (ServiceMethod method : serviceContractModel.getServiceMethods ())
452 {
453 if (method.getService ().equalsIgnoreCase (service))
454 {
455 if (method.getName ().equalsIgnoreCase (name))
456 {
457 return method;
458 }
459 }
460 }
461 return null;
462 }
463
464 public List<ServiceMethod> getServiceMethodsInService (String service)
465 {
466 List<ServiceMethod> list = new ArrayList ();
467 for (ServiceMethod method : serviceContractModel.getServiceMethods ())
468 {
469 if (method.getService ().equalsIgnoreCase (service))
470 {
471 list.add (method);
472 }
473 }
474 return list;
475 }
476
477 public List<MessageStructure> findMessageStructures (String xmlType)
478 {
479 List<MessageStructure> list = new ArrayList ();
480 for (MessageStructure ms : serviceContractModel.getMessageStructures ())
481 {
482 if (ms.getXmlObject ().equalsIgnoreCase (xmlType))
483 {
484 list.add (ms);
485 }
486 }
487 return list;
488 }
489
490 private Type defaultType = null;
491
492 public Type getDefaultType ()
493 {
494 if (defaultType != null)
495 {
496 return defaultType;
497 }
498 List<Type> list = findTypes (Type.DEFAULT);
499 if (list.size () == 0)
500 {
501 throw new DictionaryExecutionException ("No default Type found");
502 }
503 if (list.size () > 1)
504 {
505 throw new DictionaryExecutionException ("More than one default Type found");
506 }
507 defaultType = list.get (0);
508 return defaultType;
509 }
510
511 private State defaultState = null;
512
513 public State getDefaultState ()
514 {
515 if (defaultState != null)
516 {
517 return defaultState;
518 }
519 List<State> list = findStates (State.DEFAULT);
520 if (list.size () == 0)
521 {
522 throw new DictionaryExecutionException ("No default State found");
523 }
524 if (list.size () > 1)
525 {
526 throw new DictionaryExecutionException ("More than one default State found");
527 }
528 defaultState = list.get (0);
529 return defaultState;
530 }
531
532 }
533