Browse Source

Bulk Loader: Updated loader to use new tripal_core_report_error function and print errors to the screen in addition to adding them to watchdog. Hopefully this makes errors easier to read when transactions fail, etc.

Lacey Sanderson 11 years ago
parent
commit
b0b2b8104c
1 changed files with 38 additions and 21 deletions
  1. 38 21
      tripal_bulk_loader/includes/tripal_bulk_loader.loader.inc

+ 38 - 21
tripal_bulk_loader/includes/tripal_bulk_loader.loader.inc

@@ -280,13 +280,13 @@ function tripal_bulk_loader_load_data($nid, $job_id) {
             }
             else {
               print "ERROR: Template has changed after constants were assigned!\n";
-              watchdog('T_bulk_loader', 'Template has changed after constants were assigned', array(), WATCHDOG_NOTICE);
+              tripal_bulk_loader_throw_error('Template has changed after constants were assigned', array(), WATCHDOG_NOTICE);
               exit(1);
             }
           }
           else {
             print "ERROR: Template has changed after constants were assigned!\n";
-            watchdog('T_bulk_loader', 'Template has changed after constants were assigned', array(), WATCHDOG_NOTICE);
+            tripal_bulk_loader_throw_error('Template has changed after constants were assigned', array(), WATCHDOG_NOTICE);
             exit(1);
           }
         }
@@ -300,8 +300,8 @@ function tripal_bulk_loader_load_data($nid, $job_id) {
       $file = new SplFileObject($node->file, 'r');
     }
     catch (Exception $e) {
-      watchdog('T_bulk_loader', 'Could not open file %file',
-        array($node->file), WATCHDOG_ERROR);
+      tripal_bulk_loader_throw_error('Could not open file %file',
+        array('%file' => $node->file), WATCHDOG_ERROR);
       return;
     }
 
@@ -512,14 +512,15 @@ function process_data_array_for_line($priority, &$data, &$default_data, $addt) {
 
   $values = tripal_bulk_loader_regex_tranform_values($values, $table_data, $addt->line);
   if (!$values) {
-    //watchdog('T_bulk_loader', 'Line ' . $addt->line_num . ' Regex:<pre>' . print_r($values, TRUE) . print_r($table_data, TRUE) . '</pre>' . '</pre>', array(), WATCHDOG_NOTICE);
+    //tripal_bulk_loader_throw_error('Line ' . $addt->line_num . ' Regex:<pre>' . print_r($values, TRUE) . print_r($table_data, TRUE) . '</pre>' . '</pre>', array(), WATCHDOG_NOTICE);
   }
 
   // get the table description
   $table_desc = tripal_core_get_chado_table_schema($table);
   if (!$table_desc) {
-    watchdog('T_bulk_loader', "Failure: Tripal does not know about the table named '%table'. If this is a custom table,
-      please define it first", array('%table' => $table), WATCHDOG_ERROR);
+    $msg = "Tripal does not know about the table named '%table'. If this is a custom table,
+      please define it first";
+    tripal_bulk_loader_throw_error($msg,  array('%table' => $table), WATCHDOG_ERROR);
     $data[$priority]['error'] = TRUE;
     return;
   }
@@ -546,7 +547,7 @@ function process_data_array_for_line($priority, &$data, &$default_data, $addt) {
         else {
           $msg = "\nLine " . $addt->line_num . ' "' . $table_data['record_id'] .
             '" (' . $table_data['mode'] . ') Missing template required value: ' . $table . '.' . $field;
-          watchdog('T_bulk_loader', $msg, array(), WATCHDOG_WARNING);
+          tripal_bulk_loader_throw_error($msg, array(), WATCHDOG_WARNING);
           $data[$priority]['error'] = TRUE;
           $no_errors = FALSE;
         }
@@ -570,7 +571,7 @@ function process_data_array_for_line($priority, &$data, &$default_data, $addt) {
           strcmp($def['type'], 'serial') != 0) {                             // it is not a 'serial' type column
         $msg = "\nLine " . $addt->line_num . ' ' . $table_data['record_id'] .
                ' (' . $table_data['mode'] . ') Missing Database Required Value: ' . $table . '.' . $field;
-        watchdog('T_bulk_loader', $msg, array(), WATCHDOG_ERROR);
+        tripal_bulk_loader_throw_error($msg, array(), WATCHDOG_ERROR);
         $data[$priority]['error'] = TRUE;
       }
     }
@@ -581,19 +582,19 @@ function process_data_array_for_line($priority, &$data, &$default_data, $addt) {
 
   // if there was an error already -> don't insert
   if (array_key_exists('error', $data[$priority]) and $data[$priority]['error']) {
-    watchdog('T_bulk_loader','Skipping processing of %table due to previous errors',array('%table'=>$table),WATCHDOG_NOTICE);
+    tripal_bulk_loader_throw_error('Skipping processing of %table due to previous errors',array('%table'=>$table),WATCHDOG_NOTICE);
     return $no_errors;
   }
 
   // skip optional fields
   if ($skip_optional) {
-    watchdog('T_bulk_loader','Skipping an optional record (%record)',array('%record'=>$table_data['record_id']),WATCHDOG_NOTICE);
+    tripal_bulk_loader_throw_error('Skipping an optional record (%record)',array('%record'=>$table_data['record_id']),WATCHDOG_NOTICE);
     return $no_errors;
   }
 
   // check if it is already inserted
   if (array_key_exists('inserted', $table_data) and $table_data['inserted']) {
-    watchdog('T_bulk_loader','Skipping %record since it is already inserted',array('%record'=>$table_data['record_id']),WATCHDOG_NOTICE);
+    tripal_bulk_loader_throw_error('Skipping %record since it is already inserted',array('%record'=>$table_data['record_id']),WATCHDOG_NOTICE);
     return $no_errors;
   }
 
@@ -601,7 +602,7 @@ function process_data_array_for_line($priority, &$data, &$default_data, $addt) {
   // the default_data array
   if (array_key_exists('selected', $table_data) and $table_data['selected']) {
     $data[$priority]['values_array'] = $default_data[$priority]['values_array'];
-    watchdog('T_bulk_loader','%record was already selected thus we are just returning the values previously selected.',array('%record'=>$table_data['record_id']),WATCHDOG_NOTICE);
+    tripal_bulk_loader_throw_error('%record was already selected thus we are just returning the values previously selected.',array('%record'=>$table_data['record_id']),WATCHDOG_NOTICE);
     return $no_errors;
   }
 
@@ -620,7 +621,7 @@ function process_data_array_for_line($priority, &$data, &$default_data, $addt) {
   if (preg_match('/insert_unique/', $table_data['mode']) or
      $table_data['select_if_duplicate'] == 1 or
      $table_data['update_if_duplicate'] == 1) {
-    $options = array('is_duplicate' => TRUE);
+    $options = array('is_duplicate' => TRUE, 'print_errors' => TRUE);
     $duplicate = tripal_core_chado_select($table, array_keys($table_desc['fields']), $values, $options);
 
     // if this is a duplicate then substitute the values in the table_data array so
@@ -652,7 +653,7 @@ function process_data_array_for_line($priority, &$data, &$default_data, $addt) {
       }
       // return if this is a select_if_duplicate
       if ($table_data['select_if_duplicate'] == 1) {
-        watchdog('T_bulk_loader','Simply returning values for %record since it was already inserted',array('%record'=>$table_data['record_id']),WATCHDOG_NOTICE);
+        tripal_bulk_loader_throw_error('Simply returning values for %record since it was already inserted',array('%record'=>$table_data['record_id']),WATCHDOG_NOTICE);
         return $no_errors;
       }
     }
@@ -708,14 +709,16 @@ function process_data_array_for_line($priority, &$data, &$default_data, $addt) {
 			}
 			if (!empty($match)) {
 				// Now we need to check if it already exists via a select
-				$results = tripal_core_chado_select($table, array_keys($table_desc['fields']), $match);
+				$results = tripal_core_chado_select($table, array_keys($table_desc['fields']), $match, array('print_errors' => TRUE));
 				// If not then insert
 				if (empty($results)) {
 					$options['statement_name'] = 'ins_'.$options['statement_name'];
+					$options['print_errors'] = TRUE;
 					$record = tripal_core_chado_insert($table, $values, $options);
 				}
 				else {
 				  $options['return_record'] = TRUE;
+				  $options['print_errors'] = TRUE;
   		    $record = tripal_core_chado_update($table, $match, $values, $options);
   		  }
   	  }
@@ -724,12 +727,13 @@ function process_data_array_for_line($priority, &$data, &$default_data, $addt) {
         $table_data['mode'] . ') Unable to update record since none of the unique key or primary key fields were available ' .
         ' where values:' . print_r($values, TRUE);
 
-      	watchdog('T_bulk_loader', $msg, array(), WATCHDOG_ERROR);
+      	tripal_bulk_loader_throw_error($msg, array(), WATCHDOG_ERROR);
       	$data[$priority]['error'] = TRUE;
       	$no_errors = FALSE;
   	  }
     }
     else {
+      $options['print_errors'] = TRUE;
       $record = tripal_core_chado_insert($table, $values, $options);
     }
 
@@ -739,7 +743,7 @@ function process_data_array_for_line($priority, &$data, &$default_data, $addt) {
         $table_data['mode'] . ') Unable to insert record into ' . $table .
         ' where values:' . print_r($values, TRUE);
 
-      watchdog('T_bulk_loader', $msg, array(), WATCHDOG_ERROR);
+      tripal_bulk_loader_throw_error($msg, array(), WATCHDOG_ERROR);
       $data[$priority]['error'] = TRUE;
       $no_errors = FALSE;
     }
@@ -798,14 +802,14 @@ function process_data_array_for_line($priority, &$data, &$default_data, $addt) {
     // get the matches for this select
     $matches = array();
     if (is_array($values) and count($values) > 0) {
-      $matches = tripal_core_chado_select($table, array_keys($table_desc['fields']), $values);
+      $matches = tripal_core_chado_select($table, array_keys($table_desc['fields']), $values, array('print_errors' => TRUE));
     }
     // if the record doesn't exist and it's not optional then generate an error
     if (count($matches) == 0) {
       // No record on select
       if ($table_data['select_optional'] != 1) {
         $msg = "\nLine " . $addt->line_num . ' ' . $table_data['record_id'] . ' (' . $table_data['mode'] . ') No Matching record in ' . $table . ' where values:' . print_r($values, TRUE);
-        watchdog('T_bulk_loader', $msg, array(), WATCHDOG_ERROR);
+        tripal_bulk_loader_throw_error($msg, array(), WATCHDOG_ERROR);
         $data[$priority]['error'] = TRUE;
         $no_errors = FALSE;
       }
@@ -819,7 +823,7 @@ function process_data_array_for_line($priority, &$data, &$default_data, $addt) {
     if (count($matches) > 1) {
       if ($table_data['select_optional'] != 1) {
         $msg = "\nLine " . $addt->line_num . ' ' . $table_data['record_id'] . ' (' . $table_data['mode'] . ') Too many matching records in ' . $table . ' where values:' . print_r($values, TRUE);
-        watchdog('T_bulk_loader', $msg, array(), WATCHDOG_WARNING);
+        tripal_bulk_loader_throw_error($msg, array(), WATCHDOG_WARNING);
         $data[$priority]['error'] = TRUE;
         $no_errors = FALSE;
       }
@@ -927,6 +931,7 @@ function tripal_bulk_loader_add_foreignkey_to_values($table_array, $values, $dat
           array_key_exists($foreign_table, $tbl_description['foreign keys']) and
           array_key_exists($field, $tbl_description['foreign keys'][$foreign_table]['columns']) and
           $foreign_field == $tbl_description['foreign keys'][$foreign_table]['columns'][$field]) {
+          //check here for fk bug
          $values[$field] = $foreign_values;
       }
       // if the field in the Referral records is not in an FK relationship
@@ -942,6 +947,7 @@ function tripal_bulk_loader_add_foreignkey_to_values($table_array, $values, $dat
             $fvalues = array($fk_description['primary key'][0] => $foreign_values);
             $columns = array($foreign_field);
             $options = array('statement_name' => 'pk_' . $foreign_table);
+            $options['print_errors'] = TRUE;
             $record  = tripal_core_chado_select($foreign_table, $columns, $fvalues, $options);
             if ($record) {
               $values[$field] = $record[0]->$foreign_field;
@@ -962,6 +968,7 @@ function tripal_bulk_loader_add_foreignkey_to_values($table_array, $values, $dat
           $fvalues  = $foreign_values;
           $columns = array($foreign_field);
           $options = array('statement_name' => 'blk_' . $nid . $priority . $foreign_table);
+          $options['print_errors'] = TRUE;
           $record  = tripal_core_chado_select($foreign_table, $columns, $fvalues, $options);
           if ($record) {
             $values[$field] = $record[0]->$foreign_field;
@@ -1167,3 +1174,13 @@ function tripal_bulk_loader_progress_file_track_job($job_id, $record_added, $lin
     variable_set('tripal_bulk_loader_progress_file_handle', NULL);
   }
 }
+
+function tripal_bulk_loader_throw_error($msg, $args, $severity) {
+  tripal_core_report_error(
+    'tripal_bulk',
+    $severity,
+    $msg,
+    $args,
+    array('print' => TRUE)
+  );
+}