Ejemplo n.º 1
0
  @Override
  public void update(
      Comment comment, Map<String, JSONObject> commentProperties, boolean dispatchEvent) {
    if (comment == null) {
      throw new IllegalArgumentException("Comment must not be null");
    }
    if (comment.getId() == null) {
      throw new IllegalArgumentException("Comment ID must not be null");
    }

    // create persistable generic value
    GenericValue commentGV;

    // We need an in-memory copy of the old comment so we can pass it through in the fired event and
    // to make sure
    // that some fields have changed.
    Comment originalComment = getCommentById(comment.getId());
    if (originalComment == null) {
      throw new IllegalArgumentException(
          "Can not find a comment in the datastore with id: " + comment.getId());
    }

    // Make sure that either the comment body or visibility data has changed, otherwise do not
    // update the datastore
    if (!areCommentsEquivalent(originalComment, comment)) {
      try {
        commentGV = delegator.findById(COMMENT_ENTITY, comment.getId());
        populateGenericValueFromComment(comment, commentGV);
        commentGV.store();
      } catch (GenericEntityException e) {
        throw new DataAccessException(e);
      }

      // Update the issue object
      IssueFactory issueFactory = ComponentAccessor.getComponentOfType(IssueFactory.class);
      GenericValue issueGV = comment.getIssue().getGenericValue();
      MutableIssue mutableIssue = issueFactory.getIssue(issueGV);
      mutableIssue.setUpdated(UtilDateTime.nowTimestamp());
      mutableIssue.store();
    }

    // Update comment properties
    if (commentProperties != null) {
      setProperties(comment.getAuthorApplicationUser(), comment, commentProperties);
    }

    // Dispatch an event if required
    if (dispatchEvent) {
      dispatchIssueCommentEditedEvent(
          comment,
          MapBuilder.build(
              "eventsource",
              IssueEventSource.ACTION,
              EVENT_ORIGINAL_COMMENT_PARAMETER,
              originalComment));
    }
  }
Ejemplo n.º 2
0
  public Comment create(
      Issue issue,
      ApplicationUser author,
      ApplicationUser updateAuthor,
      String body,
      String groupLevel,
      Long roleLevelId,
      Date created,
      Date updated,
      Map<String, JSONObject> commentProperties,
      boolean dispatchEvent,
      boolean modifyIssueUpdateDate) {

    if (textFieldCharacterLengthValidator.isTextTooLong(body)) {
      final long maximumNumberOfCharacters =
          textFieldCharacterLengthValidator.getMaximumNumberOfCharacters();
      String errorMessage =
          getText("field.error.text.toolong", String.valueOf(maximumNumberOfCharacters));
      throw new IllegalArgumentException(errorMessage);
    }

    // create new instance of comment
    CommentImpl comment =
        new CommentImpl(
            projectRoleManager,
            author,
            updateAuthor,
            body,
            groupLevel,
            roleLevelId,
            created,
            updated,
            issue);

    // create persistable generic value
    Map<String, Object> fields = new HashMap<String, Object>();
    fields.put("issue", issue.getId());
    fields.put("type", ActionConstants.TYPE_COMMENT);

    ApplicationUser commentAuthor = comment.getAuthorApplicationUser();
    ApplicationUser commentUpdateAuthor = comment.getUpdateAuthorApplicationUser();
    fields.put("author", commentAuthor == null ? null : commentAuthor.getKey());
    fields.put("updateauthor", commentUpdateAuthor == null ? null : commentUpdateAuthor.getKey());
    fields.put("body", comment.getBody());
    fields.put("level", comment.getGroupLevel());
    fields.put("rolelevel", comment.getRoleLevelId());
    fields.put("created", new Timestamp(comment.getCreated().getTime()));
    fields.put("updated", new Timestamp(comment.getUpdated().getTime()));

    GenericValue commentGV = EntityUtils.createValue(COMMENT_ENTITY, fields);
    // set the ID on comment object
    comment.setId(commentGV.getLong(COMMENT_ID));

    // Update the issue object if required
    if (modifyIssueUpdateDate) {
      // JRA-36334: Only modify the Issue updated date if it would move forward - we don't want it
      // to go back in time
      if (comment.getUpdated().getTime() > issue.getUpdated().getTime()) {
        IssueFactory issueFactory = ComponentAccessor.getComponentOfType(IssueFactory.class);
        MutableIssue mutableIssue = issueFactory.getIssue(issue.getGenericValue());
        // JRA-15723: Use the comments updated time for the updated time of the issue.  This allows
        // users to
        // import old comments without setting the updated time on the issue to now, but to the date
        // of the old comments.
        mutableIssue.setUpdated(new Timestamp(comment.getUpdated().getTime()));
        issue.store();
      }
    }

    if (commentProperties != null) {
      setProperties(author, comment, commentProperties);
    }

    // Dispatch an event if required
    if (dispatchEvent) {
      Map<String, Object> params = new HashMap<String, Object>();
      params.put("eventsource", IssueEventSource.ACTION);
      dispatchIssueCommentAddedEvent(comment, params);
    }
    return comment;
  }