CommentNotificationEventListener implements CaseEventListener, Cacheable { protected Set<OrganizationalEntity> collectOrgEntitiesByRole(List<String> mentionedRoles, CaseCommentEvent event, StringBuilder commentContent) { Set<OrganizationalEntity> recipients = new HashSet<>(); CommentInstance comment = event.getComment(); for (String roleName : mentionedRoles) { if (comment.getRestrictedTo() != null && !comment.getRestrictedTo().isEmpty() && !comment.getRestrictedTo().contains(roleName)) { continue; } try { Collection<OrganizationalEntity> assignments = ((CaseAssignment) event.getCaseFile()).getAssignments(roleName); recipients.addAll(assignments); String assignmnetsFlatten = assignments.stream().map(oe -> oe.getId()).collect(Collectors.joining(",")); String updatedCommentContent = commentContent.toString().replaceAll("@" + roleName, assignmnetsFlatten); commentContent.setLength(0); commentContent.append(updatedCommentContent); } catch (IllegalArgumentException e) { logger.debug("Role {} does not exist in case {}", roleName, event.getCaseId()); } } return recipients; } CommentNotificationEventListener();  CommentNotificationEventListener(String sender, String template, String subject); @Override void afterCaseCommentAdded(CaseCommentEvent event); @Override void afterCaseCommentUpdated(CaseCommentEvent event); void addPublisher(NotificationPublisher publisher); @Override void close(); static final String DEFAULT_SENDER; static final String DEFAULT_TEMPLATE; static final String DEFAULT_SUBJECT; static final String CASE_ID_PARAM; static final String AUTHOR_PARAM; static final String COMMENT_PARAM; static final String COMMENT_ID_PARAM; static final String CREATED_AT_PARAM; }
@Test public void testCollectOrgEntitiesByRole() { CommentNotificationEventListener listener = new CommentNotificationEventListener(); List<String> mentionedRoles = new ArrayList<>(); mentionedRoles.add("owner"); mentionedRoles.add("manager"); CommentInstanceImpl comment = new CommentInstanceImpl("john", "simple comment for @owner and @manager", new ArrayList<>()); CaseFileInstance caseFile = buildCaseFile(mentionedRoles); CaseCommentEvent event = new CaseCommentEvent("john", caseFile.getCaseId() , caseFile, comment); StringBuilder commentContent = new StringBuilder(comment.getComment()); Set<OrganizationalEntity> collected = listener.collectOrgEntitiesByRole(mentionedRoles, event, commentContent); assertThat(collected).hasSize(2); assertThat(collected).allMatch(item -> item instanceof User); assertThat(commentContent.toString()).isEqualTo("simple comment for john and mary"); }
@Test public void testCollectOrgEntitiesByRoleNotExistingRole() { CommentNotificationEventListener listener = new CommentNotificationEventListener(); List<String> mentionedRoles = new ArrayList<>(); mentionedRoles.add("owner"); mentionedRoles.add("manager"); CommentInstanceImpl comment = new CommentInstanceImpl("john", "simple comment for @owner and @manager", new ArrayList<>()); CaseFileInstance caseFile = buildCaseFile(mentionedRoles); CaseCommentEvent event = new CaseCommentEvent("john", caseFile.getCaseId() , caseFile, comment); mentionedRoles.add("notexisting"); StringBuilder commentContent = new StringBuilder(comment.getComment()); Set<OrganizationalEntity> collected = listener.collectOrgEntitiesByRole(mentionedRoles, event, commentContent); assertThat(collected).hasSize(2); assertThat(collected).allMatch(item -> item instanceof User); assertThat(commentContent.toString()).isEqualTo("simple comment for john and mary"); }