1 package org.kuali.student.lum.course.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.Date;
9 import java.util.List;
10 import java.util.Map;
11
12 import org.kuali.student.r2.common.class1.search.SearchConfigFormatter;
13 import org.kuali.student.r2.common.class1.search.SearchConfigValidator;
14 import org.kuali.student.r2.core.search.dto.SearchCriteriaTypeInfo;
15 import org.kuali.student.r2.core.search.dto.SearchResultTypeInfo;
16 import org.kuali.student.r2.core.search.dto.SearchTypeInfo;
17 import org.springframework.context.ApplicationContext;
18 import org.springframework.context.support.ClassPathXmlApplicationContext;
19
20 public class SearchConfigTesterHelper
21 {
22
23 private String outputFileName;
24 private File file;
25 private OutputStream os;
26 private PrintStream out;
27 private String searchConfigFileName;
28 private String projectLocation;
29 private Map<String, SearchTypeInfo> searchInfoTypeMap;
30 private Map<String, SearchCriteriaTypeInfo> searchCriteriaTypeMap;
31 private Map<String, SearchResultTypeInfo> searchResultTypeInfoMap;
32 private Map<String, String> queryMap;
33
34 public SearchConfigTesterHelper (String outputFileName,
35 String projectLocation,
36 String searchConfigFileName)
37 {
38 this.outputFileName = outputFileName;
39 this.projectLocation = projectLocation;
40 this.searchConfigFileName = searchConfigFileName;
41
42 this.file = new File (this.outputFileName);
43 try
44 {
45 os = new FileOutputStream (file, false);
46 }
47 catch (FileNotFoundException ex)
48 {
49 throw new IllegalArgumentException (ex);
50 }
51 this.out = new PrintStream (os);
52 }
53
54 public void doTest ()
55 {
56 ApplicationContext ac = new ClassPathXmlApplicationContext (
57 "classpath:" + searchConfigFileName);
58 searchInfoTypeMap = ac.getBeansOfType (SearchTypeInfo.class);
59 searchCriteriaTypeMap = ac.getBeansOfType (SearchCriteriaTypeInfo.class);
60 searchResultTypeInfoMap = ac.getBeansOfType (SearchResultTypeInfo.class);
61 queryMap = (Map<String, String>) ac.getBean ("queryMap");
62
63 out.println ("(!) This page was automatically generated on " + new Date ());
64 out.println ("DO NOT UPDATE MANUALLY!");
65 out.println ("");
66 out.print ("This page represents a formatted view of [" + searchConfigFileName
67 + "|https://test.kuali.org/svn/student/trunk/" + projectLocation
68 + "/src/main/resources/"
69 + searchConfigFileName + "]");
70 out.println ("");
71 out.println ("----");
72 out.println ("{toc}");
73 out.println ("----");
74 SearchConfigValidator validator = new SearchConfigValidator(searchInfoTypeMap,
75 queryMap);
76
77 List<String> errors = validator.validate ();
78 if (errors.size () > 0)
79 {
80 throw new RuntimeException (searchConfigFileName + " failed search config validation:\n"
81 + this.formatAsString (errors));
82 }
83
84 SearchConfigFormatter formatter = new SearchConfigFormatter(searchInfoTypeMap,
85 queryMap);
86 out.println (formatter.formatForWiki ());
87 }
88
89 private String formatAsString (List<String> discrepancies)
90 {
91 int i = 0;
92 StringBuilder builder = new StringBuilder ();
93 for (String discrep : discrepancies)
94 {
95 i ++;
96 builder.append (i + ". " + discrep + "\n");
97 }
98 return builder.toString ();
99 }
100 }