From 4583e5bdad6f8d5b1a36ba0c93f3bdb6b70ed27d Mon Sep 17 00:00:00 2001 From: HoolaBoola Date: Tue, 8 Dec 2020 01:35:55 +0200 Subject: [PATCH] Searching by tags works properly + adding tags is now possible for all types, not just books --- .../RecommendationApp.java | 99 +++++++++++--- .../recommendation_library/UserInterface.java | 53 +++++++- .../dao/DatabaseRecommendationDao.java | 125 +++++++++++++++++- .../dao/InMemoryRecommendationDao.java | 34 ++++- .../dao/RecommendationDao.java | 68 +++++++--- .../domain/DatabaseService.java | 35 ++++- 6 files changed, 356 insertions(+), 58 deletions(-) diff --git a/src/main/java/recommendation_library/RecommendationApp.java b/src/main/java/recommendation_library/RecommendationApp.java index a978d64..4780c08 100644 --- a/src/main/java/recommendation_library/RecommendationApp.java +++ b/src/main/java/recommendation_library/RecommendationApp.java @@ -5,6 +5,10 @@ */ package recommendation_library; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @@ -105,17 +109,24 @@ public boolean timeStampAlreadyExists(String videoTitle, String time) { public List listBooks() { List list = service.getAllBookRecommendations(); + + return listBooks(list); + } + + public List listBooks(List list) { List recommendationStrings = new ArrayList<>(); int i = 1; for (BookRecommendation r : list) { recommendationStrings.add(System.lineSeparator() + "Book " + i++ + System.lineSeparator() - + "Author: " + r.getAuthor() + System.lineSeparator() - + "Title: " + r.getTitle() + System.lineSeparator() - + "Description: " + r.getDescription() + System.lineSeparator() - + "ISBN: " + r.getIsbn() + System.lineSeparator() - + "Page count: " + r.getPageCount() + System.lineSeparator() - + "Added: " + r.getAddDate()); + + "Author: " + r.getAuthor() + System.lineSeparator() + + "Title: " + r.getTitle() + System.lineSeparator() + + "Description: " + r.getDescription() + System.lineSeparator() + + "ISBN: " + r.getIsbn() + System.lineSeparator() + + "Page count: " + r.getPageCount() + System.lineSeparator() + + "Added: " + r.getAddDate()); + System.out.println(i); + } return recommendationStrings; @@ -123,6 +134,11 @@ public List listBooks() { public List listVideos() { List list = service.getAllVideoRecommendations(); + + return listVideos(list); + } + + public List listVideos(List list) { List recommendationStrings = new ArrayList<>(); int i = 1; @@ -136,11 +152,11 @@ public List listVideos() { List timeStampStrings = listTimestampsForVideo(r.getTitle()); recommendationStrings.add(System.lineSeparator() + "Video " + i++ + System.lineSeparator() - + "Title: " + r.getTitle() + System.lineSeparator() - + "URL: " + r.getUrl() + System.lineSeparator() - + "Timestamps: " + System.lineSeparator() + timeStampStrings + System.lineSeparator() - + "Description: " + r.getDescription() + System.lineSeparator() - + "Added: " + r.getAddDate()); + + "Title: " + r.getTitle() + System.lineSeparator() + + "URL: " + r.getUrl() + System.lineSeparator() + + "Timestamps: " + System.lineSeparator() + timeStampStrings + System.lineSeparator() + + "Description: " + r.getDescription() + System.lineSeparator() + + "Added: " + r.getAddDate()); } return recommendationStrings; } @@ -157,38 +173,48 @@ public List listTimestampsForVideo(String videotitle) { public List listBlogs() { List list = service.getAllBlogRecommendations(); + + return listBlogs(list); + } + + public List listBlogs(List list) { List recommendationStrings = new ArrayList<>(); int i = 1; for (BlogRecommendation r : list) { recommendationStrings.add(System.lineSeparator() + "Blog " + i++ + System.lineSeparator() - + "Author: " + r.getAuthor() + System.lineSeparator() - + "Title: " + r.getTitle() + System.lineSeparator() - + "Description: " + r.getDescription() + System.lineSeparator() - + "URL: " + r.getUrl() + System.lineSeparator() - + "Added: " + r.getAddDate()); + + "Author: " + r.getAuthor() + System.lineSeparator() + + "Title: " + r.getTitle() + System.lineSeparator() + + "Description: " + r.getDescription() + System.lineSeparator() + + "URL: " + r.getUrl() + System.lineSeparator() + + "Added: " + r.getAddDate()); } return recommendationStrings; } - + public List listPodcasts() { List list = service.getAllPodcastRecommendations(); + return listPodcasts(list); + } + + public List listPodcasts(List list) { List recommendationStrings = new ArrayList<>(); int i = 1; for (PodcastRecommendation r : list) { recommendationStrings.add(System.lineSeparator() + "Podcast " + i++ + System.lineSeparator() - + "Podcast name: " + r.getPodcastName() + System.lineSeparator() - + "Author: " + r.getAuthor() + System.lineSeparator() - + "Title: " + r.getTitle() + System.lineSeparator() - + "Description: " + r.getDescription() + System.lineSeparator() - + "Added: " + r.getAddDate()); + + "Podcast name: " + r.getPodcastName() + System.lineSeparator() + + "Author: " + r.getAuthor() + System.lineSeparator() + + "Title: " + r.getTitle() + System.lineSeparator() + + "Description: " + r.getDescription() + System.lineSeparator() + + "Added: " + r.getAddDate()); } return recommendationStrings; } + public List listVideoTitles() { List videoRecommendationList = service.getAllVideoRecommendations(); List videoTitleList = new ArrayList<>(); @@ -311,4 +337,33 @@ public boolean deletePodcast(String titleToDelete) { return this.service.deletePodcastRecommendation(titleToDelete); } + public List getRecommendationsWithTag(String tag) { + ArrayList list = new ArrayList<>(); + + list.addAll(getBooksWithTag(tag)); + list.addAll(getVideosWithTag(tag)); + list.addAll(getPodcastsWithTag(tag)); + list.addAll(getBlogsWithTag(tag)); + return list; + } + + public List getBooksWithTag(String tag) { + return service.getBooksWithTag(tag); + } + + public List getVideosWithTag(String tag) { + + return service.getVideosWithTag(tag); + } + + public List getPodcastsWithTag(String tag) { + + return service.getPodcastsWithTag(tag); + } + + public List getBlogsWithTag(String tag) { + + return service.getBlogsWithTag(tag); + } + } diff --git a/src/main/java/recommendation_library/UserInterface.java b/src/main/java/recommendation_library/UserInterface.java index 8d7490a..8e48f8e 100644 --- a/src/main/java/recommendation_library/UserInterface.java +++ b/src/main/java/recommendation_library/UserInterface.java @@ -7,6 +7,7 @@ import java.util.*; +import recommendation_library.domain.Recommendation; import recommendation_library.io.IO; import recommendation_library.dao.RecommendationDao; import recommendation_library.domain.BookRecommendation; @@ -46,7 +47,11 @@ public UserInterface(IO io, RecommendationApp app) { */ public void run() { while (true) { + io.print(""); this.io.print("[1] Add recommendation, [2] List recommendations, [3] Edit recommendation, [4] Delete recommendation, [5] Exit"); + io.print("[10] Search recommendations by tag"); + io.print(""); + String input = io.nextLine(); if(validateInput(input)) { int numericInput = Integer.valueOf(input); @@ -96,6 +101,9 @@ public void checkInput(int input) { case 5: listTags(); break; + case 10: + searchByTag(); + break; default: io.print("Unknown command"); } @@ -324,7 +332,10 @@ public void addPodcast() { } public void list() { + io.print(""); this.io.print("[1] List all recommendations, [2] List books, [3] List videos, [4] List blogs, [5] List podcasts, [6] List tags"); + io.print(""); + String input = String.valueOf(io.nextLine()); switch(input) { @@ -394,6 +405,44 @@ public void listTags() { } } + public void searchByTag() { + + io.print("Which tag do you want to search?"); + String tag = io.nextLine(); + + searchTagsWithType(tag); + } + + public void searchTagsWithType(String tag) { + io.print(""); + io.print("[1] List all recommendations, [2] List books, [3] List videos, [4] List blogs, [5] List podcasts"); + io.print(""); + String type = io.nextLine(); + List recommendations = new ArrayList<>(); + switch (type) { + case "1": + recommendations = recommendationApp.listBooks(recommendationApp.getBooksWithTag(tag)); + recommendations.addAll(recommendationApp.listVideos(recommendationApp.getVideosWithTag(tag))); + recommendations.addAll(recommendationApp.listBlogs(recommendationApp.getBlogsWithTag(tag))); + recommendations.addAll(recommendationApp.listPodcasts(recommendationApp.getPodcastsWithTag(tag))); + break; + case "2": + recommendations = recommendationApp.listBooks(recommendationApp.getBooksWithTag(tag)); + break; + case "3": + recommendations = recommendationApp.listVideos(recommendationApp.getVideosWithTag(tag)); + break; + case "4": + recommendations = recommendationApp.listBlogs(recommendationApp.getBlogsWithTag(tag)); + break; + case "5": + recommendations = recommendationApp.listPodcasts(recommendationApp.getPodcastsWithTag(tag)); + break; + } + + recommendations.forEach(io::print); + } + /** * list all recommendations contained within the library */ @@ -600,6 +649,7 @@ public void editBlog() { this.io.print("Recommendation with the given title doesn't exist! Try again: "); } } + public void editPodcast() { List stringFieldNames = Arrays.asList("1", "2", "3", "4"); @@ -650,8 +700,7 @@ public void editPodcast() { this.io.print("Recommendation with the given title doesn't exist! Try again: "); } } - - + public void deleteBook() { this.io.print("Enter the title of the recommendation you wish to delete:\nTitles in your library:"); List allBookTitles = recommendationApp.listBookTitles(); diff --git a/src/main/java/recommendation_library/dao/DatabaseRecommendationDao.java b/src/main/java/recommendation_library/dao/DatabaseRecommendationDao.java index ee917b5..22d7f8b 100644 --- a/src/main/java/recommendation_library/dao/DatabaseRecommendationDao.java +++ b/src/main/java/recommendation_library/dao/DatabaseRecommendationDao.java @@ -738,7 +738,7 @@ public List getAllTagsForBook(int bookId) { @Override public List getAllTagsForVideo(int videoId) { ArrayList tags = new ArrayList<>(); - String sql = "SELECT * FROM tags INNER JOIN videosTags ON tags.id = videosTags.tags_id WHERE videosTags.videos_id = ?"; + String sql = "SELECT * FROM tags INNER JOIN videosTags ON tags.id = videosTags.tags_id WHERE videosTags.books_id = ?"; try { Connection connection = this.connect(); PreparedStatement pstatement = connection.prepareStatement(sql); @@ -757,7 +757,7 @@ public List getAllTagsForVideo(int videoId) { @Override public List getAllTagsForBlog(int blogId) { ArrayList tags = new ArrayList<>(); - String sql = "SELECT * FROM tags INNER JOIN blogsTags ON tags.id = blogsTags.tags_id WHERE blogsTags.blogs_id = ?"; + String sql = "SELECT * FROM tags INNER JOIN blogsTags ON tags.id = blogsTags.tags_id WHERE blogsTags.books_id = ?"; try { Connection connection = this.connect(); PreparedStatement pstatement = connection.prepareStatement(sql); @@ -776,7 +776,7 @@ public List getAllTagsForBlog(int blogId) { @Override public List getAllTagsForPodcast(int podcastId) { ArrayList tags = new ArrayList<>(); - String sql = "SELECT * FROM tags INNER JOIN podcastsTags ON tags.id = podcastsTags.tags_id WHERE podcastsTags.blogs_id = ?"; + String sql = "SELECT * FROM tags INNER JOIN podcastsTags ON tags.id = podcastsTags.tags_id WHERE podcastsTags.books_id = ?"; try { Connection connection = this.connect(); PreparedStatement pstatement = connection.prepareStatement(sql); @@ -819,7 +819,7 @@ public void addTagToVideo(int videoId, String tagText) { createTag(tagText); tagId = getTagId(tagText); } - String booksTags = "INSERT INTO videosTags(videos_id, tags_id) VALUES(?,?)"; + String booksTags = "INSERT INTO videosTags(books_id, tags_id) VALUES(?,?)"; try { Connection conn = this.connect(); PreparedStatement statement = conn.prepareStatement(booksTags); @@ -840,7 +840,7 @@ public void addTagToBlog(int blogId, String tagText) { createTag(tagText); tagId = getTagId(tagText); } - String booksTags = "INSERT INTO blogsTags(blogs_id, tags_id) VALUES(?,?)"; + String booksTags = "INSERT INTO blogsTags(books_id, tags_id) VALUES(?,?)"; try { Connection conn = this.connect(); PreparedStatement statement = conn.prepareStatement(booksTags); @@ -861,7 +861,7 @@ public void addTagToPodcast(int podcastId, String tagText) { createTag(tagText); tagId = getTagId(tagText); } - String booksTags = "INSERT INTO podcastsTags(podcasts_id, tags_id) VALUES(?,?)"; + String booksTags = "INSERT INTO podcastsTags(books_id, tags_id) VALUES(?,?)"; try { Connection conn = this.connect(); PreparedStatement statement = conn.prepareStatement(booksTags); @@ -872,7 +872,7 @@ public void addTagToPodcast(int podcastId, String tagText) { } catch (SQLException e) { System.out.println(e.getMessage()); } - + } @Override @@ -926,4 +926,115 @@ public List getAllTags() { return tags; } + @Override + public List getRecommendationsWithTag(String tag) { + ArrayList list = new ArrayList<>(); + + list.addAll(getBooksWithTag(tag)); + list.addAll(getVideosWithTag(tag)); + list.addAll(getPodcastsWithTag(tag)); + list.addAll(getBlogsWithTag(tag)); + return list; + } + + public List getBooksWithTag(String tag) { + String sql = "SELECT Books.id, author, title, description, isbn, pagecount, created" + + " from Books" + + " join BookStags on Books.id = BooksTags.books_id" + + " join Tags on tags_id = Tags.id " + + " where tagText = ?"; + + ArrayList books = new ArrayList<>(); + try { + Connection connection = this.connect(); + PreparedStatement statement = connection.prepareStatement(sql); + statement.setString(1, tag); + ResultSet result = statement.executeQuery(); + while (result.next()) { + books.add(new BookRecommendation(result.getInt("id"), result.getString("author"), + result.getString("title"), result.getString("description"), + result.getString("isbn"), result.getInt("pageCount"), result.getString("created"))); + } + connection.close(); + } catch (SQLException e) { + System.out.println(e.getMessage()); + } + + return books; + } + + public List getVideosWithTag(String tag) { + String sql = "SELECT Videos.id, url, title, description, created" + + " from Videos" + + " join VideoStags on Videos.id = VideosTags.books_id" + + " join tags on tags_id = tags.id " + + " where tags.tagText = ?"; + + ArrayList videos = new ArrayList<>(); + try { + Connection connection = this.connect(); + PreparedStatement statement = connection.prepareStatement(sql); + statement.setString(1, tag); + ResultSet result = statement.executeQuery(); + while (result.next()) { + videos.add(new VideoRecommendation(result.getInt("id"), result.getString("url"), + result.getString("title"), result.getString("description"), + result.getString("created"))); + } + connection.close(); + } catch (SQLException e) { + System.out.println(e.getMessage()); + } + return videos; + } + + public List getPodcastsWithTag(String tag) { + String sql = "SELECT Podcasts.id, author, title, description, name, created" + + " from Podcasts" + + " join PodcastsTags on Podcasts.id = PodcastsTags.books_id" + + " join tags on tags_id = tags.id " + + " where tags.tagText = ?"; + ArrayList podcasts = new ArrayList<>(); + try { + Connection connection = this.connect(); + PreparedStatement statement = connection.prepareStatement(sql); + statement.setString(1, tag); + + ResultSet result = statement.executeQuery(); + while (result.next()) { + podcasts.add(new PodcastRecommendation(result.getInt("id"), result.getString("author"), + result.getString("title"), result.getString("description"), + result.getString("name"), result.getString("created"))); + } + connection.close(); + } catch (SQLException e) { + System.out.println(e.getMessage()); + } + return podcasts; + } + + public List getBlogsWithTag(String tag) { + String sql = "SELECT Blogs.id, url, author, title, description, created" + + " from Blogs" + + " join BlogsTags on Blogs.id = BlogsTags.books_id" + + " join tags on tags_id = tags.id " + + " where tags.tagText = ?"; + ArrayList blogs = new ArrayList<>(); + try { + Connection connection = this.connect(); + PreparedStatement statement = connection.prepareStatement(sql); + statement.setString(1, tag); + + ResultSet result = statement.executeQuery(); + while (result.next()) { + blogs.add(new BlogRecommendation(result.getInt("id"), result.getString("author"), + result.getString("url"), result.getString("title"), + result.getString("description"), result.getString("created"))); + } + connection.close(); + } catch (SQLException e) { + System.out.println(e.getMessage()); + } + return blogs; + } } diff --git a/src/main/java/recommendation_library/dao/InMemoryRecommendationDao.java b/src/main/java/recommendation_library/dao/InMemoryRecommendationDao.java index 051914e..994891a 100644 --- a/src/main/java/recommendation_library/dao/InMemoryRecommendationDao.java +++ b/src/main/java/recommendation_library/dao/InMemoryRecommendationDao.java @@ -5,16 +5,10 @@ */ package recommendation_library.dao; -import recommendation_library.domain.BookRecommendation; +import recommendation_library.domain.*; import java.util.*; import java.util.function.Function; -import recommendation_library.domain.BlogRecommendation; -import recommendation_library.domain.PodcastRecommendation; -import recommendation_library.domain.Tag; - -import recommendation_library.domain.TimeMemory; -import recommendation_library.domain.VideoRecommendation; /** * @author jenni.makinen @@ -416,6 +410,32 @@ public int getTagId(String tagText) { return 0; } + // not implemented yet + @Override + public List getRecommendationsWithTag(String tag) { + return null; + } + + @Override + public List getBooksWithTag(String tag) { + return null; + } + + @Override + public List getVideosWithTag(String tag) { + return null; + } + + @Override + public List getPodcastsWithTag(String tag) { + return null; + } + + @Override + public List getBlogsWithTag(String tag) { + return null; + } + @Override public List getAllTags() { return this.tags; diff --git a/src/main/java/recommendation_library/dao/RecommendationDao.java b/src/main/java/recommendation_library/dao/RecommendationDao.java index 6fbee61..e243075 100644 --- a/src/main/java/recommendation_library/dao/RecommendationDao.java +++ b/src/main/java/recommendation_library/dao/RecommendationDao.java @@ -5,62 +5,96 @@ */ package recommendation_library.dao; -import recommendation_library.domain.BookRecommendation; +import recommendation_library.domain.*; + +import java.util.ArrayList; import java.util.List; -import recommendation_library.domain.BlogRecommendation; -import recommendation_library.domain.PodcastRecommendation; -import recommendation_library.domain.Tag; -import recommendation_library.domain.TimeMemory; -import recommendation_library.domain.Type; -import recommendation_library.domain.VideoRecommendation; /** - * * @author anadis */ public interface RecommendationDao { - + void createBookRecommendation(String author, String title, String description, String isbn, int pageCount); + void createVideoRecommendation(String url, String title, String description); + void createBlogRecommendation(String url, String title, String author, String description); + void createPodcastRecommendation(String author, String title, String description, String name); + void createTag(String tagText); - + void addTagToBook(int bookId, String tagText); + void addTagToVideo(int videoId, String tagText); + void addTagToBlog(int blogId, String tagText); + void addTagToPodcast(int podcastId, String tagText); - + List getAllBookRecommendations(); + List getAllVideoRecommendations(); + List getAllBlogRecommendations(); + List getAllPodcastRecommendations(); + List getAllTags(); + List getAllTimestampsForVideo(int videoId); + List getAllTagsForBook(int bookId); + List getAllTagsForVideo(int videoId); + List getAllTagsForBlog(int blogId); + List getAllTagsForPodcast(int podcastId); - + void editBookRecommendation(String title, String fieldToBeEdited, String newValue); + void editVideoRecommendation(String title, String fieldToBeEdited, String newValue); + void editTimestampForVideo(int videoId, int timeStampId, String fieldToBeEdited, String newValue); + void editBlogRecommendation(String title, String fieldToBeEdited, String newValue); + void editPodcastRecommendation(String title, String fieldToBeEdited, String newValue); - + void deleteBookByTitle(String title); + void deleteTimestamp(int videoId, int timeStampId); - void deleteVideoByTitle(String title); + + void deleteVideoByTitle(String title); + void deleteBlogByTitle(String title); + void deletePodcastByTitle(String title); - + int getVideoIdByTitle(String title); + int getBookIdByTitle(String title); + int getBlogIdByTitle(String title); + int getPodcastIdByTitle(String title); - + int getTimestampIdByTitle(int videoId, String timestamp); + void addTimeStampToVideo(int videoId, String timestamp, String comment); - + int getTagId(String tagText); + + List getRecommendationsWithTag(String tag); + + + List getBooksWithTag(String tag); + + List getVideosWithTag(String tag); + + List getPodcastsWithTag(String tag); + + List getBlogsWithTag(String tag); } diff --git a/src/main/java/recommendation_library/domain/DatabaseService.java b/src/main/java/recommendation_library/domain/DatabaseService.java index ca7dbea..445377f 100644 --- a/src/main/java/recommendation_library/domain/DatabaseService.java +++ b/src/main/java/recommendation_library/domain/DatabaseService.java @@ -124,7 +124,7 @@ public boolean addVideo(String url, String title, String description, List getAllTags() { return dao.getAllTags(); } + + public List getRecommendationsWithTag(String tag) { + ArrayList list = new ArrayList<>(); + + list.addAll(getBooksWithTag(tag)); + list.addAll(getVideosWithTag(tag)); + list.addAll(getPodcastsWithTag(tag)); + list.addAll(getBlogsWithTag(tag)); + return list; + } + + public List getBooksWithTag(String tag) { + return dao.getBooksWithTag(tag); + } + + public List getVideosWithTag(String tag) { + + return dao.getVideosWithTag(tag); + } + + public List getPodcastsWithTag(String tag) { + + return dao.getPodcastsWithTag(tag); + } + + public List getBlogsWithTag(String tag) { + + return dao.getBlogsWithTag(tag); + } }