001 /* 002 * Copyright 2011 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.osedu.org/licenses/ECL-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.student.contract.model.util; 017 018 import java.util.ArrayList; 019 import java.util.Collections; 020 import java.util.Comparator; 021 import java.util.Enumeration; 022 import java.util.List; 023 import java.util.Stack; 024 025 import org.kuali.student.contract.model.MessageStructure; 026 import org.kuali.student.contract.model.Service; 027 import org.kuali.student.contract.model.ServiceContractModel; 028 import org.kuali.student.contract.model.XmlType; 029 import org.kuali.student.contract.writer.HtmlWriter; 030 031 /** 032 * 033 * @author nwright 034 */ 035 public class HtmlContractWriter { 036 037 private HtmlWriter writer; 038 private ServiceContractModel model; 039 private ModelFinder finder; 040 private String directory; 041 042 public HtmlContractWriter(String directory, 043 ServiceContractModel model) { 044 this.writer = new HtmlWriter(directory, "index.html", 045 "Service Contracts Index"); 046 this.directory = directory; 047 this.model = model; 048 this.finder = new ModelFinder(this.model); 049 } 050 051 public void write() { 052 this.writeIndexPage(); 053 for (Service service : model.getServices()) { 054 HtmlContractServiceWriter swriter = new HtmlContractServiceWriter(service, 055 directory, 056 model); 057 swriter.write(); 058 } 059 for (XmlType xmlType : model.getXmlTypes()) { 060 HtmlContractMessageStructureWriter msWriter = 061 new HtmlContractMessageStructureWriter( 062 xmlType, 063 directory, 064 model); 065 msWriter.write(); 066 } 067 } 068 private static final Comparator<XmlType> XML_TYPE_NAME_COMPARATOR = 069 new Comparator<XmlType>() { 070 071 @Override 072 public int compare(XmlType e1, XmlType e2) { 073 return e1.getName().toLowerCase().compareTo(e2.getName().toLowerCase()); 074 } 075 }; 076 private static final Comparator<Service> SERVICE_NAME_COMPARATOR = 077 new Comparator<Service>() { 078 079 @Override 080 public int compare(Service e1, Service e2) { 081 return e1.getName().compareTo(e2.getName()); 082 } 083 }; 084 private static final Comparator<Service> SERVICE_IMPL_NAME_COMPARATOR = 085 new Comparator<Service>() { 086 087 @Override 088 public int compare(Service e1, Service e2) { 089 String x1 = calcArea(e1) + "." + e1.getName(); 090 String x2 = calcArea(e2) + "." + e2.getName(); 091 return x1.compareTo(x2); 092 } 093 }; 094 095 private void writeIndexPage() { 096 writer.print("<a href=\"index.html\">home</a>"); 097 writer.writeTag("h1", "Service Contracts"); 098 writer.indentPrintln( 099 "<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 }