Search in sources :

Example 1 with CommentInstance

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);
    }
}
Also used : CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) CommentInstance(org.jbpm.casemgmt.api.model.instance.CommentInstance) ArrayList(java.util.ArrayList) ProcessInstanceDesc(org.jbpm.services.api.model.ProcessInstanceDesc)

Example 2 with CommentInstance

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);
        }
    }
}
Also used : OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) HashMap(java.util.HashMap) QueryContext(org.kie.api.runtime.query.QueryContext) CaseCommentNotFoundException(org.jbpm.casemgmt.api.CaseCommentNotFoundException) AdHocFragmentNotFoundException(org.jbpm.casemgmt.api.AdHocFragmentNotFoundException) CaseNotFoundException(org.jbpm.casemgmt.api.CaseNotFoundException) TaskNotFoundException(org.jbpm.services.api.TaskNotFoundException) CaseActiveException(org.jbpm.casemgmt.api.CaseActiveException) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) CaseInstance(org.jbpm.casemgmt.api.model.instance.CaseInstance) CommentInstance(org.jbpm.casemgmt.api.model.instance.CommentInstance) UserImpl(org.jbpm.services.task.impl.model.UserImpl) AbstractCaseServicesBaseTest(org.jbpm.casemgmt.impl.util.AbstractCaseServicesBaseTest) Test(org.junit.Test)

Example 3 with CommentInstance

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);
        }
    }
}
Also used : CommentNotificationEventListener(org.jbpm.casemgmt.impl.event.CommentNotificationEventListener) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) HashMap(java.util.HashMap) TestNotificationPublisher(org.jbpm.casemgmt.impl.util.TestNotificationPublisher) QueryContext(org.kie.api.runtime.query.QueryContext) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) CaseInstance(org.jbpm.casemgmt.api.model.instance.CaseInstance) CommentInstance(org.jbpm.casemgmt.api.model.instance.CommentInstance) UserImpl(org.jbpm.services.task.impl.model.UserImpl) AbstractCaseServicesBaseTest(org.jbpm.casemgmt.impl.util.AbstractCaseServicesBaseTest) Test(org.junit.Test)

Example 4 with CommentInstance

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);
        }
    }
}
Also used : CommentNotificationEventListener(org.jbpm.casemgmt.impl.event.CommentNotificationEventListener) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) HashMap(java.util.HashMap) TestNotificationPublisher(org.jbpm.casemgmt.impl.util.TestNotificationPublisher) QueryContext(org.kie.api.runtime.query.QueryContext) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) CaseInstance(org.jbpm.casemgmt.api.model.instance.CaseInstance) CommentInstance(org.jbpm.casemgmt.api.model.instance.CommentInstance) UserImpl(org.jbpm.services.task.impl.model.UserImpl) AbstractCaseServicesBaseTest(org.jbpm.casemgmt.impl.util.AbstractCaseServicesBaseTest) Test(org.junit.Test)

Example 5 with CommentInstance

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;
}
Also used : IdentityProvider(org.kie.internal.identity.IdentityProvider) CaseCommentNotFoundException(org.jbpm.casemgmt.api.CaseCommentNotFoundException) RegistryContext(org.drools.core.command.impl.RegistryContext) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) Collection(java.util.Collection) ClassObjectFilter(org.drools.core.ClassObjectFilter) FactHandle(org.kie.api.runtime.rule.FactHandle) CommentInstance(org.jbpm.casemgmt.api.model.instance.CommentInstance) List(java.util.List) Context(org.kie.api.runtime.Context) CaseEventSupport(org.jbpm.casemgmt.impl.event.CaseEventSupport) CaseFileInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CaseFileInstanceImpl) KieSession(org.kie.api.runtime.KieSession) CommentInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CommentInstanceImpl) AuthorizationManager(org.jbpm.casemgmt.api.auth.AuthorizationManager) CaseEventSupport(org.jbpm.casemgmt.impl.event.CaseEventSupport) FactHandle(org.kie.api.runtime.rule.FactHandle) CommentInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CommentInstanceImpl) CaseFileInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CaseFileInstanceImpl) RegistryContext(org.drools.core.command.impl.RegistryContext) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) ClassObjectFilter(org.drools.core.ClassObjectFilter) CommentInstance(org.jbpm.casemgmt.api.model.instance.CommentInstance) CaseCommentNotFoundException(org.jbpm.casemgmt.api.CaseCommentNotFoundException) KieSession(org.kie.api.runtime.KieSession)

Aggregations

CommentInstance (org.jbpm.casemgmt.api.model.instance.CommentInstance)12 HashMap (java.util.HashMap)9 CaseFileInstance (org.jbpm.casemgmt.api.model.instance.CaseFileInstance)9 OrganizationalEntity (org.kie.api.task.model.OrganizationalEntity)7 ArrayList (java.util.ArrayList)5 List (java.util.List)5 CaseInstance (org.jbpm.casemgmt.api.model.instance.CaseInstance)5 AbstractCaseServicesBaseTest (org.jbpm.casemgmt.impl.util.AbstractCaseServicesBaseTest)5 UserImpl (org.jbpm.services.task.impl.model.UserImpl)5 Test (org.junit.Test)5 QueryContext (org.kie.api.runtime.query.QueryContext)5 Collection (java.util.Collection)4 Map (java.util.Map)4 CaseCommentNotFoundException (org.jbpm.casemgmt.api.CaseCommentNotFoundException)4 CaseFileInstanceImpl (org.jbpm.casemgmt.impl.model.instance.CaseFileInstanceImpl)4 MessageFormat (java.text.MessageFormat)3 AdHocFragmentNotFoundException (org.jbpm.casemgmt.api.AdHocFragmentNotFoundException)3 AuthorizationManager (org.jbpm.casemgmt.api.auth.AuthorizationManager)3 CommentInstanceImpl (org.jbpm.casemgmt.impl.model.instance.CommentInstanceImpl)3 IdentityProvider (org.kie.internal.identity.IdentityProvider)3