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