예제 #1
0
  /**
   * DANIEL: THIS IS IMPORTANnew BadUserIDExceptionT the books will be passed with the default copy
   * no (0) If it already exists, please throw a new BadCopyNumberException with the first free
   * copyno included. Might want to search for # of copies first, rather than trying to sort through
   * the SQLExceptions coming back to see if that was the reason
   *
   * @param newBook - the book to be added to the DB
   * @throws SQLException - if Oracle complains
   * @throws BadCopyNumberException - if the copy number is already taken.
   */
  public int createNewBook(Book newBook) throws SQLException, BadCopyNumberException {
    try {
      String statement;
      ResultSet rs;

      // Copy Number

      // Check if there is a
      statement =
          "select count(*) from BookCopy where callnumber ='"
              + newBook.getCallNumber()
              + "' and copyNo="
              + newBook.getCopyNo();
      rs = sql(statement, SQLType.query);
      rs.next();

      int copyCount = rs.getInt(1);

      if (copyCount == 0) {

        // book

        statement =
            "INSERT INTO Book VALUES ('"
                + newBook.getCallNumber()
                + "', "
                + newBook.getISBN()
                + ", '"
                + newBook.getTitle()
                + "', '"
                + newBook.getMainAuthor()
                + "', '"
                + newBook.getPublisher()
                + "', "
                + newBook.getYear()
                + ")";

        System.out.println(statement);
        System.out.println(newBook.getCopyNo());
        sql(statement, SQLType.insert);

        // CopyNo

        statement =
            "INSERT INTO BookCopy VALUES ('"
                + newBook.getCallNumber()
                + "',"
                + newBook.getCopyNo()
                + ",'in')";

        System.out.println(statement);
        sql(statement, SQLType.insert);
        System.out.println(statement);
        // Author

        // Add Main Author
        statement =
            "INSERT INTO HasAuthor VALUES ('"
                + newBook.getCallNumber()
                + "', '"
                + newBook.getMainAuthor()
                + "')";
        System.out.println(statement);
        sql(statement, SQLType.insert);
        System.out.println("Size: " + newBook.getAuthors().size());
        // Add Secondary Authors
        for (String s : newBook.getAuthors()) {
          statement =
              "INSERT INTO HasAuthor VALUES ('" + newBook.getCallNumber() + "', '" + s + "')";
          System.out.println(statement);
          sql(statement, SQLType.insert);
        }

        // Subject

        for (String s : newBook.getSubjects()) {
          s = s.toLowerCase();
          statement =
              "INSERT INTO HasSubject VALUES ('" + newBook.getCallNumber() + "', '" + s + "')";
          System.out.println(statement);
          sql(statement, SQLType.insert);
        }

      } else if (copyCount >= 1) {
        System.out.println("Test: " + newBook.getCopyNo());
        statement =
            "select max(copyNo) from bookCopy where callNumber='" + newBook.getCallNumber() + "'";
        rs = sql(statement, SQLType.query);
        rs.next();
        copyCount = rs.getInt(1);

        String message =
            "The copy number"
                + newBook.getCopyNo()
                + "does not exist. The next available copy number is: ";
        copyCount++;

        statement =
            "INSERT INTO BookCopy VALUES ('"
                + newBook.getCallNumber()
                + "', "
                + copyCount
                + ", "
                + "'in' "
                + ")";
        System.out.println(statement);
        sql(statement, SQLType.insert);
        throw new BadCopyNumberException(message, copyCount);
      } else {
        throw new SQLException("wasn't suposed to get here");
      }

      return newBook.getCopyNo();

    } catch (BadCopyNumberException BDCPY) {
      throw BDCPY;
    }
  }