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