diff --git a/workflow/engine/src/BusinessModel/DataBaseConnection.php b/workflow/engine/src/BusinessModel/DataBaseConnection.php index 7735d5be3..a387275d8 100644 --- a/workflow/engine/src/BusinessModel/DataBaseConnection.php +++ b/workflow/engine/src/BusinessModel/DataBaseConnection.php @@ -180,43 +180,69 @@ class DataBaseConnection $oContent->removeContent( 'DBS_DESCRIPTION', "", $dbConnecionUid ); } - - public function testConnection($dataCon) + public function testConnection($dataCon, $returnArray = false) { $resp = array(); + $respTest = array(); $resp['resp'] = false; + $dataCon = array_change_key_case($dataCon, CASE_UPPER); + G::LoadClass( 'net' ); $Server = new \NET($dataCon['DBS_SERVER']); // STEP 1 : Resolving Host Name + $respTest['0'] = array(); + $respTest['0']['test'] = 'Resolving Host Name ' . $dataCon['DBS_SERVER']; if ($Server->getErrno() != 0) { - $resp['message'] = "Error Testting Connection: Resolving Host Name FAILED : " . $Server->error; - return $resp; + if ($returnArray) { + $respTest['0']['error'] = "Error Testting Connection: Resolving Host Name FAILED : " . $Server->error; + } else { + $resp['message'] = "Error Testting Connection: Resolving Host Name FAILED : " . $Server->error; + return $resp; + } } // STEP 2 : Checking port + $respTest['1'] = array(); + $respTest['1']['test'] = 'Checking port ' . $dataCon['DBS_PORT']; $Server->scannPort($dataCon['DBS_PORT']); if ($Server->getErrno() != 0) { - $resp['message'] = "Error Testting Connection: Checking port FAILED : " . $Server->error; - return $resp; + if ($returnArray) { + $respTest['1']['error'] = "Error Testting Connection: Checking port FAILED : " . $Server->error; + } else { + $resp['message'] = "Error Testting Connection: Checking port FAILED : " . $Server->error; + return $resp; + } } // STEP 3 : Trying to connect to host + $respTest['2'] = array(); + $respTest['2']['test'] = 'Trying to connect to host ' . $dataCon['DBS_SERVER'] . (($dataCon['DBS_PORT'] != '') ? ':'.$dataCon['DBS_PORT'] : ''); $Server->loginDbServer($dataCon['DBS_USERNAME'], $dataCon['DBS_PASSWORD']); $Server->setDataBase($dataCon['DBS_DATABASE_NAME'], $dataCon['DBS_PORT']); if ($Server->errno == 0) { $response = $Server->tryConnectServer($dataCon['DBS_TYPE']); if ($response->status != 'SUCCESS') { + if ($returnArray) { + $respTest['2']['error'] = "Error Testting Connection: Trying to connect to host FAILED : " . $Server->error; + } else { + $resp['message'] = "Error Testting Connection: Trying to connect to host FAILED : " . $Server->error; + return $resp; + } + } + } else { + if ($returnArray) { + $respTest['2']['error'] = "Error Testting Connection: Trying to connect to host FAILED : " . $Server->error; + } else { $resp['message'] = "Error Testting Connection: Trying to connect to host FAILED : " . $Server->error; return $resp; } - } else { - $resp['message'] = "Error Testting Connection: Trying to connect to host FAILED : " . $Server->error; - return $resp; } // STEP 4 : Trying to open database + $respTest['3'] = array(); + $respTest['3']['test'] = 'Trying to open database [' . $dataCon['DBS_DATABASE_NAME'] . ']'; $Server->loginDbServer($dataCon['DBS_USERNAME'], $dataCon['DBS_PASSWORD']); $Server->setDataBase($dataCon['DBS_DATABASE_NAME'], $dataCon['DBS_PORT']); if ($Server->errno == 0) { @@ -224,21 +250,37 @@ class DataBaseConnection if ($response->status == 'SUCCESS') { $response = $Server->tryOpenDataBase($dataCon['DBS_TYPE']); if ($response->status != 'SUCCESS') { + if ($returnArray) { + $respTest['3']['error'] = "Error Testting Connection: Trying to open database FAILED : " . $Server->error; + } else { + $resp['message'] = "Error Testting Connection: Trying to open database FAILED : " . $Server->error; + return $resp; + } + } + } else { + if ($returnArray) { + $respTest['3']['error'] = "Error Testting Connection: Trying to open database FAILED : " . $Server->error; + } else { $resp['message'] = "Error Testting Connection: Trying to open database FAILED : " . $Server->error; return $resp; } + } + } else { + if ($returnArray) { + $respTest['3']['error'] = "Error Testting Connection: Trying to open database FAILED : " . $Server->error; } else { $resp['message'] = "Error Testting Connection: Trying to open database FAILED : " . $Server->error; return $resp; } - } else { - $resp['message'] = "Error Testting Connection: Trying to open database FAILED : " . $Server->error; - return $resp; } - // CORRECT CONNECTION - $resp['resp'] = true; - return $resp; + if ($returnArray) { + return $respTest; + } else { + // CORRECT CONNECTION + $resp['resp'] = true; + return $resp; + } } } diff --git a/workflow/engine/src/Services/Api/ProcessMaker/Project/DataBaseConnection.php b/workflow/engine/src/Services/Api/ProcessMaker/Project/DataBaseConnection.php index 8d5ee8c9f..6043e2f16 100644 --- a/workflow/engine/src/Services/Api/ProcessMaker/Project/DataBaseConnection.php +++ b/workflow/engine/src/Services/Api/ProcessMaker/Project/DataBaseConnection.php @@ -54,6 +54,47 @@ class DataBaseConnection extends Api } } + /** + * @param string $projectUid {@min 1} {@max 32} + * @param array $request_data + * + * @param string $dbs_type {@from body} + * @param string $dbs_server {@from body} + * @param string $dbs_database_name {@from body} + * @param string $dbs_username {@from body} + * @param string $dbs_port {@from body} + * @param string $dbs_encode {@from body} + * @param string $dbs_password {@from body} + * @param string $dbs_description {@from body} + * @return array + * + * @author Brayan Pereyra (Cochalo) + * @copyright Colosa - Bolivia + * + * @url POST /:projectUid/database-connection/test + */ + public function doPostTestDataBaseConnection( + $projectUid, + $request_data, + $dbs_type, + $dbs_server, + $dbs_database_name, + $dbs_username, + $dbs_port, + $dbs_encode, + $dbs_password = '', + $dbs_description = '' + ) { + try { + $oDBConnection = new \BusinessModel\DataBaseConnection(); + $request_data['pro_uid'] = $projectUid; + $response = $oDBConnection->testConnection($request_data, true); + return $response; + } catch (\Exception $e) { + throw (new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage())); + } + } + /** * @param string $projectUid {@min 1} {@max 32} * @param array $request_data