Program: 98520
Project: 98520
Buggy source:
/** 
 * Communicates with the application facade to actually store the comment rating. The rating action is a toggle action, so this method will check the previous value of the vote. Several things can happen here: - The user wasn't authenticated, so it cannot rate the comment. - The rating changed from positive to negative or viceversa. - The rating changed from neutral to anything. - The rating changed from anything to neutral. Each of this will have a different effect in the RatingSummaryVo. It will be a change in future versions to manage this cases in a proper location.
 * @param comment The comment being rated.
 * @param value The new rating value for the comment.
 * @return 0 if no change was done. 1 if the vote was neutral and changed.-1 if the vote was opposed and changed. 2 if the vote was already the desired rateValue.
 * @throws DataBaseException
 */
private int rateComment(CommentVoVwFull comment,int value){
  CommentsFacade commentsFacade=Config.getInstance().getFacadeFactory().getCommentsFacade();
  Long commentId=comment.getId();
  int returnValue=0;
  if (user != null && user.isLoggedIn()) {
    Long userId=user.getId();
    int finalValue=0;
    try {
      BinaryRatingVo commentRatingByUserId=commentsFacade.getCommentRatingByUserId(commentId,userId);
      if (commentRatingByUserId != null) {
        int actualValue=commentRatingByUserId.getValue();
        if (actualValue == value) {
          finalValue=0;
          returnValue=2;
        }
 else         if (actualValue == 0) {
          finalValue=value;
          returnValue=1;
        }
 else {
          finalValue=value;
          returnValue=-1;
        }
      }
      Config.getInstance().getFacadeFactory().getCommentsFacade().rateComment(commentId,userId,finalValue);
    }
 catch (    Exception ex) {
      Logger.getLogger(ViewCourse.class.getName()).log(Level.SEVERE,null,ex);
      returnValue=0;
    }
  }
  return returnValue;
}

Fixed version (ground truth):
/** 
 * Communicates with the application facade to actually store the comment rating. The rating action is a toggle action, so this method will check the previous value of the vote. Several things can happen here: - The user wasn't authenticated, so it cannot rate the comment. - The rating changed from positive to negative or viceversa. - The rating changed from neutral to anything. - The rating changed from anything to neutral. Each of this will have a different effect in the RatingSummaryVo. It will be a change in future versions to manage this cases in a proper location.
 * @param comment The comment being rated.
 * @param value The new rating value for the comment.
 * @return 0 if no change was done. 1 if the vote was neutral and changed.-1 if the vote was opposed and changed. 2 if the vote was already the desired rateValue.
 * @throws DataBaseException
 */
private ratingState rateComment(CommentVoVwFull comment,int value){
  CommentsFacade commentsFacade=Config.getInstance().getFacadeFactory().getCommentsFacade();
  Long commentId=comment.getId();
  ratingState returnValue=ratingState.NO_CHANGE;
  if (user != null && user.isLoggedIn()) {
    Long userId=user.getId();
    int finalValue=0;
    try {
      BinaryRatingVo commentRatingByUserId=commentsFacade.getCommentRatingByUserId(commentId,userId);
      if (commentRatingByUserId != null) {
        int actualValue=commentRatingByUserId.getValue();
        if (actualValue == value) {
          finalValue=0;
          returnValue=ratingState.TO_NEUTRAL;
        }
 else         if (actualValue == 0) {
          finalValue=value;
          returnValue=ratingState.FROM_NEUTRAL;
        }
 else {
          finalValue=value;
          returnValue=ratingState.FROM_OPPOSED;
        }
      }
 else {
        finalValue=value;
        returnValue=ratingState.FROM_NEUTRAL;
      }
      Config.getInstance().getFacadeFactory().getCommentsFacade().rateComment(commentId,userId,finalValue);
    }
 catch (    Exception ex) {
      Logger.getLogger(ViewCourse.class.getName()).log(Level.SEVERE,null,ex);
      returnValue=ratingState.NO_CHANGE;
    }
  }
  return returnValue;
}