How to remove item collection using current context JPA, JSF, Java Collections
I want to remove comment that do not match the current movie;
info
jpaController = (CommentFacade) facesContext.getApplication().getELResolver().getValue(facesContext.getELContext(), null, "commentJpa");
movieController = (MovieController) facesContext.getApplication().evaluateExpressionGet(facesContext, "#{movie}", MovieController.class);
private List<Comment> userCommentItems = null;
private Comment userComment = null;
public List<Comment> getUserCommentItems() {
if (userCommentItems == null) {
getUserPagingInfo();
Vector comments = (Vector) jpaController.findAll();
Vector v = new Vector(comments);
for (Iterator iterator = comments.listIterator(); iterator.hasNext();){
userComments = (Comment) iterator.next();
if (userComments.getMovie().getIdMovie() != movieController.getMovie().getIdMovie()){
v.remove(userComments);
}
}
userCommntItems = v;
}
return userCommentItems ;
}
<h:panelGroup>
<h:outputText value="Item #{comment.userPagingInfo.firstItem + 1}..#{comment.userPagingInfo.lastItem} of #{comment.userPagingInfo.itemCount}"/>
<h:commandLink action="#{comment.userPrev}" value="Previous #{comment.userPagingInfo.batchSize}"
rendered="#{comment.userPagingInfo.firstItem >= comment.userPagingInfo.batchSize}"/>
<h:commandLink action="#{comment.userNext}" value="Next #{comment.userPagingInfo.batchSize}" rendered="#{comment.userPagingInfo.lastItem + comment.userPagingInfo.batchSize <= comment.userPagingInfo.itemCount}"/>
<h:commandLink action="#{comment.userNext}" value="Remaining #{comment.userPagingInfo.itemCount - comment.userPagingInfo.lastItem}"
rendered="#{c开发者_运维知识库omment.userPagingInfo.lastItem < comment.userPagingInfo.itemCount && comment.userPagingInfo.lastItem + comment.userPagingInfo.batchSize > comment.userPagingInfo.itemCount}"/>
<h:dataTable value="#{comment.userCommentItems}" var="item"
border="0" cellpadding="2" cellspacing="0" rowClasses="jsfcrud_odd_row,jsfcrud_even_row" rules="all" style="border:solid 1px"
rendered="#{not empty comment.movieController.movie.commentCollection}">
<h:column>
<f:facet name="header">
<h:outputText value="IdUser"/>
</f:facet>
<h:outputText value="#{item.idUser.user}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Text"/>
</f:facet>
<h:outputText value="#{item.text}"/>
</h:column>
</h:dataTable>
</h:panelGroup>
First of all, this is awful code. How about this version?
public List<Comment> getUserCommentItems() {
if (userCommentItems == null) {
getUserPagingInfo();
List<Comment> comments = jpaController.findAll();
for (Iterator<Comment> iterator = comments.iterator(); iterator.hasNext();){
userComments = iterator.next();
if (!userComments.getMovie().equals(movieController.getMovie()){
iterator.remove();
}
}
userCommntItems = comments;
}
return userCommentItems ;
}
Improvements:
- No more usage of deprecated Vector class (especially no more unnecessary casts)
- Compare object using
equals()
, not==
(you should implementMovie.equals(Object)
accordingly). - No need to copy the collection, work on the original (if your DAO returns something I can't change then it sucks)
But to actually solve your problem:
Are you reusing this component for different movies? If so, keeping the comments in a field is nonsense. Remove all assignments to and reads from userCommentItems
.
精彩评论