Search in sources :

Example 1 with CaseCommentNotFoundException

use of org.jbpm.casemgmt.api.CaseCommentNotFoundException 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)

Example 2 with CaseCommentNotFoundException

use of org.jbpm.casemgmt.api.CaseCommentNotFoundException in project jbpm by kiegroup.

the class CaseServiceImplTest method testUpdateNotExistingCaseComment.

@Test
public void testUpdateNotExistingCaseComment() {
    Map<String, Object> data = new HashMap<>();
    CaseFileInstance caseFile = caseService.newCaseFileInstance(deploymentUnit.getIdentifier(), USER_TASK_STAGE_AUTO_START_CASE_P_ID, data);
    String caseId = caseService.startCase(deploymentUnit.getIdentifier(), USER_TASK_STAGE_AUTO_START_CASE_P_ID, caseFile);
    assertNotNull(caseId);
    assertEquals(FIRST_CASE_ID, caseId);
    try {
        caseService.updateCaseComment(FIRST_CASE_ID, "not-existing-comment", "poul", "just a tiny comment");
        fail("Updating non-existent case comment should throw CaseCommentNotFoundException.");
    } catch (CaseCommentNotFoundException e) {
    // expected
    } finally {
        if (caseId != null) {
            caseService.cancelCase(caseId);
        }
    }
}
Also used : CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) HashMap(java.util.HashMap) CaseCommentNotFoundException(org.jbpm.casemgmt.api.CaseCommentNotFoundException) AbstractCaseServicesBaseTest(org.jbpm.casemgmt.impl.util.AbstractCaseServicesBaseTest) Test(org.junit.Test)

Example 3 with CaseCommentNotFoundException

use of org.jbpm.casemgmt.api.CaseCommentNotFoundException in project jbpm by kiegroup.

the class CaseServiceImplTest method testRemoveNotExistingCaseComment.

@Test
public void testRemoveNotExistingCaseComment() {
    Map<String, Object> data = new HashMap<>();
    CaseFileInstance caseFile = caseService.newCaseFileInstance(deploymentUnit.getIdentifier(), USER_TASK_STAGE_AUTO_START_CASE_P_ID, data);
    String caseId = caseService.startCase(deploymentUnit.getIdentifier(), USER_TASK_STAGE_AUTO_START_CASE_P_ID, caseFile);
    assertNotNull(caseId);
    assertEquals(FIRST_CASE_ID, caseId);
    try {
        caseService.removeCaseComment(FIRST_CASE_ID, "not-existing-comment");
        fail("Removing non-existent case comment should throw CaseCommentNotFoundException.");
    } catch (CaseCommentNotFoundException e) {
    // expected
    } finally {
        if (caseId != null) {
            caseService.cancelCase(caseId);
        }
    }
}
Also used : CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) HashMap(java.util.HashMap) CaseCommentNotFoundException(org.jbpm.casemgmt.api.CaseCommentNotFoundException) AbstractCaseServicesBaseTest(org.jbpm.casemgmt.impl.util.AbstractCaseServicesBaseTest) Test(org.junit.Test)

Aggregations

CaseCommentNotFoundException (org.jbpm.casemgmt.api.CaseCommentNotFoundException)3 CaseFileInstance (org.jbpm.casemgmt.api.model.instance.CaseFileInstance)3 HashMap (java.util.HashMap)2 AbstractCaseServicesBaseTest (org.jbpm.casemgmt.impl.util.AbstractCaseServicesBaseTest)2 Test (org.junit.Test)2 Collection (java.util.Collection)1 List (java.util.List)1 ClassObjectFilter (org.drools.core.ClassObjectFilter)1 RegistryContext (org.drools.core.command.impl.RegistryContext)1 AuthorizationManager (org.jbpm.casemgmt.api.auth.AuthorizationManager)1 CommentInstance (org.jbpm.casemgmt.api.model.instance.CommentInstance)1 CaseEventSupport (org.jbpm.casemgmt.impl.event.CaseEventSupport)1 CaseFileInstanceImpl (org.jbpm.casemgmt.impl.model.instance.CaseFileInstanceImpl)1 CommentInstanceImpl (org.jbpm.casemgmt.impl.model.instance.CommentInstanceImpl)1 Context (org.kie.api.runtime.Context)1 KieSession (org.kie.api.runtime.KieSession)1 FactHandle (org.kie.api.runtime.rule.FactHandle)1 IdentityProvider (org.kie.internal.identity.IdentityProvider)1