use of org.jbpm.casemgmt.api.model.instance.CommentInstance in project jbpm by kiegroup.
the class CaseServiceImpl method getCaseComments.
/*
* Case comments methods
*/
@Override
public Collection<CommentInstance> getCaseComments(String caseId, QueryContext queryContext) throws CaseNotFoundException {
authorizationManager.checkOperationAuthorization(caseId, ProtectedOperation.MODIFY_COMMENT);
ProcessInstanceDesc pi = verifyCaseIdExists(caseId);
CaseFileInstance caseFile = internalGetCaseFileInstance(caseId, pi.getDeploymentId());
List<CommentInstance> caseComments = new ArrayList<>(((CaseFileInstanceImpl) caseFile).getComments());
// apply authorization
caseComments = authorizationManager.filterByCommentAuthorization(caseId, caseFile, caseComments);
int caseCommentsSize = caseComments.size();
int offset = queryContext.getOffset();
int pageSize = queryContext.getCount();
int pageIndex = (caseCommentsSize + pageSize - 1) / pageSize;
if (caseCommentsSize < pageSize) {
return caseComments;
} else if (pageIndex == (offset / pageSize) + 1) {
return caseComments.subList(offset, caseCommentsSize);
} else {
return caseComments.subList(offset, offset + pageSize);
}
}
use of org.jbpm.casemgmt.api.model.instance.CommentInstance in project jbpm by kiegroup.
the class CaseServiceImplTest method testCaseWithCommentsPagination.
@Test
public void testCaseWithCommentsPagination() {
Map<String, OrganizationalEntity> roleAssignments = new HashMap<>();
roleAssignments.put("owner", new UserImpl("john"));
Map<String, Object> data = new HashMap<>();
CaseFileInstance caseFile = caseService.newCaseFileInstance(deploymentUnit.getIdentifier(), USER_TASK_STAGE_AUTO_START_CASE_P_ID, data, roleAssignments);
String caseId = caseService.startCase(deploymentUnit.getIdentifier(), USER_TASK_STAGE_AUTO_START_CASE_P_ID, caseFile);
assertNotNull(caseId);
assertEquals(FIRST_CASE_ID, caseId);
try {
CaseInstance cInstance = caseService.getCaseInstance(caseId);
assertNotNull(cInstance);
assertEquals(FIRST_CASE_ID, cInstance.getCaseId());
assertEquals(deploymentUnit.getIdentifier(), cInstance.getDeploymentId());
for (int i = 0; i < 55; i++) {
caseService.addCaseComment(FIRST_CASE_ID, "anna", "comment" + i);
}
int pageSize = 20;
int firstPageOffset = 0 * pageSize;
Collection<CommentInstance> firstPage = caseService.getCaseComments(FIRST_CASE_ID, new QueryContext(firstPageOffset, pageSize));
assertNotNull(firstPage);
assertEquals(20, firstPage.size());
Iterator<CommentInstance> firstPageIter = firstPage.iterator();
for (int i = 0; firstPageIter.hasNext(); i++) {
assertComment(firstPageIter.next(), "anna", "comment" + i);
}
int secondPageOffset = 1 * pageSize;
Collection<CommentInstance> secondPage = caseService.getCaseComments(FIRST_CASE_ID, new QueryContext(secondPageOffset, pageSize));
assertNotNull(secondPage);
assertEquals(20, secondPage.size());
Iterator<CommentInstance> secondPageIter = secondPage.iterator();
for (int i = 20; secondPageIter.hasNext(); i++) {
assertComment(secondPageIter.next(), "anna", "comment" + i);
}
int thirdPageOffset = 2 * pageSize;
Collection<CommentInstance> thirdPage = caseService.getCaseComments(FIRST_CASE_ID, new QueryContext(thirdPageOffset, pageSize));
assertNotNull(thirdPage);
assertEquals(15, thirdPage.size());
Iterator<CommentInstance> thirdPageIter = thirdPage.iterator();
for (int i = 40; thirdPageIter.hasNext(); i++) {
assertComment(thirdPageIter.next(), "anna", "comment" + i);
}
} catch (Exception e) {
logger.error("Unexpected error {}", e.getMessage(), e);
fail("Unexpected exception " + e.getMessage());
} finally {
if (caseId != null) {
caseService.cancelCase(caseId);
}
}
}
use of org.jbpm.casemgmt.api.model.instance.CommentInstance in project jbpm by kiegroup.
the class CaseCommentNotificationTest method testCommentsNotificationWithoutTemplate.
@Test
public void testCommentsNotificationWithoutTemplate() {
Map<String, OrganizationalEntity> roleAssignments = new HashMap<>();
roleAssignments.put("owner", new UserImpl("john"));
TestNotificationPublisher publisher = new TestNotificationPublisher(true);
CommentNotificationEventListener listener = CommentNotificationEventListenerFactory.get("test");
listener.addPublisher(publisher);
Map<String, Object> data = new HashMap<>();
CaseFileInstance caseFile = caseService.newCaseFileInstance(deploymentUnit.getIdentifier(), USER_TASK_STAGE_AUTO_START_CASE_P_ID, data, roleAssignments);
String caseId = caseService.startCase(deploymentUnit.getIdentifier(), USER_TASK_STAGE_AUTO_START_CASE_P_ID, caseFile);
assertNotNull(caseId);
assertEquals(FIRST_CASE_ID, caseId);
try {
CaseInstance cInstance = caseService.getCaseInstance(caseId);
assertNotNull(cInstance);
assertEquals(FIRST_CASE_ID, cInstance.getCaseId());
assertEquals(deploymentUnit.getIdentifier(), cInstance.getDeploymentId());
Collection<CommentInstance> caseComments = caseService.getCaseComments(FIRST_CASE_ID, new QueryContext());
assertNotNull(caseComments);
assertEquals(0, caseComments.size());
caseService.addCaseComment(FIRST_CASE_ID, "poul", "just a tiny comment refering to @owner");
caseComments = caseService.getCaseComments(FIRST_CASE_ID, new QueryContext());
assertNotNull(caseComments);
assertEquals(1, caseComments.size());
CommentInstance comment = caseComments.iterator().next();
assertComment(comment, "poul", "just a tiny comment refering to @owner");
String expectedNotification = "Publishing notification from cases@jbpm.org, with subject You have been mentioned in case (CASE-0000000001) comment to [[UserImpl:'john']] with body just a tiny comment refering to john";
List<String> published = publisher.get();
assertThat(published).hasSize(1);
assertThat(published.get(0)).isEqualTo(expectedNotification);
caseService.updateCaseComment(FIRST_CASE_ID, comment.getId(), comment.getAuthor(), "Updated " + comment.getComment());
caseComments = caseService.getCaseComments(FIRST_CASE_ID, new QueryContext());
assertNotNull(caseComments);
assertEquals(1, caseComments.size());
comment = caseComments.iterator().next();
assertComment(comment, "poul", "Updated just a tiny comment refering to @owner");
expectedNotification = "Publishing notification from cases@jbpm.org, with subject You have been mentioned in case (CASE-0000000001) comment to [[UserImpl:'john']] with body Updated just a tiny comment refering to john";
published = publisher.get();
assertThat(published).hasSize(1);
assertThat(published.get(0)).isEqualTo(expectedNotification);
} catch (Exception e) {
logger.error("Unexpected error {}", e.getMessage(), e);
fail("Unexpected exception " + e.getMessage());
} finally {
if (caseId != null) {
caseService.cancelCase(caseId);
}
}
}
use of org.jbpm.casemgmt.api.model.instance.CommentInstance in project jbpm by kiegroup.
the class CaseCommentNotificationTest method testCommentsNotificationWithTemplate.
@Test
public void testCommentsNotificationWithTemplate() {
Map<String, OrganizationalEntity> roleAssignments = new HashMap<>();
roleAssignments.put("owner", new UserImpl("john"));
TestNotificationPublisher publisher = new TestNotificationPublisher(false);
CommentNotificationEventListener listener = CommentNotificationEventListenerFactory.get("test");
listener.addPublisher(publisher);
Map<String, Object> data = new HashMap<>();
CaseFileInstance caseFile = caseService.newCaseFileInstance(deploymentUnit.getIdentifier(), USER_TASK_STAGE_AUTO_START_CASE_P_ID, data, roleAssignments);
String caseId = caseService.startCase(deploymentUnit.getIdentifier(), USER_TASK_STAGE_AUTO_START_CASE_P_ID, caseFile);
assertNotNull(caseId);
assertEquals(FIRST_CASE_ID, caseId);
try {
CaseInstance cInstance = caseService.getCaseInstance(caseId);
assertNotNull(cInstance);
assertEquals(FIRST_CASE_ID, cInstance.getCaseId());
assertEquals(deploymentUnit.getIdentifier(), cInstance.getDeploymentId());
Collection<CommentInstance> caseComments = caseService.getCaseComments(FIRST_CASE_ID, new QueryContext());
assertNotNull(caseComments);
assertEquals(0, caseComments.size());
caseService.addCaseComment(FIRST_CASE_ID, "poul", "just a tiny comment refering to @owner");
caseComments = caseService.getCaseComments(FIRST_CASE_ID, new QueryContext());
assertNotNull(caseComments);
assertEquals(1, caseComments.size());
CommentInstance comment = caseComments.iterator().next();
assertComment(comment, "poul", "just a tiny comment refering to @owner");
String expectedNotification = "Publishing notification from cases@jbpm.org, with subject You have been mentioned in case (CASE-0000000001) comment to [[UserImpl:'john']] with template mentioned-in-comment";
List<String> published = publisher.get();
assertThat(published).hasSize(1);
assertThat(published.get(0)).isEqualTo(expectedNotification);
caseService.updateCaseComment(FIRST_CASE_ID, comment.getId(), comment.getAuthor(), "Updated " + comment.getComment());
caseComments = caseService.getCaseComments(FIRST_CASE_ID, new QueryContext());
assertNotNull(caseComments);
assertEquals(1, caseComments.size());
comment = caseComments.iterator().next();
assertComment(comment, "poul", "Updated just a tiny comment refering to @owner");
expectedNotification = "Publishing notification from cases@jbpm.org, with subject You have been mentioned in case (CASE-0000000001) comment to [[UserImpl:'john']] with template mentioned-in-comment";
published = publisher.get();
assertThat(published).hasSize(1);
assertThat(published.get(0)).isEqualTo(expectedNotification);
} catch (Exception e) {
logger.error("Unexpected error {}", e.getMessage(), e);
fail("Unexpected exception " + e.getMessage());
} finally {
if (caseId != null) {
caseService.cancelCase(caseId);
}
}
}
use of org.jbpm.casemgmt.api.model.instance.CommentInstance in project jbpm by kiegroup.
the class CaseCommentCommand method execute.
@Override
public String execute(Context context) {
KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
Collection<? extends Object> caseFiles = ksession.getObjects(new ClassObjectFilter(CaseFileInstance.class));
if (caseFiles.size() != 1) {
throw new IllegalStateException("Not able to find distinct case file - found case files " + caseFiles.size());
}
CaseFileInstance caseFile = (CaseFileInstance) caseFiles.iterator().next();
FactHandle factHandle = ksession.getFactHandle(caseFile);
CaseEventSupport caseEventSupport = getCaseEventSupport(context);
String commentIdentifier = null;
if (add) {
CommentInstance commentInstance = new CommentInstanceImpl(author, comment, restrictedTo);
caseEventSupport.fireBeforeCaseCommentAdded(caseFile.getCaseId(), caseFile, commentInstance);
((CaseFileInstanceImpl) caseFile).addComment(commentInstance);
commentIdentifier = commentInstance.getId();
caseEventSupport.fireAfterCaseCommentAdded(caseFile.getCaseId(), caseFile, commentInstance);
} else if (update) {
CommentInstance toUpdate = ((CaseFileInstanceImpl) caseFile).getComments().stream().filter(c -> c.getId().equals(commentId)).findFirst().orElseThrow(() -> new CaseCommentNotFoundException("Cannot find comment with id " + commentId));
if (!this.author.equals(toUpdate.getAuthor())) {
throw new IllegalStateException("Only original author can update comment");
}
// apply authorization
authorizationManager.checkCommentAuthorization(caseFile.getCaseId(), caseFile, toUpdate);
caseEventSupport.fireBeforeCaseCommentUpdated(caseFile.getCaseId(), caseFile, toUpdate);
((CommentInstanceImpl) toUpdate).setComment(updatedText);
if (restrictedTo != null) {
((CommentInstanceImpl) toUpdate).setRestrictedTo(restrictedTo);
}
commentIdentifier = toUpdate.getId();
caseEventSupport.fireAfterCaseCommentUpdated(caseFile.getCaseId(), caseFile, toUpdate);
} else if (remove) {
CommentInstance toRemove = ((CaseFileInstanceImpl) caseFile).getComments().stream().filter(c -> c.getId().equals(commentId)).findFirst().orElseThrow(() -> new CaseCommentNotFoundException("Cannot find comment with id " + commentId));
// apply authorization
authorizationManager.checkCommentAuthorization(caseFile.getCaseId(), caseFile, toRemove);
caseEventSupport.fireBeforeCaseCommentRemoved(caseFile.getCaseId(), caseFile, toRemove);
((CaseFileInstanceImpl) caseFile).removeComment(toRemove);
commentIdentifier = toRemove.getId();
caseEventSupport.fireAfterCaseCommentRemoved(caseFile.getCaseId(), caseFile, toRemove);
}
ksession.update(factHandle, caseFile);
triggerRules(ksession);
return commentIdentifier;
}
Aggregations