/**
   * @param response
   * @param type
   * @param alphaList
   * @param title
   * @param xTitle
   * @param yTitle
   */
  protected void createChart(
      final CollStatInfo csi,
      final Vector<Pair<String, Integer>> list,
      final String xTitle,
      final String yTitle) {
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    for (Pair<String, Integer> p : list) {
      dataset.addValue(p.second, p.first, "");
    }

    // if (StringUtils.isEmpty(csi.getInstName()))
    // {
    //    csi.setInstName(getProviderNameFromInstCode(csi.getProviderId()));
    // }

    JFreeChart chart =
        ChartFactory.createBarChart(
            csi.getTitle(), xTitle, yTitle, dataset, PlotOrientation.VERTICAL, true, true, false);

    // chart.getCategoryPlot().setRenderer(new CustomColorBarChartRenderer());

    chart.setBackgroundPaint(new Color(228, 243, 255));

    try {
      Integer hashCode = csi.hashCode();
      csi.setChartFileName(hashCode + ".png");
      DataOutputStream dos =
          new DataOutputStream(
              new FileOutputStream(new File("reports/charts/" + csi.getChartFileName())));
      ChartUtilities.writeChartAsPNG(dos, chart, 700, 600);

    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
  /** @param response */
  protected boolean generateChart(
      final CollStatInfo csi, final HashMap<StatType, CollStatSQLDefs> statTypeHash) {
    System.out.println(csi.getTitle());

    int total = csi.getValue(StatType.eTotalNumRecords);

    Vector<Pair<String, Integer>> pairs = new Vector<Pair<String, Integer>>();

    double totalPercent = 0.0;
    int cnt = 0;
    int goodCnt = 0;
    for (StatType type : StatType.values()) {
      if (type == StatType.eTotalNumRecords) continue;

      CollStatSQLDefs csqd = statTypeHash.get(type);
      double dVal = (double) csi.getValue(type);
      int val = (int) (((dVal / (double) total) * 100.0));

      if (val > 0) {
        goodCnt++;
      }

      // System.out.println(csqd.getName()+"  "+val+ "  " + csi.getValue(csqd.getType())+" / "+
      // total);

      pairs.add(new Pair<String, Integer>(csqd.getName(), val));

      if (type.ordinal() < StatType.eHasYearOnly.ordinal()) {
        totalPercent += val;
        cnt++;
      }
    }
    totalPercent = Math.max(totalPercent, 0.0);
    pairs.add(new Pair<String, Integer>("Average", (int) totalPercent / cnt));

    // System.out.println("goodCnt: "+goodCnt);
    if (goodCnt > 0) {
      createChart(csi, pairs, "Statistic", "Percent");
      return true;
    }
    return false;
  }
  /* (non-Javadoc)
   * @see edu.ku.brc.specify.toycode.mexconabio.AnalysisBase#process(int, int)
   */
  @Override
  public void process(int type, int options) {
    if (loadInstsFromDB) {
      discoverInstCodesAndTotals();
    } else {
      loadInstCodesAndtotals();
    }

    for (CollStatSQLDefs csqd : getStatSQL()) {
      if (csqd.getType() == StatType.eTotalNumRecords) continue;

      System.out.println(csqd.getType().toString());
      for (Object[] row : BasicSQLUtils.query(dbSrcConn, csqd.getSQL())) {
        // String  instCode = (String)row[0];
        Integer providerId = (Integer) row[0];
        Long count = (Long) row[1];

        CollStatInfo csi = instHashMap.get(providerId);
        if (csi != null) {
          csi.setValue(csqd.getType(), count.intValue());
        } else {
          System.err.println("Couldn't find institution[" + providerId + "]");
        }
      }
    }

    for (CollStatInfo csi : institutions) {
      System.out.println("\n-------------------" + csi.getTitle() + "-------------------");
      for (CollStatSQLDefs csqd : getStatSQL()) {
        System.out.println(
            String.format("%15s %10d", csqd.getType().toString(), csi.getValue(csqd.getType())));
      }
    }

    saveInstitutions();
  }
  public void createCharts() {
    loadInstCodesAndtotals();

    // for (CollStatInfo csi : institutions)
    // {
    //    csi.setInstName(getProviderNameFromInstCode(csi.getProviderId()));
    // }

    List<CollStatSQLDefs> statTypes = getStatSQL();
    HashMap<StatType, CollStatSQLDefs> statTypeHash = new HashMap<StatType, CollStatSQLDefs>();
    for (CollStatSQLDefs cs : statTypes) {
      statTypeHash.put(cs.getType(), cs);
    }

    CollStatInfo totals = new CollStatInfo(" Totals");
    for (CollStatInfo csi : institutions) {
      for (CollStatSQLDefs csqd : statTypes) {
        StatType type = csqd.getType();
        int totVal = totals.getValue(type) + csi.getValue(type);
        totals.setValue(type, totVal);
      }
    }

    try {
      FileUtils.deleteDirectory(new File("reports/charts/"));
    } catch (IOException e) {
      e.printStackTrace();
    }

    Collections.sort(
        institutions,
        new Comparator<CollStatInfo>() {
          @Override
          public int compare(CollStatInfo o1, CollStatInfo o2) {
            return o1.getInstName().compareToIgnoreCase(o2.getInstName());
            // Integer cnt1 = o1.getTotalNumRecords();
            // Integer cnt2 = o2.getTotalNumRecords();
            // return cnt2.compareTo(cnt1);
          }
        });

    institutions.insertElementAt(totals, 0);
    CollStatInfo clsi = totals;

    // tblWriter.logHdr(titles);

    int i = 0;
    for (CollStatInfo csi : institutions) {
      if (StringUtils.isEmpty(csi.getInstName())) {
        csi.setInstName(getProviderNameFromInstCode(csi.getProviderId()));
      }

      String title = csi.getTitle() + " - " + csi.getTotalNumRecords();

      if (i == 0) {
        startLogging("reports", "charts", clsi.hashCode() + ".html", title, false);
        tblWriter.startTable();
        tblInstHash.put(tblWriter, csi);

      } else {
        tblWriter.endTable();
        startNewDocument(csi.hashCode() + ".html", title, false);
        tblInstHash.put(tblWriter, csi);
        tblWriter.startTable();
      }

      if (generateChart(csi, statTypeHash)) {
        int total = csi.getValue(StatType.eTotalNumRecords);

        tblWriter.setHasLines();
        tblWriter.print("<TR><TD>");
        tblWriter.print(String.format("<img src=\"%s\">", csi.getChartFileName()));
        tblWriter.println("<BR><BR><BR><BR></TD><TD>");
        tblWriter.startTable();
        tblWriter.logHdr("Stat", "Percent");

        int rowCnt = 0;
        int cnt = 0;
        double totalPercent = 0.0;
        for (StatType type : StatType.values()) {
          if (type == StatType.eTotalNumRecords) continue;

          CollStatSQLDefs csqd = statTypeHash.get(type);

          double dVal = (double) csi.getValue(type);
          double val = (((dVal / (double) total) * 100.0));

          if (type.ordinal() < StatType.eHasYearOnly.ordinal()) {
            tblWriter.print(String.format("<TR class=\"%s\">", (rowCnt % 2 == 0 ? "od" : "ev")));
            totalPercent += val;
            cnt++;
          } else {
            tblWriter.print(String.format("<TR>", csqd.getName()));
          }
          tblWriter.println(
              String.format(
                  "<TD>%s</TD><TD style=\"text-align:right\">%6.2f</TD></TR>",
                  csqd.getName(), val));
          rowCnt++;
        }
        totalPercent = Math.max(totalPercent, 0.0);
        double avePercent = (totalPercent / (double) cnt);
        tblWriter.println(
            String.format(
                "<TR><TD>Average</TD><TD style=\"text-align:right\">%6.2f</TD></TR>", avePercent));
        csi.setAveragePercent(avePercent);

        tblWriter.endTable();
        tblWriter.println("</TD></TR>");
      }

      i++;

      /*if (i % 25 == 0)
      {
          tblWriter.endTable();
          startNewDocument("institutions"+i+".html", "Institutions " + i, false);
          tblWriter.setHasLines();
      }*/

      // if (i == 100) break;
    }
    tblWriter.endTable();

    Vector<CollStatInfo> sortedByAvesList = new Vector<CollStatInfo>(institutions);
    Collections.sort(
        sortedByAvesList,
        new Comparator<CollStatInfo>() {
          @Override
          public int compare(CollStatInfo o1, CollStatInfo o2) {
            Double i1 = o1.getAveragePercent();
            Double i2 = o2.getAveragePercent();
            int rv = i2.compareTo(i1);
            if (rv == 0) {
              Integer cnt1 = o1.getTotalNumRecords();
              Integer cnt2 = o2.getTotalNumRecords();
              return cnt2.compareTo(cnt1);
            }
            return rv;
          }
        });

    Integer rank = 0;
    String average = "";
    startNewDocument("SortedByAverages.html", " Sorted By Averages", false);
    sortByTblWriter = tblWriter;
    tblWriter.startTable();
    tblWriter.logHdr("Rank", "Institution", "Num of Records", "Percentage");
    for (CollStatInfo csi : sortedByAvesList) {
      String aveStr = String.format("%8.2f", csi.getAveragePercent());
      Integer cnt = csi.getTotalNumRecords();
      if (!aveStr.equals(average)) {
        rank++;
        average = aveStr;
      }

      String fileName = StringUtils.replace(csi.getChartFileName(), "png", "html");
      String link = String.format("<a href=\"%s\">%s</>", fileName, csi.getTitle());
      tblWriter.log(rank.toString(), link, cnt.toString(), aveStr);
    }
    tblWriter.endTable();

    // tblWriter.println("</BODY></HTML>");
    endLogging(true);

    // saveInstitutions();
  }