PMCORE-3233

This commit is contained in:
Julio Cesar Laura Avendaño
2021-08-24 20:28:30 +00:00
parent c45f0bde07
commit e5b4254557
2 changed files with 137 additions and 48 deletions

View File

@@ -5056,11 +5056,10 @@ class WorkspaceTools
$processUid = '',
$gridKey = '',
$addTabUid = '',
$className = '',
$pathWorkspace,
int $start = 0,
int $limit = 10
) {
// Initialize DB connections
$this->initPropel();
$dbHost = explode(':', $this->dbHost);
config(['database.connections.workflow.host' => $dbHost[0]]);
@@ -5071,19 +5070,15 @@ class WorkspaceTools
config(['database.connections.workflow.port' => $dbHost[1]]);
}
//require file
set_include_path(get_include_path() . PATH_SEPARATOR . $pathWorkspace);
if (!file_exists($pathWorkspace . 'classes/' . $className . '.php')) {
throw new Exception("ERROR: " . $pathWorkspace . 'classes/' . $className . '.php' . " class file doesn't exit!");
}
require_once 'classes/model/AdditionalTables.php';
require_once $pathWorkspace . 'classes/' . $className . '.php';
//get fields
// Get fields and some specific field types
$fields = [];
$fieldsWithTypes = [];
$fieldTypes = [];
if ($addTabUid != '') {
$fields = Fields::where('ADD_TAB_UID', '=', $addTabUid)->get();
foreach ($fields as $field) {
$fieldsAux = Fields::where('ADD_TAB_UID', '=', $addTabUid)->get();
foreach ($fieldsAux as $field) {
$fields[] = $field->FLD_NAME;
$fieldsWithTypes[$field->FLD_NAME] = strtoupper($field->FLD_TYPE);
switch ($field->FLD_TYPE) {
case 'FLOAT':
case 'DOUBLE':
@@ -5096,10 +5091,11 @@ class WorkspaceTools
}
}
// Initialize variables
$context = Bootstrap::context();
$case = new Cases();
//select cases for this Process, ordered by APP_NUMBER
// Select cases of the related process, ordered by APP_NUMBER
$applications = Application::query()
->where('PRO_UID', '=', $processUid)
->where('APP_NUMBER', '>', 0)
@@ -5107,11 +5103,13 @@ class WorkspaceTools
->offset($start)
->limit($limit)
->get();
// Process applications selected
foreach ($applications as $application) {
//getting the case data
// Get case data
$appData = $case->unserializeData($application->APP_DATA);
//quick fix, map all empty values as NULL for Database
// Quick fix, map all empty values as NULL for Database
foreach ($appData as $appDataKey => $appDataValue) {
if (is_array($appDataValue) && count($appDataValue)) {
$j = key($appDataValue);
@@ -5126,10 +5124,10 @@ class WorkspaceTools
}
}
}
// normal fields
// Normal fields
$appData[$appDataKey] = $appDataValue === '' ? null : $appDataValue;
} else {
// grids
// Grids
if (is_array($appData[$appDataKey])) {
foreach ($appData[$appDataKey] as $dIndex => $dRow) {
if (is_array($dRow)) {
@@ -5144,22 +5142,30 @@ class WorkspaceTools
}
}
//populate data
// Populate data
if ($type === 'GRID') {
list($gridName, $gridUid) = explode('-', $gridKey);
$gridData = isset($appData[$gridName]) ? $appData[$gridName] : [];
foreach ($gridData as $i => $gridRow) {
try {
$obj = new $className();
$obj->fromArray($appData, BasePeer::TYPE_FIELDNAME);
$obj->setAppUid($application->APP_UID);
$obj->setAppNumber($application->APP_NUMBER);
if (method_exists($obj, 'setAppStatus')) {
$obj->setAppStatus($application->APP_STATUS);
}
$obj->fromArray(array_change_key_case($gridRow, CASE_UPPER), BasePeer::TYPE_FIELDNAME);
$obj->setRow($i);
$obj->save();
// Change keys to uppercase
$gridRow = array_change_key_case($gridRow, CASE_UPPER);
// Completing some required values in row data
$gridRow['APP_UID'] = $application->APP_UID;
$gridRow['APP_NUMBER'] = $application->APP_NUMBER;
$gridRow['APP_STATUS'] = $application->APP_STATUS;
$gridRow['ROW'] = $i;
// Build sections of the query
$fieldsSection = $this->buildFieldsSection($fields);
$valuesSection = $this->buildValuesSection($fieldsWithTypes, $gridRow);
// Build insert query
$query = "INSERT INTO `$tableName` ($fieldsSection) VALUES ($valuesSection);";
// Execute the query
DB::connection()->statement(DB::raw($query));
} catch (Exception $e) {
$context["message"] = $e->getMessage();
$context["tableName"] = $tableName;
@@ -5171,14 +5177,23 @@ class WorkspaceTools
}
} else {
try {
$obj = new $className();
$obj->fromArray(array_change_key_case($appData, CASE_UPPER), BasePeer::TYPE_FIELDNAME);
$obj->setAppUid($application->APP_UID);
$obj->setAppNumber($application->APP_NUMBER);
if (method_exists($obj, 'setAppStatus')) {
$obj->setAppStatus($application->APP_STATUS);
}
$obj->save();
// Change keys to uppercase
$appData = array_change_key_case($appData, CASE_UPPER);
// Completing some required values in case data
$appData['APP_UID'] = $application->APP_UID;
$appData['APP_NUMBER'] = $application->APP_NUMBER;
$appData['APP_STATUS'] = $application->APP_STATUS;
// Build sections of the query
$fieldsSection = $this->buildFieldsSection($fields);
$valuesSection = $this->buildValuesSection($fieldsWithTypes, $appData);
// Build insert query
$query = "INSERT INTO `$tableName` ($fieldsSection) VALUES ($valuesSection);";
// Execute the query
DB::connection()->statement(DB::raw($query));
} catch (Exception $e) {
$context["message"] = $e->getMessage();
$context["tableName"] = $tableName;
@@ -5186,7 +5201,6 @@ class WorkspaceTools
$message = 'Sql Execution';
Log::channel(':sqlExecution')->critical($message, Bootstrap::context($context));
}
unset($obj);
}
}
}
@@ -5280,4 +5294,72 @@ class WorkspaceTools
CLI::logging($e->getMessage() . PHP_EOL . PHP_EOL);
}
}
/**
* Build the fields section for the insert query
*
* @param array $fields
* @return string
*/
private function buildFieldsSection($fields)
{
// Transform records to single array
$fields = array_map(function($field) {
return "`{$field}`";
}, $fields);
return implode(', ', $fields);
}
/**
* Build values section for the insert query
*
* @param array $fieldsWithTypes
* @param array $caseData
* @return string
*/
private function buildValuesSection($fieldsWithTypes, $caseData)
{
// Initialize variables
$values = [];
// Sanitize each value in case data according to the field type
foreach ($fieldsWithTypes as $fieldName => $fieldType) {
// Get the value
$fieldName = strtoupper($fieldName);
$value = isset($caseData[$fieldName]) ? $caseData[$fieldName] : null;
// Sanitize data
switch ($fieldType) {
case 'BIGINT':
case 'INTEGER':
case 'SMALLINT':
case 'TINYINT':
case 'BOOLEAN':
$values[] = (int)$value;
break;
case 'DECIMAL':
case 'DOUBLE':
case 'FLOAT':
case 'REAL':
$values[] = (float)$value;
break;
case 'DATE':
case 'DATETIME':
case 'TIME':
if (strtotime($value) === false) {
$value = '0000-00-00 00:00:00';
}
$values[] = "'{$value}'";
break;
default: // char, mediumtext, varchar or another type
// Escape the strings
$values[] = DB::connection()->getPdo()->quote($value);
break;
}
}
// Convert to string separated by commas
return implode(', ', $values);
}
}