@Override
  public List extractData(ResultSet rs) throws Exception {
    List<Genre> genre = new ArrayList<Genre>();

    while (rs.next()) {
      Genre a = new Genre();
      a.setGenreId(rs.getInt("genre_id"));
      a.setGenreName(rs.getString("genre_name"));

      genre.add(a);
    }
    return genre;
  }
Example #2
0
  private void deleteBookGenre(Book book) {
    Connection conn;
    try {
      conn = getConnection();
      GenreDAO authorDAO = new GenreDAO(conn);
      BookDAO bookDAO = new BookDAO(conn);
      List<Genre> allGenres =
          (List<Genre>)
              authorDAO.read(
                  "SELECT * FROM tbl_genre JOIN tbl_book_genres ON tbl_genre.genre_id = tbl_book_genres.genre_id WHERE bookId = ?",
                  new Object[] {book.getBookId()});
      ArrayList<Genre> toDelete = new ArrayList<Genre>();
      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 Genre");
        int genre = getChoiceNumber(allGenres, actions);
        if (genre == -1) {
          more = false;
        } else {
          toDelete.add(allGenres.get(genre - 1));
          allGenres.remove(genre - 1);
          if (allGenres.isEmpty()) {
            more = false;
            break;
          }
          System.out.println("Delete more genres?");
          displayOptions(questions);
          int next = getInputInt(1, 2);
          more = next == 1;
        }

      } while (more);
      List<Author> curAuthors = book.getAuthors();
      for (Genre genre : toDelete) {
        authorDAO.save(
            "DELETE FROM tbl_book_genres WHERE genre_id = ? AND bookId =?",
            new Object[] {genre.getGenreId(), book.getBookId()});
      }
      conn.commit();
      conn.close();
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
Example #3
0
  private void Deletegenre() throws SQLException {
    boolean exit_Add = false;

    int id;
    Map<Integer, String> m = as.listGenre2();
    System.out.println("\nGenre ID | Genre Name ");
    System.out.println("---------------------------------------");
    for (Map.Entry<Integer, String> map : m.entrySet()) {

      System.out.println(map.getKey() + "\t  " + map.getValue());
    }
    do {

      System.out.println("Enter the Genre Id you want to delete:");

      try {

        id = Integer.parseInt(sc.nextLine().trim());

        if (m.containsKey(id)) {
          Genre g = new Genre();
          g.setGenreId(id);
          as.deleteGenre(g);
          System.out.println("Deleted successfuly");
        } else {
          System.out.println("This Id does not exist.");
        }
      } catch (Exception e) {
        System.out.println("INFO:Should be an integer!");
      }

      if (!exit_Add) {
        System.out.println();
        System.out.println();
        System.out.println("Press ' q '  to return or any other key to continue");
        String option = (sc.nextLine());
        if (option.length() > 0) {
          switch (option.charAt(0)) {
            case 'q':
              exit_Add = true;
              break;

            default:
              exit_Add = false;
              break;
          }
        }
      }

    } while (!exit_Add);
  }
Example #4
0
  private void addBookGenre(Book book) {
    Connection conn;
    try {
      conn = getConnection();
      GenreDAO genreDAO = new GenreDAO(conn);
      BookDAO bookDAO = new BookDAO(conn);
      List<Genre> allGenres =
          (List<Genre>)
              genreDAO.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<Genre> newGenres = new ArrayList<Genre>();
      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 Genre");
        int author = getChoiceNumber(allGenres, actions);
        if (author == -1) {
          more = false;
        } else {
          newGenres.add(allGenres.get(author - 1));
          allGenres.remove(author - 1);
          if (allGenres.isEmpty()) {
            more = false;
            break;
          }
          System.out.println("Add more genres?");
          displayOptions(questions);
          int next = getInputInt(1, 2);
          more = next == 1;
        }
      } while (more);
      for (Genre genre : newGenres) {
        bookDAO.save(
            "INSERT INTO tbl_book_genres (bookId,genre_id) VALUES(?,?)",
            new Object[] {book.getBookId(), genre.getGenreId()});
      }
      conn.commit();
      conn.close();
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
Example #5
0
  private void Addgenre() throws Exception {
    boolean exit_Add = false;

    String name;

    Map<Integer, String> m = as.listGenre2();
    do {

      System.out.println("Enter the Genre Name:");

      name = sc.nextLine();

      if (!m.containsValue(name)) {
        Genre g = new Genre();
        g.setGenreName(name);
        try {
          as.createGenre(g);
          System.out.println("Added successfully");
          System.out.println("---------------------------------------------");
          exit_Add = true;
        } catch (Exception e) {
          System.out.println(e.getMessage());
        }
      } else {
        System.out.println("This Genre already exists.");
      }

      if (!exit_Add) {
        System.out.println();
        System.out.println();
        System.out.println("Press ' q '  to return or any other key to continue");
        String option = (sc.nextLine());
        if (option.length() > 0) {
          switch (option.charAt(0)) {
            case 'q':
              exit_Add = true;
              break;

            default:
              exit_Add = false;
              break;
          }
        }
      }

    } while (!exit_Add);
  }
Example #6
0
  private void Updategenre2(int id) throws SQLException {
    boolean exit_Add = false;
    Map<Integer, String> m = as.listGenre2();
    String name;
    do {
      System.out.println();
      System.out.println("Enter the new Genre Name:");

      name = sc.nextLine();

      if (!m.containsKey(name)) {
        Genre g = new Genre();
        g.setGenreId(id);
        g.setGenreName(name);
        try {
          as.updateGenre(g);
          System.out.println("Update successful");
          exit_Add = true;
        } catch (Exception e) {
          System.out.println(e.getMessage());
        }

      } else {
        System.out.println("Update successful");
      }

      if (!exit_Add) {
        System.out.println();
        System.out.println();
        System.out.println("Press ' q '  to return or any other key to continue");
        String option = (sc.nextLine());
        if (option.length() > 0) {
          switch (option.charAt(0)) {
            case 'q':
              exit_Add = true;
              break;

            default:
              exit_Add = false;
              break;
          }
        }
      }

    } while (!exit_Add);
  }
Example #7
0
 @Override
 public List extractData(ResultSet rs) throws Exception {
   List<Genre> genres = new ArrayList<Genre>();
   BookDAO bdao = new BookDAO(getConnection());
   while (rs.next()) {
     Genre g = new Genre();
     g.setGenreId(rs.getInt("genre_id"));
     g.setGenreName(rs.getString("genre_name"));
     @SuppressWarnings("unchecked")
     List<Book> books =
         (List<Book>)
             bdao.readFirstLevel(
                 "select * from tbl_book where bookId In"
                     + "(select bookId from tbl_book_genres where genreId=?)",
                 new Object[] {rs.getInt("genreId")});
     genres.add(g);
   }
   return genres;
 }
 /**
  * ************************************GENRE
  * SECTION********************************************************************
  */
 public void createGenre(Genre genre) throws Exception {
   ConnectionUtil c = new ConnectionUtil();
   Connection conn = c.createConnection();
   try {
     if (genre == null
         || genre.getGenreName() == null
         || genre.getGenreName().length() == 0
         || genre.getGenreName().length() > 45) {
       throw new Exception("Genre Name cannot be empty or more than 45 Chars");
     } else {
       GenreDAO gdao = new GenreDAO(conn);
       gdao.create(genre);
       conn.commit();
     }
   } catch (Exception e) {
     e.printStackTrace();
     conn.rollback();
   } finally {
     conn.close();
   }
 }
Example #9
0
 public void delete(Genre genre) throws Exception {
   save("delete from tbl_genre where genre_id = ?", new Object[] {genre.getGenreName()});
 }
Example #10
0
 public void update(Genre genre) throws Exception {
   save(
       "update tbl_genre set genre_name = ? where genre_id = ?",
       new Object[] {genre.getGenreName()});
 }
Example #11
0
 public void create(Genre genre) throws Exception {
   save("insert into tbl_genre (genre_name) values(?)", new Object[] {genre.getGenreName()});
 }