1 | |
package org.kuali.student.core.search.service.impl; |
2 | |
|
3 | |
import java.util.Map; |
4 | |
import org.kuali.student.core.dictionary.dto.ObjectStructureDefinition; |
5 | |
import org.kuali.student.core.search.dto.QueryParamInfo; |
6 | |
import org.kuali.student.core.search.dto.ResultColumnInfo; |
7 | |
import org.kuali.student.core.search.dto.SearchTypeInfo; |
8 | |
|
9 | |
public class SearchConfigFormatter |
10 | |
{ |
11 | |
|
12 | 0 | private StringBuilder builder = new StringBuilder (5000); |
13 | |
private ObjectStructureDefinition os; |
14 | 0 | private String rowSeperator = "\n"; |
15 | 0 | private String colSeperator = "|"; |
16 | |
private Map<String, SearchTypeInfo> searchInfoTypeMap; |
17 | |
|
18 | |
|
19 | |
private Map<String, String> queryMap; |
20 | |
|
21 | |
public SearchConfigFormatter (Map<String, SearchTypeInfo> searchInfoTypeMap, |
22 | |
Map<String, String> queryMap) |
23 | 0 | { |
24 | 0 | this.searchInfoTypeMap = searchInfoTypeMap; |
25 | 0 | this.queryMap = queryMap; |
26 | 0 | } |
27 | |
|
28 | |
public String getRowSeperator () |
29 | |
{ |
30 | 0 | return rowSeperator; |
31 | |
} |
32 | |
|
33 | |
public void setRowSeperator (String rowSeperator) |
34 | |
{ |
35 | 0 | this.rowSeperator = rowSeperator; |
36 | 0 | } |
37 | |
|
38 | |
public String getColSeparator () |
39 | |
{ |
40 | 0 | return colSeperator; |
41 | |
} |
42 | |
|
43 | |
public void setColSeparator (String separator) |
44 | |
{ |
45 | 0 | this.colSeperator = separator; |
46 | 0 | } |
47 | |
|
48 | |
private String pad (String str, int size) |
49 | |
{ |
50 | 0 | StringBuilder padStr = new StringBuilder (size); |
51 | 0 | padStr.append (str); |
52 | 0 | while (padStr.length () < size) |
53 | |
{ |
54 | 0 | padStr.append (' '); |
55 | |
} |
56 | 0 | return padStr.toString (); |
57 | |
} |
58 | |
|
59 | |
public String formatForWiki () |
60 | |
{ |
61 | 0 | for (String name : this.searchInfoTypeMap.keySet ()) |
62 | |
{ |
63 | 0 | formatSearchType (name, this.searchInfoTypeMap.get (name)); |
64 | |
} |
65 | |
|
66 | 0 | return builder.toString (); |
67 | |
} |
68 | |
|
69 | |
private void formatSearchType (String name, SearchTypeInfo st) |
70 | |
{ |
71 | 0 | builder.append (rowSeperator); |
72 | 0 | builder.append ("h1. " + st.getName () + " (" + name + ")"); |
73 | 0 | builder.append ("{anchor:" + name + "}"); |
74 | 0 | builder.append (rowSeperator); |
75 | 0 | builder.append (colSeperator); |
76 | 0 | builder.append ("*Search Type Key*"); |
77 | 0 | builder.append (colSeperator); |
78 | 0 | builder.append (st.getKey ()); |
79 | 0 | builder.append (colSeperator); |
80 | 0 | builder.append (rowSeperator); |
81 | 0 | builder.append (colSeperator); |
82 | 0 | builder.append ("*Name*"); |
83 | 0 | builder.append (colSeperator); |
84 | 0 | builder.append (st.getName ()); |
85 | 0 | builder.append (colSeperator); |
86 | 0 | builder.append (rowSeperator); |
87 | 0 | builder.append (colSeperator); |
88 | 0 | builder.append ("*Description*"); |
89 | 0 | builder.append (colSeperator); |
90 | 0 | builder.append (st.getDesc ()); |
91 | 0 | builder.append (colSeperator); |
92 | 0 | builder.append (rowSeperator); |
93 | |
|
94 | 0 | builder.append (rowSeperator); |
95 | 0 | builder.append ("h2. " |
96 | |
+ st.getSearchCriteriaTypeInfo ().getQueryParams ().size () |
97 | |
+ " Possible Search Criteria"); |
98 | 0 | builder.append (rowSeperator); |
99 | 0 | builder.append (colSeperator); |
100 | 0 | builder.append ("*Type Key*"); |
101 | 0 | builder.append (colSeperator); |
102 | 0 | builder.append (st.getSearchCriteriaTypeInfo ().getKey ()); |
103 | 0 | builder.append (colSeperator); |
104 | 0 | builder.append (rowSeperator); |
105 | 0 | builder.append (colSeperator); |
106 | 0 | builder.append ("*Name*"); |
107 | 0 | builder.append (colSeperator); |
108 | 0 | builder.append (st.getSearchCriteriaTypeInfo ().getName ()); |
109 | 0 | builder.append (colSeperator); |
110 | 0 | builder.append (rowSeperator); |
111 | 0 | builder.append (colSeperator); |
112 | 0 | builder.append ("*Description*"); |
113 | 0 | builder.append (colSeperator); |
114 | 0 | builder.append (st.getSearchCriteriaTypeInfo ().getDesc ()); |
115 | 0 | builder.append (colSeperator); |
116 | 0 | builder.append (rowSeperator); |
117 | |
|
118 | |
|
119 | 0 | builder.append (rowSeperator); |
120 | 0 | builder.append (colSeperator); |
121 | 0 | builder.append (colSeperator); |
122 | 0 | builder.append ("Name"); |
123 | 0 | builder.append (colSeperator); |
124 | 0 | builder.append (colSeperator); |
125 | 0 | builder.append ("Optional"); |
126 | 0 | builder.append (colSeperator); |
127 | 0 | builder.append (colSeperator); |
128 | 0 | builder.append ("DataType"); |
129 | 0 | builder.append (colSeperator); |
130 | 0 | builder.append (colSeperator); |
131 | 0 | builder.append ("Read Only"); |
132 | 0 | builder.append (colSeperator); |
133 | 0 | builder.append (colSeperator); |
134 | 0 | builder.append ("Type Key"); |
135 | 0 | builder.append (colSeperator); |
136 | 0 | builder.append (colSeperator); |
137 | 0 | builder.append ("Implementation"); |
138 | 0 | builder.append (colSeperator); |
139 | 0 | builder.append (colSeperator); |
140 | 0 | builder.append (rowSeperator); |
141 | 0 | for (QueryParamInfo qp : st.getSearchCriteriaTypeInfo ().getQueryParams ()) |
142 | |
{ |
143 | 0 | builder.append (colSeperator); |
144 | 0 | builder.append (qp.getFieldDescriptor ().getName ()); |
145 | 0 | builder.append (colSeperator); |
146 | 0 | builder.append (qp.isOptional ()); |
147 | 0 | builder.append (colSeperator); |
148 | 0 | builder.append (qp.getFieldDescriptor ().getDataType ()); |
149 | 0 | builder.append (colSeperator); |
150 | 0 | builder.append (qp.getFieldDescriptor ().isReadOnly ()); |
151 | 0 | builder.append (colSeperator); |
152 | 0 | builder.append (qp.getKey ()); |
153 | 0 | builder.append (colSeperator); |
154 | 0 | builder.append ("{code:borderStyle=none}" + queryMap.get (qp.getKey ()) + "{code}"); |
155 | 0 | builder.append (colSeperator); |
156 | 0 | builder.append (rowSeperator); |
157 | |
} |
158 | |
|
159 | |
|
160 | 0 | builder.append (rowSeperator); |
161 | 0 | builder.append ("h2. " |
162 | |
+ st.getSearchResultTypeInfo ().getResultColumns ().size () |
163 | |
+ " Result Columns Returned"); |
164 | 0 | builder.append (rowSeperator); |
165 | 0 | builder.append (colSeperator); |
166 | 0 | builder.append ("*Type Key*"); |
167 | 0 | builder.append (colSeperator); |
168 | 0 | builder.append (st.getSearchResultTypeInfo ().getKey ()); |
169 | 0 | builder.append (colSeperator); |
170 | 0 | builder.append (rowSeperator); |
171 | 0 | builder.append (colSeperator); |
172 | 0 | builder.append ("*Name*"); |
173 | 0 | builder.append (colSeperator); |
174 | 0 | builder.append (st.getSearchResultTypeInfo ().getName ()); |
175 | 0 | builder.append (colSeperator); |
176 | 0 | builder.append (rowSeperator); |
177 | 0 | builder.append (colSeperator); |
178 | 0 | builder.append ("*Description*"); |
179 | 0 | builder.append (colSeperator); |
180 | 0 | builder.append (st.getSearchResultTypeInfo ().getDesc ()); |
181 | 0 | builder.append (colSeperator); |
182 | 0 | builder.append (rowSeperator); |
183 | |
|
184 | |
|
185 | 0 | builder.append (rowSeperator); |
186 | 0 | builder.append (colSeperator); |
187 | 0 | builder.append (colSeperator); |
188 | 0 | builder.append ("Name"); |
189 | 0 | builder.append (colSeperator); |
190 | 0 | builder.append (colSeperator); |
191 | 0 | builder.append ("DataType"); |
192 | 0 | builder.append (colSeperator); |
193 | 0 | builder.append (colSeperator); |
194 | 0 | builder.append ("Type Key"); |
195 | 0 | builder.append (colSeperator); |
196 | 0 | builder.append (colSeperator); |
197 | 0 | builder.append (rowSeperator); |
198 | 0 | for (ResultColumnInfo rc : st.getSearchResultTypeInfo ().getResultColumns ()) |
199 | |
{ |
200 | 0 | builder.append (colSeperator); |
201 | 0 | builder.append (rc.getName ()); |
202 | 0 | builder.append (colSeperator); |
203 | 0 | builder.append (rc.getDataType ()); |
204 | 0 | builder.append (colSeperator); |
205 | 0 | builder.append (rc.getKey ()); |
206 | 0 | builder.append (colSeperator); |
207 | 0 | builder.append (rowSeperator); |
208 | |
} |
209 | |
|
210 | |
|
211 | 0 | builder.append (rowSeperator); |
212 | 0 | builder.append ("h2. JPQL Implementation"); |
213 | 0 | builder.append (rowSeperator); |
214 | 0 | builder.append ("{code}"); |
215 | 0 | builder.append (rowSeperator); |
216 | 0 | builder.append (queryMap.get (st.getKey ())); |
217 | 0 | builder.append (rowSeperator); |
218 | 0 | builder.append ("{code}"); |
219 | 0 | builder.append (rowSeperator); |
220 | 0 | } |
221 | |
|
222 | |
private String escapeWiki(String str) { |
223 | 0 | StringBuilder bldr = new StringBuilder(str.length()); |
224 | 0 | boolean precededByBackSlash = false; |
225 | 0 | for (int i = 0; i < str.length(); i++) { |
226 | 0 | char c = str.charAt(i); |
227 | 0 | switch (c) { |
228 | |
case '\\': |
229 | |
case '[': |
230 | |
case '*': |
231 | |
case ']': |
232 | |
case '|': |
233 | 0 | if (!precededByBackSlash) { |
234 | 0 | bldr.append('\\'); |
235 | |
} |
236 | |
break; |
237 | |
default: |
238 | |
break; |
239 | |
} |
240 | 0 | bldr.append(c); |
241 | 0 | if (c == '\\') { |
242 | 0 | precededByBackSlash = true; |
243 | |
} else { |
244 | 0 | precededByBackSlash = false; |
245 | |
} |
246 | |
} |
247 | 0 | return bldr.toString(); |
248 | |
} |
249 | |
|
250 | |
} |