@RequestMapping(
     value = "/deleteAuthor",
     method = {RequestMethod.GET, RequestMethod.POST},
     consumes = "application/json",
     produces = "application/json")
 public List<Author> deleteAuthor(@RequestBody Author author) {
   adao.deleteAuthor(author);
   return adao.getAllAuthors(1, 5);
 }
 public List<Author> ListAuthors() throws Exception {
   ConnectionUtil c = new ConnectionUtil();
   Connection conn = c.createConnection();
   try {
     AuthorDAO adao = new AuthorDAO(conn);
     List<Author> authors = adao.readAll();
     conn.commit();
     return authors;
   } catch (Exception e) {
     e.printStackTrace();
     return null;
   } finally {
     conn.close();
   }
 }
 @RequestMapping(
     value = "/getAuthorsByBookId/{bookId}",
     method = {RequestMethod.GET, RequestMethod.POST},
     produces = "application/json")
 public List<Author> getAuthorsByBookId(@PathVariable int bookId) {
   return adao.getAuthorsByBookId(bookId);
 }
 @RequestMapping(
     value = "/getAuthorsCount",
     method = {RequestMethod.GET, RequestMethod.POST},
     produces = "application/json")
 public int getAuthorsCount() {
   return adao.getAuthorsCount();
 }
 @RequestMapping(
     value = "/getAuthorsById/{id}",
     method = {RequestMethod.GET, RequestMethod.POST},
     produces = "application/json")
 public Author getAuthorById(@PathVariable int authorId) {
   return adao.getAuthorById(authorId);
 }
 @RequestMapping(
     value = "/editAuthor",
     method = {RequestMethod.GET, RequestMethod.POST},
     consumes = "application/json")
 public void editAuthor(@RequestBody List<Author> authors) {
   for (Author author : authors) adao.updateAuthor(author);
 }
 /*Admin Author*/
 @RequestMapping(
     value = "/getAuthors/{pageNo}/{pageSize}",
     method = {RequestMethod.GET, RequestMethod.POST},
     produces = "application/json")
 public List<Author> getAuthors(@PathVariable int pageNo, @PathVariable int pageSize) {
   return adao.getAllAuthors(pageNo, pageSize);
 }
Beispiel #8
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();
    }
  }
Beispiel #9
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();
    }
  }
 @RequestMapping(
     value = "/getAuthorsByName/{searchString}/{pageNo}/{pageSize}",
     method = {RequestMethod.GET, RequestMethod.POST},
     produces = "application/json")
 public List<Author> getAuthorsByName(
     @PathVariable String searchString, @PathVariable int pageNo, @PathVariable int pageSize) {
   return adao.getAuthorsByName(searchString, pageNo, pageSize);
 }
  /** *************************************************************************************** */
  public Author ListOneAuthor(int authorId) throws Exception {

    ConnectionUtil c = new ConnectionUtil();
    Connection conn = c.createConnection();

    try {

      AuthorDAO adao = new AuthorDAO(conn);
      Author author = adao.readOne(authorId);
      return author;

    } catch (Exception e) {
      // TODO: handle exception
      e.printStackTrace();
      return null;
    } finally {
      conn.close();
    }
  }
Beispiel #12
0
  private Author showAllAuthors() {
    Author author = null;
    try {
      Connection conn = getConnection();
      AuthorDAO authorDAO = new AuthorDAO(conn);
      List<Author> allAuthors = authorDAO.readAll();
      ArrayList<String> actions = new ArrayList<String>();
      actions.add("Cancel");
      int choice = getChoiceNumber(allAuthors, actions);
      if (choice > 0 && choice <= allAuthors.size()) {
        author = allAuthors.get(choice - 1);
      }
      conn.close();
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    return author;
  }
Beispiel #13
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();
    }
  }
 /** *************************************************************************************** */
 public void DeleteAuthor(Author author) throws Exception {
   ConnectionUtil c = new ConnectionUtil();
   Connection conn = c.createConnection();
   try {
     if (author == null
         || author.getAuthorName() == null
         || author.getAuthorName().length() == 0
         || author.getAuthorName().length() > 45) {
       throw new Exception("The Author cannot be null");
     } else {
       AuthorDAO adao = new AuthorDAO(conn);
       adao.delete(author);
       conn.commit();
     }
   } catch (Exception e) {
     e.printStackTrace();
     conn.rollback();
   } finally {
     conn.close();
   }
 }
Beispiel #15
0
  private void addAuthor() {
    System.out.println("Enter new author's name: [N/A to cancel]");
    String name = getInputString();
    if (!name.equals("N/A")) {
      Author author = new Author();
      author.setAuthorName(name);
      try {
        Connection conn = getConnection();
        try {
          AuthorDAO authorDAO = new AuthorDAO(conn);
          authorDAO.create(author);
          conn.commit();
          conn.close();
        } catch (Exception e) {
          conn.rollback();
        }

      } catch (Exception e) {
        System.err.println("Error while connecting to Database");
        e.printStackTrace();
      }
    }
  }
Beispiel #16
0
 private void editAuthor(Author author) {
   try {
     Connection conn = getConnection();
     System.out.println("Enter new name for " + author + " :[N/A to cancel]");
     String name = getInputString();
     if (!name.equals("N/A")) {
       try {
         System.out.println("Committed!!!!!!!!!!!!!!!!!!!");
         AuthorDAO authorDAO = new AuthorDAO(conn);
         author.setAuthorName(name);
         authorDAO.update(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();
   }
 }
Beispiel #17
0
  // receive the necessary information to add a book
  private void addBook() {
    Connection conn;
    try {
      conn = getConnection();
      Book book = new Book();
      BookDAO bookDAO = new BookDAO(conn);
      AuthorDAO authorDAO = new AuthorDAO(conn);
      PublisherDAO pubDAO = new PublisherDAO(conn);
      GenreDAO genreDAO = new GenreDAO(conn);
      System.out.println("What is the title of the new book? [N/A to cancel operation]");
      String title = getInputString();
      if (title.equals("N/A")) {
        return;
      }
      List<Author> allAuthors = authorDAO.readAll();
      List<Author> bookAuthors = new ArrayList<Author>();
      List<Genre> allGenres = genreDAO.readAll();
      List<Genre> bookGenres = new ArrayList<Genre>();
      List<Publisher> publishers = pubDAO.readAll();
      Publisher bookPublisher = new Publisher();
      ArrayList<String> questions = new ArrayList<String>();
      ArrayList<String> actions = new ArrayList<String>();
      actions.add("Skip this detail");
      actions.add("Cancel whole operation");
      questions.add("Yes");
      questions.add("No");
      boolean more = true;

      do {
        System.out.println("Choose Author");
        actions.set(0, "Skip adding an author to this book");
        int author = getChoiceNumber(allAuthors, actions);
        if (author == -1) {
          more = false;
        } else if (author == -2) {
          return;
        } else {
          bookAuthors.add(allAuthors.get(author - 1));
          allAuthors.remove(author - 1);
          System.out.println("More authors?");
          displayOptions(questions);
          int next = getInputInt(1, 2);
          more = next == 1;
        }

      } while (more);
      more = true;
      do {
        System.out.println("Choose Genre");
        actions.set(0, "Skip adding a genre to this book");
        int genre = getChoiceNumber(allGenres, actions);
        if (genre == -1) {
          more = false;
        } else if (genre == -2) {
          return;
        } else {
          bookGenres.add(allGenres.get(genre - 1));
          allGenres.remove(genre - 1);
          System.out.println("More Genres?");
          displayOptions(questions);
          int next = getInputInt(1, 2);
          more = next == 1;
        }

      } while (more);
      System.out.println("Choose Publisher");
      actions.set(0, "Skip adding a publisher to this book");
      int pub = getChoiceNumber(publishers, actions);
      if (pub == -1) {
        more = false;
      } else if (pub == -2) {
        return;
      } else {
        bookPublisher = publishers.get(pub - 1);
      }
      book.setTitle(title);
      book.setAuthors(bookAuthors);
      book.setGenres(bookGenres);
      book.setPublisher(bookPublisher);
      bookDAO.create(book);
      conn.commit();
      conn.close();
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }