private void editComment(
      IssueChangeHolder issueChangeHolder, Map<String, Object> commentParams, String body) {
    final ApplicationUser user = authenticationContext.getUser();
    final long commentId = Long.valueOf((String) commentParams.get(PARAM_COMMENT_ID));
    final ErrorCollection errorCollection = new SimpleErrorCollection();

    MutableComment mutableComment =
        commentService.getMutableComment(user, commentId, errorCollection);
    CommentParametersBuilder builder = CommentParameters.builder(mutableComment);

    if (StringUtils.isNotBlank(body)) {
      builder.body(body);
    }
    String groupLevel = (String) commentParams.get(PARAM_GROUP_LEVEL);
    String roleLevelIdStr = (String) commentParams.get(PARAM_ROLE_LEVEL);
    builder.visibility(Visibilities.fromGroupAndStrRoleId(groupLevel, roleLevelIdStr));
    builder.commentProperties(getCommentPropertiesQuietly(commentParams));

    final CommentService.CommentUpdateValidationResult validationResult =
        commentService.validateCommentUpdate(user, commentId, builder.build());
    if (validationResult.isValid()) {
      commentService.update(user, validationResult, true);
      issueChangeHolder.setComment(mutableComment);
    } else {
      log.error(
          "Error updating comment id '"
              + commentId
              + "' Error(s): '"
              + errorCollection.toString()
              + "'");
    }
  }
  private void createComment(
      Issue issue,
      IssueChangeHolder issueChangeHolder,
      Map<String, Object> commentParams,
      String body) {
    String groupLevel = (String) commentParams.get(PARAM_GROUP_LEVEL);
    String roleLevelIdStr = (String) commentParams.get(PARAM_ROLE_LEVEL);
    final Visibility visibility = Visibilities.fromGroupAndStrRoleId(groupLevel, roleLevelIdStr);

    final ApplicationUser user = authenticationContext.getUser();

    final CommentParameters commentParameters =
        CommentParameters.builder()
            .author(user)
            .body(body)
            .commentProperties(getCommentPropertiesQuietly(commentParams))
            .visibility(visibility)
            .issue(issue)
            .build();

    final CommentService.CommentCreateValidationResult validationResult =
        commentService.validateCommentCreate(user, commentParameters);

    if (validationResult.isValid()) {
      Comment comment = commentService.create(user, validationResult, false);
      issueChangeHolder.setComment(comment);
    } else {
      log.error(
          "There was an error creating a comment value: "
              + validationResult.getErrorCollection().toString());
    }
  }
 private void validateComment() {
   // Check if we have a comment
   if (fieldUpdated(FIELD_COMMENT)) {
     // If so if the user can comment on the issue
     if (permissionManager.hasPermission(
         Permissions.COMMENT_ISSUE, getProject(), getRemoteUser())) {
       commentService.isValidCommentData(
           getRemoteUser(), getIssue(), getCommentLevel(), getCommentRoleLevel(), errorCollection);
       if (!fieldUpdated(FIELD_COMMENT_LEVEL) && !fieldUpdated(FIELD_COMMENT_ROLE_LEVEL)) {
         setCommentLevel(null);
       }
     } else {
       errorCollection.addErrorMessage(
           authenticationContext
               .getI18nHelper()
               .getText(
                   "admin.errors.user.does.not.have.permission",
                   (getRemoteUser() != null
                       ? authenticationContext
                           .getI18nHelper()
                           .getText("admin.errors.user", "'" + getRemoteUser().getName() + "'")
                       : authenticationContext
                           .getI18nHelper()
                           .getText("admin.errors.anonymous.user"))));
     }
   }
 }
 private void removeComment(IssueChangeHolder issueChangeHolder, Map commentParams) {
   final ApplicationUser user = authenticationContext.getUser();
   final ErrorCollection errorCollection = new SimpleErrorCollection();
   final long commentId = Long.valueOf((String) commentParams.get(PARAM_COMMENT_ID));
   Comment comment = commentService.getCommentById(user, commentId, errorCollection);
   commentService.delete(new JiraServiceContextImpl(user, errorCollection), comment, true);
   if (errorCollection.hasAnyErrors()) {
     log.error(
         "Error updating comment id '"
             + commentId
             + "' Error(s): '"
             + errorCollection.toString()
             + "'");
   } else {
     issueChangeHolder.setComment(comment);
   }
 }
 private Collection getRoleLevels(Issue issue) {
   Collection roles;
   if (commentService.isProjectRoleVisibilityEnabled()) {
     ApplicationUser user = authenticationContext.getUser();
     roles = projectRoleManager.getProjectRoles(user, issue.getProjectObject());
   } else {
     roles = Collections.EMPTY_LIST;
   }
   return roles;
 }
 private Collection getRoleLevels(GenericValue project) {
   if (project == null) {
     throw new NullPointerException("project GenericValue was null");
   }
   Collection roles;
   if (commentService.isProjectRoleVisibilityEnabled()) {
     ApplicationUser user = authenticationContext.getUser();
     roles = projectRoleManager.getProjectRoles(user, projectFactory.getProject(project));
   } else {
     roles = Collections.EMPTY_LIST;
   }
   return roles;
 }
 private void validateEditComment(
     ErrorCollection errorCollection,
     Issue issue,
     Map<String, Object> commentParams,
     String body,
     Visibility visibility,
     ApplicationUser user) {
   if (commentParams.get(PARAM_COMMENT_ID) != null) {
     try {
       commentService.isValidCommentBody(body, errorCollection);
       final Long commentIdAsLong = Long.valueOf((String) commentParams.get(PARAM_COMMENT_ID));
       commentService.hasPermissionToEdit(
           new JiraServiceContextImpl(user, errorCollection), commentIdAsLong);
       commentService.isValidCommentVisibility(user, issue, visibility, errorCollection);
       validateCommentProperties(commentParams, errorCollection);
     } catch (NumberFormatException ex) {
       errorCollection.addError(IssueFieldConstants.COMMENT, "invalid comment id specified.");
     }
   } else {
     errorCollection.addError(IssueFieldConstants.COMMENT, "no comment id specified.");
   }
 }
  private void validateCreateComment(
      ErrorCollection errorCollection,
      Issue issue,
      Map<String, Object> commentParams,
      String body,
      Visibility visibility,
      ApplicationUser user) {
    // Validate user has the correct permissions IF we are actually adding a comment
    if (StringUtils.isNotBlank(body)) {
      commentService.hasPermissionToCreate(user, issue, errorCollection);
    }
    boolean allowEmptyComments = true;
    if (commentParams.get(CREATE_COMMENT) != null) {
      allowEmptyComments = false;
    }

    commentService.isValidCommentBody(body, errorCollection, allowEmptyComments);

    // Validate the group and role level settings
    commentService.isValidCommentVisibility(user, issue, visibility, errorCollection);
    validateCommentProperties(commentParams, errorCollection);
  }
 /**
  * Returns the list of group names that the current user is in.
  *
  * @return the possibly empty Collection of group names (Strings)
  */
 private Collection getGroupLevels() {
   Collection groups;
   if (authenticationContext.getLoggedInUser() == null
       || !commentService.isGroupVisibilityEnabled()) {
     groups = Collections.EMPTY_LIST;
   } else {
     Collection<String> groupNames =
         groupManager.getGroupNamesForUser(authenticationContext.getLoggedInUser().getName());
     List<String> userGroups = new ArrayList<String>(groupNames);
     Collections.sort(userGroups);
     groups = userGroups;
   }
   return groups;
 }
  @Override
  public FieldJsonRepresentation getJsonFromIssue(
      final Issue issue,
      boolean renderedVersionRequired,
      @Nullable final FieldLayoutItem fieldLayoutItem) {
    final SimpleErrorCollection errorCollection = new SimpleErrorCollection();
    final List<Comment> comments =
        commentService.getCommentsForUser(authenticationContext.getUser(), issue);
    CommentsWithPaginationJsonBean commentsWithPaginationJsonBean =
        new CommentsWithPaginationJsonBean();
    commentsWithPaginationJsonBean.setMaxResults(comments.size());
    commentsWithPaginationJsonBean.setTotal(comments.size());
    commentsWithPaginationJsonBean.setStartAt(0);
    commentsWithPaginationJsonBean.setComments(
        CommentJsonBean.shortBeans(
            comments,
            jiraBaseUrls,
            projectRoleManager,
            authenticationContext.getUser(),
            emailFormatter));
    FieldJsonRepresentation fieldJsonRepresentation =
        new FieldJsonRepresentation(new JsonData(commentsWithPaginationJsonBean));

    if (renderedVersionRequired) {
      CommentsWithPaginationJsonBean renderedBean = new CommentsWithPaginationJsonBean();
      renderedBean.setMaxResults(comments.size());
      renderedBean.setTotal(comments.size());
      renderedBean.setStartAt(0);
      renderedBean.setComments(
          CommentJsonBean.renderedShortBeans(
              comments,
              jiraBaseUrls,
              projectRoleManager,
              dateTimeFormatterFactory,
              rendererManager,
              fieldLayoutItem == null ? null : fieldLayoutItem.getRendererType(),
              issue.getIssueRenderContext(),
              authenticationContext.getUser(),
              emailFormatter));
      fieldJsonRepresentation.setRenderedData(new JsonData(renderedBean));
    }

    if (!errorCollection.hasAnyErrors()) {
      return fieldJsonRepresentation;
    } else {
      log.warn("Failed to include comments in REST response" + errorCollection.toString());
      return null;
    }
  }
 private void validateRemoveComment(
     ErrorCollection errorCollectionToAddTo,
     Map<String, Object> commentParams,
     ApplicationUser user) {
   Object commentIdObj = commentParams.get(PARAM_COMMENT_ID);
   if (commentIdObj != null) {
     try {
       Long commentId = Long.valueOf((String) commentIdObj);
       commentService.hasPermissionToDelete(
           new JiraServiceContextImpl(user, errorCollectionToAddTo), commentId);
     } catch (NumberFormatException ex) {
       errorCollectionToAddTo.addError(
           IssueFieldConstants.COMMENT, "invalid comment id specified.");
     }
   } else {
     errorCollectionToAddTo.addError(IssueFieldConstants.COMMENT, "no comment id specified.");
   }
 }