diff --git a/workflow/engine/classes/WorkspaceTools.php b/workflow/engine/classes/WorkspaceTools.php index a01c84309..9e1abb024 100644 --- a/workflow/engine/classes/WorkspaceTools.php +++ b/workflow/engine/classes/WorkspaceTools.php @@ -1243,12 +1243,15 @@ class WorkspaceTools if ($action == 'ADD') { $tablesToAddColumns[$tableName] = $actionData; - // In a very old schema the primary key for table "LOGIN_LOG" was changed and we need to delete the + // In a very old schema the primary key for tables "LOGIN_LOG" and "APP_SEQUENCE" were changed and we need to delete the // primary index to avoid errors in the database upgrade // TO DO: The change of a Primary Key in a table should be generic if ($tableName == 'LOGIN_LOG' && array_key_exists('LOG_ID', $actionData)) { $database->executeQuery('DROP INDEX `PRIMARY` ON LOGIN_LOG;'); } + if ($tableName == 'APP_SEQUENCE' && array_key_exists('APP_TYPE', $actionData)) { + $database->executeQuery('DROP INDEX `PRIMARY` ON APP_SEQUENCE;'); + } } else { foreach ($actionData as $columnName => $meta) { switch ($action) { @@ -3285,22 +3288,46 @@ class WorkspaceTools } } + /** + * Add sequence numbers + */ public function checkSequenceNumber() { - $criteria = new Criteria("workflow"); + // Instance required class + $appSequenceInstance = new AppSequence(); + + // Get a record from APP_SEQUENCE table + $criteria = new Criteria('workflow'); $rsCriteria = AppSequencePeer::doSelectRS($criteria); $rsCriteria->setFetchmode(ResultSet::FETCHMODE_ASSOC); $rsCriteria->next(); $appSequenceRow = $rsCriteria->getRow(); + + // If table APP_SEQUENCE is empty, insert two records if (empty($appSequenceRow)) { - $sequenceInstance = SequencesPeer::retrieveByPK("APP_NUMBER"); - $appSequenceInstance = new AppSequence(); + // Check if exist a value in old table SEQUENCES + $sequenceInstance = SequencesPeer::retrieveByPK('APP_NUMBER'); + if (!is_null($sequenceInstance)) { + // If exists a value in SEQUENCE table, copy the same to APP_SEQUENCES table $sequenceFields = $sequenceInstance->toArray(BasePeer::TYPE_FIELDNAME); $appSequenceInstance->updateSequenceNumber($sequenceFields['SEQ_VALUE']); } else { + // If not exists a value in SEQUENCE table, insert a initial value $appSequenceInstance->updateSequenceNumber(0); } + + // Insert a initial value for the web entries + $appSequenceInstance->updateSequenceNumber(0, AppSequence::APP_TYPE_WEB_ENTRY); + } else { + // Create a new instance of Criteria class + $criteria = new Criteria('workflow'); + $criteria->add(AppSequencePeer::APP_TYPE, AppSequence::APP_TYPE_WEB_ENTRY); + + // Check if exists a record for the web entries, if not exist insert the initial value + if (AppSequencePeer::doCount($criteria) === 0) { + $appSequenceInstance->updateSequenceNumber(0, AppSequence::APP_TYPE_WEB_ENTRY); + } } } diff --git a/workflow/engine/classes/model/AppSequence.php b/workflow/engine/classes/model/AppSequence.php index 4d062260c..82498152c 100644 --- a/workflow/engine/classes/model/AppSequence.php +++ b/workflow/engine/classes/model/AppSequence.php @@ -53,26 +53,36 @@ class AppSequence extends BaseAppSequence { /** * Update sequence number * - * @return mixed + * @param int $number + * @param string $sequenceType + * * @throws Exception */ - public function updateSequenceNumber($number) + public function updateSequenceNumber($number, $sequenceType = AppSequence::APP_TYPE_NORMAL) { try { - $con = Propel::getConnection('workflow'); - $stmt = $con->createStatement(); - $c = new Criteria(); - $rs = AppSequencePeer::doSelectRS($c); - $rs->setFetchmode(ResultSet::FETCHMODE_ASSOC); - $rs->next(); - $row = $rs->getRow(); + // Get the current connection + $connection = Propel::getConnection('workflow'); + + // Create a statement instance + $statement = $connection->createStatement(); + + // Get the record according to the sequence type + $criteria = new Criteria(); + $criteria->add(AppSequencePeer::APP_TYPE, $sequenceType); + $rsCriteria = AppSequencePeer::doSelectRS($criteria); + $rsCriteria->setFetchmode(ResultSet::FETCHMODE_ASSOC); + $rsCriteria->next(); + $row = $rsCriteria->getRow(); + + // Insert/Update sequence table with the number sent if ($row) { - $sql = "UPDATE APP_SEQUENCE SET ID=LAST_INSERT_ID('$number')"; + $sql = "UPDATE APP_SEQUENCE SET ID=LAST_INSERT_ID('{$number}') WHERE APP_TYPE = '{$sequenceType}'"; } else { - $sql = "INSERT INTO APP_SEQUENCE (ID) VALUES ('$number');"; + $sql = "INSERT INTO APP_SEQUENCE (ID, APP_TYPE) VALUES ('{$number}', '{$sequenceType}')"; } - $stmt->executeQuery($sql, ResultSet::FETCHMODE_ASSOC); - } catch (\Exception $e) { + $statement->executeQuery($sql, ResultSet::FETCHMODE_ASSOC); + } catch (Exception $e) { throw ($e); } } diff --git a/workflow/engine/classes/model/map/AppSequenceMapBuilder.php b/workflow/engine/classes/model/map/AppSequenceMapBuilder.php index 2df087efc..9a09f7283 100644 --- a/workflow/engine/classes/model/map/AppSequenceMapBuilder.php +++ b/workflow/engine/classes/model/map/AppSequenceMapBuilder.php @@ -65,7 +65,7 @@ class AppSequenceMapBuilder $tMap->setUseIdGenerator(false); - $tMap->addPrimaryKey('ID', 'Id', 'int', CreoleTypes::INTEGER, true, null); + $tMap->addColumn('ID', 'Id', 'int', CreoleTypes::INTEGER, true, null); $tMap->addColumn('APP_TYPE', 'AppType', 'string', CreoleTypes::VARCHAR, true, 20); diff --git a/workflow/engine/classes/model/om/BaseAppSequence.php b/workflow/engine/classes/model/om/BaseAppSequence.php index 4978e1075..c21ac5de8 100644 --- a/workflow/engine/classes/model/om/BaseAppSequence.php +++ b/workflow/engine/classes/model/om/BaseAppSequence.php @@ -480,30 +480,33 @@ abstract class BaseAppSequence extends BaseObject implements Persistent { $criteria = new Criteria(AppSequencePeer::DATABASE_NAME); - $criteria->add(AppSequencePeer::ID, $this->id); return $criteria; } /** - * Returns the primary key for this object (row). - * @return int + * Returns NULL since this table doesn't have a primary key. + * This method exists only for BC and is deprecated! + * @return null */ public function getPrimaryKey() { - return $this->getId(); + return null; } /** - * Generic method to set the primary key (id column). + * Dummy primary key setter. * - * @param int $key Primary key. - * @return void + * This function only exists to preserve backwards compatibility. It is no longer + * needed or required by the Persistent interface. It will be removed in next BC-breaking + * release of Propel. + * + * @deprecated */ - public function setPrimaryKey($key) - { - $this->setId($key); - } + public function setPrimaryKey($pk) + { + // do nothing, because this object doesn't have any primary keys + } /** * Sets contents of passed object to values from current object. @@ -518,13 +521,13 @@ abstract class BaseAppSequence extends BaseObject implements Persistent public function copyInto($copyObj, $deepCopy = false) { + $copyObj->setId($this->id); + $copyObj->setAppType($this->app_type); $copyObj->setNew(true); - $copyObj->setId(NULL); // this is a pkey column, so set to default value - } /** diff --git a/workflow/engine/classes/model/om/BaseAppSequencePeer.php b/workflow/engine/classes/model/om/BaseAppSequencePeer.php index 1a9fd8b4b..649966b91 100644 --- a/workflow/engine/classes/model/om/BaseAppSequencePeer.php +++ b/workflow/engine/classes/model/om/BaseAppSequencePeer.php @@ -171,8 +171,8 @@ abstract class BaseAppSequencePeer } - const COUNT = 'COUNT(APP_SEQUENCE.ID)'; - const COUNT_DISTINCT = 'COUNT(DISTINCT APP_SEQUENCE.ID)'; + const COUNT = 'COUNT(*)'; + const COUNT_DISTINCT = 'COUNT(DISTINCT *)'; /** * Returns the number of rows matching criteria. @@ -381,9 +381,6 @@ abstract class BaseAppSequencePeer if ($values instanceof Criteria) { $criteria = clone $values; // rename for clarity - $comparison = $criteria->getComparison(AppSequencePeer::ID); - $selectCriteria->add(AppSequencePeer::ID, $criteria->remove(AppSequencePeer::ID), $comparison); - } else { $criteria = $values->buildCriteria(); // gets full criteria $selectCriteria = $values->buildPkeyCriteria(); // gets criteria w/ primary key(s) @@ -441,11 +438,22 @@ abstract class BaseAppSequencePeer $criteria = clone $values; // rename for clarity } elseif ($values instanceof AppSequence) { - $criteria = $values->buildPkeyCriteria(); + $criteria = $values->buildCriteria(); } else { // it must be the primary key $criteria = new Criteria(self::DATABASE_NAME); - $criteria->add(AppSequencePeer::ID, (array) $values, Criteria::IN); + // primary key is composite; we therefore, expect + // the primary key passed to be an array of pkey + // values + if (count($values) == count($values, COUNT_RECURSIVE)) { + // array is not multi-dimensional + $values = array($values); + } + $vals = array(); + foreach ($values as $value) { + + } + } // Set the correct dbName @@ -503,54 +511,6 @@ abstract class BaseAppSequencePeer return BasePeer::doValidate(AppSequencePeer::DATABASE_NAME, AppSequencePeer::TABLE_NAME, $columns); } - - /** - * Retrieve a single object by pkey. - * - * @param mixed $pk the primary key. - * @param Connection $con the connection to use - * @return AppSequence - */ - public static function retrieveByPK($pk, $con = null) - { - if ($con === null) { - $con = Propel::getConnection(self::DATABASE_NAME); - } - - $criteria = new Criteria(AppSequencePeer::DATABASE_NAME); - - $criteria->add(AppSequencePeer::ID, $pk); - - - $v = AppSequencePeer::doSelect($criteria, $con); - - return !empty($v) > 0 ? $v[0] : null; - } - - /** - * Retrieve multiple objects by pkey. - * - * @param array $pks List of primary keys - * @param Connection $con the connection to use - * @throws PropelException Any exceptions caught during processing will be - * rethrown wrapped into a PropelException. - */ - public static function retrieveByPKs($pks, $con = null) - { - if ($con === null) { - $con = Propel::getConnection(self::DATABASE_NAME); - } - - $objs = null; - if (empty($pks)) { - $objs = array(); - } else { - $criteria = new Criteria(); - $criteria->add(AppSequencePeer::ID, $pks, Criteria::IN); - $objs = AppSequencePeer::doSelect($criteria, $con); - } - return $objs; - } }