| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.student.contract.writer.search; |
| 17 | |
|
| 18 | |
import org.kuali.student.contract.model.SearchCriteria; |
| 19 | |
import org.kuali.student.contract.model.SearchCriteriaParameter; |
| 20 | |
import org.kuali.student.contract.model.SearchImplementation; |
| 21 | |
import org.kuali.student.contract.model.SearchModel; |
| 22 | |
import org.kuali.student.contract.model.SearchResult; |
| 23 | |
import org.kuali.student.contract.model.SearchResultColumn; |
| 24 | |
import org.kuali.student.contract.model.SearchType; |
| 25 | |
import org.kuali.student.contract.model.validation.DictionaryValidationException; |
| 26 | |
import org.kuali.student.contract.model.validation.SearchModelValidator; |
| 27 | |
import org.kuali.student.contract.writer.XmlWriter; |
| 28 | |
import java.io.PrintStream; |
| 29 | |
import java.util.ArrayList; |
| 30 | |
import java.util.Collection; |
| 31 | |
import java.util.Date; |
| 32 | |
import java.util.HashSet; |
| 33 | |
import java.util.List; |
| 34 | |
import java.util.Set; |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
public class SearchModelWriter |
| 41 | |
{ |
| 42 | |
|
| 43 | |
private SearchModel model; |
| 44 | |
private XmlWriter writer; |
| 45 | |
|
| 46 | |
public SearchModelWriter (PrintStream out, SearchModel model) |
| 47 | 0 | { |
| 48 | 0 | this.writer = new XmlWriter (out, 0); |
| 49 | 0 | this.model = model; |
| 50 | 0 | } |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
public void write () |
| 57 | |
{ |
| 58 | 0 | Collection<String> errors = new SearchModelValidator (model).validate (); |
| 59 | 0 | if (errors.size () > 0) |
| 60 | |
{ |
| 61 | 0 | StringBuffer buf = new StringBuffer (); |
| 62 | 0 | buf.append (errors.size () + |
| 63 | |
" errors found while validating the spreadsheet."); |
| 64 | 0 | int cnt = 0; |
| 65 | 0 | for (String msg : errors) |
| 66 | |
{ |
| 67 | 0 | cnt ++; |
| 68 | 0 | buf.append ("\n"); |
| 69 | 0 | buf.append ("*error*" + cnt + ":" + msg); |
| 70 | |
} |
| 71 | 0 | throw new DictionaryValidationException (buf.toString ()); |
| 72 | |
} |
| 73 | 0 | writeHeader (); |
| 74 | 0 | writeSearchTypes (); |
| 75 | 0 | writeSearchCriteria (); |
| 76 | 0 | writeSearchCriteriaParameters (); |
| 77 | 0 | writeSearchResultTypes (); |
| 78 | 0 | writeSearchResultColumns (); |
| 79 | 0 | writeSqlQueryMap (); |
| 80 | 0 | writeFooter (); |
| 81 | 0 | } |
| 82 | |
|
| 83 | |
private void indentPrint (String str) |
| 84 | |
{ |
| 85 | 0 | writer.indentPrintln (str); |
| 86 | 0 | } |
| 87 | |
|
| 88 | |
private void indentPrintln (String str) |
| 89 | |
{ |
| 90 | 0 | writer.indentPrintln (str); |
| 91 | 0 | } |
| 92 | |
|
| 93 | |
private void println (String str) |
| 94 | |
{ |
| 95 | 0 | writer.indentPrintln (str); |
| 96 | 0 | } |
| 97 | |
|
| 98 | |
private void writeComment (String str) |
| 99 | |
{ |
| 100 | 0 | writer.writeComment (str); |
| 101 | 0 | } |
| 102 | |
|
| 103 | |
private void incrementIndent () |
| 104 | |
{ |
| 105 | 0 | writer.incrementIndent (); |
| 106 | 0 | } |
| 107 | |
private void decrementIndent () |
| 108 | |
{ |
| 109 | 0 | writer.decrementIndent (); |
| 110 | 0 | } |
| 111 | |
|
| 112 | |
private void writeTag (String tag, String value) |
| 113 | |
{ |
| 114 | 0 | writer.writeTag (tag, value); |
| 115 | 0 | } |
| 116 | |
|
| 117 | |
private void writeAttribute (String attribute, String value) |
| 118 | |
{ |
| 119 | 0 | writer.writeAttribute (attribute, value); |
| 120 | 0 | } |
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
protected void writeHeader () |
| 128 | |
{ |
| 129 | 0 | indentPrintln ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); |
| 130 | 0 | indentPrintln ("<beans xmlns=\"http://www.springframework.org/schema/beans\""); |
| 131 | 0 | indentPrintln ("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""); |
| 132 | 0 | indentPrintln ("xmlns:search=\"http://student.kuali.org/xsd/search-extension\""); |
| 133 | 0 | indentPrintln ("xmlns:dict=\"http://student.kuali.org/xsd/dictionary-extension\""); |
| 134 | 0 | indentPrintln ("xsi:schemaLocation=\""); |
| 135 | 0 | indentPrintln ("http://student.kuali.org/xsd/search-extension http://student.kuali.org/xsd/search-extension/search-extension.xsd"); |
| 136 | 0 | indentPrintln ("http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"); |
| 137 | 0 | indentPrintln ("\">"); |
| 138 | 0 | StringBuffer buf = new StringBuffer (); |
| 139 | 0 | buf.append ("*** Automatically Generated ***"); |
| 140 | 0 | buf.append ("\n"); |
| 141 | 0 | buf.append ("on: " + (new Date ())); |
| 142 | 0 | buf.append ("\n"); |
| 143 | 0 | buf.append ("by: " + this.getClass ().getName ()); |
| 144 | 0 | buf.append ("\n"); |
| 145 | 0 | buf.append ("using: "); |
| 146 | 0 | String comma = ""; |
| 147 | 0 | for (String soureName : model.getSourceNames ()) |
| 148 | |
{ |
| 149 | 0 | buf.append (soureName); |
| 150 | 0 | buf.append (comma); |
| 151 | 0 | comma = ", "; |
| 152 | |
} |
| 153 | 0 | buf.append ("\n"); |
| 154 | 0 | writeComment (buf.toString ()); |
| 155 | 0 | } |
| 156 | |
|
| 157 | |
protected void writeFooter () |
| 158 | |
{ |
| 159 | 0 | indentPrintln ("</beans>"); |
| 160 | 0 | } |
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
protected void writeSearchTypes () |
| 167 | |
{ |
| 168 | 0 | println (""); |
| 169 | 0 | writeComment ("Search Types"); |
| 170 | 0 | for (SearchType st : model.getSearchTypes ()) |
| 171 | |
{ |
| 172 | 0 | new SearchTypeWriter (writer.getOut (), writer.getIndent () + 1, st).write (); |
| 173 | |
} |
| 174 | 0 | } |
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
protected void writeSearchResultTypes () |
| 181 | |
{ |
| 182 | 0 | println (""); |
| 183 | 0 | writeComment ("Search Results"); |
| 184 | 0 | for (SearchResult sr : getSearchResults ()) |
| 185 | |
{ |
| 186 | 0 | new SearchResultWriter (writer.getOut (), writer.getIndent () + 1, sr).write (); |
| 187 | |
} |
| 188 | |
|
| 189 | 0 | } |
| 190 | |
|
| 191 | |
private List<SearchResult> getSearchResults () |
| 192 | |
{ |
| 193 | 0 | List<SearchResult> list = new ArrayList (); |
| 194 | 0 | Set<String> keys = new HashSet (); |
| 195 | 0 | for (SearchType st : model.getSearchTypes ()) |
| 196 | |
{ |
| 197 | 0 | if (keys.add (st.getSearchResult ().getKey ())) |
| 198 | |
{ |
| 199 | 0 | list.add (st.getSearchResult ()); |
| 200 | |
} |
| 201 | |
} |
| 202 | 0 | return list; |
| 203 | |
} |
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
protected void writeSearchResultColumns () |
| 210 | |
{ |
| 211 | 0 | println (""); |
| 212 | 0 | writeComment ("Search Result Columns"); |
| 213 | 0 | for (SearchResultColumn col : getSearchResultColumns ()) |
| 214 | |
{ |
| 215 | 0 | new SearchResultColumnWriter (writer.getOut (), writer.getIndent () + 1, col). |
| 216 | |
write (); |
| 217 | |
} |
| 218 | 0 | } |
| 219 | |
|
| 220 | |
private List<SearchResultColumn> getSearchResultColumns () |
| 221 | |
{ |
| 222 | 0 | List<SearchResultColumn> list = new ArrayList (); |
| 223 | 0 | Set<String> keys = new HashSet (); |
| 224 | 0 | for (SearchResult searchResult : getSearchResults ()) |
| 225 | |
{ |
| 226 | 0 | for (SearchResultColumn col : searchResult.getResultColumns ()) |
| 227 | |
{ |
| 228 | 0 | if (keys.add (col.getKey ())) |
| 229 | |
{ |
| 230 | 0 | list.add (col); |
| 231 | |
} |
| 232 | |
|
| 233 | |
} |
| 234 | |
} |
| 235 | 0 | return list; |
| 236 | |
} |
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
protected void writeSearchCriteria () |
| 243 | |
{ |
| 244 | 0 | println (""); |
| 245 | 0 | writeComment ("Search Criteria"); |
| 246 | 0 | for (SearchCriteria sr : getSearchCriteria ()) |
| 247 | |
{ |
| 248 | 0 | new SearchCriteriaWriter (writer.getOut (), writer.getIndent () + 1, sr). |
| 249 | |
write (); |
| 250 | |
} |
| 251 | 0 | } |
| 252 | |
|
| 253 | |
private List<SearchCriteria> getSearchCriteria () |
| 254 | |
{ |
| 255 | 0 | List<SearchCriteria> list = new ArrayList (); |
| 256 | 0 | Set<String> keys = new HashSet (); |
| 257 | 0 | for (SearchType st : model.getSearchTypes ()) |
| 258 | |
{ |
| 259 | 0 | if (keys.add (st.getSearchCriteria ().getKey ())) |
| 260 | |
{ |
| 261 | 0 | list.add (st.getSearchCriteria ()); |
| 262 | |
} |
| 263 | |
} |
| 264 | 0 | return list; |
| 265 | |
} |
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
protected void writeSearchCriteriaParameters () |
| 272 | |
{ |
| 273 | 0 | println (""); |
| 274 | 0 | writeComment ("Search Criteria Parameters"); |
| 275 | 0 | for (SearchCriteriaParameter col : getSearchCriteriaParameters ()) |
| 276 | |
{ |
| 277 | 0 | new SearchCriteriaParameterWriter (writer.getOut (), writer.getIndent () + 1, col). |
| 278 | |
write (); |
| 279 | |
} |
| 280 | 0 | } |
| 281 | |
|
| 282 | |
private List<SearchCriteriaParameter> getSearchCriteriaParameters () |
| 283 | |
{ |
| 284 | 0 | List<SearchCriteriaParameter> list = new ArrayList (); |
| 285 | 0 | Set<String> keys = new HashSet (); |
| 286 | 0 | for (SearchCriteria criteria : getSearchCriteria ()) |
| 287 | |
{ |
| 288 | 0 | for (SearchCriteriaParameter parm : criteria.getParameters ()) |
| 289 | |
{ |
| 290 | 0 | if (keys.add (parm.getKey ())) |
| 291 | |
{ |
| 292 | 0 | list.add (parm); |
| 293 | |
} |
| 294 | |
|
| 295 | |
} |
| 296 | |
} |
| 297 | 0 | return list; |
| 298 | |
} |
| 299 | |
|
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
|
| 304 | |
protected void writeSqlQueryMap () |
| 305 | |
{ |
| 306 | 0 | println (""); |
| 307 | 0 | incrementIndent (); |
| 308 | 0 | writeComment ("Query Map"); |
| 309 | 0 | indentPrintln ("<bean id=\"queryMap\" class=\"org.springframework.beans.factory.config.MapFactoryBean\">"); |
| 310 | 0 | indentPrintln ("<property name=\"sourceMap\">"); |
| 311 | 0 | indentPrintln ("<map>"); |
| 312 | 0 | incrementIndent (); |
| 313 | 0 | for (SearchImplementation impl : getJPQLImplementations ()) |
| 314 | |
{ |
| 315 | 0 | println (""); |
| 316 | 0 | indentPrint ("<entry"); |
| 317 | 0 | writeAttribute ("key", impl.getKey ()); |
| 318 | 0 | indentPrintln (">"); |
| 319 | 0 | writeComment (impl.getComments ()); |
| 320 | 0 | incrementIndent (); |
| 321 | 0 | writeTag ("value", "\n" + impl.getDescription () + "\n"); |
| 322 | 0 | decrementIndent (); |
| 323 | 0 | indentPrintln ("</entry>"); |
| 324 | |
} |
| 325 | 0 | decrementIndent (); |
| 326 | 0 | indentPrintln ("</map>"); |
| 327 | 0 | indentPrintln ("</property>"); |
| 328 | 0 | indentPrintln ("</bean>"); |
| 329 | 0 | decrementIndent (); |
| 330 | 0 | } |
| 331 | |
|
| 332 | |
private List<SearchImplementation> getImplementations () |
| 333 | |
{ |
| 334 | 0 | List<SearchImplementation> list = new ArrayList (); |
| 335 | 0 | Set<String> keys = new HashSet (); |
| 336 | 0 | for (SearchType st : model.getSearchTypes ()) |
| 337 | |
{ |
| 338 | 0 | if (keys.add (st.getImplementation ().getKey ())) |
| 339 | |
{ |
| 340 | 0 | list.add (st.getImplementation ()); |
| 341 | |
} |
| 342 | |
} |
| 343 | 0 | return list; |
| 344 | |
} |
| 345 | |
|
| 346 | |
private List<SearchImplementation> getJPQLImplementations () |
| 347 | |
{ |
| 348 | 0 | List<SearchImplementation> list = new ArrayList (); |
| 349 | 0 | Set<String> keys = new HashSet (); |
| 350 | 0 | for (SearchImplementation impl : getImplementations ()) |
| 351 | |
{ |
| 352 | 0 | if (impl.getType ().equals ("JPQL")) |
| 353 | |
{ |
| 354 | 0 | list.add (impl); |
| 355 | |
} |
| 356 | |
} |
| 357 | 0 | return list; |
| 358 | |
} |
| 359 | |
|
| 360 | |
} |