Which option do you like the most?

Posted by spderosso on Stack Overflow See other posts from Stack Overflow or by spderosso
Published on 2010-04-05T18:15:36Z Indexed on 2010/04/05 18:33 UTC
Read the original article Hit count: 417

Filed under:
|
|
|
|

I need to show a list of movies ordered by the date they were registered in the system and the amount of comments users have made about them. E.g:

Title | Register Date | # of comments
Gladiator | 02-01-2010 | 30
Matrix | 01-02-2010 | 20

It is a web application that follows MVC. So, I have the object "Movie", there is a MovieManager interface that is used for retrieving/persisting data to the database and a servlet RecentlyAddedMovies.java that forwards to a .jsp to render the list. I will explain my problem by showing an example of how I am doing another similar task: Showing top ranked movies.

Regarding the MovieManager:

/**
 * Returns a collection of the x best-ranked movies
 * @param x the amount of movies to return
 * @return A collection of movies
 * @throws SQLException
 */
public Collection<Movie> getTopX(int x) throws SQLException;

The servlet:

public class Top5 extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        MovieManager movManager = JDBCMovieManager.getInstance();
        try {
            req.setAttribute("movies", movManager.getTopX(5));
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        req.getRequestDispatcher("listMovies.jsp").forward(req, resp);  

    }
}

And the listMovies.jsp has is some place something like:

<c:forEach var="movie" items="${movies}">
    ...
    <c:out value="${movie.title}"/>     
    ...
</c:forEach>

The difference between the top5 problem and the problem I am trying to solve is that with the top5 thing all the data I needed to show in the view where part of the "Movie" object, so the MovieManager simply returned a Collection of Movies. Now, the MovieManager must retrieve an ordered list of (Movie, # of comments). The Movie object has a collection of Comment associated but of course, it lacks an instance variable with the length of that collection. The following things came into my mind:

a) Create a MovieView object or something like that, that is only used by the "View" and "Controller" layer of the MVC which is not part of the "Model" in the sense that is used only for easy retrieval and display of information. This MovieView object will have a title, registerDate and # of Comments so the thing would look something like this:

     /**
     * Returns a collection of the x most recently added movies
     * @param x the amount of movies to return
     * @return A collection of movies
     * @throws SQLException
     */
    public Collection<Movie2> getXRecentlyAddedMovies(int x) throws SQLException;

The .jsp:

<c:forEach var="movie2" items="${movies2}">
        ...
        <c:out value="${movie2.title}"/>    
        <c:out value="${movie2.registerDate}"/>
        <c:out value="${movie2.noOfComments}"/>     
        ...
  </c:forEach>

b) Add to the Collection the movie has, empty Comments (there is no actual need to retrieve all the comment data from the database) so that the comment's collection.size() will show the number of comments that movie has. (This sounds horrible to me)

c) Add to the "Movie" object an instance field noOfComments with the setter and getter that will help with this type of cases even if, looking only from the object point of view, it is a redundant field since it already has a Collection that is ".size()"able .

d) Make the MovieManager retrieve some structure like a Map or a Collection of a two component array or something like that that will contain both a Movie object and the associated number of comments and with JSLT iterate over it and show it accordingly.

© Stack Overflow or respective owner

Related posts about java

Related posts about jstl