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