/*
   * (non-Javadoc)
   *
   * @see com.votingcentral.actions.DownloadAction#getStreamInfo(org.apache.struts.action.ActionMapping,
   *      org.apache.struts.action.ActionForm,
   *      javax.servlet.http.HttpServletRequest,
   *      javax.servlet.http.HttpServletResponse)
   */
  protected StreamInfo getStreamInfo(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {

    ActionMessages errors = new ActionMessages();
    long millis = System.currentTimeMillis();
    String fileName = "";
    String contentType = "";
    byte[] fileBytes = null;
    ShowPollResultsFormBean showResultsFormBean = (ShowPollResultsFormBean) form;
    String pollId = "";
    pollId =
        VCRequestHelper.getValueFromRequestOrForm(request, RequestParameterObjects.POLL_ID, pollId);
    String questionId = showResultsFormBean.getQuestionId();
    questionId =
        VCRequestHelper.getValueFromRequest(
            request, RequestParameterObjects.QUESTION_ID, questionId);
    showResultsFormBean.setPollId(pollId);
    showResultsFormBean.setQuestionId(questionId);
    PollTO pto = PollBO.getInstance().getPollByPollId(pollId);
    // if the user has not voted and the poll has not ended
    // redirect to show poll page.
    VCUserTO vto = UserBO.getInstance().getUserByUserName(VCRequestHelper.getUser(request));
    Date now = PollTimeHelper.getInstance().getCurrentDate();
    if (Votes.getInstance().canUserVote(vto.getUserId(), pollId)
        && pto.getEndTimestamp().after(now)) {
      log.debug("User has not voted, sending them to display poll.");
      errors.add(
          "pollId", new org.apache.struts.action.ActionMessage("show.poll.participation.reqd"));
      return null;
    } else {
      VCDownloadFileTypeEnum dfType =
          (showResultsFormBean.getDfType() == null
              ? VCDownloadFileTypeEnum.DEFAULT
              : VCDownloadFileTypeEnum.get(showResultsFormBean.getDfType()));
      if (dfType == VCDownloadFileTypeEnum.EXCEL) {
        fileName = "VC" + ".xls";
        contentType = "application/vnd.ms-excel";
      } else if (dfType == VCDownloadFileTypeEnum.TEXT) {
        fileName = "VC" + ".txt";
        contentType = "text/plain";
        fileBytes = getTextFormatBytes(pollId, questionId);
      } else if (dfType == VCDownloadFileTypeEnum.CSV) {
        fileName = "VC" + ".csv";
        contentType = "application/vnd.ms-excel";
        fileBytes = getTextFormatBytes(pollId, questionId);
      }
    }
    // set content type
    response.setHeader("Content-Type", "application/download");
    // Set the content disposition
    response.setHeader("Content-disposition", "attachment; filename=" + fileName);
    response.setContentLength(fileBytes.length);
    response.setHeader("Pragma", "public");
    response.setHeader("Cache-control", "must-revalidate");
    return new ByteArrayStreamInfo(contentType, fileBytes);
  }
Esempio n. 2
0
  /*
   * (non-Javadoc)
   *
   * @see com.votingcentral.model.db.dao.IVCUserDAO#updateUser(com.votingcentral.model.db.dao.to.VCUserTO)
   */
  public boolean updateUser(VCUserTO vto) throws SQLException {
    String sql1 = SQLResources.getSQLResource("update.vc.user");
    Connection conn = null;
    PreparedStatement pps1 = null;
    int rows = 0;

    try {
      conn = VCDAOFactory.getConnection();
      pps1 = conn.prepareStatement(sql1);

      if (vto.getFirstName() != null && vto.getFirstName().length() > 0) {
        pps1.setString(1, vto.getFirstName());
      } else {
        pps1.setNull(1, Types.VARCHAR);
      }

      if (vto.getLastName() != null && vto.getLastName().length() > 0) {
        pps1.setString(2, vto.getLastName());
      } else {
        pps1.setNull(2, Types.VARCHAR);
      }

      if (vto.getMiddleInitial() != null && vto.getMiddleInitial().length() > 0) {
        pps1.setString(3, vto.getMiddleInitial());
      } else {
        pps1.setNull(3, Types.VARCHAR);
      }

      if (vto.getMiddleName() != null && vto.getMiddleName().length() > 0) {
        pps1.setString(4, vto.getMiddleName());
      } else {
        pps1.setNull(4, Types.VARCHAR);
      }

      pps1.setString(5, vto.getEmailAddress());

      if (vto.getBirthDay() != null && vto.getBirthDay().trim().length() > 0) {
        pps1.setInt(6, new Integer(vto.getBirthDay()).intValue());
      } else {
        pps1.setNull(6, Types.INTEGER);
      }

      if (vto.getBirthMonth() != null && vto.getBirthMonth().trim().length() > 0) {
        pps1.setInt(7, new Integer(vto.getBirthMonth()).intValue());
      } else {
        pps1.setNull(7, Types.INTEGER);
      }

      pps1.setInt(8, new Integer(vto.getBirthYear()).intValue());

      pps1.setString(9, vto.getGender());

      pps1.setString(10, vto.getUserName());
      pps1.setString(11, vto.getDisplayUserName());
      pps1.setString(12, vto.getMailingAddress1());
      pps1.setString(13, vto.getMailingAddress2());
      pps1.setString(14, vto.getCity());
      pps1.setInt(15, vto.getStateId());
      pps1.setString(16, vto.getZipCode1());
      pps1.setString(17, vto.getZipCode2());
      pps1.setInt(18, vto.getCountryId());
      pps1.setString(19, vto.getPhoneCountryCode());
      pps1.setString(20, vto.getPhoneAreaCode());
      pps1.setString(21, vto.getPhoneNum1());
      pps1.setString(22, vto.getPhoneNum2());
      pps1.setString(23, vto.getAccountStatus());
      // for the where clause.
      pps1.setLong(24, vto.getUserId());
      rows = pps1.executeUpdate();

    } catch (SQLException e) {
      log.fatal("SQLException: " + e.getMessage());
      log.fatal("SQLState: " + e.getSQLState());
      log.fatal("VendorError: " + e.getErrorCode());
      throw e;
    } finally {
      try {
        if (pps1 != null) {
          pps1.close();
        }
        if (conn != null) {
          conn.close();
        }
      } catch (SQLException e) {
        log.fatal("Connection.close", e);
        throw e;
      }
    }
    return rows > 0 ? true : false;
  }
Esempio n. 3
0
  /*
   * (non-Javadoc)
   *
   * @see com.votingcentral.model.db.dao.IVCUserDAO#updateUser(com.votingcentral.model.db.dao.to.VCUserTO,
   *      com.votingcentral.model.db.dao.to.PersonalConfigTO)
   */
  public boolean updateUser(VCUserTO vto, PersonalConfigTO pto) throws SQLException {
    String sql1 = SQLResources.getSQLResource("update.vc.user");
    String sql2 = SQLResources.getSQLResource("update.personal.config");

    //
    // How many times do you want to retry the transaction
    // (or at least _getting_ a connection)?
    //
    int retryCount = 5;
    boolean transactionCompleted = false;
    boolean vcUserUpdate = false;
    boolean pcUpdate = false;

    Connection conn = null;
    PreparedStatement pps1 = null;
    PreparedStatement pps2 = null;
    int rows = 0;
    do {
      try {
        retryCount = 0;
        conn = VCDAOFactory.getConnection();
        conn.setAutoCommit(false);

        pps1 = conn.prepareStatement(sql1);

        if (vto.getFirstName() != null && vto.getFirstName().length() > 0) {
          pps1.setString(1, vto.getFirstName());
        } else {
          pps1.setNull(1, Types.VARCHAR);
        }

        if (vto.getLastName() != null && vto.getLastName().length() > 0) {
          pps1.setString(2, vto.getLastName());
        } else {
          pps1.setNull(2, Types.VARCHAR);
        }

        if (vto.getMiddleInitial() != null && vto.getMiddleInitial().length() > 0) {
          pps1.setString(3, vto.getMiddleInitial());
        } else {
          pps1.setNull(3, Types.VARCHAR);
        }

        if (vto.getMiddleName() != null && vto.getMiddleName().length() > 0) {
          pps1.setString(4, vto.getMiddleName());
        } else {
          pps1.setNull(4, Types.VARCHAR);
        }

        pps1.setString(5, vto.getEmailAddress());

        if (vto.getBirthDay() != null && vto.getBirthDay().trim().length() > 0) {
          pps1.setInt(6, new Integer(vto.getBirthDay()).intValue());
        } else {
          pps1.setNull(6, Types.INTEGER);
        }

        if (vto.getBirthMonth() != null && vto.getBirthMonth().trim().length() > 0) {
          pps1.setInt(7, new Integer(vto.getBirthMonth()).intValue());
        } else {
          pps1.setNull(7, Types.INTEGER);
        }

        pps1.setInt(8, new Integer(vto.getBirthYear()).intValue());

        pps1.setString(9, vto.getGender());

        pps1.setString(10, vto.getUserName());
        pps1.setString(11, vto.getDisplayUserName());
        pps1.setString(12, vto.getMailingAddress1());
        pps1.setString(13, vto.getMailingAddress2());
        pps1.setString(14, vto.getCity());
        pps1.setInt(15, vto.getStateId());
        pps1.setString(16, vto.getZipCode1());
        pps1.setString(17, vto.getZipCode2());
        pps1.setInt(18, vto.getCountryId());
        pps1.setString(19, vto.getPhoneCountryCode());
        pps1.setString(20, vto.getPhoneAreaCode());
        pps1.setString(21, vto.getPhoneNum1());
        pps1.setString(22, vto.getPhoneNum2());
        pps1.setString(23, vto.getAccountStatus());
        // for the where clause.
        pps1.setLong(24, vto.getUserId());
        rows = pps1.executeUpdate();
        if (rows == 1) {
          vcUserUpdate = true;
        }
        pps2 = conn.prepareStatement(sql2);

        pps2.setString(1, pto.getSecurityQuestion());
        pps2.setString(2, pto.getSecurityAnswer());
        pps2.setString(3, pto.getEncryptedPassword());
        // for the where clause
        pps2.setLong(4, pto.getUserId());
        rows = pps2.executeUpdate();
        if (rows == 1) {
          pcUpdate = true;
        }
        transactionCompleted = true;
        conn.commit();
        conn = null;
      } catch (SQLException e) {
        //
        // The two SQL states that are 'retry-able' are 08S01
        // for a communications error, and 41000 for deadlock.
        //
        // Only retry if the error was due to a stale connection,
        // communications problem or deadlock
        //
        log.fatal("SQLException: " + e.getMessage());
        log.fatal("SQLState: " + e.getSQLState());
        log.fatal("VendorError: " + e.getErrorCode());
        String sqlState = e.getSQLState();

        if ("08S01".equals(sqlState) || "41000".equals(sqlState)) {
          retryCount--;
        } else {
          retryCount = 0;
          throw e;
        }
      } finally {
        try {
          if (pps1 != null) {
            pps1.close();
            pps1 = null;
          }

        } catch (SQLException e) {
          log.fatal("Problem closing the prepared statements", e);
          throw e;
        }
        if (conn != null) {
          try {
            //
            // If we got here, and conn is not null, the
            // transaction should be rolled back, as not
            // all work has been done
            try {
              conn.rollback();
            } finally {
              conn.close();
            }
          } catch (SQLException sqlEx) {
            //
            // If we got an exception here, something
            // pretty serious is going on, so we better
            // pass it up the stack, rather than just
            // logging it. . .

            throw sqlEx;
          }
        }
      }
    } while (!transactionCompleted && (retryCount > 0));

    return transactionCompleted;
  }