001 /** 002 * Copyright 2004-2014 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.opensource.org/licenses/ecl2.php 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.Iterator; 023 import java.util.LinkedHashSet; 024 import java.util.List; 025 import java.util.Set; 026 import java.util.Stack; 027 028 import org.kuali.student.contract.model.MessageStructure; 029 import org.kuali.student.contract.model.Service; 030 import org.kuali.student.contract.model.ServiceContractModel; 031 import org.kuali.student.contract.model.XmlType; 032 import org.kuali.student.contract.writer.HtmlWriter; 033 import org.slf4j.Logger; 034 import org.slf4j.LoggerFactory; 035 036 /** 037 * 038 * @author nwright 039 */ 040 public class HtmlContractWriter { 041 042 private static final Logger log = LoggerFactory.getLogger(HtmlContractWriter.class); 043 044 private HtmlWriter writer; 045 private ServiceContractModel model; 046 private ModelFinder finder; 047 private String directory; 048 049 public HtmlContractWriter(String directory, 050 ServiceContractModel model) { 051 this.writer = new HtmlWriter(directory, "index.html", 052 "Service Contracts Index"); 053 this.directory = directory; 054 this.model = model; 055 this.finder = new ModelFinder(this.model); 056 } 057 058 public void write(String projectVersion, String formattedDate) { 059 this.writeIndexPage(projectVersion, formattedDate); 060 for (Service service : model.getServices()) { 061 HtmlContractServiceWriter swriter = new HtmlContractServiceWriter(service, 062 directory, 063 model); 064 swriter.write(projectVersion, formattedDate); 065 } 066 for (XmlType xmlType : model.getXmlTypes()) { 067 HtmlContractMessageStructureWriter msWriter = 068 new HtmlContractMessageStructureWriter( 069 xmlType, 070 directory, 071 model); 072 msWriter.write(projectVersion, formattedDate); 073 } 074 } 075 private static final Comparator<XmlType> XML_TYPE_NAME_COMPARATOR = 076 new Comparator<XmlType>() { 077 078 @Override 079 public int compare(XmlType e1, XmlType e2) { 080 return e1.getName().toLowerCase().compareTo(e2.getName().toLowerCase()); 081 } 082 }; 083 private static final Comparator<Service> SERVICE_NAME_COMPARATOR = 084 new Comparator<Service>() { 085 086 @Override 087 public int compare(Service e1, Service e2) { 088 return e1.getName().compareTo(e2.getName()); 089 } 090 }; 091 private static final Comparator<Service> SERVICE_IMPL_NAME_COMPARATOR = 092 new Comparator<Service>() { 093 094 @Override 095 public int compare(Service e1, Service e2) { 096 String x1 = calcArea(e1) + "." + e1.getName(); 097 String x2 = calcArea(e2) + "." + e2.getName(); 098 return x1.compareTo(x2); 099 } 100 }; 101 102 private String getOtherHomeTag () { 103 return "<a href=\"../dictionarydocs/index.html\">Dictionary Docs Home</a>"; 104 // return ""; 105 } 106 private void writeIndexPage(String projectVersion, String formattedDate) { 107 108 String otherHome = this.getOtherHomeTag(); 109 VersionLinesUtility.writeVersionTag(writer, "<a href=\"index.html\">Home</a>", otherHome, projectVersion, formattedDate); 110 111 writer.writeTag("h1", "Service Contracts"); 112 113 114 115 writer.indentPrintln( 116 "<div class=\"panel\" style=\"background-color: rgb(255, 255, 255); border: 1px solid rgb(204, 204, 204);\">"); 117 writer.indentPrintln( 118 "<div class=\"panelHeader\" style=\"border-bottom: 1px solid rgb(204, 204, 204); background-color: rgb(238, 238, 238);\">"); 119 writer.indentPrintln("<b><a name=\"Services\"></a>Services</b>"); 120 writer.indentPrintln( 121 "</div><div class=\"panelContent\" style=\"background-color: rgb(255, 255, 255);\">"); 122 writer.indentPrintln("<ul>"); 123 List<Service> services = new ArrayList<Service>(model.getServices()); 124 Collections.sort(services, SERVICE_IMPL_NAME_COMPARATOR); 125 String oldArea = ""; 126 for (Service service : services) { 127 String newArea = calcArea(service); 128 if (!newArea.equals(oldArea)) { 129 if (!oldArea.isEmpty()) { 130 writer.indentPrintln("</ul>"); 131 writer.decrementIndent(); 132 } 133 writer.indentPrintln("<li>" + calcArea(service) + "</li>"); 134 writer.incrementIndent(); 135 writer.indentPrintln("<ul>"); 136 oldArea = newArea; 137 } 138 writer.indentPrint("<li>"); 139 writer.print("<a href=\"" + service.getKey() + "Service" + ".html" 140 + "\">" + service.getName() + "</a>"); 141 writer.print("</li>"); 142 } 143 writer.indentPrintln("</ul>"); 144 writer.decrementIndent(); 145 writer.indentPrintln("</ul>"); 146 writer.indentPrintln("</div>"); 147 writer.indentPrintln("</div>"); 148 149 this.writeMainOrRootList(); 150 151 this.writeAlphabeticalList(); 152 153 writer.writeHeaderBodyAndFooterOutToFile(); 154 155 } 156 157 private static String calcArea(Service service) { 158 return calcArea(service.getImplProject()); 159 } 160 161 private static String calcArea(String implProject) { 162 // group all student services together 163 if (implProject.startsWith("org.kuali.student.")) { 164 implProject = implProject.substring("org.kuali.student.".length()); 165 // return "Kuali Student Services"; 166 } 167 if (implProject.startsWith("org.kuali.")) { 168 implProject = implProject.substring("org.kuali.".length()); 169 } 170 if (implProject.contains(".api.")) { 171 implProject = implProject.substring(0, implProject.indexOf(".api.")); 172 } 173 if (implProject.contains(".api.")) { 174 implProject = implProject.substring(0, implProject.indexOf(".api.")); 175 } 176 if (implProject.startsWith("r2.")) { 177 implProject = implProject.substring("r2.".length()); 178 } 179 if (implProject.contains(".class1.")) { 180 implProject = implProject.substring(0, implProject.indexOf(".class1.")) + 181 implProject.substring(implProject.indexOf(".class1.") + ".class1".length()); 182 } 183 if (implProject.endsWith(".service")) { 184 implProject = implProject.substring(0, implProject.length() - ".service".length()); 185 } 186 return implProject; 187 } 188 189 private void writeMainOrRootList() { 190 Stack<String> stack = new Stack(); 191 List<XmlType> types = this.getMainMessageStructures(); 192 writer.indentPrintln( 193 "<div class=\"panel\" style=\"background-color: rgb(255, 255, 255); border: 1px solid rgb(204, 204, 204);\">"); 194 writer.indentPrintln( 195 "<div class=\"panelHeader\" style=\"border-bottom: 1px solid rgb(204, 204, 204); background-color: rgb(238, 238, 238);\">"); 196 writer.indentPrintln( 197 "<b><a name=\"MessageStructures\"></a> " + types.size() + " Main (root) Message Structures</b>"); 198 writer.indentPrintln( 199 "</div><div class=\"panelContent\" style=\"background-color: rgb(255, 255, 255);\">"); 200 writer.indentPrintln("<ul>"); 201 for (XmlType type : types) { 202 this.writeLink(type); 203 if (!stack.contains(type.getName())) { 204 stack.push(type.getName()); 205 this.writeSubStructures(type, stack); 206 stack.pop(); 207 } 208 } 209 writer.indentPrintln("</ul>"); 210 writer.indentPrintln("</div>"); 211 writer.indentPrintln("</div>"); 212 } 213 214 private String stripListOffEnd(String name) { 215 if (name.endsWith("List")) { 216 return name.substring(0, name.length() - "List".length()); 217 } 218 return name; 219 } 220 221 private boolean shouldWriteSubStructure(XmlType st) { 222 if (!st.getPrimitive().equalsIgnoreCase(XmlType.COMPLEX)) { 223 return false; 224 } 225 return true; 226 } 227 228 private void writeSubStructures(XmlType type, Stack<String> stack) { 229 boolean first = true; 230 for (MessageStructure ms : finder.findMessageStructures(type.getName())) { 231 XmlType st = finder.findXmlType(this.stripListOffEnd(ms.getType())); 232 if (st == null) { 233 log.error (ms.getType() + " does not exist in the list of types with parents " + calcParents(stack)); 234 continue; 235 } 236 if (!shouldWriteSubStructure (st)) { 237 continue; 238 } 239 if (first) { 240 first = false; 241 writer.indentPrintln("<ul>"); 242 } 243 this.writeLink(st); 244 if (!stack.contains(st.getName())) { 245 stack.push(st.getName()); 246 this.writeSubStructures(st, stack); 247 stack.pop(); 248 } 249 } 250 if (!first) { 251 writer.indentPrintln("</ul>"); 252 } 253 } 254 255 private String calcParents(Stack<String> stack) { 256 StringBuilder sb = new StringBuilder(); 257 String dot = ""; 258 Enumeration<String> en = stack.elements(); 259 while (en.hasMoreElements()) { 260 sb.append(dot); 261 dot = "."; 262 sb.append(en.nextElement()); 263 } 264 return sb.toString(); 265 } 266 267 private void writeLink(XmlType type) { 268 writer.indentPrint("<li>"); 269 writer.print("<a href=\"" + type.getName() + ".html" 270 + "\">" + type.getName() + "</a>"); 271 writer.print("</li>"); 272 } 273 274 private List<XmlType> getMainMessageStructures() { 275 List<XmlType> types = new ArrayList(model.getXmlTypes().size()); 276 for (XmlType type : this.getComplexMessageStructures()) { 277 if (isMainMessageStructure(type)) { 278 types.add(type); 279 } 280 } 281 Collections.sort(types, XML_TYPE_NAME_COMPARATOR); 282 return types; 283 } 284 285 private boolean isMainMessageStructure(XmlType xmlType) { 286 if (!HtmlContractMessageStructureWriter.calcOtherXmlTypeUsages(model, 287 xmlType).isEmpty()) { 288 return false; 289 } 290 return true; 291 } 292 293 private List<XmlType> getComplexMessageStructures() { 294 List<XmlType> types = new ArrayList(model.getXmlTypes().size()); 295 for (XmlType type : model.getXmlTypes()) { 296 if (type.getPrimitive() == null) { 297 throw new NullPointerException(type.getName() 298 + " has no primitive flag set"); 299 } 300 if (type.getPrimitive().equals(XmlType.COMPLEX)) { 301 types.add(type); 302 } 303 } 304 Collections.sort(types, XML_TYPE_NAME_COMPARATOR); 305 return types; 306 } 307 308 private void writeAlphabeticalList() { 309 List<XmlType> types = this.getComplexMessageStructures(); 310 writer.indentPrintln( 311 "<div class=\"panel\" style=\"background-color: rgb(255, 255, 255); border: 1px solid rgb(204, 204, 204);\">"); 312 writer.indentPrintln( 313 "<div class=\"panelHeader\" style=\"border-bottom: 1px solid rgb(204, 204, 204); background-color: rgb(238, 238, 238);\">"); 314 writer.indentPrintln( 315 "<b><a name=\"MessageStructures\"></a>All " + types.size() + " Message Structures in Alphabetical Order</b>"); 316 writer.indentPrintln( 317 "</div><div class=\"panelContent\" style=\"background-color: rgb(255, 255, 255);\">"); 318 writer.indentPrintln("<ul>"); 319 for (XmlType type : types) { 320 this.writeLink(type); 321 } 322 writer.indentPrintln("</ul>"); 323 writer.indentPrintln("</div>"); 324 writer.indentPrintln("</div>"); 325 } 326 }