/** * Save a workflow definition to the database. * * @param ezcWorkflow $workflow * @throws ezcWorkflowDefinitionStorageException */ public function save(\ezcWorkflow $workflow) { // Verify the workflow. $workflow->verify(); if (strlen($workflow->name) == 0) { throw new \ezcWorkflowDefinitionStorageException(); } $platform = $this->conn->getDatabasePlatform(); // what mode of saving should it be? Update or Re-Generate? // // Conditions that an update sufficies: // 1. No node has been deleted // 2. No node has changed its meaning (action class or type) // 3. For simplicitly only zero or one new nodes will be created. $hasExistingNodeIds = array(); $newNodes = 0; foreach ($workflow->nodes as $node) { $oid = spl_object_hash($node); if (!isset($this->nodeMap[$oid])) { $newNodes++; } else { $hasExistingNodeIds[] = $this->nodeMap[$oid]; } } $canBeUpdate = false; if ($newNodes < 2 && count(array_diff($hasExistingNodeIds, $this->workflowNodeIds[$workflow->id])) == 0 && $workflow->id) { $canBeUpdate = true; } $this->workflowNodeIds[$workflow->id] = array(); try { $this->conn->beginTransaction(); $workflowVersion = $this->getCurrentVersion($workflow->name) + 1; $this->conn->update($this->options->workflowTable(), array('workflow_outdated' => 1), array('workflow_name' => $workflow->name)); $date = new \DateTime("now"); if ($canBeUpdate) { $this->conn->update($this->options->workflowTable(), array('workflow_version' => $workflowVersion, 'workflow_created' => $date->format($platform->getDateTimeFormatString())), array('workflow_id' => $workflow->id)); } else { $data = array('workflow_name' => $workflow->name, 'workflow_version' => $workflowVersion, 'workflow_created' => $date->format($platform->getDateTimeFormatString()), 'workflow_outdated' => 0); // For sequences: get id before insert if ($platform->prefersSequences()) { $id = (int) $this->conn->fetchColumn($platform->getSequenceNextValSQL($this->options->workflowSequence())); $data['workflow_id'] = $id; $workflow->id = $id; } $this->conn->insert($this->options->workflowTable(), $data); if ($platform->prefersIdentityColumns()) { $workflow->id = (int) $this->conn->lastInsertId(); } $workflow->definitionStorage = $this; } // Write node table rows. $nodeMap = array(); foreach ($workflow->nodes as $node) { /* @var $node \ezcWorkflowNode */ $oid = spl_object_hash($node); if ($canBeUpdate && isset($this->nodeMap[$oid])) { $nodeId = (int) $this->nodeMap[$oid]; $this->conn->update($this->options->nodeTable(), array('node_configuration' => $this->options->getSerializer()->serialize($node->getConfiguration())), array('node_id' => $nodeId)); } else { $data = array('workflow_id' => (int) $workflow->id, 'node_class' => get_class($node), 'node_configuration' => $this->options->getSerializer()->serialize($node->getConfiguration())); if ($platform->prefersSequences()) { $nodeId = (int) $this->conn->fetchColumn($platform->getSequenceNextValSQL($this->options->nodeSequence())); $data['node_id'] = $nodeId; } $this->conn->insert($this->options->nodeTable(), $data); if ($platform->prefersIdentityColumns()) { $nodeId = (int) $this->conn->lastInsertId(); } } $nodeMap[$nodeId] = $node; $this->workflowNodeIds[$workflow->id][] = $nodeId; $this->nodeMap[$oid] = $nodeId; } if ($canBeUpdate) { // Delete all the node connections, NodeMap Keys are casted to (int) so usage here is safe. $query = "DELETE FROM " . $this->options->nodeConnectionTable() . " " . "WHERE incoming_node_id IN (" . implode(",", array_keys($nodeMap)) . ") OR " . "outgoing_node_id IN (" . implode(",", array_keys($nodeMap)) . ")"; $this->conn->executeUpdate($query); } foreach ($workflow->nodes as $node) { foreach ($node->getOutNodes() as $outNode) { $incomingNodeId = null; $outgoingNodeId = null; foreach ($nodeMap as $_id => $_node) { if ($_node === $node) { $incomingNodeId = $_id; } else { if ($_node === $outNode) { $outgoingNodeId = $_id; } } if ($incomingNodeId !== NULL && $outgoingNodeId !== NULL) { break; } } $data = array('incoming_node_id' => $incomingNodeId, 'outgoing_node_id' => $outgoingNodeId); if ($platform->prefersSequences()) { $id = (int) $this->conn->fetchColumn($platform->getSequenceNextValSQL($this->options->nodeConnectionSequence())); $data['id'] = $id; } $this->conn->insert($this->options->nodeConnectionTable(), $data); } } unset($nodeMap); if ($canBeUpdate) { $this->conn->delete($this->options->variableHandlerTable(), array('workflow_id' => (int) $workflow->id)); } foreach ($workflow->getVariableHandlers() as $variable => $class) { $this->conn->insert($this->options->variableHandlerTable(), array('workflow_id' => (int) $workflow->id, 'variable' => $variable, 'class' => $class)); } $this->conn->commit(); } catch (\Exception $e) { $this->conn->rollBack(); throw new \ezcWorkflowDefinitionStorageException("Error while persisting workflow: " . $e->getMessage()); } }
/** * Load a workflow definition from a DOMDocument. * * @param DOMDocument $document * @return ezcWorkflow */ public function loadFromDocument(DOMDocument $document) { $workflowName = $document->documentElement->getAttribute('name'); $workflowVersion = (int) $document->documentElement->getAttribute('version'); // Create node objects. $nodes = array(); $xmlNodes = $document->getElementsByTagName('node'); foreach ($xmlNodes as $xmlNode) { $id = (int) $xmlNode->getAttribute('id'); $className = 'ezcWorkflowNode' . $xmlNode->getAttribute('type'); if (class_exists($className)) { $configuration = call_user_func_array(array($className, 'configurationFromXML'), array($xmlNode)); if (is_null($configuration)) { $configuration = ezcWorkflowUtil::getDefaultConfiguration($className); } } $node = new $className($configuration); $node->setId($id); if ($node instanceof ezcWorkflowNodeFinally && !isset($finallyNode)) { $finallyNode = $node; } else { if ($node instanceof ezcWorkflowNodeEnd && !isset($defaultEndNode)) { $defaultEndNode = $node; } else { if ($node instanceof ezcWorkflowNodeStart) { $startNode = $node; } } } $nodes[$id] = $node; } if (!isset($startNode) || !isset($defaultEndNode)) { throw new ezcWorkflowDefinitionStorageException('Could not load workflow definition.'); } // Connect node objects. foreach ($xmlNodes as $xmlNode) { $id = (int) $xmlNode->getAttribute('id'); $className = 'ezcWorkflowNode' . $xmlNode->getAttribute('type'); foreach ($xmlNode->getElementsByTagName('outNode') as $outNode) { $nodes[$id]->addOutNode($nodes[(int) $outNode->getAttribute('id')]); } if (is_subclass_of($className, 'ezcWorkflowNodeConditionalBranch')) { foreach (ezcWorkflowUtil::getChildNodes($xmlNode) as $childNode) { if ($childNode->tagName == 'condition') { foreach ($childNode->getElementsByTagName('else') as $elseNode) { foreach ($elseNode->getElementsByTagName('outNode') as $outNode) { $elseId = (int) $outNode->getAttribute('id'); } } $condition = self::xmlToCondition($childNode); foreach ($childNode->getElementsByTagName('outNode') as $outNode) { if (!isset($elseId)) { $nodes[$id]->addConditionalOutNode($condition, $nodes[(int) $outNode->getAttribute('id')]); } else { $nodes[$id]->addConditionalOutNode($condition, $nodes[(int) $outNode->getAttribute('id')], $nodes[$elseId]); unset($elseId); } } } } } } 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->version = $workflowVersion; // Handle the variable handlers. foreach ($document->getElementsByTagName('variableHandler') as $variableHandler) { $workflow->addVariableHandler($variableHandler->getAttribute('variable'), $variableHandler->getAttribute('class')); } // Verify the loaded workflow. $workflow->verify(); return $workflow; }
/** * Save a workflow definition to the database. * * @param ezcWorkflow $workflow * @throws ezcWorkflowDefinitionStorageException * @throws ezcDbException */ public function save(ezcWorkflow $workflow) { // Verify the workflow. $workflow->verify(); $this->db->beginTransaction(); // Calculate new version number. $workflowVersion = $this->getCurrentVersionNumber($workflow->name) + 1; // Write workflow table row. $query = $this->db->createInsertQuery(); $query->insertInto($this->db->quoteIdentifier($this->options['prefix'] . 'workflow'))->set($this->db->quoteIdentifier('workflow_name'), $query->bindValue($workflow->name))->set($this->db->quoteIdentifier('workflow_version'), $query->bindValue((int) $workflowVersion))->set($this->db->quoteIdentifier('workflow_created'), $query->bindValue(time())); $statement = $query->prepare(); $statement->execute(); $workflow->definitionStorage = $this; $workflow->id = (int) $this->db->lastInsertId('workflow_workflow_id_seq'); $workflow->version = (int) $workflowVersion; // Write node table rows. $nodeMap = array(); foreach ($workflow->nodes as $node) { $query = $this->db->createInsertQuery(); $query->insertInto($this->db->quoteIdentifier($this->options['prefix'] . 'node'))->set($this->db->quoteIdentifier('workflow_id'), $query->bindValue((int) $workflow->id))->set($this->db->quoteIdentifier('node_class'), $query->bindValue(get_class($node)))->set($this->db->quoteIdentifier('node_configuration'), $query->bindValue(ezcWorkflowDatabaseUtil::serialize($node->getConfiguration()))); $statement = $query->prepare(); $statement->execute(); $nodeMap[$this->db->lastInsertId($this->db->quoteIdentifier('node_node_id_seq'))] = $node; } // Connect node table rows. foreach ($workflow->nodes as $node) { foreach ($node->getOutNodes() as $outNode) { $incomingNodeId = null; $outgoingNodeId = null; foreach ($nodeMap as $_id => $_node) { if ($_node === $node) { $incomingNodeId = $_id; } else { if ($_node === $outNode) { $outgoingNodeId = $_id; } } if ($incomingNodeId !== NULL && $outgoingNodeId !== NULL) { break; } } $query = $this->db->createInsertQuery(); $query->insertInto($this->db->quoteIdentifier($this->options['prefix'] . 'node_connection'))->set($this->db->quoteIdentifier('incoming_node_id'), $query->bindValue($incomingNodeId))->set($this->db->quoteIdentifier('outgoing_node_id'), $query->bindValue($outgoingNodeId)); $statement = $query->prepare(); $statement->execute(); } } unset($nodeMap); // Write variable handler rows. foreach ($workflow->getVariableHandlers() as $variable => $class) { $query = $this->db->createInsertQuery(); $query->insertInto($this->db->quoteIdentifier($this->options['prefix'] . 'variable_handler'))->set($this->db->quoteIdentifier('workflow_id'), $query->bindValue((int) $workflow->id))->set($this->db->quoteIdentifier('variable'), $query->bindValue($variable))->set($this->db->quoteIdentifier('class'), $query->bindValue($class)); $statement = $query->prepare(); $statement->execute(); } $this->db->commit(); }
public function testVerify4() { $workflow = new ezcWorkflow('Test'); $workflow->finallyNode->addOutNode(new ezcWorkflowNodeFinally()); try { $workflow->verify(); } catch (ezcWorkflowDefinitionStorageException $e) { $this->assertEquals('A workflow may have only one finally node.', $e->getMessage()); return; } $this->fail('Expected an ezcWorkflowDefinitionStorageException to be thrown.'); }
/** * Save a workflow definition to the database. * * @param ezcWorkflow $workflow * @throws ezcWorkflowDefinitionStorageException * @throws ezcDbException */ public function save(ezcWorkflow $workflow) { // Verify the workflow. $workflow->verify(); $this->db->beginTransaction(); // Calculate new version number. $workflowVersion = $this->getCurrentVersionNumber($workflow->name) + 1; // Write workflow table row. $query = $this->db->createInsertQuery(); $query->insertInto($this->db->quoteIdentifier($this->options['prefix'] . 'workflow'))->set($this->db->quoteIdentifier('workflow_name'), $query->bindValue($workflow->name))->set($this->db->quoteIdentifier('workflow_version'), $query->bindValue((int) $workflowVersion))->set($this->db->quoteIdentifier('workflow_created'), $query->bindValue(time())); $statement = $query->prepare(); $statement->execute(); $workflow->definitionStorage = $this; $workflow->id = (int) $this->db->lastInsertId('workflow_workflow_id_seq'); $workflow->version = (int) $workflowVersion; // Write node table rows. $nodes = $workflow->nodes; $keys = array_keys($nodes); $numNodes = count($nodes); for ($i = 0; $i < $numNodes; $i++) { $id = $keys[$i]; $node = $nodes[$id]; $query = $this->db->createInsertQuery(); $query->insertInto($this->db->quoteIdentifier($this->options['prefix'] . 'node'))->set($this->db->quoteIdentifier('workflow_id'), $query->bindValue((int) $workflow->id))->set($this->db->quoteIdentifier('node_class'), $query->bindValue(get_class($node)))->set($this->db->quoteIdentifier('node_configuration'), $query->bindValue(ezcWorkflowDatabaseUtil::serialize($node->getConfiguration()))); $statement = $query->prepare(); $statement->execute(); $node->setId($this->db->lastInsertId($this->db->quoteIdentifier('node_node_id_seq'))); } // Connect node table rows. for ($i = 0; $i < $numNodes; $i++) { $id = $keys[$i]; $node = $nodes[$id]; foreach ($node->getOutNodes() as $outNode) { $query = $this->db->createInsertQuery(); $query->insertInto($this->db->quoteIdentifier($this->options['prefix'] . 'node_connection'))->set($this->db->quoteIdentifier('incoming_node_id'), $query->bindValue((int) $node->getId()))->set($this->db->quoteIdentifier('outgoing_node_id'), $query->bindValue((int) $outNode->getId())); $statement = $query->prepare(); $statement->execute(); } } // Write variable handler rows. foreach ($workflow->getVariableHandlers() as $variable => $class) { $query = $this->db->createInsertQuery(); $query->insertInto($this->db->quoteIdentifier($this->options['prefix'] . 'variable_handler'))->set($this->db->quoteIdentifier('workflow_id'), $query->bindValue((int) $workflow->id))->set($this->db->quoteIdentifier('variable'), $query->bindValue($variable))->set($this->db->quoteIdentifier('class'), $query->bindValue($class)); $statement = $query->prepare(); $statement->execute(); } $this->db->commit(); }