Home
Cloud
Big Data
CI
Install
Samples
Java
Ubuntu
Maven
Archive
mtitek-process-core-ws-default
|
RestProcessImpl.java
RestProcessImpl.java
RestProcessImpl.java
package mtitek.process.ws; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import javax.ws.rs.Consumes; import javax.ws.rs.HttpMethod; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpStatus; import org.kie.api.runtime.process.ProcessInstance; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; 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; import mtitek.process.runtime.ProcessRuntime; import mtitek.process.runtime.exception.ProcessRuntimeException; import mtitek.process.runtime.utils.ProcessRuntimeConstants; @Path("mtitek/bpm/v1") @Api(value = "MTITEK BPM - Rest API") @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) @Component public class RestProcessImpl { private static final Logger LOG = LoggerFactory.getLogger(RestProcessImpl.class); @Autowired private ProcessRuntime processRuntime; public ProcessRuntime getProcessRuntime() { return processRuntime; } public void setProcessRuntime(final ProcessRuntime processRuntime) { this.processRuntime = processRuntime; } @PUT @Path("/startProcess") @ApiOperation(httpMethod = HttpMethod.PUT, value = "startProcess") @ApiResponses({ @ApiResponse(code = HttpStatus.SC_OK, message = "Http status 200 OK", response = RestProcessResponse.class) }) public Response startProcess(@ApiParam(name = "body", value = "payload") final Payload payload) { RestProcessImpl.LOG.trace("startProcess: payload={}", payload); final String payloadId = payload == null ? null : payload.getId(); final Map
workItemParameters = getParameters(payload); RestProcessImpl.LOG.trace("startProcess: payloadId='{}', workItemParameters={}", payloadId, workItemParameters); Long id = null; Integer state = null; String httpStatus = ""; String httpError = ""; try { final ProcessInstance processInstance; if (payloadId == null) { processInstance = processRuntime.startProcess(null, workItemParameters); } else { processInstance = processRuntime.startProcess(payloadId, null, workItemParameters); } id = processInstance == null ? null : processInstance.getId(); state = processInstance == null ? null : processInstance.getState(); httpStatus = "200 OK"; } catch (final ProcessRuntimeException e) { RestProcessImpl.LOG.error(e.getMessage(), e); httpError = e.getMessage(); } return getResponse(id, state, httpStatus, httpError); } @PUT @Path("/resumeProcessByProcessInstanceId") @ApiOperation(httpMethod = HttpMethod.PUT, value = "resumeProcessByProcessInstanceId") @ApiResponses({ @ApiResponse(code = HttpStatus.SC_OK, message = "Http status 200 OK", response = RestProcessResponse.class) }) public Response resumeProcess( @ApiParam(value = "processInstanceId", required = true) @QueryParam(value = "processInstanceId") final Long processInstanceId, @ApiParam(name = "body", value = "payload") final Payload payload) { RestProcessImpl.LOG.trace("resumeProcessByProcessInstanceId: processInstanceId='{}', payload={}", processInstanceId, payload); final Map
workItemParameters = getParameters(payload); RestProcessImpl.LOG.trace("resumeProcessByProcessInstanceId: workItemParameters={}", workItemParameters); Long id = null; Integer state = null; String httpStatus = ""; String httpError = ""; try { final ProcessInstance processInstance = processRuntime.resumeProcess(processInstanceId, workItemParameters); id = processInstance == null ? null : processInstance.getId(); state = processInstance == null ? null : processInstance.getState(); httpStatus = "200 OK"; } catch (final ProcessRuntimeException e) { RestProcessImpl.LOG.error(e.getMessage(), e); httpError = e.getMessage(); } return getResponse(id, state, httpStatus, httpError); } @PUT @Path("/resumeProcessByPayloadId") @ApiOperation(httpMethod = HttpMethod.PUT, value = "resumeProcessByPayloadId") @ApiResponses({ @ApiResponse(code = HttpStatus.SC_OK, message = "Http status 200 OK", response = RestProcessResponse.class) }) public Response resumeProcess(@ApiParam(name = "body", value = "payload") final Payload payload) { RestProcessImpl.LOG.trace("resumeProcessByPayloadId: payload={}", payload); final String payloadId = payload == null ? null : payload.getId(); final Map
workItemParameters = getParameters(payload); RestProcessImpl.LOG.trace("resumeProcessByPayloadId: payloadId='{}', workItemParameters={}", payloadId, workItemParameters); Long id = null; Integer state = null; String httpStatus = ""; String httpError = ""; try { final ProcessInstance processInstance = processRuntime.resumeProcess(payloadId, workItemParameters); id = processInstance == null ? null : processInstance.getId(); state = processInstance == null ? null : processInstance.getState(); httpStatus = "200 OK"; } catch (final ProcessRuntimeException e) { RestProcessImpl.LOG.error(e.getMessage(), e); httpError = e.getMessage(); } return getResponse(id, state, httpStatus, httpError); } @PUT @Path("/cancelProcessByProcessInstanceId") @ApiOperation(httpMethod = HttpMethod.PUT, value = "cancelProcessByProcessInstanceId") @ApiResponses({ @ApiResponse(code = HttpStatus.SC_OK, message = "Http status 200 OK", response = RestProcessResponse.class) }) public Response cancelProcess( @ApiParam(value = "processInstanceId", required = true) @QueryParam(value = "processInstanceId") final Long processInstanceId) { RestProcessImpl.LOG.trace("cancelProcessByProcessInstanceId: processInstanceId='{}'", processInstanceId); Long id = null; Integer state = null; String httpStatus = ""; String httpError = ""; try { final ProcessInstance processInstance = processRuntime.cancelProcess(processInstanceId); id = processInstance == null ? null : processInstance.getId(); state = processInstance == null ? null : processInstance.getState(); httpStatus = "200 OK"; } catch (final ProcessRuntimeException e) { RestProcessImpl.LOG.error(e.getMessage(), e); httpError = e.getMessage(); } return getResponse(id, state, httpStatus, httpError); } @PUT @Path("/cancelProcessByPayloadId") @ApiOperation(httpMethod = HttpMethod.PUT, value = "cancelProcessByPayloadId") @ApiResponses({ @ApiResponse(code = HttpStatus.SC_OK, message = "Http status 200 OK", response = RestProcessResponse.class) }) public Response cancelProcess( @ApiParam(value = "payloadId", required = true) @QueryParam(value = "payloadId") final String payloadId) { RestProcessImpl.LOG.trace("cancelProcessByPayloadId: payloadId='{}'", payloadId); Long id = null; Integer state = null; String httpStatus = ""; String httpError = ""; try { final ProcessInstance processInstance = processRuntime.cancelProcess(payloadId); id = processInstance == null ? null : processInstance.getId(); state = processInstance == null ? null : processInstance.getState(); httpStatus = "200 OK"; } catch (final ProcessRuntimeException e) { RestProcessImpl.LOG.error(e.getMessage(), e); httpError = e.getMessage(); } return getResponse(id, state, httpStatus, httpError); } private Response getResponse(final Long processInstanceId, final Integer processInstanceState, String httpStatus, final String httpError) { RestProcessResponse restProcessResponse = new RestProcessResponse(); restProcessResponse.setProcessInstanceId(processInstanceId); if (processInstanceState != null) { switch (processInstanceState) { case ProcessInstance.STATE_PENDING: restProcessResponse.setProcessInstanceState(ProcessRuntimeConstants.PROCESS_RUNTIME_STATE_PENDING); break; case ProcessInstance.STATE_ACTIVE: restProcessResponse.setProcessInstanceState(ProcessRuntimeConstants.PROCESS_RUNTIME_STATE_ACTIVE); break; case ProcessInstance.STATE_COMPLETED: restProcessResponse.setProcessInstanceState(ProcessRuntimeConstants.PROCESS_RUNTIME_STATE_COMPLETED); break; case ProcessInstance.STATE_ABORTED: restProcessResponse.setProcessInstanceState(ProcessRuntimeConstants.PROCESS_RUNTIME_STATE_ABORTED); break; case ProcessInstance.STATE_SUSPENDED: restProcessResponse.setProcessInstanceState(ProcessRuntimeConstants.PROCESS_RUNTIME_STATE_SUSPENDED); break; } } restProcessResponse.setHttpStatus(httpStatus); restProcessResponse.setHttpError(httpError); return Response.ok(restProcessResponse).build(); } private Map
getParameters(final Payload payload) { final Map
parameters = new ConcurrentHashMap<>(); if (payload != null) { final List
payloadParameters = payload.getParameters(); if (payloadParameters != null && !payloadParameters.isEmpty()) { for (PayloadParameter payloadParameter : payloadParameters) { final String name = payloadParameter.getName(); final String value = payloadParameter.getValue(); if (StringUtils.isNotBlank(name) && StringUtils.isNotBlank(value)) { parameters.put(name, value); } } } } return parameters; } }
© 2010-2022
mti
tek