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