/**
  * Import CSV file
  *
  * @param report_file_path The file path to import.
  * @param test_session_id The session's id.
  */
 public static function import_csv($report_file_path, $test_session_id, $conn, $merge_flag)
 {
     $qa_generic = sfConfig::get("app_table_qa_generic");
     // Retrieve table_name_id from table_name table
     $table_name_test_result = Doctrine_Core::getTable("TableName")->findOneByName("test_result");
     $table_name_test_result_id = $table_name_test_result->getId();
     $test_result_exists_flag = false;
     // Open the file
     $csv_report_file = @fopen($report_file_path, 'r');
     if ($csv_report_file) {
         // Get file first line
         $first_line = fgets($csv_report_file);
         // Detect delimiter
         if (preg_match("#,#", $first_line)) {
             $delimiter = ',';
         } else {
             if (preg_match("#;#", $first_line)) {
                 $delimiter = ';';
             } else {
                 return 2001;
             }
         }
         // Detect structure type
         // CSV old
         if (preg_match("#(?=.*Feature)(?=.*Check ?points)(?=.*Notes ?\\(bugs\\))(?=.*Status)#i", $first_line)) {
             // Return at the beginning of file
             fseek($csv_report_file, 0);
             // Determine order between different fields on the first line
             $idx = 0;
             $ref_tab = array();
             foreach (fgetcsv($csv_report_file, 0, $delimiter) as $element) {
                 if (preg_match("#^ ?Feature.?\$#i", $element)) {
                     $ref_tab[0] = $idx;
                 } elseif (preg_match("#^ ?Check ?points.?\$#i", $element)) {
                     $ref_tab[1] = $idx;
                 } elseif (preg_match("#^ ?Notes ?\\(bugs\\).?\$#i", $element)) {
                     $ref_tab[2] = $idx;
                 } elseif (preg_match("#^ ?Status.?\$#i", $element)) {
                     $ref_tab[3] = $idx;
                 } elseif (preg_match("#^ ?Duration.?\$#i", $element)) {
                     $ref_tab[4] = $idx;
                 } elseif (preg_match("#^ ?Bug.?\$#i", $element)) {
                     $ref_tab[5] = $idx;
                 }
                 $idx++;
             }
             // Go through file and fill datas
             $data_tab = array();
             while ($data_tab = fgetcsv($csv_report_file, 0, $delimiter)) {
                 if (count($data_tab) > 3) {
                     // Check if feature is not empty
                     if (empty($data_tab[$ref_tab[0]])) {
                         return 2101;
                     } else {
                         $feature = $data_tab[$ref_tab[0]];
                     }
                     // Check if check points is not empty
                     if (empty($data_tab[$ref_tab[1]])) {
                         return 2102;
                     } else {
                         $case_id = $data_tab[$ref_tab[1]];
                     }
                     // Check if status is not empty
                     if (empty($data_tab[$ref_tab[3]])) {
                         return 2103;
                     } else {
                         $status = $data_tab[$ref_tab[3]];
                     }
                     // Check if execution time is not empty
                     if (!empty($data_tab[$ref_tab[4]])) {
                         $executionTime = $data_tab[$ref_tab[4]];
                     } else {
                         $executionTime = 0;
                     }
                     // Check if bug list is not empty
                     if (!empty($data_tab[$ref_tab[5]])) {
                         $bugs = $data_tab[$ref_tab[5]];
                     } else {
                         $bugs = "";
                     }
                     // Write datas into qa_generic database
                     if (preg_match("#^ ?pass(ed)? ?\$#i", $status)) {
                         $decision_criteria_id = -1;
                     } else {
                         if (preg_match("#^ ?fail(ed)? ?\$#i", $status)) {
                             $decision_criteria_id = -2;
                         } else {
                             if (preg_match("#^ ?block(ed)? ?\$#i", $status)) {
                                 $decision_criteria_id = -3;
                             } else {
                                 if (preg_match("#^ ?defer(red)? ?\$#i", $status)) {
                                     $decision_criteria_id = -4;
                                 } else {
                                     if (preg_match("#^ ?not ?run ?\$#i", $status) || preg_match("#^ ?not_run ?\$#i", $status)) {
                                         $decision_criteria_id = -5;
                                     } else {
                                         return 2104;
                                     }
                                 }
                             }
                         }
                     }
                     // Status hard coded
                     $resultStatus = 0;
                     if ($merge_flag) {
                         // Retrieve test result id relying on test name and feature label, if it exists
                         $query = "SELECT tr.id tr_id\n\t\t\t\t\t\t\t\t\tFROM " . $qa_generic . ".test_result tr\n\t\t\t\t\t\t\t\t\t\tJOIN " . $qa_generic . ".table_name tn ON tn.name = 'test_result'\n\t\t\t\t\t\t\t\t\t\tJOIN " . $qa_generic . ".complementary_tool_relation ctr ON ctr.table_name_id = tn.id\n\t\t\t\t\t\t\t\t\t\t\tAND ctr.table_entry_id = tr.id\n\t\t\t\t\t\t\t\t\t\t\tAND ctr.label = '" . addslashes($feature) . "'\n\t\t\t\t\t\t\t\t\tWHERE tr.test_session_id = " . $test_session_id . "\n\t\t\t\t\t\t\t\t\t\tAND tr.name = '" . addslashes($case_id) . "'";
                         $result = Doctrine_Manager::getInstance()->getCurrentConnection()->execute($query)->fetch(PDO::FETCH_ASSOC);
                         if (!empty($result)) {
                             $test_result_exists_flag = true;
                             $testResultId = $result['tr_id'];
                         }
                     }
                     // Write into test_result table
                     if ($test_result_exists_flag) {
                         $testResult = Doctrine_Core::getTable("TestResult")->findOneById($testResultId);
                     } else {
                         $testResult = new TestResult();
                         $testResult->setName($case_id);
                         $testResult->setTestSessionId($test_session_id);
                     }
                     $testResult->setDecisionCriteriaId($decision_criteria_id);
                     $testResult->setComplement($test_case);
                     $testResult->setComment($comment);
                     $testResult->setStatus($resultStatus);
                     $testResult->setExecutionTime($executionTime);
                     $testResult->setBugs($bugs);
                     $testResult->save($conn);
                     // Retrieve test_result id created
                     $test_result_id = $testResult->getId();
                     // Category = Feature = 1
                     $category = 1;
                     // Write into complementary_tool_relation table
                     if (!$test_result_exists_flag) {
                         $complementaryToolRelation = new ComplementaryToolRelation();
                         $complementaryToolRelation->setLabel($feature);
                         $complementaryToolRelation->setTableNameId($table_name_test_result_id);
                         $complementaryToolRelation->setTableEntryId($test_result_id);
                         $complementaryToolRelation->setCategory($category);
                         $complementaryToolRelation->save($conn);
                     }
                 }
                 $data_tab = array();
                 $test_result_exists_flag = false;
             }
         } elseif (preg_match("#(?=.*Feature)(?=.*Check ?points)(?=.*Notes ?\\(bugs\\))(?=.*Pass)(?=.*Fail)(?=.*N/A)#i", $first_line)) {
             // Return at the beginning of file
             fseek($csv_report_file, 0);
             // Determine order between different fields on the first line
             $idx = 0;
             $ref_tab = array();
             foreach (fgetcsv($csv_report_file, 0, $delimiter) as $element) {
                 if (preg_match("#^ ?Feature.?\$#i", $element)) {
                     $ref_tab[0] = $idx;
                 } elseif (preg_match("#^ ?Check ?points.?\$#i", $element)) {
                     $ref_tab[1] = $idx;
                 } elseif (preg_match("#^ ?Notes ?\\(bugs\\).?\$#i", $element)) {
                     $ref_tab[2] = $idx;
                 } elseif (preg_match("#^ ?Pass.?\$#i", $element)) {
                     $ref_tab[3] = $idx;
                 } elseif (preg_match("#^ ?Fail.?\$#i", $element)) {
                     $ref_tab[4] = $idx;
                 } elseif (preg_match("#^ ?N/?A.?\$#i", $element)) {
                     $ref_tab[5] = $idx;
                 } elseif (preg_match("#^ ?Defer(red)?.?\$#i", $element)) {
                     $ref_tab[8] = $idx;
                 } elseif (preg_match("#^ ?Not ?run.?\$#i", $element) || preg_match("#^ ?Not_run.?\$#i", $element)) {
                     $ref_tab[9] = $idx;
                 } elseif (preg_match("#^ ?Duration.?\$#i", $element)) {
                     $ref_tab[6] = $idx;
                 } elseif (preg_match("#^ ?Bug.?\$#i", $element)) {
                     $ref_tab[7] = $idx;
                 }
                 $idx++;
             }
             // Go through file and fill datas
             $data_tab = array();
             while ($data_tab = fgetcsv($csv_report_file, 0, $delimiter)) {
                 if (count($data_tab) > 5) {
                     // Check if feature is not empty
                     if (empty($data_tab[$ref_tab[0]])) {
                         return 2201;
                     } else {
                         $feature = $data_tab[$ref_tab[0]];
                     }
                     // Check if case_id is not empty
                     if (empty($data_tab[$ref_tab[1]])) {
                         return 2202;
                     } else {
                         $case_id = $data_tab[$ref_tab[1]];
                     }
                     // Check if bug list is not empty
                     if (empty($data_tab[$ref_tab[7]])) {
                         $bugs = "";
                     } else {
                         $bugs = $data_tab[$ref_tab[7]];
                     }
                     $comment = empty($data_tab[$ref_tab[2]]) ? " " : $data_tab[$ref_tab[2]];
                     if (!($data_tab[$ref_tab[3]] == "1" or $data_tab[$ref_tab[4]] == "1" or $data_tab[$ref_tab[5]] == "1" or $data_tab[$ref_tab[8]] == "1" or $data_tab[$ref_tab[9]] == "1")) {
                         return 2203;
                     }
                     $pass = empty($data_tab[$ref_tab[3]]) ? "" : $data_tab[$ref_tab[3]];
                     $fail = empty($data_tab[$ref_tab[4]]) ? "" : $data_tab[$ref_tab[4]];
                     $block = empty($data_tab[$ref_tab[5]]) ? "" : $data_tab[$ref_tab[5]];
                     $deferred = empty($data_tab[$ref_tab[8]]) ? "" : $data_tab[$ref_tab[8]];
                     $notrun = empty($data_tab[$ref_tab[9]]) ? "" : $data_tab[$ref_tab[9]];
                     if ($pass == "1") {
                         $decision_criteria_id = -1;
                     } elseif ($fail == "1") {
                         $decision_criteria_id = -2;
                     } elseif ($block == "1") {
                         $decision_criteria_id = -3;
                     } elseif ($deferred == "1") {
                         $decision_criteria_id = -4;
                     } elseif ($notrun == "1") {
                         $decision_criteria_id = -5;
                     }
                     if (!empty($data_tab[$ref_tab[6]])) {
                         $executionTime = $data_tab[$ref_tab[6]];
                     } else {
                         $executionTime = 0;
                     }
                     // Status hard coded
                     $status = 0;
                     if ($merge_flag) {
                         // Retrieve test result id relying on test name and feature label, if it exists
                         $query = "SELECT tr.id tr_id\n\t\t\t\t\t\t\t\t\tFROM " . $qa_generic . ".test_result tr\n\t\t\t\t\t\t\t\t\t\tJOIN " . $qa_generic . ".table_name tn ON tn.name = 'test_result'\n\t\t\t\t\t\t\t\t\t\tJOIN " . $qa_generic . ".complementary_tool_relation ctr ON ctr.table_name_id = tn.id\n\t\t\t\t\t\t\t\t\t\t\tAND ctr.table_entry_id = tr.id\n\t\t\t\t\t\t\t\t\t\t\tAND ctr.label = '" . addslashes($feature) . "'\n\t\t\t\t\t\t\t\t\tWHERE tr.test_session_id = " . $test_session_id . "\n\t\t\t\t\t\t\t\t\t\tAND tr.name = '" . addslashes($case_id) . "'";
                         $result = Doctrine_Manager::getInstance()->getCurrentConnection()->execute($query)->fetch(PDO::FETCH_ASSOC);
                         if (!empty($result)) {
                             $test_result_exists_flag = true;
                             $testResultId = $result['tr_id'];
                         }
                     }
                     // Write into test_result table
                     if ($test_result_exists_flag) {
                         $testResult = Doctrine_Core::getTable("TestResult")->findOneById($testResultId);
                     } else {
                         $testResult = new TestResult();
                         $testResult->setName($case_id);
                         $testResult->setTestSessionId($test_session_id);
                     }
                     $testResult->setDecisionCriteriaId($decision_criteria_id);
                     $testResult->setComplement($test_case);
                     $testResult->setComment($comment);
                     $testResult->setStatus($status);
                     $testResult->setExecutionTime($executionTime);
                     $testResult->setBugs($bugs);
                     $testResult->save($conn);
                     // Retrieve test_result id created
                     $test_result_id = $testResult->getId();
                     // Category = Feature = 1
                     $category = 1;
                     // Write into complementary_tool_relation table
                     if (!$test_result_exists_flag) {
                         $complementaryToolRelation = new ComplementaryToolRelation();
                         $complementaryToolRelation->setLabel($feature);
                         $complementaryToolRelation->setTableNameId($table_name_test_result_id);
                         $complementaryToolRelation->setTableEntryId($test_result_id);
                         $complementaryToolRelation->setCategory($category);
                         $complementaryToolRelation->save($conn);
                     }
                 }
                 $data_tab = array();
                 $test_result_exists_flag = false;
             }
         } elseif (preg_match("#(?=.*Feature)(?=.*Case ?id)(?=.*Check ?points)(?=.*Notes)(?=.*Pass)(?=.*Fail)(?=.*N/A)#i", $first_line)) {
             // Return at the beginning of file
             fseek($csv_report_file, 0);
             // Determine order between different fields on the first line
             $idx = 0;
             $ref_tab = array();
             foreach (fgetcsv($csv_report_file, 0, $delimiter) as $element) {
                 if (preg_match("#^ ?Feature.?\$#i", $element)) {
                     $ref_tab[0] = $idx;
                 } elseif (preg_match("#^ ?Case ?id.?\$#i", $element)) {
                     $ref_tab[1] = $idx;
                 } elseif (preg_match("#^ ?Check ?points.?\$#i", $element)) {
                     $ref_tab[2] = $idx;
                 } elseif (preg_match("#^ ?Notes.?\$#i", $element)) {
                     $ref_tab[3] = $idx;
                 } elseif (preg_match("#^ ?Pass.?\$#i", $element)) {
                     $ref_tab[4] = $idx;
                 } elseif (preg_match("#^ ?Fail.?\$#i", $element)) {
                     $ref_tab[5] = $idx;
                 } elseif (preg_match("#^ ?N/?A.?\$#i", $element)) {
                     $ref_tab[6] = $idx;
                 } elseif (preg_match("#^ ?Defer(red)?.?\$#i", $element)) {
                     $ref_tab[9] = $idx;
                 } elseif (preg_match("#^ ?Not ?run.?\$#i", $element) || preg_match("#^ ?Not_run.?\$#i", $element)) {
                     $ref_tab[10] = $idx;
                 } elseif (preg_match("#^ ?Duration.?\$#i", $element)) {
                     $ref_tab[7] = $idx;
                 } elseif (preg_match("#^ ?Bug.?\$#i", $element)) {
                     $ref_tab[8] = $idx;
                 }
                 $idx++;
             }
             // Go through file and fill datas
             $data_tab = array();
             while ($data_tab = fgetcsv($csv_report_file, 0, $delimiter)) {
                 if (count($data_tab) > 6) {
                     // Check if feature is not empty
                     if (empty($data_tab[$ref_tab[0]])) {
                         return 2301;
                     } else {
                         $feature = $data_tab[$ref_tab[0]];
                     }
                     // Check if case_id is not empty
                     if (empty($data_tab[$ref_tab[1]])) {
                         return 2302;
                     } else {
                         $case_id = $data_tab[$ref_tab[1]];
                     }
                     $test_case = empty($data_tab[$ref_tab[2]]) ? " " : $data_tab[$ref_tab[2]];
                     $comment = empty($data_tab[$ref_tab[3]]) ? " " : $data_tab[$ref_tab[3]];
                     if (!($data_tab[$ref_tab[4]] == "1" or $data_tab[$ref_tab[5]] == "1" or $data_tab[$ref_tab[6]] == "1")) {
                         return 2303;
                     }
                     $pass = empty($data_tab[$ref_tab[4]]) ? "" : $data_tab[$ref_tab[4]];
                     $fail = empty($data_tab[$ref_tab[5]]) ? "" : $data_tab[$ref_tab[5]];
                     $block = empty($data_tab[$ref_tab[6]]) ? "" : $data_tab[$ref_tab[6]];
                     $deferred = empty($data_tab[$ref_tab[9]]) ? "" : $data_tab[$ref_tab[9]];
                     $notrun = empty($data_tab[$ref_tab[10]]) ? "" : $data_tab[$ref_tab[10]];
                     if ($pass == "1") {
                         $decision_criteria_id = -1;
                     } elseif ($fail == "1") {
                         $decision_criteria_id = -2;
                     } elseif ($block == "1") {
                         $decision_criteria_id = -3;
                     } elseif ($deferred == "1") {
                         $decision_criteria_id = -4;
                     } elseif ($notrun == "1") {
                         $decision_criteria_id = -5;
                     }
                     // Check if execution time is not empty
                     if (!empty($data_tab[$ref_tab[7]])) {
                         $executionTime = $data_tab[$ref_tab[7]];
                     } else {
                         $executionTime = 0;
                     }
                     // Check if bug list is not empty
                     if (empty($data_tab[$ref_tab[8]])) {
                         $bugs = "";
                     } else {
                         $bugs = $data_tab[$ref_tab[8]];
                     }
                     // Status hard coded
                     $status = 0;
                     if ($merge_flag) {
                         // Retrieve test result id relying on test name and feature label, if it exists
                         $query = "SELECT tr.id tr_id\n\t\t\t\t\t\t\t\t\tFROM " . $qa_generic . ".test_result tr\n\t\t\t\t\t\t\t\t\t\tJOIN " . $qa_generic . ".table_name tn ON tn.name = 'test_result'\n\t\t\t\t\t\t\t\t\t\tJOIN " . $qa_generic . ".complementary_tool_relation ctr ON ctr.table_name_id = tn.id\n\t\t\t\t\t\t\t\t\t\t\tAND ctr.table_entry_id = tr.id\n\t\t\t\t\t\t\t\t\t\t\tAND ctr.label = '" . addslashes($feature) . "'\n\t\t\t\t\t\t\t\t\tWHERE tr.test_session_id = " . $test_session_id . "\n\t\t\t\t\t\t\t\t\t\tAND tr.name = '" . addslashes($case_id) . "'";
                         $result = Doctrine_Manager::getInstance()->getCurrentConnection()->execute($query)->fetch(PDO::FETCH_ASSOC);
                         if (!empty($result)) {
                             $test_result_exists_flag = true;
                             $testResultId = $result['tr_id'];
                         }
                     }
                     // Write into test_result table
                     if ($test_result_exists_flag) {
                         $testResult = Doctrine_Core::getTable("TestResult")->findOneById($testResultId);
                     } else {
                         $testResult = new TestResult();
                         $testResult->setName($case_id);
                         $testResult->setTestSessionId($test_session_id);
                     }
                     $testResult->setDecisionCriteriaId($decision_criteria_id);
                     $testResult->setComplement($test_case);
                     $testResult->setComment($comment);
                     $testResult->setStatus($status);
                     $testResult->setExecutionTime($executionTime);
                     $testResult->setBugs($bugs);
                     $testResult->save($conn);
                     // Retrieve test_result id created
                     $test_result_id = $testResult->getId();
                     // Category = Feature = 1
                     $category = 1;
                     // Write into complementary_tool_relation table
                     if (!$test_result_exists_flag) {
                         $complementaryToolRelation = new ComplementaryToolRelation();
                         $complementaryToolRelation->setLabel($feature);
                         $complementaryToolRelation->setTableNameId($table_name_test_result_id);
                         $complementaryToolRelation->setTableEntryId($test_result_id);
                         $complementaryToolRelation->setCategory($category);
                         $complementaryToolRelation->save($conn);
                     }
                 }
                 $data_tab = array();
                 $test_result_exists_flag = false;
             }
         } elseif (preg_match("#(?=.*Feature)(?=.*Case ?Id)(?=.*Test ?Case)(?=.*Pass)(?=.*Fail)(?=.*N/A)(?=.*Comment)#i", $first_line)) {
             // Return at the beginning of file
             fseek($csv_report_file, 0);
             // Determine order between different fields on the first line
             $idx = 0;
             $ref_tab = array();
             foreach (fgetcsv($csv_report_file, 0, $delimiter) as $element) {
                 if (preg_match("#^ ?Feature.?\$#i", $element)) {
                     $ref_tab[0] = $idx;
                 } elseif (preg_match("#^ ?Case ?Id.?\$#i", $element)) {
                     $ref_tab[1] = $idx;
                 } elseif (preg_match("#^ ?Test ?Case.?\$#i", $element)) {
                     $ref_tab[2] = $idx;
                 } elseif (preg_match("#^ ?Pass.?\$#i", $element)) {
                     $ref_tab[3] = $idx;
                 } elseif (preg_match("#^ ?Fail.?\$#i", $element)) {
                     $ref_tab[4] = $idx;
                 } elseif (preg_match("#^ ?N/?A.?\$#i", $element)) {
                     $ref_tab[5] = $idx;
                 } elseif (preg_match("#^ ?Measured.?\$#i", $element)) {
                     $ref_tab[6] = $idx;
                 } elseif (preg_match("#^ ?Comment.?\$#i", $element)) {
                     $ref_tab[7] = $idx;
                 } elseif (preg_match("#^ ?Measurement ?Name.?\$#i", $element)) {
                     $ref_tab[8] = $idx;
                 } elseif (preg_match("#^ ?Value.?\$#i", $element)) {
                     $ref_tab[9] = $idx;
                 } elseif (preg_match("#^ ?Unit.?\$#i", $element)) {
                     $ref_tab[10] = $idx;
                 } elseif (preg_match("#^ ?Target.?\$#i", $element)) {
                     $ref_tab[11] = $idx;
                 } elseif (preg_match("#^ ?Failure.?\$#i", $element)) {
                     $ref_tab[12] = $idx;
                 } elseif (preg_match("#^ ?Duration.?\$#i", $element)) {
                     $ref_tab[13] = $idx;
                 } elseif (preg_match("#^ ?Bug.?\$#i", $element)) {
                     $ref_tab[14] = $idx;
                 }
                 $idx++;
             }
             // Go through file and fill datas
             $data_tab = array();
             while ($data_tab = fgetcsv($csv_report_file, 0, $delimiter)) {
                 if (count($data_tab) > 6) {
                     // Check if feature is not empty
                     if (empty($data_tab[$ref_tab[0]])) {
                         return 2401;
                     } else {
                         $feature = $data_tab[$ref_tab[0]];
                     }
                     // Check if case_id is not empty
                     if (empty($data_tab[$ref_tab[1]])) {
                         return 2402;
                     } else {
                         $case_id = $data_tab[$ref_tab[1]];
                     }
                     if (!($data_tab[$ref_tab[3]] == "1" or $data_tab[$ref_tab[4]] == "1" or $data_tab[$ref_tab[5]] == "1" or $data_tab[$ref_tab[6]] == "1")) {
                         return 2403;
                     }
                     $test_case = empty($data_tab[$ref_tab[2]]) ? " " : $data_tab[$ref_tab[2]];
                     $pass = empty($data_tab[$ref_tab[3]]) ? "" : $data_tab[$ref_tab[3]];
                     $fail = empty($data_tab[$ref_tab[4]]) ? "" : $data_tab[$ref_tab[4]];
                     $block = empty($data_tab[$ref_tab[5]]) ? "" : $data_tab[$ref_tab[5]];
                     $deferred = empty($data_tab[$ref_tab[15]]) ? "" : $data_tab[$ref_tab[15]];
                     $notrun = empty($data_tab[$ref_tab[16]]) ? "" : $data_tab[$ref_tab[16]];
                     $measured = empty($data_tab[$ref_tab[6]]) ? "" : $data_tab[$ref_tab[6]];
                     $comment = empty($data_tab[$ref_tab[7]]) ? " " : $data_tab[$ref_tab[7]];
                     $measurement_name = empty($data_tab[$ref_tab[8]]) ? "" : $data_tab[$ref_tab[8]];
                     $value = empty($data_tab[$ref_tab[9]]) ? "" : preg_replace('#(,)#', '.', $data_tab[$ref_tab[9]]);
                     $unit = empty($data_tab[$ref_tab[10]]) ? " " : $data_tab[$ref_tab[10]];
                     $target = empty($data_tab[$ref_tab[11]]) ? "" : preg_replace('#(,)#', '.', $data_tab[$ref_tab[11]]);
                     $failure = empty($data_tab[$ref_tab[12]]) ? "" : preg_replace('#(,)#', '.', $data_tab[$ref_tab[12]]);
                     $executionTime = empty($data_tab[$ref_tab[13]]) ? 0 : $data_tab[$ref_tab[13]];
                     $bugs = empty($data_tab[$ref_tab[14]]) ? "" : $data_tab[$ref_tab[14]];
                     preg_replace('#(,)#', '.', $data_tab[$ref_tab[9]]);
                     // Write datas into qa_generic database
                     if ($pass == "1") {
                         $decision_criteria_id = -1;
                     } elseif ($fail == "1") {
                         $decision_criteria_id = -2;
                     } elseif ($block == "1") {
                         $decision_criteria_id = -3;
                     } elseif ($deferred == "1") {
                         $decision_criteria_id = -4;
                     } elseif ($notrun == "1") {
                         $decision_criteria_id = -5;
                     } elseif ($measured == "1") {
                         if (empty($failure)) {
                             if ($value < $target) {
                                 $decision_criteria_id = -2;
                             } else {
                                 $decision_criteria_id = -1;
                             }
                         } else {
                             if ($failure > $target) {
                                 if ($value < $failure) {
                                     $decision_criteria_id = -1;
                                 } else {
                                     $decision_criteria_id = -2;
                                 }
                             } elseif ($failure < $target) {
                                 if ($value > $failure) {
                                     $decision_criteria_id = -1;
                                 } else {
                                     $decision_criteria_id = -2;
                                 }
                             } else {
                                 if ($value < $target) {
                                     $decision_criteria_id = -2;
                                 } else {
                                     $decision_criteria_id = -1;
                                 }
                             }
                         }
                     }
                     // Status hard coded
                     $status = 0;
                     if ($merge_flag) {
                         // Retrieve test result id relying on test name and feature label, if it exists
                         $query = "SELECT tr.id tr_id\n\t\t\t\t\t\t\t\t\tFROM " . $qa_generic . ".test_result tr\n\t\t\t\t\t\t\t\t\t\tJOIN " . $qa_generic . ".table_name tn ON tn.name = 'test_result'\n\t\t\t\t\t\t\t\t\t\tJOIN " . $qa_generic . ".complementary_tool_relation ctr ON ctr.table_name_id = tn.id\n\t\t\t\t\t\t\t\t\t\t\tAND ctr.table_entry_id = tr.id\n\t\t\t\t\t\t\t\t\t\t\tAND ctr.label = '" . addslashes($feature) . "'\n\t\t\t\t\t\t\t\t\tWHERE tr.test_session_id = " . $test_session_id . "\n\t\t\t\t\t\t\t\t\t\tAND tr.name = '" . addslashes($case_id) . "'";
                         $result = Doctrine_Manager::getInstance()->getCurrentConnection()->execute($query)->fetch(PDO::FETCH_ASSOC);
                         if (!empty($result)) {
                             $test_result_exists_flag = true;
                             $testResultId = $result['tr_id'];
                         }
                     }
                     // Write into test_result table
                     if ($test_result_exists_flag) {
                         $measures = Doctrine_Query::create()->select('*')->from('Measure')->where('test_result_id = ?', $testResultId)->execute();
                         foreach ($measures as $measure) {
                             $measure->delete($conn);
                         }
                         $testResult = Doctrine_Core::getTable("TestResult")->findOneById($testResultId);
                     } else {
                         $testResult = new TestResult();
                         $testResult->setName($case_id);
                         $testResult->setTestSessionId($test_session_id);
                     }
                     $testResult->setDecisionCriteriaId($decision_criteria_id);
                     $testResult->setComplement($test_case);
                     $testResult->setComment($comment);
                     $testResult->setStatus($status);
                     $testResult->setExecutionTime($executionTime);
                     $testResult->setBugs($bugs);
                     $testResult->save($conn);
                     // Retrieve test_result id created
                     $test_result_id = $testResult->getId();
                     // Category = Feature = 1
                     $category = 1;
                     // Write into complementary_tool_relation table
                     if (!$test_result_exists_flag) {
                         $complementaryToolRelation = new ComplementaryToolRelation();
                         $complementaryToolRelation->setLabel($feature);
                         $complementaryToolRelation->setTableNameId($table_name_test_result_id);
                         $complementaryToolRelation->setTableEntryId($test_result_id);
                         $complementaryToolRelation->setCategory($category);
                         $complementaryToolRelation->save($conn);
                     }
                     // Write into measure table
                     if (!empty($measurement_name) and !empty($value)) {
                         $measureObject = new Measure();
                         $measureObject->setTestResultId($test_result_id);
                         $measureObject->setValue($value);
                         $measureObject->setUnit($unit);
                         $measureObject->setDescription($measurement_name);
                         $measureObject->setCategory(1);
                         $measureObject->setOperator(1);
                         $measureObject->save($conn);
                         if (!empty($target)) {
                             $measureObject = new Measure();
                             $measureObject->setTestResultId($test_result_id);
                             $measureObject->setValue($target);
                             $measureObject->setUnit($unit);
                             $measureObject->setDescription($measurement_name);
                             $measureObject->setCategory(2);
                             $measureObject->setOperator(1);
                             $measureObject->save($conn);
                         }
                         if (!empty($failure)) {
                             $measureObject = new Measure();
                             $measureObject->setTestResultId($test_result_id);
                             $measureObject->setValue($failure);
                             $measureObject->setUnit($unit);
                             $measureObject->setDescription($measurement_name);
                             $measureObject->setCategory(3);
                             $measureObject->setOperator(1);
                             $measureObject->save($conn);
                         }
                     }
                 }
                 $data_tab = array();
                 $test_result_exists_flag = false;
             }
         } elseif (preg_match("#(?=.*Name)(?=.*\\b ?Status ?\\b)#i", $first_line)) {
             // Return at the beginning of file
             fseek($csv_report_file, 0);
             // Determine order between different fields on the first line
             $idx = 0;
             $ref_tab = array();
             $entityTypeFound = false;
             foreach (fgetcsv($csv_report_file, 0, $delimiter) as $element) {
                 if (preg_match("#^ ?Component.?\$#i", $element)) {
                     $ref_tab[0] = $idx;
                 } elseif (preg_match("#^ ?Name.?\$#i", $element)) {
                     $ref_tab[1] = $idx;
                 } elseif (preg_match("#^ ?Status.?\$#i", $element)) {
                     $ref_tab[2] = $idx;
                 } elseif (preg_match("#^ ?Feature.?\$#i", $element)) {
                     $ref_tab[3] = $idx;
                 } elseif (preg_match("#^ ?Description.?\$#i", $element)) {
                     $ref_tab[4] = $idx;
                 } elseif (preg_match("#^ ?Comment.?\$#i", $element)) {
                     $ref_tab[5] = $idx;
                 } elseif (preg_match("#^ ?Bug.?\$#i", $element)) {
                     $ref_tab[6] = $idx;
                 } elseif (preg_match("#^ ?Package.?\$#i", $element)) {
                     $ref_tab[7] = $idx;
                 } elseif (preg_match("#^ ?Measurement ?Name.?\$#i", $element)) {
                     $ref_tab[8] = $idx;
                 } elseif (preg_match("#^ ?Value.?\$#i", $element)) {
                     $ref_tab[9] = $idx;
                 } elseif (preg_match("#^ ?Unit.?\$#i", $element)) {
                     $ref_tab[10] = $idx;
                 } elseif (preg_match("#^ ?Target.?\$#i", $element)) {
                     $ref_tab[11] = $idx;
                 } elseif (preg_match("#^ ?Failure.?\$#i", $element)) {
                     $ref_tab[12] = $idx;
                 } elseif (preg_match("#^ ?StepActualResult.?\$#i", $element)) {
                     $ref_tab[13] = $idx;
                 } elseif (preg_match("# ?EntityType.?#i", $element)) {
                     $ref_tab[14] = $idx;
                     $entityTypeFound = true;
                 } elseif (preg_match("#^ ?Duration.?\$#i", $element)) {
                     $ref_tab[15] = $idx;
                 } elseif (preg_match("#^ ?StepNotes.?\$#i", $element)) {
                     $ref_tab[16] = $idx;
                 }
                 $idx++;
             }
             if (!isset($ref_tab[1])) {
                 return 2501;
             }
             if (!isset($ref_tab[2])) {
                 return 2502;
             }
             if (!isset($ref_tab[0]) && !isset($ref_tab[7])) {
                 return 2509;
             }
             // Go through file and fill datas
             $data_tab = array();
             while ($data_tab = fgetcsv($csv_report_file, 0, $delimiter)) {
                 if (count($data_tab) > 2) {
                     // If 'EntityType' label is found, pass over 'StepRunResult' lines
                     if ($entityTypeFound) {
                         if (empty($data_tab[$ref_tab[14]])) {
                             return 2503;
                         } elseif (!preg_match("#^ ?TestScriptAssignment.?\$#i", $data_tab[$ref_tab[14]])) {
                             continue;
                         }
                     }
                     // Check if feature is not empty
                     if (empty($data_tab[$ref_tab[0]]) and empty($data_tab[$ref_tab[7]])) {
                         return 2504;
                     } else {
                         if (!empty($data_tab[$ref_tab[0]])) {
                             $feature = $data_tab[$ref_tab[0]];
                         } else {
                             $feature = $data_tab[$ref_tab[7]];
                         }
                     }
                     // If 'EntityType' label is found, keep last two string (delimited by '|') in the feature label
                     if ($entityTypeFound) {
                         $feat_delimiter = '|';
                         $feature_tab = explode($feat_delimiter, $feature);
                         $tab_length = count($feature_tab);
                         $feature = $feature_tab[$tab_length - 2] . $feat_delimiter . $feature_tab[$tab_length - 1];
                     }
                     // Check if case_id is not empty
                     if (empty($data_tab[$ref_tab[1]])) {
                         return 2506;
                     } else {
                         $case_id = $data_tab[$ref_tab[1]];
                     }
                     // Check if status is not empty
                     if (empty($data_tab[$ref_tab[2]])) {
                         return 2507;
                     } else {
                         $status = $data_tab[$ref_tab[2]];
                     }
                     $test_case = empty($data_tab[$ref_tab[4]]) ? " " : $data_tab[$ref_tab[4]];
                     $comment = empty($data_tab[$ref_tab[5]]) ? "" : $data_tab[$ref_tab[5]];
                     // $bug = (empty($data_tab[$ref_tab[6]])) ? "" : $data_tab[$ref_tab[6]];
                     $step_actual_result = empty($data_tab[$ref_tab[13]]) ? "" : $data_tab[$ref_tab[13]];
                     $step_notes = empty($data_tab[$ref_tab[16]]) ? "" : $data_tab[$ref_tab[16]];
                     $measurement_name = empty($data_tab[$ref_tab[8]]) ? "" : $data_tab[$ref_tab[8]];
                     $value = empty($data_tab[$ref_tab[9]]) ? "" : preg_replace('#(,)#', '.', $data_tab[$ref_tab[9]]);
                     $unit = empty($data_tab[$ref_tab[10]]) ? " " : $data_tab[$ref_tab[10]];
                     $target = empty($data_tab[$ref_tab[11]]) ? "" : preg_replace('#(,)#', '.', $data_tab[$ref_tab[11]]);
                     $failure = empty($data_tab[$ref_tab[12]]) ? "" : preg_replace('#(,)#', '.', $data_tab[$ref_tab[12]]);
                     $executionTime = empty($data_tab[$ref_tab[15]]) ? 0 : $data_tab[$ref_tab[12]];
                     // Concatenate notes
                     if (empty($comment)) {
                         if (empty($step_notes)) {
                             $notes = "";
                         } else {
                             $notes = $step_notes;
                         }
                     } else {
                         if (empty($step_notes)) {
                             $notes = $comment;
                         } else {
                             $notes = $comment . " " . $step_notes;
                         }
                     }
                     // Check if step_actual_result is not empty
                     if (empty($data_tab[$ref_tab[13]])) {
                         $bugs = "";
                     } else {
                         $bugs = $data_tab[$ref_tab[13]];
                     }
                     // Write datas into qa_generic database
                     if (preg_match("#^ ?pass(ed)? ?\$#i", $status)) {
                         $decision_criteria_id = -1;
                     } elseif (preg_match("#^ ?fail(ed)? ?\$#i", $status)) {
                         $decision_criteria_id = -2;
                     } elseif (preg_match("#^ ?block(ed)? ?\$#i", $status)) {
                         $decision_criteria_id = -3;
                     } elseif (preg_match("#^ ?defer(red)? ?\$#i", $status)) {
                         $decision_criteria_id = -4;
                     } elseif (preg_match("#^ ?not_run ?\$#i", $status) || preg_match("#^ ?not ?run ?\$#i", $status)) {
                         $decision_criteria_id = -5;
                     } elseif (preg_match("#^ ?measured ?\$#i", $status)) {
                         if (empty($failure)) {
                             if ($value < $target) {
                                 $decision_criteria_id = -2;
                             } else {
                                 $decision_criteria_id = -1;
                             }
                         } else {
                             if ($failure > $target) {
                                 if ($value < $failure) {
                                     $decision_criteria_id = -1;
                                 } else {
                                     $decision_criteria_id = -2;
                                 }
                             } elseif ($failure < $target) {
                                 if ($value > $failure) {
                                     $decision_criteria_id = -1;
                                 } else {
                                     $decision_criteria_id = -2;
                                 }
                             } else {
                                 if ($value < $target) {
                                     $decision_criteria_id = -2;
                                 } else {
                                     $decision_criteria_id = -1;
                                 }
                             }
                         }
                     } else {
                         if (preg_match("#^ ?in ?progress ?\$#i", $status)) {
                             $decision_criteria_id = -5;
                         } else {
                             return 2508;
                         }
                     }
                     // Status hard coded
                     $resultStatus = 0;
                     if ($merge_flag) {
                         // Retrieve test result id relying on test name and feature label, if it exists
                         $query = "SELECT tr.id tr_id\n\t\t\t\t\t\t\t\t\tFROM " . $qa_generic . ".test_result tr\n\t\t\t\t\t\t\t\t\t\tJOIN " . $qa_generic . ".table_name tn ON tn.name = 'test_result'\n\t\t\t\t\t\t\t\t\t\tJOIN " . $qa_generic . ".complementary_tool_relation ctr ON ctr.table_name_id = tn.id\n\t\t\t\t\t\t\t\t\t\t\tAND ctr.table_entry_id = tr.id\n\t\t\t\t\t\t\t\t\t\t\tAND ctr.label = '" . addslashes($feature) . "'\n\t\t\t\t\t\t\t\t\tWHERE tr.test_session_id = " . $test_session_id . "\n\t\t\t\t\t\t\t\t\t\tAND tr.name = '" . addslashes($case_id) . "'";
                         $result = Doctrine_Manager::getInstance()->getCurrentConnection()->execute($query)->fetch(PDO::FETCH_ASSOC);
                         if (!empty($result)) {
                             $test_result_exists_flag = true;
                             $testResultId = $result['tr_id'];
                         }
                     }
                     // Write into test_result table
                     if ($test_result_exists_flag) {
                         $measures = Doctrine_Query::create()->select('*')->from('Measure')->where('test_result_id = ?', $testResultId)->execute();
                         foreach ($measures as $measure) {
                             $measure->delete($conn);
                         }
                         $testResult = Doctrine_Core::getTable("TestResult")->findOneById($testResultId);
                     } else {
                         $testResult = new TestResult();
                         $testResult->setName($case_id);
                         $testResult->setTestSessionId($test_session_id);
                     }
                     $testResult->setDecisionCriteriaId($decision_criteria_id);
                     $testResult->setComplement($test_case);
                     $testResult->setStatus($resultStatus);
                     $testResult->setExecutionTime($executionTime);
                     $testResult->setBugs($bugs);
                     $testResult->setComment($notes);
                     $testResult->save($conn);
                     // Retrieve test_result id created
                     $test_result_id = $testResult->getId();
                     // Category = Feature = 1
                     $category = 1;
                     // Write into complementary_tool_relation table
                     if (!$test_result_exists_flag) {
                         $complementaryToolRelation = new ComplementaryToolRelation();
                         $complementaryToolRelation->setLabel($feature);
                         $complementaryToolRelation->setTableNameId($table_name_test_result_id);
                         $complementaryToolRelation->setTableEntryId($test_result_id);
                         $complementaryToolRelation->setCategory($category);
                         $complementaryToolRelation->save($conn);
                     }
                     // Write into measure table
                     if (!empty($measurement_name) and !empty($value)) {
                         $measureObject = new Measure();
                         $measureObject->setTestResultId($test_result_id);
                         $measureObject->setValue($value);
                         $measureObject->setUnit($unit);
                         $measureObject->setDescription($measurement_name);
                         $measureObject->setCategory(1);
                         $measureObject->setOperator(1);
                         $measureObject->save($conn);
                         if (!empty($target)) {
                             $measureObject = new Measure();
                             $measureObject->setTestResultId($test_result_id);
                             $measureObject->setValue($target);
                             $measureObject->setUnit($unit);
                             $measureObject->setDescription($measurement_name);
                             $measureObject->setCategory(2);
                             $measureObject->setOperator(1);
                             $measureObject->save($conn);
                         }
                         if (!empty($failure)) {
                             $measureObject = new Measure();
                             $measureObject->setTestResultId($test_result_id);
                             $measureObject->setValue($failure);
                             $measureObject->setUnit($unit);
                             $measureObject->setDescription($measurement_name);
                             $measureObject->setCategory(3);
                             $measureObject->setOperator(1);
                             $measureObject->save($conn);
                         }
                     }
                 }
                 $data_tab = array();
                 $test_result_exists_flag = false;
             }
         } else {
             // Close the file
             fclose($csv_report_file);
             return 2000;
         }
         // Close the file
         fclose($csv_report_file);
         return 0;
     } else {
         return 10;
     }
 }