View Javadoc

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