/**
   * Method declaration
   *
   * @param con
   * @param statsType
   * @param conf
   * @throws SQLException
   * @see
   */
  public static void makeStatCumul(Connection con, StatType statsType, StatisticsConfig conf)
      throws SQLException {
    StringBuilder selectStatementBuf =
        new StringBuilder("SELECT * FROM ").append(conf.getTableName(statsType));
    String selectStatement;
    String keyNameCurrent;
    Statement stmt = null;
    ResultSet rs = null;

    selectStatement = selectStatementBuf.toString();
    SilverTrace.info(
        "silverstatistics",
        "SilverStatisticsManagerDAO.makeStatCumul",
        "root.MSG_GEN_PARAM_VALUE",
        "selectStatement=" + selectStatement);
    try {
      stmt = con.createStatement();
      rs = stmt.executeQuery(selectStatement);
      Collection<String> theKeys = conf.getAllKeys(statsType);
      String addToValueKeys = "";

      while (rs.next()) {
        List<String> valueKeys = new ArrayList<String>();

        for (String theKey : theKeys) {
          keyNameCurrent = theKey;
          StatDataType currentType =
              StatDataType.valueOf(conf.getKeyType(statsType, keyNameCurrent));
          switch (currentType) {
            case INTEGER:
              int tmpInt = rs.getInt(keyNameCurrent);
              if (rs.wasNull()) {
                addToValueKeys = "";
              } else {
                addToValueKeys = String.valueOf(tmpInt);
              }
              break;
            case DECIMAL:
              long longValue = rs.getLong(keyNameCurrent);
              if (rs.wasNull()) {
                addToValueKeys = "";
              } else {
                addToValueKeys = String.valueOf(longValue);
              }
              break;
            case VARCHAR:
              addToValueKeys = rs.getString(keyNameCurrent);
              if (addToValueKeys == null) {
                addToValueKeys = "";
              }
              break;
          }
          valueKeys.add(addToValueKeys);
        }
        putDataStatsCumul(con, statsType, valueKeys, conf);
      }
    } catch (SQLException e) {
      SilverTrace.error(
          "silverstatistics",
          "SilverStatisticsManagerDAO.makeStatCumul",
          "silverstatistics.MSG_ALIMENTATION_BD",
          e);
      throw e;
    } finally {
      DBUtil.close(rs, stmt);
    }
  }
  /**
   * Method declaration
   *
   * @param con
   * @param statsType
   * @param valueKeys
   * @param conf
   * @throws SQLException
   * @throws IOException
   * @see
   */
  public static void putDataStatsCumul(
      Connection con, StatType statsType, List<String> valueKeys, StatisticsConfig conf)
      throws SQLException {
    StringBuilder selectStatementBuf = new StringBuilder("SELECT ");
    StringBuilder updateStatementBuf = new StringBuilder("UPDATE ");
    String tableName = conf.getTableName(statsType);

    Statement stmt = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    boolean rowExist = false;
    boolean firstKeyInWhere = true;
    int k = 0;
    int intToAdd;

    updateStatementBuf.append(tableName).append("Cumul");
    updateStatementBuf.append(" SET ");

    Collection<String> theKeys = conf.getAllKeys(statsType);
    Iterator<String> iteratorKeys = theKeys.iterator();

    while (iteratorKeys.hasNext()) {
      String keyNameCurrent = iteratorKeys.next();
      selectStatementBuf.append(keyNameCurrent);
      if (iteratorKeys.hasNext()) {
        selectStatementBuf.append(",");
      }
      if (conf.isCumulKey(statsType, keyNameCurrent)) {
        updateStatementBuf.append(keyNameCurrent);
        updateStatementBuf.append("=? ,");
      }
    }

    updateStatementBuf.deleteCharAt(updateStatementBuf.length() - 1);

    selectStatementBuf.append(" FROM ").append(tableName).append("Cumul" + " WHERE ");
    updateStatementBuf.append(" WHERE ");

    iteratorKeys = theKeys.iterator();
    while (iteratorKeys.hasNext()) {
      String keyNameCurrent = iteratorKeys.next();
      if (!conf.isCumulKey(statsType, keyNameCurrent)) {
        if (!firstKeyInWhere) {
          selectStatementBuf.append(" AND ");
          updateStatementBuf.append(" AND ");
        }
        selectStatementBuf.append(keyNameCurrent);
        updateStatementBuf.append(keyNameCurrent);
        StatDataType currentType = StatDataType.valueOf(conf.getKeyType(statsType, keyNameCurrent));
        switch (currentType) {
          case DECIMAL:
          case INTEGER:
            if (!StringUtil.isDefined(valueKeys.get(k))) {
              selectStatementBuf.append("=NULL");
              updateStatementBuf.append("=NULL");
            } else {
              selectStatementBuf.append("=").append(valueKeys.get(k));
              updateStatementBuf.append("=").append(valueKeys.get(k));
            }
            break;
          case VARCHAR:
            if (keyNameCurrent.equals("dateStat")) {
              String dateFirstDayOfMonth = valueKeys.get(k).substring(0, 8);
              dateFirstDayOfMonth = dateFirstDayOfMonth + "01";
              selectStatementBuf.append("='").append(dateFirstDayOfMonth).append("'");
              updateStatementBuf.append("='").append(dateFirstDayOfMonth).append("'");
            } else {
              if (!StringUtil.isDefined(valueKeys.get(k))) {
                selectStatementBuf.append("=NULL");
                updateStatementBuf.append("=NULL");
              } else {
                selectStatementBuf.append("='").append(valueKeys.get(k)).append("'");
                updateStatementBuf.append("='").append(valueKeys.get(k)).append("'");
              }
            }
            break;
        }
        firstKeyInWhere = false;
      }
      k++;
    }
    try {
      stmt = con.createStatement();
      String selectStatement = selectStatementBuf.toString();
      SilverTrace.info(
          "silverstatistics",
          "SilverStatisticsManagerDAO.putDataStatsCumul",
          "root.MSG_GEN_PARAM_VALUE",
          "selectStatement=" + selectStatement);

      rs = stmt.executeQuery(selectStatement);
      String updateStatement = updateStatementBuf.toString();
      SilverTrace.info(
          "silverstatistics",
          "SilverStatisticsManagerDAO.putDataStatsCumul",
          "root.MSG_GEN_PARAM_VALUE",
          "updateStatement=" + updateStatement);
      pstmt = con.prepareStatement(updateStatement);
      while (rs.next()) {
        rowExist = true;
        int countCumulKey = 0;
        iteratorKeys = theKeys.iterator();
        while (iteratorKeys.hasNext()) {
          String keyNameCurrent = iteratorKeys.next();
          if (conf.isCumulKey(statsType, keyNameCurrent)) {
            countCumulKey++;
            StatDataType currentType =
                StatDataType.valueOf(conf.getKeyType(statsType, keyNameCurrent));
            if (StatDataType.INTEGER == currentType) {
              intToAdd = Integer.valueOf(valueKeys.get(conf.indexOfKey(statsType, keyNameCurrent)));
              if (conf.getModeCumul(statsType) == StatisticMode.Add) {
                pstmt.setInt(countCumulKey, rs.getInt(keyNameCurrent) + intToAdd);
              }
              if (conf.getModeCumul(statsType) == StatisticMode.Replace) {
                pstmt.setInt(countCumulKey, intToAdd);
              }
            }
            if (StatDataType.DECIMAL == currentType) {
              Long myLong = Long.valueOf(valueKeys.get(conf.indexOfKey(statsType, keyNameCurrent)));

              if (conf.getModeCumul(statsType) == StatisticMode.Add) {
                pstmt.setLong(countCumulKey, (rs.getLong(keyNameCurrent) + myLong));
              }
              if (conf.getModeCumul(statsType) == StatisticMode.Replace) {
                pstmt.setLong(countCumulKey, myLong);
              }
            }
          }
        }
        pstmt.executeUpdate();
      }

    } catch (SQLException e) {
      SilverTrace.error(
          "silverstatistics",
          "SilverStatisticsManagerDAO.putDataStatsCumul",
          "silverstatistics.MSG_ALIMENTATION_BD",
          e);
      throw e;
    } finally {
      DBUtil.close(rs, stmt);
      DBUtil.close(pstmt);

      if (!rowExist) {
        insertDataStatsCumul(con, statsType, valueKeys, conf);
      }
    }
  }