Search in sources :

Example 1 with ProcessInstanceEntity

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;
        }
    });
}
Also used : AbstractProcessInstance(io.automatiko.engine.workflow.AbstractProcessInstance) ProcessInstanceEntity(io.automatiko.engine.addons.persistence.db.model.ProcessInstanceEntity) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) WorkflowProcessInstance(io.automatiko.engine.api.runtime.process.WorkflowProcessInstance)

Example 2 with ProcessInstanceEntity

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();
    }
}
Also used : ProcessInstanceEntity(io.automatiko.engine.addons.persistence.db.model.ProcessInstanceEntity) Optional(java.util.Optional)

Example 3 with ProcessInstanceEntity

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);
        }
    }
}
Also used : ProcessInstanceEntity(io.automatiko.engine.addons.persistence.db.model.ProcessInstanceEntity) ConflictingVersionException(io.automatiko.engine.api.workflow.ConflictingVersionException) OptimisticLockException(javax.persistence.OptimisticLockException) StaleObjectStateException(org.hibernate.StaleObjectStateException)

Example 4 with ProcessInstanceEntity

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;
}
Also used : ProcessInstanceEntity(io.automatiko.engine.addons.persistence.db.model.ProcessInstanceEntity) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) WorkflowProcessInstance(io.automatiko.engine.api.runtime.process.WorkflowProcessInstance)

Example 5 with ProcessInstanceEntity

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));
}
Also used : ProcessInstanceEntity(io.automatiko.engine.addons.persistence.db.model.ProcessInstanceEntity)

Aggregations

ProcessInstanceEntity (io.automatiko.engine.addons.persistence.db.model.ProcessInstanceEntity)5 WorkflowProcessInstance (io.automatiko.engine.api.runtime.process.WorkflowProcessInstance)2 VariableScopeInstance (io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance)2 ConflictingVersionException (io.automatiko.engine.api.workflow.ConflictingVersionException)1 AbstractProcessInstance (io.automatiko.engine.workflow.AbstractProcessInstance)1 Optional (java.util.Optional)1 OptimisticLockException (javax.persistence.OptimisticLockException)1 StaleObjectStateException (org.hibernate.StaleObjectStateException)1