View Javadoc

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