/** * Load a workflow definition by ID. * * Providing the name of the workflow that is to be loaded as the * optional second parameter saves a database query. * * @param int $workflowId * @param string $workflowName * @param int $workflowVersion * @return ezcWorkflow * @throws ezcWorkflowDefinitionStorageException * @throws ezcDbException */ public function loadById($workflowId, $workflowName = '', $workflowVersion = 0) { // Query the database for the name and version of the workflow. if (empty($workflowName) || $workflowVersion == 0) { $query = $this->db->createSelectQuery(); $query->select($this->db->quoteIdentifier('workflow_name'))->select($this->db->quoteIdentifier('workflow_version'))->from($this->db->quoteIdentifier($this->options['prefix'] . 'workflow'))->where($query->expr->eq($this->db->quoteIdentifier('workflow_id'), $query->bindValue((int) $workflowId))); $stmt = $query->prepare(); $stmt->execute(); $result = $stmt->fetchAll(PDO::FETCH_ASSOC); if ($result !== false && isset($result[0])) { $workflowName = $result[0]['workflow_name']; $workflowVersion = $result[0]['workflow_version']; } else { throw new ezcWorkflowDefinitionStorageException('Could not load workflow definition.'); } } // Query the database for the nodes of the workflow to be loaded. $query = $this->db->createSelectQuery(); $query->select($this->db->quoteIdentifier('node_id'))->select($this->db->quoteIdentifier('node_class'))->select($this->db->quoteIdentifier('node_configuration'))->from($this->db->quoteIdentifier($this->options['prefix'] . 'node'))->where($query->expr->eq($this->db->quoteIdentifier('workflow_id'), $query->bindValue((int) $workflowId))); $stmt = $query->prepare(); $stmt->execute(); $result = $stmt->fetchAll(PDO::FETCH_ASSOC); $nodes = array(); // Create node objects. foreach ($result as $node) { $configuration = ezcWorkflowDatabaseUtil::unserialize($node['node_configuration'], null); if (is_null($configuration)) { $configuration = ezcWorkflowUtil::getDefaultConfiguration($node['node_class']); } $nodes[$node['node_id']] = new $node['node_class']($configuration); if ($nodes[$node['node_id']] instanceof ezcWorkflowNodeFinally && !isset($finallyNode)) { $finallyNode = $nodes[$node['node_id']]; } else { if ($nodes[$node['node_id']] instanceof ezcWorkflowNodeEnd && !isset($defaultEndNode)) { $defaultEndNode = $nodes[$node['node_id']]; } else { if ($nodes[$node['node_id']] instanceof ezcWorkflowNodeStart && !isset($startNode)) { $startNode = $nodes[$node['node_id']]; } } } } if (!isset($startNode) || !isset($defaultEndNode)) { throw new ezcWorkflowDefinitionStorageException('Could not load workflow definition.'); } // Connect node objects. $query = $this->db->createSelectQuery(); $query->select($query->alias('node_connection.incoming_node_id', $this->db->quoteIdentifier('incoming_node_id')))->select($query->alias('node_connection.outgoing_node_id', $this->db->quoteIdentifier('outgoing_node_id')))->from($query->innerJoin($this->db->quoteIdentifier($this->options['prefix'] . 'node_connection'), $this->db->quoteIdentifier($this->options['prefix'] . 'node'), 'node_connection.incoming_node_id', 'node.node_id'))->where($query->expr->eq('node.workflow_id', $query->bindValue((int) $workflowId)))->orderBy($this->db->quoteIdentifier('node_connection_id')); $stmt = $query->prepare(); $stmt->execute(); $connections = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach ($connections as $connection) { $nodes[$connection['incoming_node_id']]->addOutNode($nodes[$connection['outgoing_node_id']]); } if (!isset($finallyNode) || count($finallyNode->getInNodes()) > 0) { $finallyNode = null; } // Create workflow object and add the node objects to it. $workflow = new ezcWorkflow($workflowName, $startNode, $defaultEndNode, $finallyNode); $workflow->definitionStorage = $this; $workflow->id = (int) $workflowId; $workflow->version = (int) $workflowVersion; // Query the database for the variable handlers. $query = $this->db->createSelectQuery(); $query->select($this->db->quoteIdentifier('variable'))->select($this->db->quoteIdentifier('class'))->from($this->db->quoteIdentifier($this->options['prefix'] . 'variable_handler'))->where($query->expr->eq($this->db->quoteIdentifier('workflow_id'), $query->bindValue((int) $workflowId))); $stmt = $query->prepare(); $stmt->execute(); $result = $stmt->fetchAll(PDO::FETCH_ASSOC); $nodes = array(); if ($result !== false) { foreach ($result as $variableHandler) { $workflow->addVariableHandler($variableHandler['variable'], $variableHandler['class']); } } // Verify the loaded workflow. $workflow->verify(); return $workflow; }
/** * Load execution state. * * @param int $executionId ID of the execution to load. * @throws ezcWorkflowExecutionException */ protected function loadExecution($executionId) { $query = $this->db->createSelectQuery(); $query->select($this->db->quoteIdentifier('workflow_id'))->select($this->db->quoteIdentifier('execution_variables'))->select($this->db->quoteIdentifier('execution_threads'))->select($this->db->quoteIdentifier('execution_next_thread_id'))->select($this->db->quoteIdentifier('execution_waiting_for'))->from($this->db->quoteIdentifier($this->options['prefix'] . 'execution'))->where($query->expr->eq($this->db->quoteIdentifier('execution_id'), $query->bindValue((int) $executionId))); $stmt = $query->prepare(); $stmt->execute(); $result = $stmt->fetchAll(PDO::FETCH_ASSOC); if ($result === false || empty($result)) { throw new ezcWorkflowExecutionException('Could not load execution state.'); } $this->id = $executionId; $this->nextThreadId = $result[0]['execution_next_thread_id']; $this->threads = ezcWorkflowDatabaseUtil::unserialize($result[0]['execution_threads']); $this->variables = ezcWorkflowDatabaseUtil::unserialize($result[0]['execution_variables']); $this->waitingFor = ezcWorkflowDatabaseUtil::unserialize($result[0]['execution_waiting_for']); $workflowId = $result[0]['workflow_id']; $this->workflow = $this->properties['definitionStorage']->loadById($workflowId); $query = $this->db->createSelectQuery(); $query->select($this->db->quoteIdentifier('node_id'))->select($this->db->quoteIdentifier('node_state'))->select($this->db->quoteIdentifier('node_activated_from'))->select($this->db->quoteIdentifier('node_thread_id'))->from($this->db->quoteIdentifier($this->options['prefix'] . 'execution_state'))->where($query->expr->eq($this->db->quoteIdentifier('execution_id'), $query->bindValue((int) $executionId))); $stmt = $query->prepare(); $stmt->execute(); $result = $stmt->fetchAll(PDO::FETCH_ASSOC); $active = array(); foreach ($result as $row) { $active[$row['node_id']] = array('activated_from' => ezcWorkflowDatabaseUtil::unserialize($row['node_activated_from']), 'state' => ezcWorkflowDatabaseUtil::unserialize($row['node_state'], null), 'thread_id' => $row['node_thread_id']); } foreach ($this->workflow->nodes as $node) { $nodeId = $node->getId(); if (isset($active[$nodeId])) { $node->setActivationState(ezcWorkflowNode::WAITING_FOR_EXECUTION); $node->setThreadId($active[$nodeId]['thread_id']); $node->setState($active[$nodeId]['state'], null); $node->setActivatedFrom($active[$nodeId]['activated_from']); $this->activate($node, false); } } $this->cancelled = false; $this->ended = false; $this->loaded = true; $this->resumed = false; $this->suspended = true; }
public function unserialize($value, $defaultValue = array()) { return \ezcWorkflowDatabaseUtil::unserialize($value, $defaultValue); }