Exemplo n.º 1
0
 public function executeImportRestApiMerge(sfWebRequest $request)
 {
     $qa_generic = sfConfig::get("app_table_qa_generic");
     $qa_core = sfConfig::get("app_table_qa_core");
     // Retrieve test session id to update
     $testSessionId = $request->getParameter("id");
     // Retrieve $_GET
     $get_params['auth_token'] = $request->getGetParameter("auth_token");
     // Check if id parameter is empty
     if (empty($testSessionId)) {
         echo "{\"ok\":\"0\",\"errors\":{\"Parameters error\":\"Missing test session id parameter\"}}\n";
         exit;
     }
     // Check if auth_token parameter is empty
     if (empty($get_params['auth_token'])) {
         echo "{\"ok\":\"0\",\"errors\":{\"Parameters error\":\"Missing auth_token parameter\"}}\n";
         exit;
     }
     // Check authorized token exists, and retrieve user_id
     $sfGuardUserProfileObject = Doctrine_Core::getTable("sfGuardUserProfile")->findOneByToken($get_params['auth_token']);
     if (empty($sfGuardUserProfileObject)) {
         echo "{\"ok\":\"0\",\"errors\":{\"auth_token\":\"Authorized token is not valid\"}}\n";
         exit;
     }
     $user_id = $sfGuardUserProfileObject->getUserId();
     // Customize database connection to begin a transactionnal query
     $conn = Doctrine_Manager::getInstance()->getConnection("qa_generic");
     $conn->setAttribute(Doctrine_Core::ATTR_AUTOCOMMIT, FALSE);
     $conn->beginTransaction();
     // Update test_session table
     $testSession = Doctrine_Core::getTable("TestSession")->findOneById($testSessionId);
     $testSession->setUpdatedAt(date("Y-m-d H:i:s"));
     $testSession->save($conn);
     // Retrieve table_name_test_session_id from table_name
     $tableName = Doctrine_Core::getTable("TableName")->findOneByName("test_session");
     $tableNameTestSessionId = $tableName->getId();
     // Get file attachments id into an array
     $fileAttachmentIdList = array();
     $fileAttachments = Doctrine_Query::create()->select('*')->from('FileAttachment')->where('table_entry_id = ?', $testSessionId)->andWhere('table_name_id = ?', $tableNameTestSessionId)->execute();
     foreach ($fileAttachments as $fileAttachment) {
         $fileAttachmentIdList[] = $fileAttachment->getId();
     }
     // Concatenate directory path
     $dir_path = sfConfig::get('sf_upload_dir') . "/testsession_" . $testSessionId;
     $fileAttachmentResultIds = array();
     $similarFileFound = false;
     // Get all files sent
     $files = $request->getFiles();
     // Check if there is any report file to import
     if (empty($files)) {
         $conn->rollback();
         $conn->setAttribute(Doctrine_Core::ATTR_AUTOCOMMIT, TRUE);
         echo "{\"ok\":\"0\",\"errors\":\"Missing report file\"}\n";
         exit;
     }
     // Import each report file and register attachment files
     $report_file_found = false;
     foreach ($files as $key => $file) {
         $reportType = false;
         $fileName = $file['name'];
         $fileSize = $file['size'];
         $fileType = $file['type'];
         $fileError = $file['error'];
         $fileChecksum = sha1_file($file["tmp_name"]);
         $fileAttachmentResult = Doctrine_Query::create()->select('*')->from('FileAttachment')->where('table_entry_id = ?', $testSessionId)->andWhere('table_name_id = ?', $tableNameTestSessionId)->andWhere('checksum = ?', $fileChecksum)->execute();
         if (!empty($fileAttachmentResult[0])) {
             $fileAttachmentResultIds[] = $fileAttachmentResult[0]->getId();
             $similarFileFound = true;
         }
         // Check file error and file size
         if (!$fileError and $fileSize <= sfConfig::get('app_max_file_size', '10000000')) {
             if (!is_dir($dir_path)) {
                 mkdir($dir_path, 0777, true);
             }
             $dest_path = $dir_path . "/" . $fileName;
             $idx = 0;
             while (is_file($dest_path)) {
                 $idx++;
                 $dest_path = $dir_path . "/" . "(" . $idx . ")" . $fileName;
             }
             // Move file to uploads directory
             move_uploaded_file($file['tmp_name'], $dest_path);
             if ($idx == 0) {
                 $web_path = "/uploads" . "/testsession_" . $testSessionId . "/" . $fileName;
             } else {
                 $web_path = "/uploads" . "/testsession_" . $testSessionId . "/" . "(" . $idx . ")" . $fileName;
             }
             $fileAttachment = new FileAttachment();
             $fileAttachment->setName($fileName);
             $fileAttachment->setUserId($user_id);
             $fileAttachment->setUploadedAt(date("Y-m-d H:i:s"));
             $fileAttachment->setFilename($fileName);
             $fileAttachment->setFileSize($fileSize);
             $fileAttachment->setFileMimeType($fileType);
             $fileAttachment->setLink($web_path);
             $fileAttachment->setChecksum($fileChecksum);
             $fileAttachment->setTableNameId($tableNameTestSessionId);
             $fileAttachment->setTableEntryId($testSessionId);
             if ((preg_match("#\\.xml\$#i", $fileName) | preg_match("#\\.csv\$#i", $fileName)) & !preg_match("#attachment.?[0-9]*#i", $key)) {
                 $report_file_found = true;
                 $reportType = true;
                 $fileAttachment->setCategory(1);
             } else {
                 if (preg_match("#attachment.?[0-9]*#i", $key)) {
                     $fileAttachment->setCategory(2);
                 } else {
                     $conn->rollback();
                     $conn->setAttribute(Doctrine_Core::ATTR_AUTOCOMMIT, TRUE);
                     echo "{\"ok\":\"0\",\"errors\":\"Only upload files with the extension .xml or .csv\"}\n";
                     exit;
                 }
             }
             $fileAttachment->save($conn);
             // If it is an XML or CSV file, parse it and fill qa_generic database
             if ($reportType) {
                 if ($err_code = Import::file($dest_path, $testSessionId, $configurationId, $conn, true)) {
                     // Delete new files
                     $fileAttachments = Doctrine_Query::create()->select('*')->from('FileAttachment')->where('table_entry_id = ?', $testSessionId)->andWhere('table_name_id = ?', $tableNameTestSessionId)->andWhereNotIn('id', $fileAttachmentIdList)->execute();
                     foreach ($fileAttachments as $fileAttachment) {
                         unlink(sfConfig::get('sf_web_dir') . $fileAttachment->getLink());
                     }
                     $error_message = Import::getImportErrorMessage($err_code);
                     $conn->rollback();
                     $conn->setAttribute(Doctrine_Core::ATTR_AUTOCOMMIT, TRUE);
                     echo "{\"ok\":\"0\",\"errors\":\"File " . $fileName . " is not valid = " . $error_message . "\"}\n";
                     exit;
                 }
             }
         } else {
             // Delete new files
             $fileAttachments = Doctrine_Query::create()->select('*')->from('FileAttachment')->where('table_entry_id = ?', $testSessionId)->andWhere('table_name_id = ?', $tableNameTestSessionId)->andWhereNotIn('id', $fileAttachmentIdList)->execute();
             foreach ($fileAttachments as $fileAttachment) {
                 unlink(sfConfig::get('sf_web_dir') . $fileAttachment->getLink());
             }
             $conn->rollback();
             $conn->setAttribute(Doctrine_Core::ATTR_AUTOCOMMIT, TRUE);
             echo "{\"ok\":\"0\",\"errors\":\"File " . $fileName . " exceed maximum size\"}\n";
             exit;
         }
     }
     // If only attachment files have been found, cancel the new test session
     if (!$report_file_found) {
         $conn->rollback();
         $conn->setAttribute(Doctrine_Core::ATTR_AUTOCOMMIT, TRUE);
         echo "{\"ok\":\"0\",\"errors\":\"Missing report file\"}\n";
         exit;
     }
     if ($similarFileFound) {
         // Delete similar files and attachment entries
         $fileAttachments = Doctrine_Query::create()->select('*')->from('FileAttachment')->where('table_entry_id = ?', $testSessionId)->andWhere('table_name_id = ?', $tableNameTestSessionId)->andWhereIn('id', $fileAttachmentResultIds)->execute();
         foreach ($fileAttachments as $fileAttachment) {
             unlink(sfConfig::get('sf_web_dir') . $fileAttachment->getLink());
             $fileAttachment->delete($conn);
         }
     }
     $conn->commit();
     $conn->setAttribute(Doctrine_Core::ATTR_AUTOCOMMIT, TRUE);
     // Retrieve project name_slug, product name_slug, test environment name_slug and image name_slug
     $query = "SELECT i.name_slug image_name_slug, te.name_slug test_environment_name_slug, p.name_slug project_name_slug, pt.name_slug product_name_slug\n\t\t\t\t\tFROM " . $qa_generic . ".test_session ts\n\t\t\t\t\tJOIN " . $qa_generic . ".configuration c ON c.id = ts.configuration_id\n\t\t\t\t\tJOIN " . $qa_generic . ".image i ON i.id = c.image_id\n\t\t\t\t\tJOIN " . $qa_generic . ".test_environment te ON te.id = c.test_environment_id\n\t\t\t\t\tJOIN " . $qa_generic . ".project_to_product ptp ON ptp.id = c.project_to_product_id\n\t\t\t\t\tJOIN " . $qa_generic . ".project p ON p.id = ptp.project_id\n\t\t\t\t\tJOIN " . $qa_core . ".product_type pt ON pt.id = ptp.product_id\n\t\t\t\t\tWHERE ts.id = " . $testSessionId;
     $configInfo = Doctrine_Manager::getInstance()->getCurrentConnection()->execute($query)->fetch(PDO::FETCH_ASSOC);
     $projectNameSlug = $configInfo['project_name_slug'];
     $productNameSlug = $configInfo['product_name_slug'];
     $testEnvironmentNameSlug = $configInfo['test_environment_name_slug'];
     $imageNameSlug = $configInfo['image_name_slug'];
     // Return datas to CATS
     $url_to_return = $request->getUriPrefix() . $this->generateUrl("test_session", array('project' => $projectNameSlug, 'product' => $productNameSlug, 'environment' => $testEnvironmentNameSlug, 'image' => $imageNameSlug, 'id' => $testSessionId));
     echo "{\"ok\":\"1\",\"url\":\"" . $url_to_return . "\"}\n";
     // Return is done (with echo) so make sure nothing else will be sent
     exit;
 }