*/
@Override
public void handleOperation(Exchange exchange, KnowledgeOperation operation) throws HandlerException {
Integer sessionId = null;
Long processInstanceId = null;
Message inputMessage = exchange.getMessage();
ExchangePattern exchangePattern = exchange.getContract().getProviderOperation().getExchangePattern();
Map<String, Object> expressionVariables = new HashMap<String, Object>();
AS7TransactionHelper utx = new AS7TransactionHelper(_persistent);
BPMOperationType operationType = (BPMOperationType)operation.getType();
switch (operationType) {
case START_PROCESS: {
try {
utx.begin();
KnowledgeSession session = getBPMSession(exchange, inputMessage);
sessionId = session.getId();
setGlobals(inputMessage, operation, session);
Map<String, Object> inputMap = getInputMap(inputMessage, operation, session);
ProcessInstance processInstance;
CorrelationKey correlationKey = getCorrelationKey(exchange, inputMessage);
if (correlationKey != null) {
processInstance = ((CorrelationAwareProcessRuntime)session.getStateful()).startProcess(_processId, correlationKey, inputMap);
} else {
processInstance = session.getStateful().startProcess(_processId, inputMap);
}
processInstanceId = Long.valueOf(processInstance.getId());
if (ExchangePattern.IN_OUT.equals(exchangePattern)) {
expressionVariables.putAll(getGlobalVariables(session));
expressionVariables.putAll(getProcessInstanceVariables(processInstance));
}
utx.commit();
} catch (RuntimeException re) {
utx.rollback();
throw re;
}
break;
}
case SIGNAL_EVENT:
case SIGNAL_EVENT_ALL: {
try {
utx.begin();
KnowledgeSession session = getBPMSession(exchange, inputMessage);
sessionId = session.getId();
setGlobals(inputMessage, operation, session);
Object eventObject = getInput(inputMessage, operation, session);
String eventId = operation.getEventId();
if (BPMOperationType.SIGNAL_EVENT.equals(operationType)) {
processInstanceId = getProcessInstanceId(exchange, inputMessage, session);
if (processInstanceId == null) {
throw BPMMessages.MESSAGES.cannotSignalEventUnknownProcessInstanceIdOrUnknownunmatchedCorrelationKey();
}
if (ExchangePattern.IN_OUT.equals(exchangePattern)) {
ProcessInstance processInstance = session.getStateful().getProcessInstance(processInstanceId);
processInstance.signalEvent(eventId, eventObject);
expressionVariables.putAll(getGlobalVariables(session));
expressionVariables.putAll(getProcessInstanceVariables(processInstance));
} else {
session.getStateful().signalEvent(eventId, eventObject, processInstanceId);
}
} else if (BPMOperationType.SIGNAL_EVENT_ALL.equals(operationType)) {
session.getStateful().signalEvent(eventId, eventObject);
if (ExchangePattern.IN_OUT.equals(exchangePattern)) {
expressionVariables.putAll(getGlobalVariables(session));
}
}
utx.commit();
} catch (RuntimeException re) {
utx.rollback();
throw re;
}
break;
}
case ABORT_PROCESS_INSTANCE: {
try {
utx.begin();
KnowledgeSession session = getBPMSession(exchange, inputMessage);
sessionId = session.getId();
processInstanceId = getProcessInstanceId(exchange, inputMessage, session);
if (processInstanceId == null) {
throw BPMMessages.MESSAGES.cannotAbortProcessInstance();
}
if (ExchangePattern.IN_OUT.equals(exchangePattern)) {
expressionVariables.putAll(getGlobalVariables(session));
ProcessInstance processInstance = session.getStateful().getProcessInstance(processInstanceId);
expressionVariables.putAll(getProcessInstanceVariables(processInstance));
}
session.getStateful().abortProcessInstance(processInstanceId);
utx.commit();
} catch (RuntimeException re) {
utx.rollback();
throw re;
}
break;
}
default: {
throw BPMMessages.MESSAGES.unsupportedOperationType(operationType);
}
}
if (ExchangePattern.IN_OUT.equals(exchangePattern)) {
Message outputMessage = exchange.createMessage();
Context outputContext = exchange.getContext(outputMessage);
if (sessionId != null && sessionId.intValue() > 0) {
outputContext.setProperty(BPMConstants.SESSION_ID_PROPERTY, sessionId);
}
if (processInstanceId != null && processInstanceId.longValue() > 0) {
outputContext.setProperty(BPMConstants.PROCESSS_INSTANCE_ID_PROPERTY, processInstanceId);
}
setFaults(outputMessage, operation, expressionVariables);
if (outputMessage.getContent() != null) {
exchange.sendFault(outputMessage);
} else {
setOutputs(outputMessage, operation, expressionVariables);
exchange.send(outputMessage);
}