@Post
  @Path("/reports/{errorEntry.id}/comments")
  public void addComment(ErrorEntry errorEntry, String newComment) {
    newComment = sanitizer.sanitize(newComment, true);
    ErrorEntry errorEntryFromDB = errorEntryDAO.retrieve(new Long(errorEntry.getId()));

    LOG.debug("errorEntry: " + errorEntryFromDB);
    LOG.debug("newComment: " + newComment);
    if (newComment.trim().isEmpty()) {
      validator.add(
          new ValidationMessage(
              ExceptionMessages.COMMENT_SHOULD_NOT_BE_EMPTY, ExceptionMessages.ERROR));
      validator
          .onErrorUse(Results.logic())
          .redirectTo(ErrorReportController.class)
          .details(errorEntryFromDB);
    } else if (newComment.trim().length() > COMMENT_MAX_SIZE) {
      validator.add(
          new ValidationMessage(
              ExceptionMessages.COMMENT_SHOULD_NOT_EXCEED_CHAR, ExceptionMessages.ERROR));
      validator
          .onErrorUse(Results.logic())
          .redirectTo(ErrorReportController.class)
          .details(errorEntryFromDB);
    } else {
      errorEntryLogic.addCommentToErrorEntry(
          errorEntryFromDB.getId(), loggedUser.getUser().getId(), newComment);
    }

    result.include("gaEventCommentAdded", true);

    result.redirectTo(ErrorReportController.class).details(errorEntryFromDB);
  }
 @Post
 @Path("/reports/{errorEntry.id}/comments/{comment.id}/answers")
 @LoggedIn
 public void addAnswerToComment(ErrorEntry errorEntry, Comment comment, String answer) {
   answer = sanitizer.sanitize(answer, true);
   errorEntryLogic.addAnswerToComment(comment.getId(), loggedUser.getUser().getId(), answer);
   result.include("gaEventCommentAdded", true);
   result.redirectTo(ErrorReportController.class).details(errorEntry);
 }
  @Get
  @Path("/reports")
  public void list() {
    List<ErrorEntry> reports = errorEntryLogic.getAllReports();
    if (LOG.isDebugEnabled()) {
      LOG.debug("Will list of size: " + reports.size());
    }

    result.include("errorEntryList", reports);
    if (!loggedUser.isLogged()) {
      Calendar now = Calendar.getInstance();
      now.add(Calendar.DATE, -7);
      result.include("oneWeekAgo", now.getTime());
    }
    result
        .include("headerTitle", messages.getString("LIST_ERROR_REPORT_HEADER"))
        .include("headerDescription", messages.getString("LIST_ERROR_REPORT_DESCRIPTION"))
        .include("stats", errorEntryLogic.getStats());
  }
 @Put
 @Path("/reports/{errorEntry.id}/state")
 @LoggedIn
 public void errorEntrySetState(ErrorEntry errorEntry, String state) {
   if (loggedUser.getUser().getRole().getCanSetErrorReportState()) {
     errorEntryLogic.setState(errorEntry, Enum.valueOf(State.class, state));
     result.include("gaEventStateChanged", true);
     result.redirectTo(ErrorReportController.class).details(errorEntry);
   } else {
     LOG.info("Invalid user tried to set priority: " + loggedUser.getUser());
   }
 }
  @Post
  @Path("/reports")
  @LoggedIn
  public void addReport(
      String text,
      List<String> badint,
      List<String> comments,
      List<String> badintStart,
      List<String> badintEnd,
      List<String> badintRule,
      List<String> omissionClassification,
      List<String> customOmissionText,
      List<String> omissionComment,
      List<String> omissionReplaceBy,
      List<String> omissionStart,
      List<String> omissionEnd) {

    if (LOG.isDebugEnabled()) {
      LOG.debug("Adding new report.");
    }
    text = sanitizer.sanitize(text, false, true);

    comments = sanitizer.sanitize(comments, true);

    customOmissionText = sanitizer.sanitize(customOmissionText, false);

    omissionComment = sanitizer.sanitize(omissionComment, true);

    omissionReplaceBy = sanitizer.sanitize(omissionReplaceBy, false);

    errorEntryLogic.addErrorEntry(
        loggedUser.getUser(),
        text,
        badint,
        comments,
        badintStart,
        badintEnd,
        badintRule,
        omissionClassification,
        customOmissionText,
        omissionComment,
        omissionReplaceBy,
        omissionStart,
        omissionEnd);

    result.include("okMessage", "Problema reportado com sucesso!");
    result.include("gaEventErrorReported", true);

    if (LOG.isDebugEnabled()) {
      LOG.debug("New report added.");
    }
    result.redirectTo(getClass()).list();
  }
 @Delete
 @Path("/reports/{errorEntry.id}/comments/{comment.id}/answers/{answer.id}")
 @LoggedIn
 public void remove(Comment answer, Comment comment) {
   comment = commentDAO.retrieve(comment.getId());
   answer = commentDAO.retrieve(answer.getId());
   if ((loggedUser.getUser().getRole().getCanDeleteOwnCommment()
           && loggedUser.getUser().equals(answer.getUser()))
       || loggedUser.getUser().getRole().getCanDeleteOtherUserCommment()) {
     errorEntryLogic.removeAnswer(answer, comment);
   } else {
     LOG.warn("Invalid user tried to delete answer " + loggedUser.getUser() + " : " + comment);
   }
 }
 @Delete
 @Path("/reports/{errorEntry.id}/comments/{comment.id}")
 @LoggedIn
 public void remove(Comment comment) {
   if (LOG.isDebugEnabled()) {
     LOG.debug("Will delete cooment." + commentDAO);
   }
   comment = commentDAO.retrieve(comment.getId());
   if ((loggedUser.getUser().getRole().getCanDeleteOwnCommment()
           && loggedUser.getUser().equals(comment.getUser()))
       || loggedUser.getUser().getRole().getCanDeleteOtherUserCommment()) {
     errorEntryLogic.removeComment(comment);
   } else {
     LOG.warn("Invalid user tried to delete comment " + loggedUser.getUser() + " : " + comment);
   }
 }
  @Put
  @Path("/reports/edit")
  @LoggedIn
  public void multipleEdit(
      Boolean applycomment,
      Boolean applypriority,
      Boolean applystate,
      Integer errorlist_lenght,
      Long[] errorEntryID,
      String newComment,
      String priority,
      String state) {
    if (loggedUser.getUser().getRole().getCanEditErrorReport()) {

      String comment = null;
      if (applycomment != null && applycomment) {
        comment = sanitizer.sanitize(newComment, true);
        ;
      }
      Priority priorityEnum = null;
      if (applypriority != null && applypriority) {
        priorityEnum = Enum.valueOf(Priority.class, priority);
      }
      State stateEnum = null;
      if (applystate != null && applystate) {
        stateEnum = Enum.valueOf(State.class, state);
      }
      List<ErrorEntry> entries = new ArrayList<ErrorEntry>();
      if (errorEntryID != null && errorEntryID.length > 0) {
        LOG.info("Selected errors: " + Arrays.toString(errorEntryID));

        for (int i = 0; i < errorEntryID.length; i++) {
          if (errorEntryID[i] != null) {
            LOG.info("Will get error " + i);
            ErrorEntry error = this.errorEntryDAO.retrieve(errorEntryID[i]);
            LOG.info("Got " + error);
            entries.add(error);
          }
        }
      }
      errorEntryLogic.multipleEdit(entries, priorityEnum, stateEnum, comment);
    } else {
      LOG.info("Invalid user tried to set priority: " + loggedUser.getUser());
    }

    result.redirectTo(getClass()).list();
  }
 @Delete
 @Path("/reports/{errorEntry.id}")
 @LoggedIn
 public void remove(ErrorEntry errorEntry) {
   errorEntry = errorEntryDAO.retrieve(errorEntry.getId());
   if (loggedUser.getUser().equals(errorEntry.getSubmitter())
       || loggedUser.getUser().getRole().getCanDeleteOtherUserErrorReport()) {
     if (LOG.isDebugEnabled()) {
       LOG.debug("errorEntry: " + errorEntry);
     }
     errorEntryLogic.remove(errorEntry);
   } else {
     LOG.info(
         "Unauthorized user tried to delete errorEntry: "
             + loggedUser.getUser()
             + " : "
             + errorEntry);
   }
 }
  @Get
  @Path("/reportStatusRefresh")
  @LoggedIn
  public void reportStatus() {
    User user = loggedUser.getUser();

    if (user != null) {
      if (user.getRole().getCanRefreshStatus()) {

        LOG.warn("Report Status");

        errorEntryLogic.refreshReports();
      } else {
        LOG.warn("Não tem credenciais");
      }
    }

    result.redirectTo(this).list();
  }
  /**
   * Submit an error report via OpenOffice plugin. Only CoGrOO users are allowed.
   *
   * @param username
   * @param token
   * @param error
   */
  @Post
  @Path("/submitErrorReport")
  public void submitErrorEntry(String username, String token, String error) {
    error = securityUtil.decodeURLSafeString(error);

    LOG.debug(
        "Got new error report from: "
            + username
            + " encrypted token: "
            + token
            + " error: "
            + error);
    try {
      errorEntryLogic.addErrorEntry("cogroo", username, error);
      LOG.debug("Error handled, will set response");
      result.include("result", RestUtil.prepareResponse("result", "OK"));
    } catch (CommunityException e) {
      LOG.error("Error handling error submition", e);
    }
  }