Skip to content

Commit

Permalink
[JBPM-9624] Return the process variables for synchronous execution us…
Browse files Browse the repository at this point in the history
…e cases
  • Loading branch information
elguardian committed Mar 9, 2021
1 parent 8121917 commit 32590be
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 3 deletions.
Expand Up @@ -87,6 +87,7 @@ public class RestURI {
// uris
// process related prefixed by PROCESS_URI
public static final String START_PROCESS_POST_URI = "{" + PROCESS_ID + "}/instances";
public static final String START_SYNC_PROCESS_POST_URI = "{" + PROCESS_ID + "}/syncInstances";
public static final String START_PROCESS_FROM_NODES_POST_URI = "{" + PROCESS_ID + "}/instances/fromNodes";
public static final String START_PROCESS_FROM_NODES_WITH_CORRELATION_KEY_POST_URI = "{" + PROCESS_ID + "}/instances/correlation/{" + CORRELATION_KEY + "}/fromNodes";
public static final String START_PROCESS_WITH_CORRELATION_KEY_POST_URI = "{" + PROCESS_ID + "}/instances/correlation/{" + CORRELATION_KEY + "}";
Expand Down
Expand Up @@ -42,16 +42,25 @@
"revapi": {
"_comment": "Changes between 7.48.0.Final and the current branch. These changes are desired and thus ignored.",
"ignore": [
{
{
"code": "java.method.addedToInterface",
"new": "method org.kie.server.api.model.ServiceResponse<org.kie.api.runtime.ExecutionResults> org.kie.server.client.RuleServicesClient::executeCommandsWithResults(java.lang.String, org.kie.api.command.Command<?>, javax.ws.rs.core.Response.Status)",
"package": "org.kie.server.client",
"classSimpleName": "RuleServicesClient",
"methodName": "executeCommandsWithResults",
"elementKind": "method",
"justification": "https://issues.redhat.com/browse/JBPM-9611"
}
]
},
{
"code": "java.method.addedToInterface",
"new": "method java.util.Map<java.lang.String, java.lang.Object> org.kie.server.client.ProcessServicesClient::startSynchronousProcess(java.lang.String, java.lang.String, java.util.Map<java.lang.String, java.lang.Object>)",
"package": "org.kie.server.client",
"classSimpleName": "ProcessServicesClient",
"methodName": "startSynchronousProcess",
"elementKind": "method",
"justification": "[JBPM-9624] Return the process variables for synchronous execution use cases"
}
]
}
}
}
Expand Up @@ -57,6 +57,8 @@ public interface ProcessServicesClient {

Long startProcess(String containerId, String processId, Map<String, Object> variables);

Map<String, Object> startSynchronousProcess(String containerId, String processId, Map<String, Object> variables);

Long startProcess(String containerId, String processId, CorrelationKey correlationKey);

Long startProcess(String containerId, String processId, CorrelationKey correlationKey, Map<String, Object> variables);
Expand Down
Expand Up @@ -90,6 +90,7 @@
import static org.kie.server.api.rest.RestURI.START_PROCESS_FROM_NODES_WITH_CORRELATION_KEY_POST_URI;
import static org.kie.server.api.rest.RestURI.START_PROCESS_POST_URI;
import static org.kie.server.api.rest.RestURI.START_PROCESS_WITH_CORRELATION_KEY_POST_URI;
import static org.kie.server.api.rest.RestURI.START_SYNC_PROCESS_POST_URI;
import static org.kie.server.api.rest.RestURI.TASK_NAME;
import static org.kie.server.api.rest.RestURI.VAR_NAME;
import static org.kie.server.api.rest.RestURI.WORK_ITEM_ID;
Expand Down Expand Up @@ -329,6 +330,35 @@ public Long startProcess(String containerId, String processId, Map<String, Objec
}


@Override
public Map<String, Object> startSynchronousProcess(String containerId, String processId, Map<String, Object> variables) {
Object result = null;

if( config.isRest() ) {

Map<String, Object> valuesMap = new HashMap<>();
valuesMap.put(CONTAINER_ID, containerId);
valuesMap.put(PROCESS_ID, processId);

result = makeHttpPostRequestAndCreateCustomResponse(
build(loadBalancer.getUrl(), PROCESS_URI + "/" + START_SYNC_PROCESS_POST_URI, valuesMap), variables,
Object.class);

} else {
CommandScript script = new CommandScript(singletonList(
(KieServerCommand) new DescriptorCommand( "ProcessService", "startSynchronousProcess", serialize(safeMap(variables)), marshaller.getFormat().getType(), new Object[]{containerId, processId}) ) );
ServiceResponse<String> response = (ServiceResponse<String>) executeJmsCommand( script, DescriptorCommand.class.getName(), "BPM", containerId ).getResponses().get(0);

throwExceptionOnFailure(response);
if (shouldReturnWithNullResponse(response)) {
return null;
}
result = deserialize(response.getResult(), Map.class);
}

return (Map<String, Object>) result;
}

@Override
public Long startProcessFromNodeIds(String containerId, String processId, Map<String, Object> variables, String... nodes) {
Object result = null;
Expand Down
Expand Up @@ -93,6 +93,7 @@
import static org.kie.server.api.rest.RestURI.START_PROCESS_FROM_NODES_WITH_CORRELATION_KEY_POST_URI;
import static org.kie.server.api.rest.RestURI.START_PROCESS_POST_URI;
import static org.kie.server.api.rest.RestURI.START_PROCESS_WITH_CORRELATION_KEY_POST_URI;
import static org.kie.server.api.rest.RestURI.START_SYNC_PROCESS_POST_URI;
import static org.kie.server.remote.rest.common.util.RestUtils.badRequest;
import static org.kie.server.remote.rest.common.util.RestUtils.buildConversationIdHeader;
import static org.kie.server.remote.rest.common.util.RestUtils.createCorrectVariant;
Expand Down Expand Up @@ -199,6 +200,52 @@ public Response startProcess(@javax.ws.rs.core.Context HttpHeaders headers,
}
}


@ApiOperation(value="Starts a new synchronous process instance of a specified process.")
@ApiResponses(value = {
@ApiResponse(code = 201, response=Map.class, message = "Process instance started", examples=@Example(value= {
@ExampleProperty(mediaType=JSON, value=VAR_MAP_JSON),
@ExampleProperty(mediaType=XML, value=VAR_MAP_XML)})),
@ApiResponse(code = 500, message = "Unexpected error"),
@ApiResponse(code = 404, message = "Process ID or Container Id not found"),
@ApiResponse(code = 403, message = "User does not have permission to access this asset")})
@POST
@Path(START_SYNC_PROCESS_POST_URI)
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response startSynchronousProcess(@javax.ws.rs.core.Context HttpHeaders headers,
@ApiParam(value = "container id where the process definition resides", required = true, example = "evaluation_1.0.0-SNAPSHOT") @PathParam(CONTAINER_ID) String containerId,
@ApiParam(value = "process id that new instance should be created from", required = true, example = "evaluation") @PathParam(PROCESS_ID) String processId,
@ApiParam(value = "optional map of process variables", required = false, examples=@Example(value= {
@ExampleProperty(mediaType=JSON, value=VAR_MAP_JSON),
@ExampleProperty(mediaType=XML, value=VAR_MAP_XML)})) @DefaultValue("") String payload) {
Variant v = getVariant(headers);
String type = getContentType(headers);
Header conversationIdHeader = buildConversationIdHeader(containerId, context, headers);

try {
String response = processServiceBase.startSynchronousProcess(containerId, processId, payload, type);

logger.debug("Returning CREATED response with content '{}'", response);
return createResponse(response, v, Response.Status.CREATED, conversationIdHeader);
} catch (DeploymentNotActiveException e) {
return badRequest(
e.getMessage(), v);
} catch (DeploymentNotFoundException e) {
return notFound(
MessageFormat.format(CONTAINER_NOT_FOUND, containerId), v);
} catch (ProcessDefinitionNotFoundException e) {
return notFound(
MessageFormat.format(PROCESS_DEFINITION_NOT_FOUND, processId, containerId), v);
} catch (SecurityException e) {
return forbidden(errorMessage(e, e.getMessage()), v, conversationIdHeader);
} catch (Exception e) {
logger.error("Unexpected error during processing {}", e.getMessage(), e);
return internalServerError(
MessageFormat.format(CREATE_RESPONSE_ERROR, e.getMessage()), v);
}
}

@ApiOperation(value = "Starts a new process instance from the specific nodes")
@ApiResponses(value = {@ApiResponse(code = 201, response = Long.class, message = "Process instance created",
examples = @Example(value = {@ExampleProperty(mediaType = JSON, value = LONG_RESPONSE_JSON),
Expand Down
Expand Up @@ -85,6 +85,21 @@ public String startProcess(String containerId, String processId, String marshall
return response;
}

public String startSynchronousProcess(String containerId, String processId, String payload, String marshallingType) {
containerId = context.getContainerId(containerId, ContainerLocatorProvider.get().getLocator());
// check validity of deployment and process id
definitionService.getProcessDefinition(containerId, processId);

logger.debug("About to unmarshal parameters from start spec parameters: '{}'", payload);
Map<String, Object> parameters = marshallerHelper.unmarshal(containerId, payload, marshallingType, Map.class);

logger.debug("Calling start sync process with id {} on container {} and parameters {}", processId, containerId, null);
Map<String, Object> outcome = processService.startSynchronousProcess(containerId, processId, parameters);

// return response
return marshallerHelper.marshal(containerId, marshallingType, outcome);
}


public String startProcess(String containerId, String processId, String payload, String marshallingType) {
containerId = context.getContainerId(containerId, ContainerLocatorProvider.get().getLocator());
Expand Down
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<definitions id="Definition"
targetNamespace="http://www.example.org/MinimalExample"
typeLanguage="http://www.java.com/javaTypes"
expressionLanguage="http://www.mvel.org/2.0"
xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL BPMN20.xsd"
xmlns:g="http://www.jboss.org/drools/flow/gpd"
xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI"
xmlns:dc="http://www.omg.org/spec/DD/20100524/DC"
xmlns:di="http://www.omg.org/spec/DD/20100524/DI"
xmlns:tns="http://www.jboss.org/drools">

<itemDefinition id="_idItem" structureRef="String" />

<process processType="Private" isExecutable="true" id="restart-project.syncProcess" name="Hello Process" tns:packageName="com.sample" >

<!-- process variables -->
<property id="name" itemSubjectRef="_idItem"/>

<extensionElements>
<tns:import name="java.util.List" />
<tns:import name="java.util.ArrayList" />
</extensionElements>
<!-- nodes -->
<startEvent id="_1" name="Start" isInterrupting="true"/>
<scriptTask id="_2" name="Hello" scriptFormat="http://www.java.com/java" >
<script>kcontext.setVariable("name", "bye");</script>
</scriptTask>
<endEvent id="_3" name="End" >
<terminateEventDefinition />
</endEvent>

<!-- connections -->
<sequenceFlow id="_1-_2" sourceRef="_1" targetRef="_2" />
<sequenceFlow id="_2-_3" sourceRef="_2" targetRef="_3" />

</process>

<bpmndi:BPMNDiagram>
<bpmndi:BPMNPlane bpmnElement="hello" >
<bpmndi:BPMNShape bpmnElement="_1" >
<dc:Bounds x="16" y="16" width="48" height="48" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="_2" >
<dc:Bounds x="96" y="16" width="80" height="48" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="_3" >
<dc:Bounds x="208" y="16" width="48" height="48" />
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="_1-_2" >
<di:waypoint x="40" y="40" />
<di:waypoint x="136" y="40" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="_2-_3" >
<di:waypoint x="136" y="40" />
<di:waypoint x="232" y="40" />
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>

</definitions>
Expand Up @@ -61,6 +61,7 @@ public abstract class JbpmKieServerBaseIntegrationTest extends RestJmsSharedBase
protected static final String CONTAINER_ID_NOTIFICATION = "notification-project";

protected static final String PROCESS_ID_RESTART = "restart-project.simple";
protected static final String PROCESS_SYNC_ID = "restart-project.syncProcess";
protected static final String PROCESS_ID_USERTASK = "definition-project.usertask";
protected static final String PROCESS_ID_USERTASK2 = "definition-project.usertask2";
protected static final String PROCESS_ID_EVALUATION = "definition-project.evaluation";
Expand Down
Expand Up @@ -15,6 +15,7 @@

package org.kie.server.integrationtests.jbpm;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -133,4 +134,12 @@ public void testStartProcessFromNodeIdWithCorrelationKeyProcess() {
fail(e.getMessage());
}
}

@Test()
public void testStartSyncProcess() {
Map<String, Object> outcome = processClient.startSynchronousProcess(CONTAINER_ID_RESTART, PROCESS_SYNC_ID, Collections.singletonMap("name", "hello"));
assertNotNull(outcome);
assertEquals("bye", outcome.get("name"));

}
}

0 comments on commit 32590be

Please sign in to comment.