org.acme.travels.ApprovalsResource Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of process-usertasks-with-security-springboot
Show all versions of process-usertasks-with-security-springboot
Kogito usertasks orchestration with security enabled on REST api - Spring Boot
package org.acme.travels;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
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.kie.kogito.process.impl.Sig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.acme.travels.ApprovalsModel;
import org.acme.travels.ApprovalsModelOutput;
@Path("/approvals")
@org.springframework.stereotype.Component()
public class ApprovalsResource {
@org.springframework.beans.factory.annotation.Autowired()
@org.springframework.beans.factory.annotation.Qualifier("approvals")
Process process;
@org.springframework.beans.factory.annotation.Autowired()
Application application;
@POST()
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@javax.annotation.security.RolesAllowed({ "employees", "managers" })
public ApprovalsModelOutput createResource_approvals(@Context HttpHeaders httpHeaders, @QueryParam("businessKey") String businessKey, @javax.validation.Valid() @javax.validation.constraints.NotNull() ApprovalsModelInput resource) {
if (resource == null) {
resource = new ApprovalsModelInput();
}
final ApprovalsModelInput value = resource;
return org.kie.kogito.services.uow.UnitOfWorkExecutor.executeInUnitOfWork(application.unitOfWorkManager(), () -> {
ProcessInstance pi = process.createInstance(businessKey, mapInput(value, new ApprovalsModel()));
String startFromNode = httpHeaders.getHeaderString("X-KOGITO-StartFromNode");
if (startFromNode != null) {
pi.startFrom(startFromNode);
} else {
pi.start();
}
return getModel(pi);
});
}
@GET()
@Produces(MediaType.APPLICATION_JSON)
@javax.annotation.security.RolesAllowed({ "employees", "managers" })
public List getResources_approvals() {
return process.instances().values().stream().map(pi -> mapOutput(new ApprovalsModelOutput(), pi.variables())).collect(Collectors.toList());
}
@GET()
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
@javax.annotation.security.RolesAllowed({ "employees", "managers" })
public ApprovalsModelOutput getResource_approvals(@PathParam("id") String id) {
return process.instances().findById(id).map(pi -> mapOutput(new ApprovalsModelOutput(), pi.variables())).orElse(null);
}
@DELETE()
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
@javax.annotation.security.RolesAllowed({ "employees", "managers" })
public ApprovalsModelOutput deleteResource_approvals(@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)
@javax.annotation.security.RolesAllowed({ "employees", "managers" })
public ApprovalsModelOutput updateModel_approvals(@PathParam("id") String id, ApprovalsModel 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 mapOutput(new ApprovalsModelOutput(), pi.variables());
}
});
}
@GET()
@Path("/{id}/tasks")
@Produces(MediaType.APPLICATION_JSON)
@javax.annotation.security.RolesAllowed({ "employees", "managers" })
public Map getTasks_approvals(@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 ApprovalsModelOutput 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 mapOutput(new ApprovalsModelOutput(), 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) };
}
protected ApprovalsModel mapInput(@javax.validation.Valid() @javax.validation.constraints.NotNull() ApprovalsModelInput input, ApprovalsModel resource) {
resource.fromMap(input.toMap());
return resource;
}
protected ApprovalsModelOutput mapOutput(ApprovalsModelOutput output, ApprovalsModel resource) {
output.fromMap(resource.getId(), resource.toMap());
return output;
}
@POST()
@Path("/{id}/firstLineApproval/{workItemId}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public ApprovalsModelOutput completeTask_3(@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 Approvals_3_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}/firstLineApproval/{workItemId}")
@Produces(MediaType.APPLICATION_JSON)
public Approvals_3_TaskInput getTask_3(@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 Approvals_3_TaskInput.fromMap(workItem.getId(), workItem.getName(), workItem.getParameters());
}
} catch (WorkItemNotFoundException e) {
return null;
}
}
@DELETE()
@Path("/{id}/firstLineApproval/{workItemId}")
@Produces(MediaType.APPLICATION_JSON)
public ApprovalsModelOutput abortTask_3(@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;
}
}
@POST()
@Path("/{id}/secondLineApproval/{workItemId}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public ApprovalsModelOutput 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 Approvals_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}/secondLineApproval/{workItemId}")
@Produces(MediaType.APPLICATION_JSON)
public Approvals_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 Approvals_4_TaskInput.fromMap(workItem.getId(), workItem.getName(), workItem.getParameters());
}
} catch (WorkItemNotFoundException e) {
return null;
}
}
@DELETE()
@Path("/{id}/secondLineApproval/{workItemId}")
@Produces(MediaType.APPLICATION_JSON)
public ApprovalsModelOutput 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;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy