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.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 void writeIndexPage(String projectVersion, String formattedDate) {
103            
104            VersionLinesUtility.writeVersionTag(writer, "<a href=\"index.html\">Home</a>", "<a href=\"../dictionarydocs/index.html\">Dictionary Docs Home</a>", projectVersion, formattedDate);
105            
106            writer.writeTag("h1", "Service Contracts");
107            
108            
109    
110            writer.indentPrintln(
111                    "<div class=\"panel\" style=\"background-color: rgb(255, 255, 255); border: 1px solid rgb(204, 204, 204);\">");
112            writer.indentPrintln(
113                    "<div class=\"panelHeader\" style=\"border-bottom: 1px solid rgb(204, 204, 204); background-color: rgb(238, 238, 238);\">");
114            writer.indentPrintln("<b><a name=\"Services\"></a>Services</b>");
115            writer.indentPrintln(
116                    "</div><div class=\"panelContent\" style=\"background-color: rgb(255, 255, 255);\">");
117            writer.indentPrintln("<ul>");
118            List<Service> services = new ArrayList<Service>(model.getServices());
119            Collections.sort(services, SERVICE_IMPL_NAME_COMPARATOR);
120            String oldArea = "";
121            for (Service service : services) {
122                String newArea = calcArea(service);
123                if (!newArea.equals(oldArea)) {
124                    if (!oldArea.isEmpty()) {
125                        writer.indentPrintln("</ul>");
126                        writer.decrementIndent();
127                    }
128                    writer.indentPrintln("<li>" + calcArea(service) + "</li>");
129                    writer.incrementIndent();
130                    writer.indentPrintln("<ul>");
131                    oldArea = newArea;
132                }
133                writer.indentPrint("<li>");
134                writer.print("<a href=\"" + service.getKey() + "Service" + ".html"
135                        + "\">" + service.getName() + "</a>");
136                writer.print("</li>");
137            }
138            writer.indentPrintln("</ul>");
139            writer.decrementIndent();
140            writer.indentPrintln("</ul>");        
141            writer.indentPrintln("</div>");
142            writer.indentPrintln("</div>");
143    
144            this.writeMainOrRootList();
145    
146            this.writeAlphabeticalList();
147    
148            writer.writeHeaderBodyAndFooterOutToFile();
149    
150        }
151    
152        private static String calcArea(Service service) {
153            return calcArea(service.getImplProject());
154        }
155    
156        private static String calcArea(String implProject) {
157            // group all student services together
158            if (implProject.startsWith("org.kuali.student")) {
159                return "Kuali Student Services";
160            }
161            if (implProject.startsWith("org.kuali.")) {
162                implProject = implProject.substring("org.kuali.".length());
163            }
164            if (implProject.contains(".api.")) {
165                implProject = implProject.substring(0, implProject.indexOf(".api."));
166            }
167            return implProject;
168        }
169    
170        private void writeMainOrRootList() {
171            Stack<String> stack = new Stack();
172            List<XmlType> types = this.getMainMessageStructures();
173            writer.indentPrintln(
174                    "<div class=\"panel\" style=\"background-color: rgb(255, 255, 255); border: 1px solid rgb(204, 204, 204);\">");
175            writer.indentPrintln(
176                    "<div class=\"panelHeader\" style=\"border-bottom: 1px solid rgb(204, 204, 204); background-color: rgb(238, 238, 238);\">");
177            writer.indentPrintln(
178                    "<b><a name=\"MessageStructures\"></a> " + types.size() + " Main (root) Message Structures</b>");
179            writer.indentPrintln(
180                    "</div><div class=\"panelContent\" style=\"background-color: rgb(255, 255, 255);\">");
181            writer.indentPrintln("<ul>");
182            for (XmlType type : types) {
183                this.writeLink(type);
184                if (!stack.contains(type.getName())) {
185                    stack.push(type.getName());
186                    this.writeSubStructures(type, stack);
187                    stack.pop();
188                }
189            }
190            writer.indentPrintln("</ul>");
191            writer.indentPrintln("</div>");
192            writer.indentPrintln("</div>");
193        }
194    
195        private String stripListOffEnd(String name) {
196            if (name.endsWith("List")) {
197                return name.substring(0, name.length() - "List".length());
198            }
199            return name;
200        }
201    
202        private void writeSubStructures(XmlType type, Stack<String> stack) {
203            boolean first = true;
204            for (MessageStructure ms : finder.findMessageStructures(type.getName())) {
205                XmlType st = finder.findXmlType(this.stripListOffEnd(ms.getType()));
206                if (st == null) {
207                    log.error (ms.getType() + " does not exist in the list of types with parents " + calcParents(stack));
208                    continue;
209                }
210                if (!st.getPrimitive().equalsIgnoreCase(XmlType.COMPLEX)) {
211                    continue;
212                }
213                if (first) {
214                    first = false;
215                    writer.indentPrintln("<ul>");
216                }
217                this.writeLink(st);
218                if (!stack.contains(st.getName())) {
219                    stack.push(st.getName());
220                    this.writeSubStructures(st, stack);
221                    stack.pop();
222                }
223            }
224            if (!first) {
225                writer.indentPrintln("</ul>");
226            }
227        }
228    
229        private String calcParents(Stack<String> stack) {
230            StringBuilder sb = new StringBuilder();
231            String dot = "";
232            Enumeration<String> en = stack.elements();
233            while (en.hasMoreElements()) {
234                sb.append(dot);
235                dot = ".";
236                sb.append(en.nextElement());
237            }
238            return sb.toString();
239        }
240    
241        private void writeLink(XmlType type) {
242            writer.indentPrint("<li>");
243            writer.print("<a href=\"" + type.getName() + ".html"
244                    + "\">" + type.getName() + "</a>");
245            writer.print("</li>");
246        }
247    
248        private List<XmlType> getMainMessageStructures() {
249            List<XmlType> types = new ArrayList(model.getXmlTypes().size());
250            for (XmlType type : this.getComplexMessageStructures()) {
251                if (isMainMessageStructure(type)) {
252                    types.add(type);
253                }
254            }
255            Collections.sort(types, XML_TYPE_NAME_COMPARATOR);
256            return types;
257        }
258    
259        private boolean isMainMessageStructure(XmlType xmlType) {
260            if (!HtmlContractMessageStructureWriter.calcOtherXmlTypeUsages(model,
261                    xmlType).isEmpty()) {
262                return false;
263            }
264            return true;
265        }
266    
267        private List<XmlType> getComplexMessageStructures() {
268            List<XmlType> types = new ArrayList(model.getXmlTypes().size());
269            for (XmlType type : model.getXmlTypes()) {
270                if (type.getPrimitive() == null) {
271                    throw new NullPointerException(type.getName()
272                            + " has no primitive flag set");
273                }
274                if (type.getPrimitive().equals(XmlType.COMPLEX)) {
275                    types.add(type);
276                }
277            }
278            Collections.sort(types, XML_TYPE_NAME_COMPARATOR);
279            return types;
280        }
281    
282        private void writeAlphabeticalList() {
283            List<XmlType> types = this.getComplexMessageStructures();
284            writer.indentPrintln(
285                    "<div class=\"panel\" style=\"background-color: rgb(255, 255, 255); border: 1px solid rgb(204, 204, 204);\">");
286            writer.indentPrintln(
287                    "<div class=\"panelHeader\" style=\"border-bottom: 1px solid rgb(204, 204, 204); background-color: rgb(238, 238, 238);\">");
288            writer.indentPrintln(
289                    "<b><a name=\"MessageStructures\"></a>All " + types.size() + " Message Structures in Alphabetical Order</b>");
290            writer.indentPrintln(
291                    "</div><div class=\"panelContent\" style=\"background-color: rgb(255, 255, 255);\">");
292            writer.indentPrintln("<ul>");
293            for (XmlType type : types) {
294                this.writeLink(type);
295            }
296            writer.indentPrintln("</ul>");
297            writer.indentPrintln("</div>");
298            writer.indentPrintln("</div>");
299        }
300    }