org.kie.kogito.examples.OrdersResource 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.OrdersModel;
@Path("/orders")
@javax.enterprise.context.ApplicationScoped()
public class OrdersResource {
@javax.inject.Inject()
@javax.inject.Named("demo.orders")
Process process;
@javax.inject.Inject()
Application application;
@POST()
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public OrdersModel createResource_orders(@Context HttpHeaders httpHeaders, OrdersModel resource) {
if (resource == null) {
resource = new OrdersModel();
}
final OrdersModel value = resource;
return org.kie.kogito.services.uow.UnitOfWorkExecutor.executeInUnitOfWork(application.unitOfWorkManager(), () -> {
ProcessInstance pi = process.createInstance(value);
String startFromNode = httpHeaders.getHeaderString("X-KOGITO-StartFromNode");
if (startFromNode != null) {
pi.startFrom(startFromNode);
} else {
pi.start();
}
return getModel(pi);
});
}
@GET()
@Produces(MediaType.APPLICATION_JSON)
public List getResources_orders() {
return process.instances().values().stream().map(ProcessInstance::variables).collect(Collectors.toList());
}
@GET()
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public OrdersModel getResource_orders(@PathParam("id") String id) {
return process.instances().findById(id).map(ProcessInstance::variables).orElse(null);
}
@DELETE()
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public OrdersModel deleteResource_orders(@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 OrdersModel updateModel_orders(@PathParam("id") String id, OrdersModel 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_orders(@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 OrdersModel 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) };
}
}