| 1 |  |  package org.kuali.student.core.dictionary.service.impl; | 
  | 2 |  |   | 
  | 3 |  |  import java.io.File; | 
  | 4 |  |  import java.io.FileNotFoundException; | 
  | 5 |  |  import java.io.FileOutputStream; | 
  | 6 |  |  import java.io.OutputStream; | 
  | 7 |  |  import java.io.PrintStream; | 
  | 8 |  |  import java.util.ArrayList; | 
  | 9 |  |  import java.util.Date; | 
  | 10 |  |  import java.util.HashMap; | 
  | 11 |  |  import java.util.HashSet; | 
  | 12 |  |  import java.util.LinkedHashSet; | 
  | 13 |  |  import java.util.List; | 
  | 14 |  |  import java.util.Map; | 
  | 15 |  |  import java.util.Set; | 
  | 16 |  |  import org.kuali.student.core.dictionary.dto.ObjectStructureDefinition; | 
  | 17 |  |  import org.springframework.context.ApplicationContext; | 
  | 18 |  |  import org.springframework.context.support.ClassPathXmlApplicationContext; | 
  | 19 |  |   | 
  | 20 |  |  public class DictionaryTesterHelper | 
  | 21 |  |  { | 
  | 22 |  |   | 
  | 23 |  |   private String outputFileName; | 
  | 24 |  |   private File file; | 
  | 25 |  |   private OutputStream outputStream; | 
  | 26 |  |   private PrintStream out; | 
  | 27 |  |   private Set<String> startingClasses; | 
  | 28 |  |   private String dictFileName; | 
  | 29 | 0 |   private boolean processSubstructures = false; | 
  | 30 |  |   | 
  | 31 |  |   public DictionaryTesterHelper (String outputFileName, | 
  | 32 |  |                                  Set<String> startingClasses, | 
  | 33 |  |                                  String dictFileName, | 
  | 34 |  |                                  boolean processSubstructures) | 
  | 35 | 0 |   { | 
  | 36 | 0 |    this.outputFileName = outputFileName; | 
  | 37 | 0 |    this.startingClasses = startingClasses; | 
  | 38 | 0 |    this.dictFileName = dictFileName; | 
  | 39 | 0 |    this.processSubstructures = processSubstructures; | 
  | 40 |  |     | 
  | 41 | 0 |    this.file = new File (this.outputFileName); | 
  | 42 |  |    try | 
  | 43 |  |    { | 
  | 44 | 0 |     outputStream = new FileOutputStream (file, false); | 
  | 45 |  |    } | 
  | 46 | 0 |    catch (FileNotFoundException ex) | 
  | 47 |  |    { | 
  | 48 | 0 |     throw new IllegalArgumentException (ex); | 
  | 49 | 0 |    } | 
  | 50 | 0 |    this.out = new PrintStream (outputStream); | 
  | 51 | 0 |   } | 
  | 52 |  |   | 
  | 53 |  |   private transient Map<String, ObjectStructureDefinition> objectStructures; | 
  | 54 |  |   | 
  | 55 |  |   public List<String> doTest () | 
  | 56 |  |   { | 
  | 57 | 0 |    ApplicationContext ac = new ClassPathXmlApplicationContext ( | 
  | 58 |  |        "classpath:" + dictFileName); | 
  | 59 | 0 |    objectStructures = new HashMap (); | 
  | 60 | 0 |    Map<String, ObjectStructureDefinition> beansOfType = | 
  | 61 |  |                                           (Map<String, ObjectStructureDefinition>) ac. | 
  | 62 |  |        getBeansOfType (ObjectStructureDefinition.class); | 
  | 63 | 0 |    for (ObjectStructureDefinition objStr: beansOfType.values ()) | 
  | 64 |  |    { | 
  | 65 | 0 |     objectStructures.put (objStr.getName (), objStr); | 
  | 66 | 0 |     System.out.println ("Loading object structure: " + objStr.getName ()); | 
  | 67 |  |    } | 
  | 68 |  |     | 
  | 69 | 0 |    for (String className: startingClasses) | 
  | 70 |  |    { | 
  | 71 | 0 |     ObjectStructureDefinition os = null; | 
  | 72 | 0 |     os = objectStructures.get (className); | 
  | 73 | 0 |     if (os == null) | 
  | 74 |  |     { | 
  | 75 | 0 |      throw new RuntimeException ("className is not defined in dictionary: " + className); | 
  | 76 |  |     } | 
  | 77 | 0 |     DictionaryValidator validator = new DictionaryValidator (os, | 
  | 78 |  |                                                              new HashSet (), | 
  | 79 |  |                                                              false); | 
  | 80 | 0 |     List<String> errors = validator.validate (); | 
  | 81 | 0 |     if (errors.size () > 0) | 
  | 82 |  |     { | 
  | 83 | 0 |      return errors; | 
  | 84 |  |     } | 
  | 85 | 0 |    } | 
  | 86 |  |   | 
  | 87 |  |   | 
  | 88 | 0 |    Set<String> allStructures = new LinkedHashSet (); | 
  | 89 | 0 |    for (String className: startingClasses) | 
  | 90 |  |    { | 
  | 91 | 0 |     allStructures.addAll (getComplexSubStructures (className)); | 
  | 92 |  |    } | 
  | 93 | 0 |    Set<String> classesToProcess = null; | 
  | 94 | 0 |    if (this.processSubstructures) | 
  | 95 |  |    { | 
  | 96 | 0 |     classesToProcess = startingClasses; | 
  | 97 |  |   | 
  | 98 |  |    } | 
  | 99 |  |    else | 
  | 100 |  |    { | 
  | 101 | 0 |     classesToProcess = allStructures; | 
  | 102 |  |   | 
  | 103 |  |    } | 
  | 104 |  |   | 
  | 105 | 0 |    out.println ("(!) This page was automatically generated on " + new Date ()); | 
  | 106 | 0 |    out.println ("DO NOT UPDATE MANUALLY!"); | 
  | 107 | 0 |    out.println (""); | 
  | 108 | 0 |    out.print ("This page represents a formatted view of [" + dictFileName | 
  | 109 |  |               + "|https://test.kuali.org/svn/student/trunk/ks-lum/ks-lum-impl/src/main/resources/" | 
  | 110 |  |               + dictFileName + "]"); | 
  | 111 | 0 |    out.println ( | 
  | 112 |  |        " and is compared to the following java classes (and their sub-classes) for discrepancies:"); | 
  | 113 | 0 |    for (String className: startingClasses) | 
  | 114 |  |    { | 
  | 115 | 0 |     out.println ("# " + className); | 
  | 116 |  |    } | 
  | 117 | 0 |    out.println (""); | 
  | 118 | 0 |    out.println ("----"); | 
  | 119 | 0 |    out.println ("{toc}"); | 
  | 120 | 0 |    out.println ("----"); | 
  | 121 | 0 |    for (String className: classesToProcess) | 
  | 122 |  |    { | 
  | 123 |  |   | 
  | 124 | 0 |     doTestOnClass (className, ac); | 
  | 125 |  |    } | 
  | 126 | 0 |    out.close (); | 
  | 127 | 0 |    return new ArrayList (); | 
  | 128 |  |   } | 
  | 129 |  |   | 
  | 130 |  |   private Set<String> getComplexSubStructures (String className) | 
  | 131 |  |   { | 
  | 132 | 0 |    return new ComplexSubstructuresHelper ().getComplexStructures (className); | 
  | 133 |  |   } | 
  | 134 |  |   | 
  | 135 |  |   private void doTestOnClass (String className, ApplicationContext ac) | 
  | 136 |  |   { | 
  | 137 | 0 |    ObjectStructureDefinition os =   os = objectStructures.get (className); | 
  | 138 | 0 |    String simpleName = calcSimpleName (className); | 
  | 139 | 0 |    if (os == null) | 
  | 140 |  |    { | 
  | 141 |  |   | 
  | 142 | 0 |     out.println ("h1. " + simpleName); | 
  | 143 | 0 |     out.println ("{anchor:" + simpleName + "}"); | 
  | 144 | 0 |     out.println ("h2. Error could not find a corresponding dictionary definition"); | 
  | 145 | 0 |     return; | 
  | 146 |  |    } | 
  | 147 | 0 |    DictionaryFormatter formatter = | 
  | 148 |  |                        new DictionaryFormatter (className, | 
  | 149 |  |                                                 className, | 
  | 150 |  |                                                 os, | 
  | 151 |  |                                                 new HashSet (), | 
  | 152 |  |                                                 1,  | 
  | 153 |  |                                                 this.processSubstructures); | 
  | 154 | 0 |    out.println (formatter.formatForWiki ()); | 
  | 155 | 0 |   } | 
  | 156 |  |   | 
  | 157 |  |   private String calcSimpleName (String name) | 
  | 158 |  |   { | 
  | 159 | 0 |    if (name.lastIndexOf (".") != -1) | 
  | 160 |  |    { | 
  | 161 | 0 |     name = name.substring (name.lastIndexOf (".") + 1); | 
  | 162 |  |    } | 
  | 163 | 0 |    return name; | 
  | 164 |  |   } | 
  | 165 |  |   | 
  | 166 |  |  } |