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