From 91e32a78c8ea96fb3dbcecb96c3d96b0e10a3b5a Mon Sep 17 00:00:00 2001 From: Paula Quispe Date: Tue, 31 Jul 2018 08:34:16 -0400 Subject: [PATCH] HOR-4541 --- .../engine/classes/model/AdditionalTables.php | 101 ++++++++- .../src/ProcessMaker/BusinessModel/Table.php | 203 +++++++++++++----- .../src/ProcessMaker/Services/Api/Pmtable.php | 13 +- 3 files changed, 262 insertions(+), 55 deletions(-) diff --git a/workflow/engine/classes/model/AdditionalTables.php b/workflow/engine/classes/model/AdditionalTables.php index 557384cc3..60c041a75 100644 --- a/workflow/engine/classes/model/AdditionalTables.php +++ b/workflow/engine/classes/model/AdditionalTables.php @@ -56,8 +56,41 @@ function validateType($value, $type) class AdditionalTables extends BaseAdditionalTables { - public $fields = array(); - public $primaryKeys = array(); + const FLD_TYPE_VALUES = [ + 'BIGINT', + 'BOOLEAN', + 'CHAR', + 'DATE', + 'DATETIME', + 'DECIMAL', + 'DOUBLE', + 'FLOAT', + 'INTEGER', + 'LONGVARCHAR', + 'REAL', + 'SMALLINT', + 'TIME', + 'TIMESTAMP', + 'TINYINT', + 'VARCHAR' + ]; + const FLD_TYPE_WITH_AUTOINCREMENT = [ + 'BIGINT', + 'INTEGER', + 'SMALLINT', + 'TINYINT' + ]; + const FLD_TYPE_WITH_SIZE = [ + 'BIGINT', + 'CHAR', + 'DECIMAL', + 'FLOAT', + 'INTEGER', + 'LONGVARCHAR', + 'VARCHAR' + ]; + public $fields = []; + public $primaryKeys = []; /** * Function load @@ -1126,6 +1159,70 @@ class AdditionalTables extends BaseAdditionalTables return array('rows' => $addTables, 'count' => $count); } + /** + * Get the table properties + * + * @param string $tabUid + * @param array $tabData + * @param boolean $isReportTable + * + * @return array + * @throws Exception + */ + public static function getTableProperties($tabUid, $tabData = [], $isReportTable = false) + { + $criteria = new Criteria('workflow'); + $criteria->add(AdditionalTablesPeer::ADD_TAB_UID, $tabUid, Criteria::EQUAL); + $dataset = AdditionalTablesPeer::doSelectOne($criteria); + + $dataValidate = []; + if (!is_null($dataset)) { + $dataValidate['rep_tab_uid'] = $tabUid; + + $tableName = $dataset->getAddTabName(); + if (substr($tableName, 0, 4) === 'PMT_') { + $tableNameWithoutPrefixPmt = substr($tableName, 4); + } else { + $tableNameWithoutPrefixPmt = $tableName; + } + + $dataValidate['rep_tab_name'] = $tableNameWithoutPrefixPmt; + $dataValidate['rep_tab_name_old_name'] = $tableName; + $dataValidate['pro_uid'] = $dataset->getProUid(); + $dataValidate['rep_tab_dsc'] = $dataset->getAddTabDescription(); + $dataValidate['rep_tab_connection'] = $dataset->getDbsUid(); + $dataValidate['rep_tab_type'] = $dataset->getAddTabType(); + $dataValidate['rep_tab_grid'] = ''; + + if ($isReportTable) { + if (!empty($tabData['pro_uid']) && $dataValidate['pro_uid'] !== $tabData['pro_uid']) { + throw (new Exception("The property pro_uid: '". $tabData['pro_uid'] . "' is incorrect.")); + } + if (!empty($tabData['rep_tab_name']) && $tableName !== $tabData['rep_tab_name']) { + throw (new Exception("The property rep_tab_name: '". $tabData['rep_tab_name'] . "' is incorrect.")); + } + if (!empty($dataValidate['rep_tab_dsc'])) { + $dataValidate['rep_tab_dsc'] = $tabData['rep_tab_dsc']; + } + $tabGrid = $dataset->getAddTabGrid(); + if (strpos($tabGrid, '-')) { + list($gridName, $gridId) = explode('-', $tabGrid); + $dataValidate['rep_tab_grid'] = $gridId; + } + } else { + if (!empty($tabData['rep_tab_name']) && $tableName !== $tabData['pmt_tab_name']) { + throw (new Exception("The property pmt_tab_name: '". $tabData['pmt_tab_name'] . "' is incorrect.")); + } + if (!empty($dataValidate['pmt_tab_dsc'])) { + $dataValidate['rep_tab_dsc'] = $tabData['pmt_tab_dsc']; + } + } + $dataValidate['fields'] = $tabData['fields']; + } + + return $dataValidate; + } + /** * DEPRECATED createPropelClasses() * diff --git a/workflow/engine/src/ProcessMaker/BusinessModel/Table.php b/workflow/engine/src/ProcessMaker/BusinessModel/Table.php index c7f5b2e1e..7f85689d6 100644 --- a/workflow/engine/src/ProcessMaker/BusinessModel/Table.php +++ b/workflow/engine/src/ProcessMaker/BusinessModel/Table.php @@ -1,10 +1,12 @@ - * @copyright Colosa - Bolivia + * @deprecated Method deprecated in Release 3.3.1 * * @return array + * @throws Exception */ public function saveTable($tab_data, $pro_uid = '', $reportFlag = false, $createRep = true) { @@ -508,59 +511,163 @@ class Table } /** - * Update Data for Table - * @var string $tab_data. Data for table - * @var string $pro_uid. Uid for process - * @var string $reportFlag. If is report table + * Update Data for PmTable and Report Table * - * @author Brayan Pereyra (Cochalo) - * @copyright Colosa - Bolivia + * @var string $tableData: Data for table + * @var string $pro_uid: Uid for process + * @var boolean $isReportTable: If is report table * - * @return void + * @return object + * @throws Exception */ - public function updateTable($tab_data, $pro_uid = '', $reportFlag = false) + public function updateTable($tableData, $proUid = '', $isReportTable = false) { - if ($reportFlag) { - $tab_uid = $tab_data['rep_uid']; - $pro_uid = $this->validateProUid($pro_uid); + $tableDsc = false; + $tableFields = false; + if ($isReportTable) { + $tabUid = $tableData['rep_uid']; + $proUid = $this->validateProUid($proUid); + $tableData['pro_uid'] = $proUid; + $errorMssg = "The property rep_uid: '$tabUid' is incorrect."; } else { - $tab_uid = $tab_data['pmt_uid']; + $tabUid = $tableData['pmt_uid']; + $errorMssg = "The property pmt_uid: '$tabUid' is incorrect."; + } + $tabUid = $this->validateTabUid($tabUid, $isReportTable); + $addTables = new AdditionalTables(); + $dataValidate = $addTables->getTableProperties($tabUid, $tableData, $isReportTable); + if (empty($dataValidate)) { + throw (new Exception($errorMssg)); + } + if ($isReportTable) { + if (!empty($tableData['rep_tab_dsc'])) { + $dataValidate['rep_tab_dsc'] = $tableData['rep_tab_dsc']; + $tableDsc = true; + } + } else { + if (!empty($tableData['pmt_tab_dsc'])) { + $dataValidate['rep_tab_dsc'] = $tableData['pmt_tab_dsc']; + $tableDsc = true; + } + } + if (!empty($tableData['fields'])) { + $dataValidate['fields'] = $tableData['fields']; + $tableFields = true; + } else { + throw (new Exception('Body doesn\'t contain fields arguments')); + } + if (!$tableDsc && !$tableFields) { + throw (new Exception('Body doesn\'t contain pmt_tad_dsc or fields arguments')); } - $tab_uid = $this->validateTabUid($tab_uid, $reportFlag); - $dataValidate = array(); - $oCriteria = new \Criteria('workflow'); - $oCriteria->add(\AdditionalTablesPeer::ADD_TAB_UID, $tab_uid, \Criteria::EQUAL); - $oDataset = \AdditionalTablesPeer::doSelectRS($oCriteria); - $oDataset->setFetchmode(\ResultSet::FETCHMODE_ASSOC); + //We will validate the fields after update the pmTable structure + $result = $this->validateTableBeforeUpdate($dataValidate); - if ($oDataset->next()) { - $row = $oDataset->getRow(); - if ($reportFlag) { - $dataValidate['rep_uid'] = $tab_uid; - $dataValidate['rep_tab_name'] = $row['ADD_TAB_NAME']; - $dataValidate['rep_tab_dsc'] = $tab_data['rep_tab_dsc']; - $dataValidate['rep_tab_connection'] = $row['DBS_UID']; - $dataValidate['rep_tab_type'] = $row['ADD_TAB_TYPE']; - $dataValidate['rep_tab_grid'] = ''; - if (strpos($row['ADD_TAB_GRID'], '-')) { - list($gridName, $gridId) = explode( '-', $row['ADD_TAB_GRID'] ); - $dataValidate['rep_tab_grid'] = $gridId; + return $result; + } + + /** + * Will be validate the fields before saveStructureOfTable + * + * @param array $tableFields Properties for table + * + * @return object + * @throws Exception + */ + public function validateTableBeforeUpdate($tableFields) + { + $propertiesUpdate = []; + if (!empty($tableFields)){ + $propertiesUpdate = array_change_key_case($tableFields, CASE_UPPER); + $propertiesUpdate['keepData'] = '1'; + } + + $columnsTable = []; + $flagKey = false; + if (!empty($propertiesUpdate['FIELDS'])) { + $columns = $propertiesUpdate['FIELDS']; + foreach ($columns as $i => $column) { + $columnsTable[$i] = []; + //Required fld_uid + if (!empty($columns[$i]['fld_uid'])) { + $columnsTable[$i]['field_uid'] = $columnsTable[$i]['uid'] = G::toUpper($columns[$i]['fld_uid']); + } else { + throw (new Exception(G::LoadTranslation("ID_CAN_NOT_BE_EMPTY", ['fld_uid']))); } - } else { - $dataValidate['pmt_uid'] = $tab_uid; - $dataValidate['pmt_tab_name'] = $row['ADD_TAB_NAME']; - $dataValidate['pmt_tab_dsc'] = $tab_data['pmt_tab_dsc']; - } - $dataValidate['fields'] = $tab_data['fields']; - } else { - if ($reportFlag) { - throw (new \Exception("The property rep_uid: '$tab_uid' is incorrect.")); - } else { - throw (new \Exception("The property pmt_uid: '$tab_uid' is incorrect.")); + //Not required fld_dyn + $columnsTable[$i]['field_dyn'] = ''; + if (!empty($columns[$i]['fld_dyn'])) { + $columnsTable[$i]['field_dyn'] = G::toUpper($columns[$i]['fld_dyn']); + } + //Required fld_name + if (!empty($columns[$i]['fld_name'])) { + $columnsTable[$i]['field_name'] = G::toUpper($columns[$i]['fld_name']); + } else { + throw (new Exception(G::LoadTranslation("ID_CAN_NOT_BE_EMPTY", ['fld_name']))); + } + //Required fld_label + if (!empty($columns[$i]['fld_label'])) { + $columnsTable[$i]['field_label'] = G::toUpper($columns[$i]['fld_label']); + } else { + throw (new Exception(G::LoadTranslation("ID_CAN_NOT_BE_EMPTY", ['fld_label']))); + } + + //We will to define the autoincrement + $columnsTable[$i]['field_autoincrement'] = false; + + //Required fld_type + if (!empty($columns[$i]['fld_type'])) { + $columnsTable[$i]['field_type'] = G::toUpper($columns[$i]['fld_type']); + //Will be validate if is the correct type of column + if (!in_array($columnsTable[$i]['field_type'], AdditionalTables::FLD_TYPE_VALUES)) { + throw (new Exception("The property fld_type: '" . $columns[$i]['fld_type'] . "' is incorrect.")); + } + //Will be review if the column type has the correct definition with autoincrement + if (!empty($columns[$i]['fld_autoincrement']) && $columns[$i]['fld_autoincrement']) { + if ($columns[$i]['fld_key'] && in_array($columns[$i]['fld_type'], AdditionalTables::FLD_TYPE_WITH_AUTOINCREMENT)) { + $columnsTable[$i]['field_autoincrement'] = true; + } else { + throw (new Exception("The property field_autoincrement: '" . $columns[$i]['fld_autoincrement'] . "' is incorrect. ")); + } + } + } else { + throw (new Exception(G::LoadTranslation("ID_CAN_NOT_BE_EMPTY", ['fld_type']))); + } + //Required fld_size depends of fld_type + $columnsTable[$i]['field_size'] = 0; + if (in_array($columns[$i]['fld_type'], AdditionalTables::FLD_TYPE_WITH_SIZE)) { + if (empty($columns[$i]['fld_size'])) { + throw (new Exception(G::LoadTranslation("ID_CAN_NOT_BE_EMPTY", ['fld_size']))); + } + if ((integer)$columns[$i]['fld_size'] === 0) { + throw (new Exception("The property fld_size: '" . $columns[$i]['fld_size'] . "' is incorrect.")); + } + $columnsTable[$i]['field_size'] = (integer)$columns[$i]['fld_size']; + } + //Required only for one column + $columnsTable[$i]['field_key'] = false; + if (!empty($columns[$i]['fld_key'])) { + $flagKey = true; + $columnsTable[$i]['field_key'] = (boolean)$columns[$i]['fld_key']; + } + //Not required fld_null + $columnsTable[$i]['field_null'] = false; + if (!empty($columns[$i]['fld_null'])) { + $columnsTable[$i]['field_null'] = G::toUpper($columns[$i]['fld_null']); + } + //Not required fld_filter + $columnsTable[$i]['field_filter'] = false; } } - $this->saveTable($dataValidate, $pro_uid, $reportFlag, false); + if (!$flagKey) { + throw (new Exception("The table doesn't have a primary key 'fld_key'")); + } + + $propertiesUpdate['columns'] = G::json_encode($columnsTable); + $reportTable = new BusinessModelRpt(); + $result = $reportTable->saveStructureOfTable($propertiesUpdate); + + return $result; } /** diff --git a/workflow/engine/src/ProcessMaker/Services/Api/Pmtable.php b/workflow/engine/src/ProcessMaker/Services/Api/Pmtable.php index 36698c25a..c378f0c71 100644 --- a/workflow/engine/src/ProcessMaker/Services/Api/Pmtable.php +++ b/workflow/engine/src/ProcessMaker/Services/Api/Pmtable.php @@ -1,8 +1,10 @@ updateTable($request_data); - } catch (\Exception $e) { + $pmTable = new BusinessModelTable(); + $response = $pmTable->updateTable($request_data); + } catch (Exception $e) { throw (new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage())); } }