Skip to content

Commit

Permalink
fix examples
Browse files Browse the repository at this point in the history
  • Loading branch information
elguardian committed Sep 11, 2024
1 parent 0c2a24d commit 0960f52
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,56 @@
*/
package org.acme.travels.usertasks;

import java.util.Optional;

import org.kie.kogito.Application;
import org.kie.kogito.auth.SecurityPolicy;
import org.kie.kogito.internal.process.workitem.KogitoWorkItem;
import org.kie.kogito.internal.process.workitem.KogitoWorkItemHandler;
import org.kie.kogito.internal.process.workitem.KogitoWorkItemManager;
import org.kie.kogito.internal.process.workitem.WorkItemLifeCycle;
import org.kie.kogito.internal.process.workitem.WorkItemPhaseState;
import org.kie.kogito.internal.process.workitem.WorkItemTerminationType;
import org.kie.kogito.internal.process.workitem.WorkItemTransition;
import org.kie.kogito.process.workitems.impl.DefaultKogitoWorkItemHandler;
import org.kie.kogito.process.workitems.impl.DefaultWorkItemLifeCycle;
import org.kie.kogito.process.workitems.impl.DefaultWorkItemLifeCyclePhase;

public class CustomHumanTaskWorkItemHandler extends DefaultKogitoWorkItemHandler {

public static final String TRANSITION_COMPLETE = "complete";
public static final String TRANSITION_ABORT = "abort";
public static final String TRANSITION_ACTIVATE = "activate";
public static final String TRANSITION_START = "start";
public static final String TRANSITION_SKIP = "skip";

private WorkItemPhaseState initialized = WorkItemPhaseState.initialized();
private WorkItemPhaseState completed = WorkItemPhaseState.of("Completed", WorkItemTerminationType.COMPLETE);
private WorkItemPhaseState aborted = WorkItemPhaseState.of("Aborted", WorkItemTerminationType.ABORT);
private WorkItemPhaseState activated = WorkItemPhaseState.of("Activated");
private WorkItemPhaseState started = WorkItemPhaseState.of("Started");

protected Application application;

protected WorkItemLifeCycle workItemLifeCycle;

@Override
public WorkItemLifeCycle initialize() {
WorkItemPhaseState initialized = WorkItemPhaseState.initialized();
WorkItemPhaseState completed = WorkItemPhaseState.of("Completed", WorkItemTerminationType.COMPLETE);
WorkItemPhaseState aborted = WorkItemPhaseState.of("Aborted", WorkItemTerminationType.ABORT);
WorkItemPhaseState activated = WorkItemPhaseState.of("Activated");
WorkItemPhaseState started = WorkItemPhaseState.of("Started");

DefaultWorkItemLifeCyclePhase active = new DefaultWorkItemLifeCyclePhase(TRANSITION_ACTIVATE, initialized, activated, this::activateWorkItemHandler);
DefaultWorkItemLifeCyclePhase start = new DefaultWorkItemLifeCyclePhase(TRANSITION_START, activated, started, this::activateWorkItemHandler);
DefaultWorkItemLifeCyclePhase complete = new DefaultWorkItemLifeCyclePhase(TRANSITION_COMPLETE, started, completed, this::completeWorkItemHandler);
DefaultWorkItemLifeCyclePhase abort = new DefaultWorkItemLifeCyclePhase(TRANSITION_ABORT, started, aborted, this::abortWorkItemHandler);

return new DefaultWorkItemLifeCycle(active, start, abort, complete);
}

@Override
public Optional<WorkItemTransition> completeWorkItemHandler(KogitoWorkItemManager manager, KogitoWorkItemHandler handler, KogitoWorkItem workitem, WorkItemTransition transition) {
getUserFromTransition(transition).ifPresent(e -> workitem.setOutput("ActorId", e));
return Optional.empty();
}

private Optional<String> getUserFromTransition(WorkItemTransition transition) {
Optional<SecurityPolicy> securityPolicy = transition.policies().stream().filter(SecurityPolicy.class::isInstance).map(SecurityPolicy.class::cast).findAny();
if (securityPolicy.isPresent()) {
return Optional.ofNullable(securityPolicy.get().getUser());
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@

import io.quarkus.test.junit.QuarkusTest;

import static java.util.Collections.singletonList;

import jakarta.inject.Inject;
import jakarta.inject.Named;

Expand Down Expand Up @@ -77,8 +79,10 @@ public void testApprovalProcess() {
results.put("approved", true);
KogitoWorkItemHandler handler = approvalsProcess.getKogitoWorkItemHandler(workItems.get(0).getWorkItemHandlerName());
processInstance.transitionWorkItem(workItems.get(0).getId(), handler.newTransition("start", workItems.get(0).getPhaseStatus(), Collections.emptyMap(), policy));
workItems = processInstance.workItems(policy);
processInstance.completeWorkItem(workItems.get(0).getId(), results, policy);

policy = SecurityPolicy.of(IdentityProviders.of("admin", singletonList("mgmt")));
workItems = processInstance.workItems(policy);
assertEquals(0, workItems.size());

Expand All @@ -92,6 +96,7 @@ public void testApprovalProcess() {

results.put("approved", false);
processInstance.transitionWorkItem(workItems.get(0).getId(), handler.newTransition("start", workItems.get(0).getPhaseStatus(), Collections.emptyMap(), policy));
workItems = processInstance.workItems(policy);
processInstance.completeWorkItem(workItems.get(0).getId(), results, policy);
assertEquals(org.kie.api.runtime.process.ProcessInstance.STATE_COMPLETED, processInstance.status());

Expand Down Expand Up @@ -134,8 +139,10 @@ public void testApprovalProcessViaPhases() {
// now test going through phases
KogitoWorkItemHandler handler = approvalsProcess.getKogitoWorkItemHandler(workItems.get(0).getWorkItemHandlerName());
processInstance.transitionWorkItem(workItems.get(0).getId(), handler.newTransition("start", workItems.get(0).getPhaseStatus(), Collections.emptyMap(), policy));
workItems = processInstance.workItems(policy);
processInstance.transitionWorkItem(workItems.get(0).getId(), handler.newTransition("complete", workItems.get(0).getPhaseStatus(), Collections.singletonMap("approved", true), policy));

policy = SecurityPolicy.of(IdentityProviders.of("admin", singletonList("mgmt")));
workItems = processInstance.workItems(policy);
assertEquals(0, workItems.size());

Expand All @@ -148,7 +155,8 @@ public void testApprovalProcessViaPhases() {
assertEquals(1, workItems.size());

processInstance.transitionWorkItem(workItems.get(0).getId(), handler.newTransition("start", workItems.get(0).getPhaseStatus(), Collections.emptyMap(), policy));
processInstance.transitionWorkItem(workItems.get(0).getId(), handler.newTransition("complete", workItems.get(0).getPhaseStatus(), Collections.singletonMap("approved", true), policy));
workItems = processInstance.workItems(policy);
processInstance.transitionWorkItem(workItems.get(0).getId(), handler.newTransition("complete", workItems.get(0).getPhaseStatus(), Collections.singletonMap("approved", false), policy));

assertEquals(org.kie.api.runtime.process.ProcessInstance.STATE_COMPLETED, processInstance.status());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@

package org.acme.travels.usertasks;

import org.kie.kogito.Application;
import java.util.Optional;

import org.kie.kogito.auth.SecurityPolicy;
import org.kie.kogito.internal.process.workitem.KogitoWorkItem;
import org.kie.kogito.internal.process.workitem.KogitoWorkItemHandler;
import org.kie.kogito.internal.process.workitem.KogitoWorkItemManager;
import org.kie.kogito.internal.process.workitem.WorkItemLifeCycle;
import org.kie.kogito.internal.process.workitem.WorkItemPhaseState;
import org.kie.kogito.internal.process.workitem.WorkItemTerminationType;
import org.kie.kogito.internal.process.workitem.WorkItemTransition;
import org.kie.kogito.process.workitems.impl.DefaultKogitoWorkItemHandler;
import org.kie.kogito.process.workitems.impl.DefaultWorkItemLifeCycle;
import org.kie.kogito.process.workitems.impl.DefaultWorkItemLifeCyclePhase;
Expand All @@ -35,23 +41,33 @@ public class CustomHumanTaskWorkItemHandler extends DefaultKogitoWorkItemHandler
public static final String TRANSITION_START = "start";
public static final String TRANSITION_SKIP = "skip";

private WorkItemPhaseState initialized = WorkItemPhaseState.initialized();
private WorkItemPhaseState completed = WorkItemPhaseState.of("Completed", WorkItemTerminationType.COMPLETE);
private WorkItemPhaseState aborted = WorkItemPhaseState.of("Aborted", WorkItemTerminationType.ABORT);
private WorkItemPhaseState activated = WorkItemPhaseState.of("Activated");
private WorkItemPhaseState started = WorkItemPhaseState.of("Started");

protected Application application;

protected WorkItemLifeCycle workItemLifeCycle;

@Override
public WorkItemLifeCycle initialize() {
WorkItemPhaseState initialized = WorkItemPhaseState.initialized();
WorkItemPhaseState completed = WorkItemPhaseState.of("Completed", WorkItemTerminationType.COMPLETE);
WorkItemPhaseState aborted = WorkItemPhaseState.of("Aborted", WorkItemTerminationType.ABORT);
WorkItemPhaseState activated = WorkItemPhaseState.of("Activated");
WorkItemPhaseState started = WorkItemPhaseState.of("Started");

DefaultWorkItemLifeCyclePhase active = new DefaultWorkItemLifeCyclePhase(TRANSITION_ACTIVATE, initialized, activated, this::activateWorkItemHandler);
DefaultWorkItemLifeCyclePhase start = new DefaultWorkItemLifeCyclePhase(TRANSITION_ACTIVATE, activated, started, this::activateWorkItemHandler);
DefaultWorkItemLifeCyclePhase start = new DefaultWorkItemLifeCyclePhase(TRANSITION_START, activated, started, this::activateWorkItemHandler);
DefaultWorkItemLifeCyclePhase complete = new DefaultWorkItemLifeCyclePhase(TRANSITION_COMPLETE, started, completed, this::completeWorkItemHandler);
DefaultWorkItemLifeCyclePhase abort = new DefaultWorkItemLifeCyclePhase(TRANSITION_ABORT, started, aborted, this::abortWorkItemHandler);

return new DefaultWorkItemLifeCycle(active, start, abort, complete);
}

@Override
public Optional<WorkItemTransition> completeWorkItemHandler(KogitoWorkItemManager manager, KogitoWorkItemHandler handler, KogitoWorkItem workitem, WorkItemTransition transition) {
getUserFromTransition(transition).ifPresent(e -> workitem.setOutput("ActorId", e));
return Optional.empty();
}

private Optional<String> getUserFromTransition(WorkItemTransition transition) {
Optional<SecurityPolicy> securityPolicy = transition.policies().stream().filter(SecurityPolicy.class::isInstance).map(SecurityPolicy.class::cast).findAny();
if (securityPolicy.isPresent()) {
return Optional.ofNullable(securityPolicy.get().getUser());
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public void testApprovalProcess() {
processInstance.transitionWorkItem(workItems.get(0).getId(), handler.newTransition("start", workItems.get(0).getPhaseStatus(), Collections.emptyMap(), policy));
processInstance.completeWorkItem(workItems.get(0).getId(), results, policy);

policy = SecurityPolicy.of(IdentityProviders.of("admin", singletonList("mgmt")));
workItems = processInstance.workItems(policy);
assertEquals(0, workItems.size());

Expand All @@ -93,8 +94,9 @@ public void testApprovalProcess() {

workItems = processInstance.workItems(policy);
assertEquals(1, workItems.size());
processInstance.transitionWorkItem(workItems.get(0).getId(), handler.newTransition("start", workItems.get(0).getPhaseStatus(), Collections.emptyMap(), policy));

results.put("approved", false);
processInstance.transitionWorkItem(workItems.get(0).getId(), handler.newTransition("start", workItems.get(0).getPhaseStatus(), Collections.emptyMap(), policy));
processInstance.completeWorkItem(workItems.get(0).getId(), results, policy);
assertEquals(org.kie.api.runtime.process.ProcessInstance.STATE_COMPLETED, processInstance.status());

Expand Down Expand Up @@ -139,8 +141,10 @@ public void testApprovalProcessViaPhases() {

// now test going through phases
processInstance.transitionWorkItem(workItems.get(0).getId(), handler.newTransition("start", workItems.get(0).getPhaseStatus(), Collections.emptyMap(), policy));
workItems = processInstance.workItems(policy);
processInstance.transitionWorkItem(workItems.get(0).getId(), handler.newTransition("complete", workItems.get(0).getPhaseStatus(), singletonMap("approved", true), policy));

policy = SecurityPolicy.of(IdentityProviders.of("admin", singletonList("mgmt")));
workItems = processInstance.workItems(policy);
assertEquals(0, workItems.size());

Expand All @@ -153,6 +157,7 @@ public void testApprovalProcessViaPhases() {

// test that claim can be skipped
processInstance.transitionWorkItem(workItems.get(0).getId(), handler.newTransition("start", workItems.get(0).getPhaseStatus(), Collections.emptyMap(), policy));
workItems = processInstance.workItems(policy);
processInstance.transitionWorkItem(workItems.get(0).getId(), handler.newTransition("complete", workItems.get(0).getPhaseStatus(), singletonMap("approved", false), policy));

assertEquals(org.kie.api.runtime.process.ProcessInstance.STATE_COMPLETED, processInstance.status());
Expand Down

0 comments on commit 0960f52

Please sign in to comment.