class-templates.ReactiveRestResourceTemplate Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of automatiko-engine-codegen Show documentation
Show all versions of automatiko-engine-codegen Show documentation
The Code generation for Automatiko Engine
The newest version!
package com.myspace.demo;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.DefaultValue;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
import io.automatiko.engine.api.Application;
import io.automatiko.engine.api.auth.SecurityPolicy;
import io.automatiko.engine.api.workflow.Process;
import io.automatiko.engine.api.workflow.ProcessInstance;
import io.automatiko.engine.api.workflow.ProcessInstanceExecutionException;
import io.automatiko.engine.api.workflow.WorkItem;
import io.automatiko.engine.api.workflow.workitem.Policy;
@Path("/$name$")
public class $Type$ReactiveResource {
Process<$Type$> process;
Application application;
@POST()
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public CompletionStage<$Type$Output> create_$name$(@Context HttpHeaders httpHeaders, @QueryParam("businessKey") String businessKey, $Type$Input resource) {
if (resource == null) {
resource = new $Type$Input();
}
final $Type$Input value = resource;
return CompletableFuture.supplyAsync(() -> {
return io.automatiko.engine.services.uow.UnitOfWorkExecutor.executeInUnitOfWork(application.unitOfWorkManager(), () -> {
ProcessInstance<$Type$> pi = process.createInstance(businessKey, mapInput(value, new $Type$()));
String startFromNode = httpHeaders.getHeaderString("X-AUTOMATIK-StartFromNode");
if (startFromNode != null) {
pi.startFrom(startFromNode);
} else {
pi.start();
}
return getModel(pi);
});
});
}
@GET()
@Produces(MediaType.APPLICATION_JSON)
public CompletionStage> getAll_$name$() {
return CompletableFuture.supplyAsync(() -> {
return process.instances().values(1, 10).stream()
.map(pi -> mapOutput(new $Type$Output(), pi.variables()))
.collect(Collectors.toList());
});
}
@GET()
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public CompletionStage<$Type$Output> get_$name$(@PathParam("id") String id) {
return CompletableFuture.supplyAsync(() -> {
return process.instances()
.findById(id)
.map(pi -> mapOutput(new $Type$Output(), pi.variables()))
.orElse(null);
});
}
@DELETE()
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public CompletionStage<$Type$Output> delete_$name$(@PathParam("id") final String id) {
return CompletableFuture.supplyAsync(() -> {
return io.automatiko.engine.services.uow.UnitOfWorkExecutor.executeInUnitOfWork(application.unitOfWorkManager(), () -> {
ProcessInstance<$Type$> 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 CompletionStage<$Type$Output> updateModel_$name$(@PathParam("id") String id, $Type$ resource) {
return CompletableFuture.supplyAsync(() -> {
return io.automatiko.engine.services.uow.UnitOfWorkExecutor.executeInUnitOfWork(application.unitOfWorkManager(), () -> {
ProcessInstance<$Type$> pi = process.instances()
.findById(id)
.orElse(null);
if (pi == null) {
return null;
} else {
pi.updateVariables(resource);
return mapOutput(new $Type$Output(), pi.variables());
}
});
});
}
@GET()
@Path("/{id}/tasks")
@Produces(MediaType.APPLICATION_JSON)
public CompletionStage