if (primary == null && bGlobal)
{
return null;
}
InstanceList list = (primary == null) ? null :
(bGlobal) ? forInstance(metaclass, primary, actx) : getCachedFlowList(metaclass, primary);
if (list == null && primary != null)
{
list = new InstanceArrayList(4);
list.setLazy(false);
}
else if (bGlobal)
{
for (int i = 0, n = list.getCount(); i != n; ++i)
{
if (list.getInstance(i).getValue("name").equals(sName))
{
return null;
}
}
}
Flow flow = getFlow(sName);
if (primary != null)
{
if (flow.getMetaclass() == null || !flow.getMetaclass().isUpcast(primary.getMetaclass()))
{
throw new WorkflowException("err.workflow.invalidClass",
new Object[]{flow.getName(), primary.getMetaclass().getName()});
}
if (primary.getMetaclass().getPersistenceMapping() == null)
{
throw new MetadataException("err.meta.workflow.nonPersistentClass",
new Object[]{primary.getMetaclass().getName(), flow.getFullName()});
}
}
if (s_logger.isDebugEnabled())
{
s_logger.debug("Starting " + flow + " for " + primary);
}
boolean bSecure = m_context.isSecure();
Instance instance = null;
try
{
m_context.setSecure(false);
instance = new Instance(metaclass, m_context);
if (list != null)
{
list.add(instance);
if (m_key == null)
{
m_key = new FlowKey();
}
m_key.instance = primary;
m_context.getUnitOfWork().cacheTransient(m_key, list);
m_key = null;
}
State state = new State(flow, true);
state.setReservedValue(0, instance);
state.setReservedValue(1, state);
instance.setNew();
instance.setValue("name", flow.getName());
instance.setValue("version", Primitive.createInteger(flow.getVersion()));
instance.setValue("oid", EMPTY_BINARY);
instance.setValue("class", (primary == null) ? "" : primary.getMetaclass().getName());
instance.setValue("local", (bGlobal) ? EMPTY_BINARY : GUIDUtil.generateGUID());
instance.setValue("object", primary);
instance.setValue("state", state);
instance.setValue("serializedState", null);
instance.setValue("serializedVariables", null);
instance.invoke("create");
m_context.setSecure(bSecure);
instance.invoke("run");
}
catch (Throwable e)
{
if (instance != null)
{
m_context.setSecure(false);
instance.invoke("delete");
if (list != null)
{
list.remove(instance);
}
}
ObjUtil.rethrow(e);
}