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.ConfigurableApplicationContext;
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 ConfigurableApplicationContext 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 ac.close();
63
64 out.println ("(!) This page was automatically generated on " + new Date ());
65 out.println ("DO NOT UPDATE MANUALLY!");
66 out.println ("");
67 out.print ("This page represents a formatted view of [" + searchConfigFileName
68 + "|https://test.kuali.org/svn/student/trunk/" + projectLocation
69 + "/src/main/resources/"
70 + searchConfigFileName + "]");
71 out.println ("");
72 out.println ("----");
73 out.println ("{toc}");
74 out.println ("----");
75 SearchConfigValidator validator = new SearchConfigValidator(searchInfoTypeMap,
76 queryMap);
77
78 List<String> errors = validator.validate ();
79 if (errors.size () > 0)
80 {
81 throw new RuntimeException (searchConfigFileName + " failed search config validation:\n"
82 + this.formatAsString (errors));
83 }
84
85 SearchConfigFormatter formatter = new SearchConfigFormatter(searchInfoTypeMap,
86 queryMap);
87 out.println (formatter.formatForWiki ());
88 }
89
90 private String formatAsString (List<String> discrepancies)
91 {
92 int i = 0;
93 StringBuilder builder = new StringBuilder ();
94 for (String discrep : discrepancies)
95 {
96 i ++;
97 builder.append (i + ". " + discrep + "\n");
98 }
99 return builder.toString ();
100 }
101 }