/** * Remove a rating by user for movie * * @param u * @param m * @return */ public static boolean removeRating(User u, Movie m) { Logger.getInstance().log("Remove rating"); // Check if (u == null || m == null) { Logger.getInstance().warn("User or Movie is null"); return false; } Statement state; boolean result = false; try { state = ConnectSingleton.getInstance().createStatement(); // Compute the request String request = MessageFormat.format( DELETE_RATING_OF_A_MOVIE_BY_USING_TABLE_USER_RATING, Integer.toString(u.getId()), Integer.toString(m.getId())); Logger.getInstance().debug("EXECUTE REQUEST", "REMOVE RATING", request); result = state.execute(request); } catch (SQLException e) { Logger.getInstance().warn("Error with database"); e.printStackTrace(); } return result; }
public static int retrieveUserRating(User u, Movie m) { Logger.getInstance().log("Retrieve User Rating"); int rate = -1; if (u != null) { try { Statement state = ConnectSingleton.getInstance().createStatement(); String request = MessageFormat.format( RETRIEVE_USER_RATING_BY_USERID_AND_MOVIEID, Integer.toString(u.getId()), Integer.toString(m.getId())); Logger.getInstance().debug("RETRIEVE USER RATING", request); ResultSet rs = state.executeQuery(request); if (rs.next()) { rate = rs.getInt(1); } } catch (SQLException e) { Logger.getInstance().warn("Error with database"); e.printStackTrace(); } } return rate; }
/** * Retrieve all ratings for a user * * @param u * @return */ public static ArrayList<Rating> retrieveAllRatings(User u) { Logger.getInstance().log("Retrieve All Ratings"); // Check if (u == null) { Logger.getInstance().warn("User is null"); return null; } ArrayList<Rating> ratings = new ArrayList<Rating>(); Statement state; try { state = ConnectSingleton.getInstance().createStatement(); // Compute the request String request = MessageFormat.format(RETRIEVE_ALL_RATINGS, Integer.toString(u.getId())); Logger.getInstance().debug("EXECUTE REQUEST", "RETRIEVE RATINGS", request); ResultSet rs = state.executeQuery(request); Rating r; Movie m; while (rs.next()) { r = new Rating(); m = new Movie(); m.setTitle(rs.getString(6)); m.setId(rs.getInt(2)); m.setPicUrl(rs.getString("coverIMDB")); r.setUser(u); r.setMovie(m); r.setRate(rs.getInt(3)); r.setTime(rs.getLong(4)); ratings.add(r); } } catch (Exception e) { Logger.getInstance().warn("Error with database"); e.printStackTrace(); } return ratings; }
/** * Change the rating of a movie * * @param u * @param m * @param newRate */ public static boolean modifyRating(User u, Movie m, int newRate) { Logger.getInstance().log("Modify rating"); // Check if (u == null || m == null) { Logger.getInstance().warn("User or Movie is null"); return false; } Statement state; try { state = ConnectSingleton.getInstance().createStatement(); String check = MessageFormat.format( RETRIEVE_USER_RATING_BY_USERID_AND_MOVIEID, Integer.toString(u.getId()), Integer.toString(m.getId())); ResultSet rs = state.executeQuery(check); if (!rs.next()) { return addRating(u, m, newRate); } // Compute the request String request = MessageFormat.format( MODIFY_RATING_OF_A_MOVIE_BY_USING_TABLE_USER_RATING, Integer.toString(u.getId()), Integer.toString(m.getId()), newRate, new Date().getTime() + ""); Logger.getInstance().debug("EXECUTE REQUEST", "MODIFY RATING", request); int res = state.executeUpdate(request); return res != 0; } catch (SQLException e) { Logger.getInstance().warn("Error with database"); e.printStackTrace(); } return false; }