blob: 0fecb2fde77e487bb31c4c8cd98337ad60550eeb [file] [log] [blame]
/**
*
* Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
*
*/
package org.eclipse.osbp.bpm.api;
import java.util.HashSet;
import org.drools.runtime.process.ProcessContext;
import org.drools.runtime.process.ProcessInstance;
import org.jbpm.ruleflow.instance.RuleFlowProcessInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class BlipBPMProcessProtocolCache extends HashSet<IBlipBPMProcessProtocol> implements IBlipBPMProcessProtocolCache {
private static final long serialVersionUID = 73752867741992785L;
private final static Logger log = LoggerFactory.getLogger(BlipBPMProcessProtocolCache.class);
protected long getProcessId(Object context) {
Exception e = null;
try {
if (context instanceof Long) {
return (Long)context;
}
if (context instanceof Integer) {
return (Long)context;
}
if (context instanceof ProcessInstance) {
return ((ProcessInstance)context).getId();
}
if (context instanceof ProcessContext) {
return (((ProcessContext) context).getNodeInstance().getProcessInstance().getId());
}
}
catch (Exception e1) {
e = e1;
}
String error = "Given context "+context.getClass().getCanonicalName()+" can't be inspected for the process instance id";
if (e != null) {
error += " "+e.getLocalizedMessage();
}
log.error(error);
throw new IllegalArgumentException(error);
}
protected long getParentProcessId(Object context) {
Exception e = null;
try {
if (context instanceof ProcessContext) {
if (((ProcessContext) context).getNodeInstance().getNodeInstanceContainer() instanceof RuleFlowProcessInstance) {
RuleFlowProcessInstance ruleFlowProcessInstance = (RuleFlowProcessInstance) ((ProcessContext) context).getNodeInstance().getNodeInstanceContainer();
if (ruleFlowProcessInstance.getMetaData().containsKey("ParentProcessInstanceId")) {
return (Long)ruleFlowProcessInstance.getMetaData().get("ParentProcessInstanceId");
}
}
}
}
catch (Exception e1) {
}
return 0;
}
@Override
public IBlipBPMProcessProtocol mergeToProtocol(Object context, IBlipBPMProcessProtocol toBeMerged) {
return addToProtocol(context, null, toBeMerged);
}
@Override
public IBlipBPMProcessProtocol addToProtocol(Object context, String line) {
return addToProtocol(context, line, null);
}
@Override
public IBlipBPMProcessProtocol addToProtocol(Object context, String line, IBlipBPMProcessProtocol toBeMerged) {
IBlipBPMProcessProtocol protocol = getProtocol(context);
protocol.addToProtocol(getProcessId(context), line, toBeMerged);
return protocol;
}
@Override
public IBlipBPMProcessProtocol getProtocol(Object context) {
long processId = getProcessId(context);
long parentProcessId = getParentProcessId(context);
IBlipBPMProcessProtocol protocol = get(processId);
if ((protocol == null) && (parentProcessId > 0)) {
protocol = get(parentProcessId);
if (protocol != null) {
protocol.updateProcessId(processId);
}
}
if (protocol == null) {
protocol = new BlipBPMProcessProtocol(processId);
add(protocol);
}
// else if (protocol.getProcessId() == 0) {
// protocol.updateProcessId(processId);
// }
else if (!protocol.containsProcessId(processId)) {
String error = "Wrong process id for context "+processId+" and protocol found "+protocol.processIdsAsString();
log.error(error);
throw new IllegalArgumentException(error);
}
return protocol;
}
private IBlipBPMProcessProtocol get(long searchProcessId) {
for (IBlipBPMProcessProtocol protocol : this) {
if (protocol.containsProcessId(searchProcessId)) {
return protocol;
}
}
return null;
}
@Override
public void removeFromCache(Object context) {
remove(getProcessId(context));
}
}