コード例 #1
0
ファイル: AdminService.java プロジェクト: bahizi/gcit_library
  private void deleteBookAuthor(Book book) {
    Connection conn;
    try {
      conn = getConnection();
      AuthorDAO authorDAO = new AuthorDAO(conn);
      BookDAO bookDAO = new BookDAO(conn);
      List<Author> allAuthors =
          (List<Author>)
              authorDAO.read(
                  "SELECT * FROM tbl_author JOIN tbl_book_authors ON tbl_author.authorId = tbl_book_authors.authorId WHERE bookId = ?",
                  new Object[] {book.getBookId()});
      ArrayList<Author> toDelete = new ArrayList<Author>();
      ArrayList<String> actions = new ArrayList<String>();
      actions.add("Cancel");

      ArrayList<String> questions = new ArrayList<String>();
      questions.add("Yes");
      questions.add("No");
      boolean more = true;
      do {
        System.out.println("Choose Author");
        int author = getChoiceNumber(allAuthors, actions);
        if (author == -1) {
          more = false;
        } else {
          toDelete.add(allAuthors.get(author - 1));
          allAuthors.remove(author - 1);
          System.out.println("Delete more authors?");
          displayOptions(questions);
          int next = getInputInt(1, 2);
          more = next == 1;
        }

      } while (more);
      List<Author> curAuthors = book.getAuthors();
      for (Author auth : toDelete) {
        authorDAO.save(
            "DELETE FROM tbl_book_authors WHERE authorId = ? AND bookId =?",
            new Object[] {auth.getAuthorId(), book.getBookId()});
      }
      conn.commit();
      conn.close();
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
コード例 #2
0
ファイル: AdminService.java プロジェクト: bahizi/gcit_library
  private void addBookAuthor(Book book) {
    Connection conn;
    try {
      conn = getConnection();
      AuthorDAO authorDAO = new AuthorDAO(conn);
      BookDAO bookDAO = new BookDAO(conn);
      List<Author> allAuthors =
          (List<Author>)
              authorDAO.read(
                  "SELECT * FROM tbl_author WHERE tbl_author.authorId NOT IN (SELECT tbl_book_authors.authorId FROM tbl_book_authors WHERE bookId=?)",
                  new Object[] {book.getBookId()});
      ArrayList<Author> newAuthors = new ArrayList<Author>();
      ArrayList<String> actions = new ArrayList<String>();
      actions.add("Cancel");

      ArrayList<String> questions = new ArrayList<String>();
      questions.add("Yes");
      questions.add("No");
      boolean more = true;
      do {
        System.out.println("Choose Author");
        int author = getChoiceNumber(allAuthors, actions);
        if (author == -1) {
          more = false;
        } else {
          newAuthors.add(allAuthors.get(author - 1));
          allAuthors.remove(author - 1);
          System.out.println("Add more authors?");
          displayOptions(questions);
          int next = getInputInt(1, 2);
          more = next == 1;
        }
      } while (more);
      for (Author auth : newAuthors) {
        bookDAO.save(
            "INSERT INTO tbl_book_authors (bookId,authorId) VALUES(?,?)",
            new Object[] {book.getBookId(), auth.getAuthorId()});
      }
      conn.commit();

      conn.close();
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }