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