org.kie.kogito.examples.OrderItemsResource Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jbpm-quarkus-example
Show all versions of jbpm-quarkus-example
Order management service
package org.kie.kogito.examples;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ws.rs.Consumes;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import org.kie.api.runtime.process.WorkItemNotFoundException;
import org.kie.kogito.Application;
import org.kie.kogito.auth.SecurityPolicy;
import org.kie.kogito.process.Process;
import org.kie.kogito.process.ProcessInstance;
import org.kie.kogito.process.ProcessInstanceExecutionException;
import org.kie.kogito.process.WorkItem;
import org.kie.kogito.process.workitem.Policy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.kie.kogito.examples.OrderItemsModel;
@Path("/orderItems")
@javax.enterprise.context.ApplicationScoped()
public class OrderItemsResource {
@javax.inject.Inject()
@javax.inject.Named("demo.orderItems")
Process process;
@javax.inject.Inject()
Application application;
@GET()
@Produces(MediaType.APPLICATION_JSON)
public List getResources_orderItems() {
return process.instances().values().stream().map(ProcessInstance::variables).collect(Collectors.toList());
}
@GET()
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public OrderItemsModel getResource_orderItems(@PathParam("id") String id) {
return process.instances().findById(id).map(ProcessInstance::variables).orElse(null);
}
@DELETE()
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public OrderItemsModel deleteResource_orderItems(@PathParam("id") final String id) {
return org.kie.kogito.services.uow.UnitOfWorkExecutor.executeInUnitOfWork(application.unitOfWorkManager(), () -> {
ProcessInstance pi = process.instances().findById(id).orElse(null);
if (pi == null) {
return null;
} else {
pi.abort();
return getModel(pi);
}
});
}
@POST()
@Path("/{id}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public OrderItemsModel updateModel_orderItems(@PathParam("id") String id, OrderItemsModel resource) {
return org.kie.kogito.services.uow.UnitOfWorkExecutor.executeInUnitOfWork(application.unitOfWorkManager(), () -> {
ProcessInstance pi = process.instances().findById(id).orElse(null);
if (pi == null) {
return null;
} else {
pi.updateVariables(resource);
return pi.variables();
}
});
}
@GET()
@Path("/{id}/tasks")
@Produces(MediaType.APPLICATION_JSON)
public Map getTasks_orderItems(@PathParam("id") String id, @QueryParam("user") final String user, @QueryParam("group") final List groups) {
return process.instances().findById(id).map(pi -> pi.workItems(policies(user, groups))).map(l -> l.stream().collect(Collectors.toMap(WorkItem::getId, WorkItem::getName))).orElse(null);
}
protected OrderItemsModel getModel(ProcessInstance pi) {
if (pi.status() == ProcessInstance.STATE_ERROR && pi.error().isPresent()) {
throw new ProcessInstanceExecutionException(pi.id(), pi.error().get().failedNodeId(), pi.error().get().errorMessage());
}
return pi.variables();
}
protected Policy[] policies(String user, List groups) {
if (user == null) {
return new Policy[0];
}
org.kie.kogito.auth.IdentityProvider identity = null;
if (user != null) {
identity = new org.kie.kogito.services.identity.StaticIdentityProvider(user, groups);
}
return new Policy[] { SecurityPolicy.of(identity) };
}
@POST()
@Path("/{id}/Verify_order/{workItemId}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public OrderItemsModel completeTask_4(@PathParam("id") final String id, @PathParam("workItemId") final String workItemId, @QueryParam("phase") @DefaultValue("complete") final String phase, @QueryParam("user") final String user, @QueryParam("group") final List groups, final OrderItems_4_TaskOutput model) {
try {
return org.kie.kogito.services.uow.UnitOfWorkExecutor.executeInUnitOfWork(application.unitOfWorkManager(), () -> {
ProcessInstance pi = process.instances().findById(id).orElse(null);
if (pi == null) {
return null;
} else {
org.kie.kogito.auth.IdentityProvider identity = null;
if (user != null) {
identity = new org.kie.kogito.services.identity.StaticIdentityProvider(user, groups);
}
org.jbpm.process.instance.impl.humantask.HumanTaskTransition transition = new org.jbpm.process.instance.impl.humantask.HumanTaskTransition(phase, model.toMap(), identity);
pi.transitionWorkItem(workItemId, transition);
return getModel(pi);
}
});
} catch (WorkItemNotFoundException e) {
return null;
}
}
@GET()
@Path("/{id}/Verify_order/{workItemId}")
@Produces(MediaType.APPLICATION_JSON)
public OrderItems_4_TaskInput getTask_4(@PathParam("id") String id, @PathParam("workItemId") String workItemId, @QueryParam("user") final String user, @QueryParam("group") final List groups) {
try {
ProcessInstance pi = process.instances().findById(id).orElse(null);
if (pi == null) {
return null;
} else {
WorkItem workItem = pi.workItem(workItemId, policies(user, groups));
if (workItem == null) {
return null;
}
return OrderItems_4_TaskInput.fromMap(workItem.getId(), workItem.getName(), workItem.getParameters());
}
} catch (WorkItemNotFoundException e) {
return null;
}
}
@DELETE()
@Path("/{id}/Verify_order/{workItemId}")
@Produces(MediaType.APPLICATION_JSON)
public OrderItemsModel abortTask_4(@PathParam("id") final String id, @PathParam("workItemId") final String workItemId, @QueryParam("phase") @DefaultValue("abort") final String phase, @QueryParam("user") final String user, @QueryParam("group") final List groups) {
try {
return org.kie.kogito.services.uow.UnitOfWorkExecutor.executeInUnitOfWork(application.unitOfWorkManager(), () -> {
ProcessInstance pi = process.instances().findById(id).orElse(null);
if (pi == null) {
return null;
} else {
org.kie.kogito.auth.IdentityProvider identity = null;
if (user != null) {
identity = new org.kie.kogito.services.identity.StaticIdentityProvider(user, groups);
}
org.jbpm.process.instance.impl.humantask.HumanTaskTransition transition = new org.jbpm.process.instance.impl.humantask.HumanTaskTransition(phase, null, identity);
pi.transitionWorkItem(workItemId, transition);
return getModel(pi);
}
});
} catch (WorkItemNotFoundException e) {
return null;
}
}
}