All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
io.nflow.rest.v1.WorkflowInstanceResource Maven / Gradle / Ivy
package io.nflow.rest.v1;
import static io.nflow.engine.workflow.instance.WorkflowInstanceAction.WorkflowActionType.externalChange;
import static java.lang.String.format;
import static java.util.Arrays.asList;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static javax.ws.rs.core.Response.noContent;
import static javax.ws.rs.core.Response.status;
import static javax.ws.rs.core.Response.Status.CONFLICT;
import static org.apache.commons.lang3.StringUtils.defaultIfBlank;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.trimToEmpty;
import static org.apache.commons.lang3.StringUtils.trimToNull;
import static org.joda.time.DateTime.now;
import static org.springframework.util.StringUtils.isEmpty;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import javax.inject.Inject;
import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import org.springframework.stereotype.Component;
import io.nflow.engine.service.WorkflowInstanceService;
import io.nflow.engine.workflow.instance.QueryWorkflowInstances;
import io.nflow.engine.workflow.instance.WorkflowInstance;
import io.nflow.engine.workflow.instance.WorkflowInstance.WorkflowInstanceStatus;
import io.nflow.engine.workflow.instance.WorkflowInstanceAction;
import io.nflow.rest.config.NflowCors;
import io.nflow.rest.v1.converter.CreateWorkflowConverter;
import io.nflow.rest.v1.converter.ListWorkflowInstanceConverter;
import io.nflow.rest.v1.msg.CreateWorkflowInstanceRequest;
import io.nflow.rest.v1.msg.CreateWorkflowInstanceResponse;
import io.nflow.rest.v1.msg.ListWorkflowInstanceResponse;
import io.nflow.rest.v1.msg.UpdateWorkflowInstanceRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
@Path("/nflow/v1/workflow-instance")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@Api("nFlow workflow instance management")
@Component
@NflowCors
public class WorkflowInstanceResource {
private final WorkflowInstanceService workflowInstances;
private final CreateWorkflowConverter createWorkflowConverter;
private final ListWorkflowInstanceConverter listWorkflowConverter;
public static final String currentStateVariables = "currentStateVariables";
public static final String actions = "actions";
public static final String actionStateVariables = "actionStateVariables";
public static final String childWorkflows = "childWorkflows";
public static final String INCLUDE_PARAM_VALUES = currentStateVariables + "," + actions + "," + actionStateVariables + ","
+ childWorkflows;
private static final String INCLUDE_PARAM_DESC = "Data to include in response. " + currentStateVariables
+ " = current stateVariables for worfklow, " + actions + " = state transitions, " + actionStateVariables
+ " = state variable changes for actions, " + childWorkflows + " = map of created child workflow instance IDs by action ID";
@Inject
public WorkflowInstanceResource(WorkflowInstanceService workflowInstances, CreateWorkflowConverter createWorkflowConverter,
ListWorkflowInstanceConverter listWorkflowConverter) {
this.workflowInstances = workflowInstances;
this.createWorkflowConverter = createWorkflowConverter;
this.listWorkflowConverter = listWorkflowConverter;
}
@PUT
@ApiOperation(value = "Submit new workflow instance")
@ApiResponses(@ApiResponse(code = 201, message = "Workflow was created", response = CreateWorkflowInstanceResponse.class))
public Response createWorkflowInstance(
@Valid @ApiParam(value = "Submitted workflow instance information", required = true) CreateWorkflowInstanceRequest req) {
WorkflowInstance instance = createWorkflowConverter.convert(req);
int id = workflowInstances.insertWorkflowInstance(instance);
instance = workflowInstances.getWorkflowInstance(id);
return Response.created(URI.create(String.valueOf(id))).entity(createWorkflowConverter.convert(instance)).build();
}
@PUT
@Path("/{id}")
@ApiOperation(value = "Update workflow instance", notes = "The service is typically used in manual state "
+ "transition via nFlow Explorer or a business UI.")
@ApiResponses({ @ApiResponse(code = 204, message = "If update was successful"),
@ApiResponse(code = 409, message = "If workflow was executing and no update was done") })
public Response updateWorkflowInstance(@ApiParam("Internal id for workflow instance") @PathParam("id") int id,
@ApiParam("Submitted workflow instance information") UpdateWorkflowInstanceRequest req) {
WorkflowInstance.Builder builder = new WorkflowInstance.Builder().setId(id).setNextActivation(req.nextActivationTime);
String msg = defaultIfBlank(req.actionDescription, "");
if (!isEmpty(req.state)) {
builder.setState(req.state);
if (isBlank(req.actionDescription)) {
msg = "API changed state to " + req.state + ". ";
}
}
if (req.nextActivationTime != null && isBlank(req.actionDescription)) {
msg += "API changed nextActivationTime to " + req.nextActivationTime + ". ";
}
if (msg.isEmpty()) {
return noContent().build();
}
WorkflowInstance instance = builder.setStateText(msg).build();
boolean updated = workflowInstances.updateWorkflowInstance(instance, new WorkflowInstanceAction.Builder(instance)
.setType(externalChange).setStateText(trimToNull(msg)).setExecutionEnd(now()).build());
return (updated ? noContent() : status(CONFLICT)).build();
}
@GET
@Path("/{id}")
@ApiOperation(value = "Fetch a workflow instance", notes = "Fetch full state and action history of a single workflow instance.")
public ListWorkflowInstanceResponse fetchWorkflowInstance(
@ApiParam("Internal id for workflow instance") @PathParam("id") int id,
@QueryParam("include") @ApiParam(value = INCLUDE_PARAM_DESC, allowableValues = INCLUDE_PARAM_VALUES, allowMultiple = true) String include,
@QueryParam("maxActions") @ApiParam("Maximum number of actions returned for each workflow instance") Long maxActions) {
Collection instances = listWorkflowInstances(asList(id), Collections.emptyList(), null, null,
Collections. emptyList(), Collections. emptyList(), null, null, include, 1L, maxActions);
if (instances.isEmpty()) {
throw new NotFoundException(format("Workflow instance %s not found", id));
}
return instances.iterator().next();
}
@GET
@ApiOperation(value = "List workflow instances", response = ListWorkflowInstanceResponse.class, responseContainer = "List")
public Collection listWorkflowInstances(
@QueryParam("id") @ApiParam("Internal id of workflow instance") List ids,
@QueryParam("type") @ApiParam("Workflow definition type of workflow instance") List types,
@QueryParam("parentWorkflowId") @ApiParam("Id of parent workflow instance") Integer parentWorkflowId,
@QueryParam("parentActionId") @ApiParam("Id of parent workflow instance action") Integer parentActionId,
@QueryParam("state") @ApiParam("Current state of workflow instance") List states,
@QueryParam("status") @ApiParam("Current status of workflow instance") List statuses,
@QueryParam("businessKey") @ApiParam("Business key for workflow instance") String businessKey,
@QueryParam("externalId") @ApiParam("External id for workflow instance") String externalId,
@QueryParam("include") @ApiParam(value = INCLUDE_PARAM_DESC, allowableValues = INCLUDE_PARAM_VALUES, allowMultiple = true) String include,
@QueryParam("maxResults") @ApiParam("Maximum number of workflow instances to be returned") Long maxResults,
@QueryParam("maxActions") @ApiParam("Maximum number of actions returned for each workflow instance") Long maxActions) {
List includes = parseIncludes(include);
QueryWorkflowInstances q = new QueryWorkflowInstances.Builder() //
.addIds(ids.toArray(new Integer[ids.size()])) //
.addTypes(types.toArray(new String[types.size()])) //
.setParentWorkflowId(parentWorkflowId) //
.setParentActionId(parentActionId) //
.addStates(states.toArray(new String[states.size()])) //
.addStatuses(statuses.toArray(new WorkflowInstanceStatus[statuses.size()])) //
.setBusinessKey(businessKey) //
.setExternalId(externalId) //
.setIncludeCurrentStateVariables(includes.contains(currentStateVariables)) //
.setIncludeActions(includes.contains(actions)) //
.setIncludeActionStateVariables(includes.contains(actionStateVariables)) //
.setMaxResults(maxResults) //
.setMaxActions(maxActions) //
.setIncludeChildWorkflows(includes.contains(childWorkflows)).build();
Collection instances = workflowInstances.listWorkflowInstances(q);
List resp = new ArrayList<>();
for (WorkflowInstance instance : instances) {
resp.add(listWorkflowConverter.convert(instance, q));
}
return resp;
}
private List parseIncludes(String include) {
return asList(trimToEmpty(include).split(","));
}
}