
org.kuali.student.contract.model.util.HtmlContractWriter Maven / Gradle / Ivy
/*
* Copyright 2011 The Kuali Foundation
*
* Licensed under the Educational Community License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.osedu.org/licenses/ECL-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kuali.student.contract.model.util;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.Stack;
import org.kuali.student.contract.model.MessageStructure;
import org.kuali.student.contract.model.Service;
import org.kuali.student.contract.model.ServiceContractModel;
import org.kuali.student.contract.model.XmlType;
import org.kuali.student.contract.writer.HtmlWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author nwright
*/
public class HtmlContractWriter {
private static final Logger log = LoggerFactory.getLogger(HtmlContractWriter.class);
private HtmlWriter writer;
private ServiceContractModel model;
private ModelFinder finder;
private String directory;
public HtmlContractWriter(String directory,
ServiceContractModel model) {
this.writer = new HtmlWriter(directory, "index.html",
"Service Contracts Index");
this.directory = directory;
this.model = model;
this.finder = new ModelFinder(this.model);
}
public void write(String projectVersion, String formattedDate) {
this.writeIndexPage(projectVersion, formattedDate);
for (Service service : model.getServices()) {
HtmlContractServiceWriter swriter = new HtmlContractServiceWriter(service,
directory,
model);
swriter.write(projectVersion, formattedDate);
}
for (XmlType xmlType : model.getXmlTypes()) {
HtmlContractMessageStructureWriter msWriter =
new HtmlContractMessageStructureWriter(
xmlType,
directory,
model);
msWriter.write(projectVersion, formattedDate);
}
}
private static final Comparator XML_TYPE_NAME_COMPARATOR =
new Comparator() {
@Override
public int compare(XmlType e1, XmlType e2) {
return e1.getName().toLowerCase().compareTo(e2.getName().toLowerCase());
}
};
private static final Comparator SERVICE_NAME_COMPARATOR =
new Comparator() {
@Override
public int compare(Service e1, Service e2) {
return e1.getName().compareTo(e2.getName());
}
};
private static final Comparator SERVICE_IMPL_NAME_COMPARATOR =
new Comparator() {
@Override
public int compare(Service e1, Service e2) {
String x1 = calcArea(e1) + "." + e1.getName();
String x2 = calcArea(e2) + "." + e2.getName();
return x1.compareTo(x2);
}
};
private void writeIndexPage(String projectVersion, String formattedDate) {
VersionLinesUtility.writeVersionTag(writer, "Home", "Dictionary Docs Home", projectVersion, formattedDate);
writer.writeTag("h1", "Service Contracts");
writer.indentPrintln(
"");
writer.indentPrintln(
"");
writer.indentPrintln("");
List services = new ArrayList(model.getServices());
Collections.sort(services, SERVICE_IMPL_NAME_COMPARATOR);
String oldArea = "";
for (Service service : services) {
String newArea = calcArea(service);
if (!newArea.equals(oldArea)) {
if (!oldArea.isEmpty()) {
writer.indentPrintln("
");
writer.decrementIndent();
}
writer.indentPrintln("" + calcArea(service) + " ");
writer.incrementIndent();
writer.indentPrintln("");
oldArea = newArea;
}
writer.indentPrint("- ");
writer.print("" + service.getName() + "");
writer.print("
");
}
writer.indentPrintln("
");
writer.decrementIndent();
writer.indentPrintln("");
writer.indentPrintln("");
writer.indentPrintln("");
this.writeMainOrRootList();
this.writeAlphabeticalList();
writer.writeHeaderBodyAndFooterOutToFile();
}
private static String calcArea(Service service) {
return calcArea(service.getImplProject());
}
private static String calcArea(String implProject) {
// group all student services together
if (implProject.startsWith("org.kuali.student")) {
return "Kuali Student Services";
}
if (implProject.startsWith("org.kuali.")) {
implProject = implProject.substring("org.kuali.".length());
}
if (implProject.contains(".api.")) {
implProject = implProject.substring(0, implProject.indexOf(".api."));
}
return implProject;
}
private void writeMainOrRootList() {
Stack stack = new Stack();
List types = this.getMainMessageStructures();
writer.indentPrintln(
"");
writer.indentPrintln(
"");
writer.indentPrintln(
" " + types.size() + " Main (root) Message Structures");
writer.indentPrintln(
"");
writer.indentPrintln("");
for (XmlType type : types) {
this.writeLink(type);
if (!stack.contains(type.getName())) {
stack.push(type.getName());
this.writeSubStructures(type, stack);
stack.pop();
}
}
writer.indentPrintln("
");
writer.indentPrintln("");
writer.indentPrintln("");
}
private String stripListOffEnd(String name) {
if (name.endsWith("List")) {
return name.substring(0, name.length() - "List".length());
}
return name;
}
private void writeSubStructures(XmlType type, Stack stack) {
boolean first = true;
for (MessageStructure ms : finder.findMessageStructures(type.getName())) {
XmlType st = finder.findXmlType(this.stripListOffEnd(ms.getType()));
if (st == null) {
log.error (ms.getType() + " does not exist in the list of types with parents " + calcParents(stack));
continue;
}
if (!st.getPrimitive().equalsIgnoreCase(XmlType.COMPLEX)) {
continue;
}
if (first) {
first = false;
writer.indentPrintln("");
}
this.writeLink(st);
if (!stack.contains(st.getName())) {
stack.push(st.getName());
this.writeSubStructures(st, stack);
stack.pop();
}
}
if (!first) {
writer.indentPrintln("
");
}
}
private String calcParents(Stack stack) {
StringBuilder sb = new StringBuilder();
String dot = "";
Enumeration en = stack.elements();
while (en.hasMoreElements()) {
sb.append(dot);
dot = ".";
sb.append(en.nextElement());
}
return sb.toString();
}
private void writeLink(XmlType type) {
writer.indentPrint("");
writer.print("" + type.getName() + "");
writer.print(" ");
}
private List getMainMessageStructures() {
List types = new ArrayList(model.getXmlTypes().size());
for (XmlType type : this.getComplexMessageStructures()) {
if (isMainMessageStructure(type)) {
types.add(type);
}
}
Collections.sort(types, XML_TYPE_NAME_COMPARATOR);
return types;
}
private boolean isMainMessageStructure(XmlType xmlType) {
if (!HtmlContractMessageStructureWriter.calcOtherXmlTypeUsages(model,
xmlType).isEmpty()) {
return false;
}
return true;
}
private List getComplexMessageStructures() {
List types = new ArrayList(model.getXmlTypes().size());
for (XmlType type : model.getXmlTypes()) {
if (type.getPrimitive() == null) {
throw new NullPointerException(type.getName()
+ " has no primitive flag set");
}
if (type.getPrimitive().equals(XmlType.COMPLEX)) {
types.add(type);
}
}
Collections.sort(types, XML_TYPE_NAME_COMPARATOR);
return types;
}
private void writeAlphabeticalList() {
List types = this.getComplexMessageStructures();
writer.indentPrintln(
"");
writer.indentPrintln(
"");
writer.indentPrintln(
"All " + types.size() + " Message Structures in Alphabetical Order");
writer.indentPrintln(
"");
writer.indentPrintln("");
for (XmlType type : types) {
this.writeLink(type);
}
writer.indentPrintln("
");
writer.indentPrintln("");
writer.indentPrintln("");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy