View Javadoc

1   /*
2    * Copyright 2011 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.model.util;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.Comparator;
21  import java.util.Enumeration;
22  import java.util.Iterator;
23  import java.util.LinkedHashSet;
24  import java.util.List;
25  import java.util.Set;
26  import java.util.Stack;
27  
28  import org.kuali.student.contract.model.MessageStructure;
29  import org.kuali.student.contract.model.Service;
30  import org.kuali.student.contract.model.ServiceContractModel;
31  import org.kuali.student.contract.model.XmlType;
32  import org.kuali.student.contract.writer.HtmlWriter;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  /**
37   *
38   * @author nwright
39   */
40  public class HtmlContractWriter {
41  	
42  	private static final Logger log = LoggerFactory.getLogger(HtmlContractWriter.class);
43  
44      private HtmlWriter writer;
45      private ServiceContractModel model;
46      private ModelFinder finder;
47      private String directory;
48  
49      public HtmlContractWriter(String directory,
50              ServiceContractModel model) {
51          this.writer = new HtmlWriter(directory, "index.html",
52                  "Service Contracts Index");
53          this.directory = directory;
54          this.model = model;
55          this.finder = new ModelFinder(this.model);
56      }
57  
58      public void write(String projectVersion, String formattedDate) {
59          this.writeIndexPage(projectVersion, formattedDate);
60          for (Service service : model.getServices()) {
61              HtmlContractServiceWriter swriter = new HtmlContractServiceWriter(service,
62                      directory,
63                      model);
64              swriter.write(projectVersion, formattedDate);
65          }
66          for (XmlType xmlType : model.getXmlTypes()) {
67              HtmlContractMessageStructureWriter msWriter =
68                      new HtmlContractMessageStructureWriter(
69                      xmlType,
70                      directory,
71                      model);
72              msWriter.write(projectVersion, formattedDate);
73          }
74      }
75      private static final Comparator<XmlType> XML_TYPE_NAME_COMPARATOR =
76              new Comparator<XmlType>() {
77  
78                  @Override
79                  public int compare(XmlType e1, XmlType e2) {
80                      return e1.getName().toLowerCase().compareTo(e2.getName().toLowerCase());
81                  }
82              };
83      private static final Comparator<Service> SERVICE_NAME_COMPARATOR =
84              new Comparator<Service>() {
85  
86                  @Override
87                  public int compare(Service e1, Service e2) {
88                      return e1.getName().compareTo(e2.getName());
89                  }
90              };
91      private static final Comparator<Service> SERVICE_IMPL_NAME_COMPARATOR =
92              new Comparator<Service>() {
93  
94                  @Override
95                  public int compare(Service e1, Service e2) {
96                      String x1 = calcArea(e1) + "." + e1.getName();
97                      String x2 = calcArea(e2) + "." + e2.getName();
98                      return x1.compareTo(x2);
99                  }
100             };
101 
102     private void writeIndexPage(String projectVersion, String formattedDate) {
103         
104         VersionLinesUtility.writeVersionTag(writer, "<a href=\"index.html\">Home</a>", "<a href=\"../dictionarydocs/index.html\">Dictionary Docs Home</a>", projectVersion, formattedDate);
105         
106         writer.writeTag("h1", "Service Contracts");
107         
108         
109 
110         writer.indentPrintln(
111                 "<div class=\"panel\" style=\"background-color: rgb(255, 255, 255); border: 1px solid rgb(204, 204, 204);\">");
112         writer.indentPrintln(
113                 "<div class=\"panelHeader\" style=\"border-bottom: 1px solid rgb(204, 204, 204); background-color: rgb(238, 238, 238);\">");
114         writer.indentPrintln("<b><a name=\"Services\"></a>Services</b>");
115         writer.indentPrintln(
116                 "</div><div class=\"panelContent\" style=\"background-color: rgb(255, 255, 255);\">");
117         writer.indentPrintln("<ul>");
118         List<Service> services = new ArrayList<Service>(model.getServices());
119         Collections.sort(services, SERVICE_IMPL_NAME_COMPARATOR);
120         String oldArea = "";
121         for (Service service : services) {
122             String newArea = calcArea(service);
123             if (!newArea.equals(oldArea)) {
124                 if (!oldArea.isEmpty()) {
125                     writer.indentPrintln("</ul>");
126                     writer.decrementIndent();
127                 }
128                 writer.indentPrintln("<li>" + calcArea(service) + "</li>");
129                 writer.incrementIndent();
130                 writer.indentPrintln("<ul>");
131                 oldArea = newArea;
132             }
133             writer.indentPrint("<li>");
134             writer.print("<a href=\"" + service.getKey() + "Service" + ".html"
135                     + "\">" + service.getName() + "</a>");
136             writer.print("</li>");
137         }
138         writer.indentPrintln("</ul>");
139         writer.decrementIndent();
140         writer.indentPrintln("</ul>");        
141         writer.indentPrintln("</div>");
142         writer.indentPrintln("</div>");
143 
144         this.writeMainOrRootList();
145 
146         this.writeAlphabeticalList();
147 
148         writer.writeHeaderBodyAndFooterOutToFile();
149 
150     }
151 
152     private static String calcArea(Service service) {
153         return calcArea(service.getImplProject());
154     }
155 
156     private static String calcArea(String implProject) {
157         // group all student services together
158         if (implProject.startsWith("org.kuali.student")) {
159             return "Kuali Student Services";
160         }
161         if (implProject.startsWith("org.kuali.")) {
162             implProject = implProject.substring("org.kuali.".length());
163         }
164         if (implProject.contains(".api.")) {
165             implProject = implProject.substring(0, implProject.indexOf(".api."));
166         }
167         return implProject;
168     }
169 
170     private void writeMainOrRootList() {
171         Stack<String> stack = new Stack();
172         List<XmlType> types = this.getMainMessageStructures();
173         writer.indentPrintln(
174                 "<div class=\"panel\" style=\"background-color: rgb(255, 255, 255); border: 1px solid rgb(204, 204, 204);\">");
175         writer.indentPrintln(
176                 "<div class=\"panelHeader\" style=\"border-bottom: 1px solid rgb(204, 204, 204); background-color: rgb(238, 238, 238);\">");
177         writer.indentPrintln(
178                 "<b><a name=\"MessageStructures\"></a> " + types.size() + " Main (root) Message Structures</b>");
179         writer.indentPrintln(
180                 "</div><div class=\"panelContent\" style=\"background-color: rgb(255, 255, 255);\">");
181         writer.indentPrintln("<ul>");
182         for (XmlType type : types) {
183             this.writeLink(type);
184             if (!stack.contains(type.getName())) {
185                 stack.push(type.getName());
186                 this.writeSubStructures(type, stack);
187                 stack.pop();
188             }
189         }
190         writer.indentPrintln("</ul>");
191         writer.indentPrintln("</div>");
192         writer.indentPrintln("</div>");
193     }
194 
195     private String stripListOffEnd(String name) {
196         if (name.endsWith("List")) {
197             return name.substring(0, name.length() - "List".length());
198         }
199         return name;
200     }
201 
202     private void writeSubStructures(XmlType type, Stack<String> stack) {
203         boolean first = true;
204         for (MessageStructure ms : finder.findMessageStructures(type.getName())) {
205             XmlType st = finder.findXmlType(this.stripListOffEnd(ms.getType()));
206             if (st == null) {
207             	log.error (ms.getType() + " does not exist in the list of types with parents " + calcParents(stack));
208             	continue;
209             }
210             if (!st.getPrimitive().equalsIgnoreCase(XmlType.COMPLEX)) {
211                 continue;
212             }
213             if (first) {
214                 first = false;
215                 writer.indentPrintln("<ul>");
216             }
217             this.writeLink(st);
218             if (!stack.contains(st.getName())) {
219                 stack.push(st.getName());
220                 this.writeSubStructures(st, stack);
221                 stack.pop();
222             }
223         }
224         if (!first) {
225             writer.indentPrintln("</ul>");
226         }
227     }
228 
229     private String calcParents(Stack<String> stack) {
230         StringBuilder sb = new StringBuilder();
231         String dot = "";
232         Enumeration<String> en = stack.elements();
233         while (en.hasMoreElements()) {
234             sb.append(dot);
235             dot = ".";
236             sb.append(en.nextElement());
237         }
238         return sb.toString();
239     }
240 
241     private void writeLink(XmlType type) {
242         writer.indentPrint("<li>");
243         writer.print("<a href=\"" + type.getName() + ".html"
244                 + "\">" + type.getName() + "</a>");
245         writer.print("</li>");
246     }
247 
248     private List<XmlType> getMainMessageStructures() {
249         List<XmlType> types = new ArrayList(model.getXmlTypes().size());
250         for (XmlType type : this.getComplexMessageStructures()) {
251             if (isMainMessageStructure(type)) {
252                 types.add(type);
253             }
254         }
255         Collections.sort(types, XML_TYPE_NAME_COMPARATOR);
256         return types;
257     }
258 
259     private boolean isMainMessageStructure(XmlType xmlType) {
260         if (!HtmlContractMessageStructureWriter.calcOtherXmlTypeUsages(model,
261                 xmlType).isEmpty()) {
262             return false;
263         }
264         return true;
265     }
266 
267     private List<XmlType> getComplexMessageStructures() {
268         List<XmlType> types = new ArrayList(model.getXmlTypes().size());
269         for (XmlType type : model.getXmlTypes()) {
270             if (type.getPrimitive() == null) {
271                 throw new NullPointerException(type.getName()
272                         + " has no primitive flag set");
273             }
274             if (type.getPrimitive().equals(XmlType.COMPLEX)) {
275                 types.add(type);
276             }
277         }
278         Collections.sort(types, XML_TYPE_NAME_COMPARATOR);
279         return types;
280     }
281 
282     private void writeAlphabeticalList() {
283         List<XmlType> types = this.getComplexMessageStructures();
284         writer.indentPrintln(
285                 "<div class=\"panel\" style=\"background-color: rgb(255, 255, 255); border: 1px solid rgb(204, 204, 204);\">");
286         writer.indentPrintln(
287                 "<div class=\"panelHeader\" style=\"border-bottom: 1px solid rgb(204, 204, 204); background-color: rgb(238, 238, 238);\">");
288         writer.indentPrintln(
289                 "<b><a name=\"MessageStructures\"></a>All " + types.size() + " Message Structures in Alphabetical Order</b>");
290         writer.indentPrintln(
291                 "</div><div class=\"panelContent\" style=\"background-color: rgb(255, 255, 255);\">");
292         writer.indentPrintln("<ul>");
293         for (XmlType type : types) {
294             this.writeLink(type);
295         }
296         writer.indentPrintln("</ul>");
297         writer.indentPrintln("</div>");
298         writer.indentPrintln("</div>");
299     }
300 }