// UPDATE
  public void update(Author author) throws Exception {
    Query query = new Query();
    query.addCriteria(Criteria.where("_id").is(author.getAuthorId()));

    Update update = new Update();
    update.set("authorName", author.getAuthorName());

    // FindAndModifyOptions().returnNew(true) = newly updated document
    // FindAndModifyOptions().returnNew(false) = old document (not update yet)
    mongoOps.findAndModify(
        query, update, new FindAndModifyOptions().returnNew(true), Author.class, AUTHOR_COLLECTION);
  }
예제 #2
0
  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();
    }
  }
예제 #3
0
  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();
    }
  }
예제 #4
0
  private void deleteAuthor(Author author) {
    try {
      Connection conn = getConnection();
      try {
        AuthorDAO authorDAO = new AuthorDAO(conn);
        BookDAO bookDAO = new BookDAO(conn);
        List<Book> authorBooks =
            (List<Book>)
                bookDAO.read(
                    "SELECT * FROM tbl_book WHERE tbl_book.bookId IN (SELECT tbl_book_authors.bookId FROM tbl_book_authors WHERE authorId = ?)",
                    new Object[] {author.getAuthorId()});
        if (authorBooks.size() > 0) {
          ArrayList<String> answers = new ArrayList<String>();
          answers.add("No, nevermind");
          answers.add("Yes, delete this author");
          System.out.println(
              author + " has (co)authored " + authorBooks.size() + " books in our records");
          System.out.println(
              "Are you sure you still want to delete? Some books may remain with no author");
          displayOptions(answers);
          int in = getInputInt(1, 2);
          if (in == 1) {
            return;
          }
        }
        System.out.println("here");

        authorDAO.delete(author);
        conn.commit();
        conn.close();
      } catch (Exception e) {
        conn.rollback();
        conn.close();
      }

    } catch (Exception e) {
      // TODO Auto-generated catch block
      System.err.println("Error while connecting to database");
      e.printStackTrace();
    }
  }
예제 #5
0
 public void delete(Author author) throws Exception {
   save("delete from tbl_author where authorId = ?", new Object[] {author.getAuthorId()});
 }
예제 #6
0
 public void update(Author author) throws Exception {
   save(
       "update tbl_author set authorName = ? where authorId = ?",
       new Object[] {author.getAuthorName(), author.getAuthorId()});
 }