View Javadoc

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