001package edu.pdx.cs410J.grader;
002
003import com.google.common.annotations.VisibleForTesting;
004import edu.pdx.cs410J.ParserException;
005import org.slf4j.Logger;
006import org.slf4j.LoggerFactory;
007
008import java.io.*;
009
010public class SurveyResponsesFromD2LGenerator {
011
012  private static final Logger logger = LoggerFactory.getLogger("edu.pdx.cs410J.grader");
013
014  public static void main(String[] args) throws IOException, ParserException {
015    String d2lCsvFileName = null;
016    String htmlFileName = null;
017
018    for (String arg : args) {
019      if (d2lCsvFileName == null) {
020        d2lCsvFileName = arg;
021
022      } else if (htmlFileName == null) {
023        htmlFileName = arg;
024
025      } else {
026        usage("Extraneous command line argument: " + arg);
027      }
028    }
029
030    if (d2lCsvFileName == null) {
031      usage("Missing D2L CSV file name");
032    }
033
034    if (htmlFileName == null) {
035      usage("Missing html file name");
036    }
037
038    SurveyResponsesFromD2L d2lSurveyResponses = parseD2lCsvFile(d2lCsvFileName);
039    HtmlGenerator htmlGenerator = getHtmlGenerator(htmlFileName);
040    generateHtmlForSurveyResponses(d2lSurveyResponses, htmlGenerator);
041  }
042
043  @VisibleForTesting
044  static void generateHtmlForSurveyResponses(SurveyResponsesFromD2L d2lSurveyResponses, HtmlGenerator html) {
045    String title = "Previously on CS410J...";
046
047    html.beginTag("html");
048
049    outputHtmlHead(html, title);
050
051    html.beginTag("body");
052
053    outputHtmlBodyHeader(html, title);
054    outputHtmlForEachSurveyResponse(d2lSurveyResponses, html);
055
056    html.endTag();
057
058    html.endTag();
059  }
060
061  private static void outputHtmlForEachSurveyResponse(SurveyResponsesFromD2L allResponses, HtmlGenerator html) {
062    html.beginTag("ol");
063
064    allResponses.getQuestions().forEach((question) -> {
065      html.beginTag("li");
066      html.text(question);
067      html.endTag();
068      html.beginTag("ul");
069
070      allResponses.getResponsesTo(question).forEach((response) -> {
071        html.beginTag("li");
072        html.text(response);
073        html.endTag();
074      });
075
076      html.endTag();
077    });
078
079    html.endTag();
080  }
081
082  private static void outputHtmlBodyHeader(HtmlGenerator html, String title) {
083    html.beginTag("h1");
084    html.text(title);
085    html.endTag();
086    html.beginTag("p");
087    html.text("Say something witty here");
088    html.endTag();
089  }
090
091  private static void outputHtmlHead(HtmlGenerator html, String title) {
092    html.beginTag("head");
093    html.beginTag("title");
094    html.text(title);
095    html.endTag();
096    html.endTag();
097  }
098
099  private static HtmlGenerator getHtmlGenerator(String htmlFileName) throws IOException {
100    File htmlFile = new File(htmlFileName).getAbsoluteFile();
101    File parent = htmlFile.getParentFile();
102    if (!parent.exists()) {
103      usage("Parent of HTML file " + htmlFileName + " does not exist");
104    }
105
106    Writer writer = new FileWriter(htmlFile);
107    return new HtmlGenerator(writer);
108  }
109
110
111  private static SurveyResponsesFromD2L parseD2lCsvFile(String d2lCsvFileName) throws IOException {
112    File d2lCsvFile = new File(d2lCsvFileName);
113    if (!d2lCsvFile.exists()) {
114      usage("D2L CSV file \"" + d2lCsvFile + "\" does not exist");
115    }
116
117    D2LSurveyResponsesCSVParser parser = new D2LSurveyResponsesCSVParser(new FileReader(d2lCsvFile));
118    return parser.getSurveyResponses();
119  }
120
121  private static void usage(String message) {
122    PrintStream err = System.err;
123
124    err.println("+++ " + message);
125    err.println();
126    err.println("usage: java SurveyResponsesFromD2LGenerator d2lSurveyResponsesCsvFileName htmlFileName");
127    err.println("    d2lSurveyResponsesCsvFileName     Name of the CSV survey reponses file exported from D2L");
128    err.println("    htmlFileName                      Name of file to which generated HTML is written");
129    err.println();
130    err.println("Generates an HTML file that lists the responses to a survey from D2L");
131    err.println();
132
133    System.exit(1);
134  }
135}