/**
   * Messages will be read from the given filepath and stored in the array list (messagesList)
   *
   * @param filePath Text file to be read
   */
  public static List<String> readFile(String filePath) {
    BufferedReader bufferedReader = null;
    StringBuffer message = new StringBuffer("");
    final String asterixLine = "*****";
    List<String> messagesList = new ArrayList<String>();
    try {

      String line;
      bufferedReader = new BufferedReader(new FileReader(filePath));
      while ((line = bufferedReader.readLine()) != null) {
        if ((line.equals(asterixLine.trim()) && !"".equals(message.toString().trim()))) {
          messagesList.add(message.toString());
          message = new StringBuffer("");
        } else {
          message = message.append(String.format("\n%s", line));
        }
      }
      if (!"".equals(message.toString().trim())) {
        messagesList.add(message.toString());
      }
    } catch (FileNotFoundException e) {
      log.error("Error in reading file " + filePath, e);
    } catch (IOException e) {
      log.error("Error in reading file " + filePath, e);
    } finally {
      try {
        if (bufferedReader != null) {
          bufferedReader.close();
        }
      } catch (IOException e) {
        log.error("Error occurred when closing the file : " + e.getMessage(), e);
      }
    }
    return messagesList;
  }
Example #2
0
  /**
   * Get the list of bids related to a specific item.
   *
   * @return a string in html format
   * @since 1.1
   */
  public String getBidHistory(int itemId) throws RemoteException {
    StringBuffer html = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    Connection conn = null;
    String date = null, bidderName = null, itemName = null;
    float bid = 0;
    int userId = -1;

    // get the item
    try {
      conn = dataSource.getConnection();
      stmt = conn.prepareStatement("SELECT name FROM items WHERE id=?");
      stmt.setInt(1, itemId);
      rs = stmt.executeQuery();
    } catch (Exception e) {
      try {
        if (stmt != null) stmt.close();
        if (conn != null) conn.close();
      } catch (Exception ignore) {
      }
      throw new RemoteException("Failed to execute Query for item in items table: " + e);
    }
    try {
      if (!rs.first()) {
        stmt.close();
        stmt = conn.prepareStatement("SELECT name FROM old_items WHERE id=?");
        stmt.setInt(1, itemId);
        rs = stmt.executeQuery();
      }
    } catch (Exception e) {
      try {
        if (stmt != null) stmt.close();
        if (conn != null) conn.close();
      } catch (Exception ignore) {
      }
      throw new RemoteException("Failed to execute Query for item in old_items table: " + e);
    }
    try {
      if ((rs == null) || (!rs.first())) // This item does not exist
      {
        stmt.close();
        conn.close();
        return "";
      } else {
        itemName = rs.getString("name");
        html = new StringBuffer("<center><h3>Bid History for " + itemName + "<br></h3></center>");
      }
      stmt.close();
    } catch (Exception e) {
      try {
        if (conn != null) conn.close();
      } catch (Exception ignore) {
      }
      throw new RemoteException("This item does not exist (got exception: " + e + ")<br>");
    }
    // Get the list of the user's last bids
    try {
      stmt = conn.prepareStatement("SELECT * FROM bids WHERE item_id=? ORDER BY date DESC");
      stmt.setInt(1, itemId);
      rs = stmt.executeQuery();
      if (!rs.first()) {
        stmt.close();
        conn.close();
        return html.append("<h3>There is no bid corresponding to this item.</h3><br>").toString();
      }
    } catch (SQLException e) {
      try {
        if (stmt != null) stmt.close();
        if (conn != null) conn.close();
      } catch (Exception ignore) {
      }
      throw new RemoteException("Exception getting bids list: " + e + "<br>");
    }
    PreparedStatement userStmt = null;
    try {
      html.append(printBidHistoryHeader());
      userStmt = conn.prepareStatement("SELECT nickname FROM users WHERE id=?");
      ResultSet urs = null;
      do {
        // Get the bids
        date = rs.getString("date");
        bid = rs.getFloat("bid");
        userId = rs.getInt("user_id");

        userStmt.setInt(1, userId);
        urs = userStmt.executeQuery();
        if (urs.first()) bidderName = urs.getString("nickname");

        html.append(printBidHistory(userId, bidderName, bid, date));
      } while (rs.next());
      html.append(printBidHistoryFooter());
      userStmt.close();
      stmt.close();
      conn.close();
    } catch (SQLException e) {
      try {
        if (userStmt != null) userStmt.close();
        if (stmt != null) stmt.close();
        if (conn != null) conn.close();
      } catch (Exception ignore) {
      }
      throw new RemoteException("Exception getting bid: " + e + "<br>");
    }
    return html.toString();
  }