@Override public CorrelationKey unmarshal(String key) throws Exception { List<String> keys = new ArrayList<String>(); for (String k : key.split(",")) { keys.add(key); } return KieInternalServices.Factory.get().newCorrelationKeyFactory().newCorrelationKey(keys); }
public class ReopenCaseCommand extends CaseCommand<Void> { private static final long serialVersionUID = 6811181095390934146L; private static final Logger logger = LoggerFactory.getLogger(ReopenCaseCommand.class); private static CorrelationKeyFactory correlationKeyFactory = KieInternalServices.Factory.get().newCorrelationKeyFactory(); private String caseId; private String deploymentId; private String caseDefinitionId; private Map<String, Object> data; private transient ProcessService processService; public ReopenCaseCommand( String caseId, String deploymentId, String caseDefinitionId, Map<String, Object> data, ProcessService processService) { this.caseId = caseId; this.deploymentId = deploymentId; this.caseDefinitionId = caseDefinitionId; this.data = data; this.processService = processService; } @Override public Void execute(Context context) { CaseEventSupport caseEventSupport = getCaseEventSupport(context); caseEventSupport.fireBeforeCaseReopened(caseId, deploymentId, caseDefinitionId, data); KieSession ksession = ((RegistryContext) context).lookup(KieSession.class); if (data != null && !data.isEmpty()) { logger.debug("Updating case file in working memory"); Collection<? extends Object> caseFiles = ksession.getObjects(new ClassObjectFilter(CaseFileInstance.class)); if (caseFiles.size() == 0) { throw new CaseNotFoundException("Case with id " + caseId + " was not found"); } CaseFileInstance caseFile = (CaseFileInstance) caseFiles.iterator().next(); FactHandle factHandle = ksession.getFactHandle(caseFile); caseFile.addAll(data); ksession.update(factHandle, caseFile); } logger.debug( "Starting process instance for case {} and case definition {}", caseId, caseDefinitionId); CorrelationKey correlationKey = correlationKeyFactory.newCorrelationKey(caseId); Map<String, Object> params = new HashMap<>(); // set case id to allow it to use CaseContext when creating runtime engine params.put(EnvironmentName.CASE_ID, caseId); long processInstanceId = processService.startProcess(deploymentId, caseDefinitionId, correlationKey, params); logger.debug( "Case {} successfully reopened (process instance id {})", caseId, processInstanceId); caseEventSupport.fireAfterCaseReopened( caseId, deploymentId, caseDefinitionId, data, processInstanceId); return null; } public void setProcessService(ProcessService processService) { this.processService = processService; } }
@Path("server/" + PROCESS_URI) public class ProcessResource { public static final Logger logger = LoggerFactory.getLogger(ProcessResource.class); private ProcessServiceBase processServiceBase; private DefinitionServiceBase definitionServiceBase; private RuntimeDataServiceBase runtimeDataServiceBase; private MarshallerHelper marshallerHelper; private KieServerRegistry context; private CorrelationKeyFactory correlationKeyFactory = KieInternalServices.Factory.get().newCorrelationKeyFactory(); public ProcessResource() {} public ProcessResource( ProcessServiceBase processServiceBase, DefinitionServiceBase definitionService, RuntimeDataServiceBase runtimeDataServiceBase, KieServerRegistry context) { this.processServiceBase = processServiceBase; this.definitionServiceBase = definitionServiceBase; this.runtimeDataServiceBase = runtimeDataServiceBase; this.context = context; this.marshallerHelper = new MarshallerHelper(context); } protected static String getRelativePath(HttpServletRequest httpRequest) { String url = httpRequest.getRequestURI(); url = url.replaceAll(".*/rest", ""); return url; } @POST @Path(START_PROCESS_POST_URI) @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public Response startProcess( @javax.ws.rs.core.Context HttpHeaders headers, @PathParam("id") String containerId, @PathParam("pId") String processId, @DefaultValue("") String payload) { Variant v = getVariant(headers); String type = getContentType(headers); try { String response = processServiceBase.startProcess(containerId, processId, payload, type); logger.debug("Returning CREATED response with content '{}'", response); Header conversationIdHeader = buildConversationIdHeader(containerId, context, headers); return createResponse(response, v, Response.Status.CREATED, conversationIdHeader); } catch (Exception e) { logger.error("Unexpected error during processing {}", e.getMessage(), e); return internalServerError(MessageFormat.format(CREATE_RESPONSE_ERROR, e.getMessage()), v); } } @POST @Path(START_PROCESS_WITH_CORRELATION_KEY_POST_URI) @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public Response startProcessWithCorrelation( @javax.ws.rs.core.Context HttpHeaders headers, @PathParam("id") String containerId, @PathParam("pId") String processId, @PathParam("correlationKey") String correlationKey, @DefaultValue("") String payload) { Variant v = getVariant(headers); String type = getContentType(headers); try { String response = processServiceBase.startProcessWithCorrelation( containerId, processId, correlationKey, payload, type); logger.debug("Returning CREATED response with content '{}'", response); Header conversationIdHeader = buildConversationIdHeader(containerId, context, headers); return createResponse(response, v, Response.Status.CREATED, conversationIdHeader); } catch (Exception e) { logger.error("Unexpected error during processing {}", e.getMessage(), e); return internalServerError(MessageFormat.format(CREATE_RESPONSE_ERROR, e.getMessage()), v); } } @DELETE @Path(ABORT_PROCESS_INST_DEL_URI) @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public Response abortProcessInstance( @javax.ws.rs.core.Context HttpHeaders headers, @PathParam("id") String containerId, @PathParam("pInstanceId") Long processInstanceId) { Variant v = getVariant(headers); Header conversationIdHeader = buildConversationIdHeader(containerId, context, headers); try { processServiceBase.abortProcessInstance(containerId, processInstanceId); // produce 204 NO_CONTENT response code return noContent(v, conversationIdHeader); } catch (ProcessInstanceNotFoundException e) { return notFound( MessageFormat.format(PROCESS_INSTANCE_NOT_FOUND, processInstanceId), v, conversationIdHeader); } catch (DeploymentNotFoundException e) { return notFound( MessageFormat.format(CONTAINER_NOT_FOUND, containerId), v, conversationIdHeader); } catch (Exception e) { logger.error("Unexpected error during processing {}", e.getMessage(), e); return internalServerError( MessageFormat.format(UNEXPECTED_ERROR, e.getMessage()), v, conversationIdHeader); } } @DELETE @Path(ABORT_PROCESS_INSTANCES_DEL_URI) @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public Response abortProcessInstances( @javax.ws.rs.core.Context HttpHeaders headers, @PathParam("id") String containerId, @QueryParam("instanceId") List<Long> processInstanceIds) { Variant v = getVariant(headers); Header conversationIdHeader = buildConversationIdHeader(containerId, context, headers); try { processServiceBase.abortProcessInstances(containerId, processInstanceIds); // produce 204 NO_CONTENT response code return noContent(v, conversationIdHeader); } catch (ProcessInstanceNotFoundException e) { return notFound( MessageFormat.format(PROCESS_INSTANCE_NOT_FOUND, processInstanceIds), v, conversationIdHeader); } catch (DeploymentNotFoundException e) { return notFound( MessageFormat.format(CONTAINER_NOT_FOUND, containerId), v, conversationIdHeader); } catch (Exception e) { logger.error("Unexpected error during processing {}", e.getMessage(), e); return internalServerError( MessageFormat.format(UNEXPECTED_ERROR, e.getMessage()), v, conversationIdHeader); } } @POST @Path(SIGNAL_PROCESS_INST_POST_URI) @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public Response signalProcessInstance( @javax.ws.rs.core.Context HttpHeaders headers, @PathParam("id") String containerId, @PathParam("pInstanceId") Long processInstanceId, @PathParam("sName") String signalName, String eventPayload) { Variant v = getVariant(headers); String type = getContentType(headers); Header conversationIdHeader = buildConversationIdHeader(containerId, context, headers); try { processServiceBase.signalProcessInstance( containerId, processInstanceId, signalName, eventPayload, type); return createResponse(null, v, Response.Status.OK, conversationIdHeader); } catch (ProcessInstanceNotFoundException e) { return notFound( MessageFormat.format(PROCESS_INSTANCE_NOT_FOUND, processInstanceId), v, conversationIdHeader); } catch (DeploymentNotFoundException e) { return notFound( MessageFormat.format(CONTAINER_NOT_FOUND, containerId), v, conversationIdHeader); } catch (Exception e) { logger.error("Unexpected error during processing {}", e.getMessage(), e); return internalServerError( MessageFormat.format(UNEXPECTED_ERROR, e.getMessage()), v, conversationIdHeader); } } @POST @Path(SIGNAL_PROCESS_INSTANCES_PORT_URI) @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public Response signalProcessInstances( @javax.ws.rs.core.Context HttpHeaders headers, @PathParam("id") String containerId, @QueryParam("instanceId") List<Long> processInstanceIds, @PathParam("sName") String signalName, String eventPayload) { Variant v = getVariant(headers); String type = getContentType(headers); Header conversationIdHeader = buildConversationIdHeader(containerId, context, headers); try { if (processInstanceIds != null && !processInstanceIds.isEmpty()) { logger.debug("Signaling given process instances - {}", processInstanceIds); processServiceBase.signalProcessInstances( containerId, processInstanceIds, signalName, eventPayload, type); } else { logger.debug("No process instances given, signal container.."); processServiceBase.signal(containerId, signalName, eventPayload, type); } return createResponse("", v, Response.Status.OK, conversationIdHeader); } catch (ProcessInstanceNotFoundException e) { return notFound( MessageFormat.format(PROCESS_INSTANCE_NOT_FOUND, processInstanceIds), v, conversationIdHeader); } catch (DeploymentNotFoundException e) { return notFound( MessageFormat.format(CONTAINER_NOT_FOUND, containerId), v, conversationIdHeader); } catch (Exception e) { logger.error("Unexpected error during processing {}", e.getMessage(), e); return internalServerError( MessageFormat.format(UNEXPECTED_ERROR, e.getMessage()), v, conversationIdHeader); } } @GET @Path(PROCESS_INSTANCE_GET_URI) @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public Response getProcessInstance( @javax.ws.rs.core.Context HttpHeaders headers, @PathParam("id") String containerId, @PathParam("pInstanceId") Long processInstanceId, @QueryParam("withVars") boolean withVars) { Variant v = getVariant(headers); String type = getContentType(headers); try { String response = processServiceBase.getProcessInstance(containerId, processInstanceId, withVars, type); logger.debug("Returning OK response with content '{}'", response); Header conversationIdHeader = buildConversationIdHeader(containerId, context, headers); return createResponse(response, v, Response.Status.OK, conversationIdHeader); } catch (Exception e) { logger.error("Unexpected error during processing {}", e.getMessage(), e); return internalServerError(MessageFormat.format(UNEXPECTED_ERROR, e.getMessage()), v); } } @PUT @Path(PROCESS_INSTANCE_VAR_PUT_URI) @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public Response setProcessVariable( @javax.ws.rs.core.Context HttpHeaders headers, @PathParam("id") String containerId, @PathParam("pInstanceId") Long processInstanceId, @PathParam("varName") String varName, String variablePayload) { Variant v = getVariant(headers); String type = getContentType(headers); Header conversationIdHeader = buildConversationIdHeader(containerId, context, headers); try { processServiceBase.setProcessVariable( containerId, processInstanceId, varName, variablePayload, type); return createResponse("", v, Response.Status.CREATED, conversationIdHeader); } catch (ProcessInstanceNotFoundException e) { return notFound( MessageFormat.format(PROCESS_INSTANCE_NOT_FOUND, processInstanceId), v, conversationIdHeader); } catch (DeploymentNotFoundException e) { return notFound( MessageFormat.format(CONTAINER_NOT_FOUND, containerId), v, conversationIdHeader); } catch (Exception e) { logger.error("Unexpected error during processing {}", e.getMessage(), e); return internalServerError( MessageFormat.format(UNEXPECTED_ERROR, e.getMessage()), v, conversationIdHeader); } } @POST @Path(PROCESS_INSTANCE_VARS_POST_URI) @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public Response setProcessVariables( @javax.ws.rs.core.Context HttpHeaders headers, @PathParam("id") String containerId, @PathParam("pInstanceId") Long processInstanceId, String variablePayload) { Variant v = getVariant(headers); String type = getContentType(headers); Header conversationIdHeader = buildConversationIdHeader(containerId, context, headers); try { processServiceBase.setProcessVariables(containerId, processInstanceId, variablePayload, type); return createResponse("", v, Response.Status.OK, conversationIdHeader); } catch (ProcessInstanceNotFoundException e) { return notFound( MessageFormat.format(PROCESS_INSTANCE_NOT_FOUND, processInstanceId), v, conversationIdHeader); } catch (DeploymentNotFoundException e) { return notFound( MessageFormat.format(CONTAINER_NOT_FOUND, containerId), v, conversationIdHeader); } catch (Exception e) { logger.error("Unexpected error during processing {}", e.getMessage(), e); return internalServerError( MessageFormat.format(UNEXPECTED_ERROR, e.getMessage()), v, conversationIdHeader); } } @GET @Path(PROCESS_INSTANCE_VAR_GET_URI) @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public Object getProcessInstanceVariable( @javax.ws.rs.core.Context HttpHeaders headers, @PathParam("id") String containerId, @PathParam("pInstanceId") Long processInstanceId, @PathParam("varName") String varName) { Variant v = getVariant(headers); String type = getContentType(headers); Header conversationIdHeader = buildConversationIdHeader(containerId, context, headers); try { String response = processServiceBase.getProcessInstanceVariable( containerId, processInstanceId, varName, type); logger.debug("Returning OK response with content '{}'", response); return createResponse(response, v, Response.Status.OK, conversationIdHeader); } catch (ProcessInstanceNotFoundException e) { return notFound( MessageFormat.format(PROCESS_INSTANCE_NOT_FOUND, processInstanceId), v, conversationIdHeader); } catch (DeploymentNotFoundException e) { return notFound( MessageFormat.format(CONTAINER_NOT_FOUND, containerId), v, conversationIdHeader); } catch (Exception e) { logger.error("Unexpected error during processing {}", e.getMessage(), e); return internalServerError( MessageFormat.format(UNEXPECTED_ERROR, e.getMessage()), v, conversationIdHeader); } } @GET @Path(PROCESS_INSTANCE_VARS_GET_URI) @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public Response getProcessInstanceVariables( @javax.ws.rs.core.Context HttpHeaders headers, @PathParam("id") String containerId, @PathParam("pInstanceId") Long processInstanceId) { Variant v = getVariant(headers); String type = getContentType(headers); Header conversationIdHeader = buildConversationIdHeader(containerId, context, headers); try { String response = processServiceBase.getProcessInstanceVariables(containerId, processInstanceId, type); logger.debug("Returning OK response with content '{}'", response); return createResponse(response, v, Response.Status.OK, conversationIdHeader); } catch (ProcessInstanceNotFoundException e) { return notFound( MessageFormat.format(PROCESS_INSTANCE_NOT_FOUND, processInstanceId), v, conversationIdHeader); } catch (DeploymentNotFoundException e) { return notFound( MessageFormat.format(CONTAINER_NOT_FOUND, containerId), v, conversationIdHeader); } catch (Exception e) { logger.error("Unexpected error during processing {}", e.getMessage(), e); return internalServerError( MessageFormat.format(UNEXPECTED_ERROR, e.getMessage()), v, conversationIdHeader); } } @GET @Path(PROCESS_INSTANCE_SIGNALS_GET_URI) @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public Response getAvailableSignals( @javax.ws.rs.core.Context HttpHeaders headers, @PathParam("id") String containerId, @PathParam("pInstanceId") Long processInstanceId) { Variant v = getVariant(headers); String type = getContentType(headers); Header conversationIdHeader = buildConversationIdHeader(containerId, context, headers); try { String response = processServiceBase.getAvailableSignals(containerId, processInstanceId, type); logger.debug("Returning OK response with content '{}'", response); return createResponse(response, v, Response.Status.OK, conversationIdHeader); } catch (ProcessInstanceNotFoundException e) { return notFound( MessageFormat.format(PROCESS_INSTANCE_NOT_FOUND, processInstanceId), v, conversationIdHeader); } catch (DeploymentNotFoundException e) { return notFound( MessageFormat.format(CONTAINER_NOT_FOUND, containerId), v, conversationIdHeader); } catch (Exception e) { logger.error("Unexpected error during processing {}", e.getMessage(), e); return internalServerError( MessageFormat.format(UNEXPECTED_ERROR, e.getMessage()), v, conversationIdHeader); } } @PUT @Path(PROCESS_INSTANCE_WORK_ITEM_COMPLETE_PUT_URI) @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public Response completeWorkItem( @javax.ws.rs.core.Context HttpHeaders headers, @PathParam("id") String containerId, @PathParam("pInstanceId") Long processInstanceId, @PathParam("workItemId") Long workItemId, String resultPayload) { Variant v = getVariant(headers); String type = getContentType(headers); Header conversationIdHeader = buildConversationIdHeader(containerId, context, headers); try { processServiceBase.completeWorkItem( containerId, processInstanceId, workItemId, resultPayload, type); return createResponse("", v, Response.Status.CREATED, conversationIdHeader); } catch (ProcessInstanceNotFoundException e) { return notFound( MessageFormat.format(PROCESS_INSTANCE_NOT_FOUND, processInstanceId), v, conversationIdHeader); } catch (DeploymentNotFoundException e) { return notFound( MessageFormat.format(CONTAINER_NOT_FOUND, containerId), v, conversationIdHeader); } catch (Exception e) { logger.error("Unexpected error during processing {}", e.getMessage(), e); return internalServerError( MessageFormat.format(UNEXPECTED_ERROR, e.getMessage()), v, conversationIdHeader); } } @PUT @Path(PROCESS_INSTANCE_WORK_ITEM_ABORT_PUT_URI) @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public Response abortWorkItem( @javax.ws.rs.core.Context HttpHeaders headers, @PathParam("id") String containerId, @PathParam("pInstanceId") Long processInstanceId, @PathParam("workItemId") Long workItemId) { Variant v = getVariant(headers); Header conversationIdHeader = buildConversationIdHeader(containerId, context, headers); try { processServiceBase.abortWorkItem(containerId, processInstanceId, workItemId); return createResponse("", v, Response.Status.CREATED, conversationIdHeader); } catch (ProcessInstanceNotFoundException e) { return notFound( MessageFormat.format(PROCESS_INSTANCE_NOT_FOUND, processInstanceId), v, conversationIdHeader); } catch (DeploymentNotFoundException e) { return notFound( MessageFormat.format(CONTAINER_NOT_FOUND, containerId), v, conversationIdHeader); } catch (Exception e) { logger.error("Unexpected error during processing {}", e.getMessage(), e); return internalServerError( MessageFormat.format(UNEXPECTED_ERROR, e.getMessage()), v, conversationIdHeader); } } @GET @Path(PROCESS_INSTANCE_WORK_ITEM_BY_ID_GET_URI) @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public Response getWorkItem( @javax.ws.rs.core.Context HttpHeaders headers, @PathParam("id") String containerId, @PathParam("pInstanceId") Long processInstanceId, @PathParam("workItemId") Long workItemId) { Variant v = getVariant(headers); String type = getContentType(headers); Header conversationIdHeader = buildConversationIdHeader(containerId, context, headers); try { String response = processServiceBase.getWorkItem(containerId, processInstanceId, workItemId, type); logger.debug("Returning OK response with content '{}'", response); return createResponse(response, v, Response.Status.OK, conversationIdHeader); } catch (ProcessInstanceNotFoundException e) { return notFound( MessageFormat.format(PROCESS_INSTANCE_NOT_FOUND, processInstanceId), v, conversationIdHeader); } catch (DeploymentNotFoundException e) { return notFound( MessageFormat.format(CONTAINER_NOT_FOUND, containerId), v, conversationIdHeader); } catch (WorkItemNotFoundException e) { return notFound( MessageFormat.format(WORK_ITEM_NOT_FOUND, workItemId), v, conversationIdHeader); } catch (Exception e) { logger.error("Unexpected error during processing {}", e.getMessage(), e); return internalServerError( MessageFormat.format(UNEXPECTED_ERROR, e.getMessage()), v, conversationIdHeader); } } @GET @Path(PROCESS_INSTANCE_WORK_ITEMS_BY_PROC_INST_ID_GET_URI) @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public Response getWorkItemByProcessInstance( @javax.ws.rs.core.Context HttpHeaders headers, @PathParam("id") String containerId, @PathParam("pInstanceId") Long processInstanceId) { Variant v = getVariant(headers); String type = getContentType(headers); Header conversationIdHeader = buildConversationIdHeader(containerId, context, headers); try { String response = processServiceBase.getWorkItemByProcessInstance(containerId, processInstanceId, type); logger.debug("Returning OK response with content '{}'", response); return createResponse(response, v, Response.Status.OK, conversationIdHeader); } catch (ProcessInstanceNotFoundException e) { return notFound( MessageFormat.format(PROCESS_INSTANCE_NOT_FOUND, processInstanceId), v, conversationIdHeader); } catch (DeploymentNotFoundException e) { return notFound( MessageFormat.format(CONTAINER_NOT_FOUND, containerId), v, conversationIdHeader); } catch (Exception e) { logger.error("Unexpected error during processing {}", e.getMessage(), e); return internalServerError( MessageFormat.format(UNEXPECTED_ERROR, e.getMessage()), v, conversationIdHeader); } } }