gt:         "questionPK =" + questionPK);
context: /**
   * Copyright (C) 2000 - 2012 Silverpeas
   *
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the GNU Affero General Public License as
   * published by the Free Software Foundation, either version 3 of the
   * License, or (at your option) any later version.
   *
   * As a special exception to the terms and conditions of version 3.0 of
   * the GPL, you may redistribute this Program in connection with Free/Libre
   * Open Source Software ("FLOSS") applications as described in Silverpeas's
   * FLOSS exception.  You should have received a copy of the text describing
   * the FLOSS exception, and it is also available here:
   * "http://www.silverpeas.org/docs/core/legal/floss_exception.html"
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU Affero General Public License for more details.
   *
   * You should have received a copy of the GNU Affero General Public License
   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
   */
  
  package com.stratelia.webactiv.util.questionResult.ejb;
  
  import java.sql.Connection;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.text.SimpleDateFormat;
  import java.util.ArrayList;
  import java.util.Collection;
  import java.util.List;
  
  import com.silverpeas.util.ForeignPK;
  import com.stratelia.silverpeas.silvertrace.SilverTrace;
  import com.stratelia.webactiv.util.DBUtil;
  import com.stratelia.webactiv.util.answer.model.AnswerPK;
  import com.stratelia.webactiv.util.exception.SilverpeasRuntimeException;
  import com.stratelia.webactiv.util.exception.UtilException;
  import com.stratelia.webactiv.util.questionResult.model.QuestionResult;
  import com.stratelia.webactiv.util.questionResult.model.QuestionResultPK;
  import com.stratelia.webactiv.util.questionResult.model.QuestionResultRuntimeException;
  
  /**
   * This class is made to access database only (table SB_Question_Answer)
   * @author neysseri
   */
  public class QuestionResultDAO {
  
    public static final String QUESTIONRESULTCOLUMNNAMES =
        "qrId, questionId, userId, answerId, qrOpenAnswer, qrNbPoints, qrPollDate, qrElapsedTime, qrParticipationId";
    private static final SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
  
    private static QuestionResult getQuestionResultFromResultSet(ResultSet rs, ForeignPK questionPK)
        throws SQLException {
      SilverTrace.info(
          "questionResult",
          "QuestionResultDAO.getQuestionResultFromResultSet()",
          "root.MSG_GEN_ENTER_METHOD",
          "questionPK =" + questionPK);
      String id = Integer.toString(rs.getInt(1));
      String questionId = Integer.toString(rs.getInt(2));
      String userId = rs.getString(3);
      String answerId = Integer.toString(rs.getInt(4));
      String openAnswer = rs.getString(5);
      int nbPoints = rs.getInt(6);
      String pollDate = rs.getString(7);
      int elapsedTime = rs.getInt(8);
      int participationId = rs.getInt(9);
      QuestionResult result =
          new QuestionResult(
          new QuestionResultPK(id, questionPK),
          new ForeignPK(questionId, questionPK),
          new AnswerPK(answerId, questionPK),
          userId,
          openAnswer,
          nbPoints,
          pollDate,
          elapsedTime,
          participationId);
  
      return result;
    }
  
    public static Collection<QuestionResult> getUserQuestionResultsToQuestion(Connection con,
        String userId,
        ForeignPK questionPK)
        throws SQLException {
      SilverTrace.info(
          "questionResult",
          "QuestionResultDAO.getUserQuestionResultsToQuestion()",
          "root.MSG_GEN_ENTER_METHOD",
          "userId = " + userId + ", questionPK =" + questionPK);
      ResultSet rs = null;
      QuestionResult questionResult = null;
      String tableName = new QuestionResultPK("", questionPK).getTableName();
  
      String selectStatement =
          "select "
          + QUESTIONRESULTCOLUMNNAMES
          + " from "
          + tableName
          + " where questionId = ? "
          + " and userId = ? order By answerId";
  
      PreparedStatement prepStmt = null;
  
      try {
        prepStmt = con.prepareStatement(selectStatement);
        prepStmt.setInt(1, Integer.parseInt(questionPK.getId()));
        prepStmt.setString(2, userId);
        rs = prepStmt.executeQuery();
        List<QuestionResult> result = new ArrayList<QuestionResult>();
        while (rs.next()) {
          questionResult = getQuestionResultFromResultSet(rs, questionPK);
          result.add(questionResult);
        }
        return result;
      } finally {
        DBUtil.close(rs, prepStmt);
      }
    }
  
    public static Collection<String> getUsersByAnswer(Connection con, String answerId)
        throws SQLException {
      SilverTrace.info("questionResult", "QuestionResultDAO.getUserQuestionResultsToQuestion()",
          "root.MSG_GEN_ENTER_METHOD", "answerId = " + answerId);
      ResultSet rs = null;
      String tableName = "SB_Question_QuestionResult";
  
      String selectStatement = "select userId from " + tableName + " where answerId = ? ";
      PreparedStatement prepStmt = null;
  
      try {
        prepStmt = con.prepareStatement(selectStatement);
        prepStmt.setInt(1, Integer.parseInt(answerId));
        rs = prepStmt.executeQuery();
        ArrayList<String> result = new ArrayList<String>();
        String userId;
        while (rs.next()) {
          userId = rs.getString(1);
          result.add(userId);
        }
        return result;
      } finally {
        DBUtil.close(rs, prepStmt);
      }
    }
  
    public static Collection<QuestionResult> getUserQuestionResultsToQuestionByParticipation(
        Connection con,
        String userId,
        ForeignPK questionPK,
        int participationId)
        throws SQLException {
      SilverTrace.info(
          "questionResult",
          "QuestionResultDAO.getUserQuestionResultsToQuestionByParticipation()",
          "root.MSG_GEN_ENTER_METHOD",
          "userId = " + userId + ", questionPK =" + questionPK + ", participationId = " +
          participationId);
      ResultSet rs = null;
      QuestionResult questionResult = null;
      String tableName = new QuestionResultPK("", questionPK).getTableName();
  
      String selectStatement =
          "select "
          + QUESTIONRESULTCOLUMNNAMES
          + " from "
          + tableName
          + " where questionId = ? "
          + " and userId = ? "
          + " and qrParticipationId = ? ";
  
      PreparedStatement prepStmt = null;
  
      try {
        prepStmt = con.prepareStatement(selectStatement);
        prepStmt.setInt(1, Integer.parseInt(questionPK.getId()));
        prepStmt.setString(2, userId);
        prepStmt.setInt(3, participationId);
        rs = prepStmt.executeQuery();
        List<QuestionResult> result = new ArrayList<QuestionResult>();
        while (rs.next()) {
          questionResult = getQuestionResultFromResultSet(rs, questionPK);
          result.add(questionResult);
        }
        return result;
      } finally {
        DBUtil.close(rs, prepStmt);
      }
    }
  
    public static Collection<QuestionResult> getQuestionResultToQuestion(Connection con,
        ForeignPK questionPK)
        throws SQLException {
      SilverTrace.info(
          "questionResult",
          "QuestionResultDAO.getQuestionResultToQuestion()",
          "root.MSG_GEN_ENTER_METHOD",
  