use of io.automatiko.engine.addons.persistence.db.model.ProcessInstanceEntity in project automatiko-engine by automatiko-io.
the class DatabaseProcessInstances method disconnect.
protected void disconnect(ProcessInstance<ProcessInstanceEntity> instance) {
((AbstractProcessInstance<?>) instance).internalRemoveProcessInstance(() -> {
try {
ProcessInstanceEntity entity = (ProcessInstanceEntity) JpaOperations.INSTANCE.findById(type, resolveId(instance.id(), instance));
byte[] reloaded = codec.decode(entity.content);
WorkflowProcessInstance wpi = marshaller.unmarshallWorkflowProcessInstance(reloaded, process);
entity.toMap().forEach((k, v) -> {
if (v != null) {
v.toString();
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((ProcessInstanceImpl) wpi).getContextInstance(VariableScope.VARIABLE_SCOPE);
variableScopeInstance.internalSetVariable(k, v);
}
});
return wpi;
} catch (RuntimeException e) {
LOGGER.error("Unexpected exception thrown when reloading process instance {}", instance.id(), e);
return null;
}
});
}
use of io.automatiko.engine.addons.persistence.db.model.ProcessInstanceEntity in project automatiko-engine by automatiko-io.
the class DatabaseProcessInstances method findById.
@SuppressWarnings("unchecked")
@Override
public Optional<ProcessInstance<ProcessInstanceEntity>> findById(String id, int status, ProcessInstanceReadMode mode) {
String resolvedId = resolveId(id);
Optional<ProcessInstanceEntity> found = (Optional<ProcessInstanceEntity>) JpaOperations.INSTANCE.findByIdOptional(type, resolvedId);
if (found.isEmpty()) {
return Optional.empty();
}
ProcessInstanceEntity entity = found.get();
if (entity.state == status) {
return Optional.of(unmarshallInstance(mode, entity));
} else {
return Optional.empty();
}
}
use of io.automatiko.engine.addons.persistence.db.model.ProcessInstanceEntity in project automatiko-engine by automatiko-io.
the class DatabaseProcessInstances method store.
protected void store(String id, ProcessInstance<ProcessInstanceEntity> instance) {
String resolvedId = resolveId(id, instance);
if (isActive(instance)) {
ProcessInstanceEntity entity = instance.variables();
byte[] data = codec.encode(marshaller.marhsallProcessInstance(instance));
if (data == null) {
return;
}
entity.content = data;
entity.entityId = resolvedId;
entity.name = instance.description();
entity.businessKey = instance.businessKey();
entity.processId = instance.process().id();
entity.processName = instance.process().name();
entity.processVersion = instance.process().version();
entity.startDate = instance.startDate();
entity.state = instance.status();
entity.tags = new HashSet<>(instance.tags().values());
try {
JpaOperations.INSTANCE.persist(entity);
} catch (OptimisticLockException | StaleObjectStateException e) {
throw new ConflictingVersionException("Process instance with id '" + instance.id() + "' has older version than tha stored one");
} finally {
disconnect(instance);
}
}
}
use of io.automatiko.engine.addons.persistence.db.model.ProcessInstanceEntity in project automatiko-engine by automatiko-io.
the class DatabaseProcessInstances method unmarshallInstance.
@SuppressWarnings("unchecked")
protected ProcessInstance<ProcessInstanceEntity> unmarshallInstance(ProcessInstanceReadMode mode, ProcessInstanceEntity entity) {
ProcessInstance<ProcessInstanceEntity> pi;
if (mode == MUTABLE) {
WorkflowProcessInstance wpi = marshaller.unmarshallWorkflowProcessInstance(codec.decode(entity.content), process);
entity.toMap().forEach((k, v) -> {
if (v != null) {
v.toString();
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((ProcessInstanceImpl) wpi).getContextInstance(VariableScope.VARIABLE_SCOPE);
variableScopeInstance.internalSetVariable(k, v);
}
});
pi = ((AbstractProcess<ProcessInstanceEntity>) process).createInstance(wpi, entity, entity.version);
} else {
WorkflowProcessInstance wpi = marshaller.unmarshallWorkflowProcessInstance(codec.decode(entity.content), process);
entity.toMap().forEach((k, v) -> {
if (v != null) {
v.toString();
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((ProcessInstanceImpl) wpi).getContextInstance(VariableScope.VARIABLE_SCOPE);
variableScopeInstance.internalSetVariable(k, v);
}
});
pi = ((AbstractProcess<ProcessInstanceEntity>) process).createReadOnlyInstance(wpi, entity);
}
return pi;
}
use of io.automatiko.engine.addons.persistence.db.model.ProcessInstanceEntity in project automatiko-engine by automatiko-io.
the class DatabaseProcessInstances method remove.
@Override
public void remove(String id, ProcessInstance<ProcessInstanceEntity> instance) {
ProcessInstanceEntity entity = instance.variables();
// run persist to make sure entities of the root are stored
JpaOperations.INSTANCE.persist(entity);
// then delete the root one
JpaOperations.INSTANCE.deleteById(type, resolveId(id, instance));
}
Aggregations