/**
   * Checks to see if a valid record id has been entered.
   *
   * @param record_id the record id
   * @return True if the id is valid. False otherwise.
   */
  public boolean isValidID(int record_id) {
    db = new Database();
    db.connect();
    conn = db.getConnection();
    String sql = "select count(*) from radiology_record where record_id = " + record_id;
    int count = 0;
    try {
      stmt = conn.createStatement();
      rset = stmt.executeQuery(sql);
      while (rset != null && rset.next()) {
        count = (rset.getInt(1));
      }
    } catch (SQLException e) {
      response_message = e.getMessage();
    } finally {
      db.close(conn, stmt, null, rset);
    }

    if (count == 0) {
      return false;
    }
    return true;
  }
  @SuppressWarnings({"unchecked", "deprecation"})
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    response_message = "";
    HttpSession session = request.getSession();
    FileItem image_file = null;
    int record_id = 0;
    int image_id;
    InputStream instream = null;
    OutputStream full_outstream = null;
    OutputStream thumb_outstream = null;
    OutputStream regular_outstream = null;

    // Check if there is any input in the record id field
    if (request.getParameter("recordID") == null || request.getParameter("recordID").equals("")) {
      response_message = "<p><font color=ff0000>No ID entered!</font></p>";
      session.setAttribute("msg", response_message);
      response.sendRedirect("uploadImage.jsp");
    }

    try {
      // Parse the HTTP request to get the image stream
      DiskFileUpload fu = new DiskFileUpload();
      List<FileItem> FileItems = fu.parseRequest(request);

      // Process the uploaded items, assuming only 1 image file uploaded
      Iterator<FileItem> i = FileItems.iterator();

      while (i.hasNext()) {
        FileItem item = (FileItem) i.next();
        if (item.isFormField()) {
          if (item.getFieldName().equals("recordID")) {
            record_id = Integer.parseInt(item.getString());
            if (!isValidID(record_id)) {
              response_message = "<p><font color=ff0000>Invalid record id!</font></p>";
              session.setAttribute("msg", response_message);
              response.sendRedirect("uploadImage.jsp");
            }
          }
          // out.println(item.getFieldName() + ": " +
          // item.getString());
        } else {
          image_file = item;
          if (image_file.getName().equals("")) {
            response_message = "<p><font color=ff0000>No file selected!</font></p>";
            session.setAttribute("msg", response_message);
            response.sendRedirect("uploadImage.jsp");
          }
        }
      }

      // Get the image stream
      instream = image_file.getInputStream();

      BufferedImage full_image = ImageIO.read(instream);
      BufferedImage thumbnail = shrink(full_image, THUMBNAIL_SHRINK);
      BufferedImage regular_image = shrink(full_image, REGULAR_SHRINK);

      // Connect to the database
      db = new Database();
      db.connect();
      conn = db.getConnection();
      stmt = conn.createStatement();

      /*
       * First, to generate a unique pic_id using an SQL sequence
       */
      ResultSet rset1 = stmt.executeQuery(SQL_IMAGE_ID);
      rset1.next();
      image_id = rset1.getInt(1);

      // Insert an empty blob into the table first. Note that you have to
      // use the Oracle specific function empty_blob() to create an empty
      // blob
      stmt.execute(
          "INSERT INTO pacs_images VALUES("
              + record_id
              + ","
              + image_id
              + ", empty_blob(), empty_blob(), empty_blob())");

      // to retrieve the lob_locator
      // Note that you must use "FOR UPDATE" in the select statement
      String cmd = "SELECT * FROM pacs_images WHERE image_id = " + image_id + " FOR UPDATE";
      ResultSet rset = stmt.executeQuery(cmd);
      rset.next();
      BLOB thumb = ((OracleResultSet) rset).getBLOB("thumbnail");
      BLOB regular = ((OracleResultSet) rset).getBLOB("regular_size");
      BLOB full = ((OracleResultSet) rset).getBLOB("full_size");

      // Write the image to the blob object
      full_outstream = full.getBinaryOutputStream();
      ImageIO.write(full_image, "jpg", full_outstream);
      thumb_outstream = thumb.getBinaryOutputStream();
      ImageIO.write(thumbnail, "jpg", thumb_outstream);
      regular_outstream = regular.getBinaryOutputStream();
      ImageIO.write(regular_image, "jpg", regular_outstream);

      stmt.executeUpdate("commit");
      response_message = "<p>Upload OK!</p>";
      session.setAttribute("msg", response_message);
      response.sendRedirect("uploadImage.jsp");

    } catch (Exception ex) {
      response_message = ex.getMessage();
    } finally {
      if (instream != null) {
        instream.close();
      }
      if (full_outstream != null) {
        full_outstream.close();
      }
      if (thumb_outstream != null) {
        thumb_outstream.close();
      }
      if (regular_outstream != null) {
        regular_outstream.close();
      }
      db.close(conn, stmt, null, rset);
    }
  }