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