@Test
 public void testInsertDataCumul() throws Exception {
   MockConnection connexion = factory.getMockConnection();
   List<String> data = Arrays.asList("2011-04-17", "/var/data/silverpeas/kmelia72", "262");
   SilverStatisticsManagerDAO.insertDataStatsCumul(connexion, typeofStat, data, config);
   module.verifyAllStatementsClosed();
   List<?> statements = module.getPreparedStatements();
   assertNotNull(statements);
   assertThat(statements, hasSize(1));
   MockPreparedStatement pstmt = module.getPreparedStatement(0);
   assertThat(
       pstmt.getSQL(),
       is("INSERT INTO SB_Stat_SizeDirCumul(dateStat,fileDir,sizeDir) " + "VALUES(?,?,?)"));
   Map parameters = pstmt.getParameterMap();
   assertThat(parameters.size(), is(3));
   assertThat((String) parameters.get(1), is("2011-04-01"));
   assertThat((String) parameters.get(2), is("/var/data/silverpeas/kmelia72"));
   assertThat((Long) parameters.get(3), is(262L));
 }
  /**
   * 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);
      }
    }
  }