Merged in bugfix/HOR-4541 (pull request #6577)

HOR-4541

Approved-by: Julio Cesar Laura Avendaño <contact@julio-laura.com>
This commit is contained in:
Paula Quispe
2018-08-07 12:12:24 +00:00
committed by Julio Cesar Laura Avendaño
3 changed files with 262 additions and 55 deletions

View File

@@ -56,8 +56,41 @@ function validateType($value, $type)
class AdditionalTables extends BaseAdditionalTables class AdditionalTables extends BaseAdditionalTables
{ {
public $fields = array(); const FLD_TYPE_VALUES = [
public $primaryKeys = array(); '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 * Function load
@@ -1126,6 +1159,70 @@ class AdditionalTables extends BaseAdditionalTables
return array('rows' => $addTables, 'count' => $count); 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() * DEPRECATED createPropelClasses()
* *

View File

@@ -1,10 +1,12 @@
<?php <?php
namespace ProcessMaker\BusinessModel; namespace ProcessMaker\BusinessModel;
use G;
use AdditionalTables; use AdditionalTables;
use Fields;
use DynaformHandler; use DynaformHandler;
use Exception;
use Fields;
use G;
use ProcessMaker\BusinessModel\ReportTable as BusinessModelRpt;
class Table class Table
{ {
@@ -184,15 +186,16 @@ class Table
/** /**
* Save Data for Table * Save Data for Table
*
* @var string $tab_data. Data for table * @var string $tab_data. Data for table
* @var string $pro_uid. Uid for process * @var string $pro_uid. Uid for process
* @var string $reportFlag. If is report table * @var boolean $reportFlag. If is report table
* @var string $createRep. Flag for create table * @var boolean $createRep. Flag for create table
* *
* @author Brayan Pereyra (Cochalo) <brayan@colosa.com> * @deprecated Method deprecated in Release 3.3.1
* @copyright Colosa - Bolivia
* *
* @return array * @return array
* @throws Exception
*/ */
public function saveTable($tab_data, $pro_uid = '', $reportFlag = false, $createRep = true) public function saveTable($tab_data, $pro_uid = '', $reportFlag = false, $createRep = true)
{ {
@@ -508,59 +511,163 @@ class Table
} }
/** /**
* Update Data for Table * Update Data for PmTable and Report Table
* @var string $tab_data. Data for table
* @var string $pro_uid. Uid for process
* @var string $reportFlag. If is report table
* *
* @author Brayan Pereyra (Cochalo) <brayan@colosa.com> * @var string $tableData: Data for table
* @copyright Colosa - Bolivia * @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) { $tableDsc = false;
$tab_uid = $tab_data['rep_uid']; $tableFields = false;
$pro_uid = $this->validateProUid($pro_uid); if ($isReportTable) {
$tabUid = $tableData['rep_uid'];
$proUid = $this->validateProUid($proUid);
$tableData['pro_uid'] = $proUid;
$errorMssg = "The property rep_uid: '$tabUid' is incorrect.";
} else { } 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(); //We will validate the fields after update the pmTable structure
$oCriteria = new \Criteria('workflow'); $result = $this->validateTableBeforeUpdate($dataValidate);
$oCriteria->add(\AdditionalTablesPeer::ADD_TAB_UID, $tab_uid, \Criteria::EQUAL);
$oDataset = \AdditionalTablesPeer::doSelectRS($oCriteria);
$oDataset->setFetchmode(\ResultSet::FETCHMODE_ASSOC);
if ($oDataset->next()) { return $result;
$row = $oDataset->getRow(); }
if ($reportFlag) {
$dataValidate['rep_uid'] = $tab_uid; /**
$dataValidate['rep_tab_name'] = $row['ADD_TAB_NAME']; * Will be validate the fields before saveStructureOfTable
$dataValidate['rep_tab_dsc'] = $tab_data['rep_tab_dsc']; *
$dataValidate['rep_tab_connection'] = $row['DBS_UID']; * @param array $tableFields Properties for table
$dataValidate['rep_tab_type'] = $row['ADD_TAB_TYPE']; *
$dataValidate['rep_tab_grid'] = ''; * @return object
if (strpos($row['ADD_TAB_GRID'], '-')) { * @throws Exception
list($gridName, $gridId) = explode( '-', $row['ADD_TAB_GRID'] ); */
$dataValidate['rep_tab_grid'] = $gridId; 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'])));
}
//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 { } else {
$dataValidate['pmt_uid'] = $tab_uid; throw (new Exception(G::LoadTranslation("ID_CAN_NOT_BE_EMPTY", ['fld_type'])));
$dataValidate['pmt_tab_name'] = $row['ADD_TAB_NAME'];
$dataValidate['pmt_tab_dsc'] = $tab_data['pmt_tab_dsc'];
} }
$dataValidate['fields'] = $tab_data['fields']; //Required fld_size depends of fld_type
} else { $columnsTable[$i]['field_size'] = 0;
if ($reportFlag) { if (in_array($columns[$i]['fld_type'], AdditionalTables::FLD_TYPE_WITH_SIZE)) {
throw (new \Exception("The property rep_uid: '$tab_uid' is incorrect.")); if (empty($columns[$i]['fld_size'])) {
} else { throw (new Exception(G::LoadTranslation("ID_CAN_NOT_BE_EMPTY", ['fld_size'])));
throw (new \Exception("The property pmt_uid: '$tab_uid' is incorrect.")); }
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;
} }
/** /**

View File

@@ -1,8 +1,10 @@
<?php <?php
namespace ProcessMaker\Services\Api; namespace ProcessMaker\Services\Api;
use \ProcessMaker\Services\Api; use Exception;
use \Luracast\Restler\RestException; use Luracast\Restler\RestException;
use ProcessMaker\BusinessModel\Table as BusinessModelTable;
use ProcessMaker\Services\Api;
/** /**
* Pmtable Api Controller * Pmtable Api Controller
@@ -149,6 +151,7 @@ class Pmtable extends Api
* *
* @access protected * @access protected
* @class AccessControl {@permission PM_SETUP_PM_TABLES} * @class AccessControl {@permission PM_SETUP_PM_TABLES}
* @throws RestException
*/ */
public function doPutPmTable( public function doPutPmTable(
$pmt_uid, $pmt_uid,
@@ -156,9 +159,9 @@ class Pmtable extends Api
) { ) {
try { try {
$request_data['pmt_uid'] = $pmt_uid; $request_data['pmt_uid'] = $pmt_uid;
$oReportTable = new \ProcessMaker\BusinessModel\Table(); $pmTable = new BusinessModelTable();
$response = $oReportTable->updateTable($request_data); $response = $pmTable->updateTable($request_data);
} catch (\Exception $e) { } catch (Exception $e) {
throw (new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage())); throw (new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()));
} }
} }