Browse Source

Tripal: Bulk Loader -added insert_once per file ability for records such as a single library, analysis, etc.

laceysanderson 14 years ago
parent
commit
a5ec8916f6

+ 14 - 2
tripal_bulk_loader/tripal_bulk_loader.admin.inc

@@ -357,6 +357,17 @@ function tripal_bulk_loader_modify_template_base_form_submit($form, &$form_state
       );
       drupal_goto('admin/tripal/tripal_bulk_loader_template/add_field', $query);
     break;
+
+    case 'Duplicate Record':
+      // original record (one to be duplicated)
+      $orig_priority = $form_state['clicked_button']['#name'];
+      $record = $form_state['storage']['template'][ $orig_priority ];
+      
+      // new record
+      $new_priority = sizeof($form_state['storage']['template']) + 1;
+      $record['record_id'] = $record['record_id'] . '_' . date('YzHi');
+      $form_state['storage']['template'][ $new_priority ] = $record;
+    break;
     
     case 'Edit Field':
       $field_data_index = $form_state['clicked_button']['#name'];
@@ -649,9 +660,10 @@ function tripal_bulk_loader_edit_template_record_form (&$form_state = NULL) {
     '#type' => 'radios',
     '#title' => 'Action to take when Loading Record',
     '#options' => array(
-      'insert' => 'INSERT: Insert the record if it doesn\'t already exist',
       'select' => 'SELECT: Don\'t insert this record: it\'s used to define a foreign key in another record',
-      'optional' => 'OPTIONAL: Record will only be inserted if all required data is filled in'
+      'insert' => 'INSERT: Insert the record if it doesn\'t already exist',
+      'optional' => 'OPTIONAL: Record will only be inserted if all required data is filled in',
+      'insert_once' => 'INSERT ONCE: Record will be inserted once for the entire spreadsheet'
     ),
     '#default_value' => 'insert'
   );

+ 33 - 3
tripal_bulk_loader/tripal_bulk_loader.loader.inc

@@ -141,12 +141,20 @@ function tripal_bulk_loader_load_data($nid) {
     foreach ($data as $priority => $table_data) {
       $table = $table_data['table'];
       $values = $table_data['values_array'];
+
+//print 'Table: '.$table."\n";      
+//print 'Default Values:'.print_r($values,TRUE)."\n";
+      
       if ($table_data['need_further_processing']) {
         $values = tripal_bulk_loader_add_spreadsheetdata_to_values ($values, $line, $field2column[$priority]);
+//print 'W/ Spreadsheet Values:'.print_r($values,TRUE)."\n";
         $values = tripal_bulk_loader_add_foreignkey_to_values($values, $data, $record2priority);
+//print 'W/ Foreign Key Values:'.print_r($values,TRUE)."\n";
       }
       $values = tripal_bulk_loader_regex_tranform_values($values, $table_data, $line);
 
+//print 'W/ Regex Values:'.print_r($values,TRUE)."\n";
+
       if (!$values) {
         $msg = $table_data['record_id'].' ('.$table_data['mode'].') Aborted due to error in previous record.';
         watchdog('T_bulk_loader', $msg, array(), WATCHDOG_WARNING); 
@@ -183,12 +191,22 @@ function tripal_bulk_loader_load_data($nid) {
       }
       
       if ($data[$priority]['error']) {
+        // print debugging information
+        print 'Default Table Data: '.print_r($table_data,TRUE)."\n";
+        print 'Supplemented Values: '.print_r($values,TRUE)."\n";
+        
+        // continue to next record
         continue;
       }
       
       // add new values array into the data array
       $data[$priority]['values_array'] = $values;
-       
+      
+      // check if it is already inserted
+      if ($table_data['inserted']) {
+        continue;
+      }
+      
       // first check if it already exists
       $exists = tripal_core_chado_select($table, array_keys($values), $values, array('has_record'=>TRUE));
       //print "Number of Records:".$exists.".\n";
@@ -214,7 +232,10 @@ function tripal_bulk_loader_load_data($nid) {
             print "ERROR: ".$msg."\n";  
             $data[$priority]['error'] = TRUE;
           } else {
-            //print "Inserted ".$table_data['record_id']." into ".$table."\n";
+            // if mode=insert_once then ensure we only insert it once
+            if (preg_match('/insert_once/',$table_data['mode'])) {
+              $default_data[$priority]['inserted'] = TRUE;
+            }
           }
         } else {
           $msg = $table_data['record_id'].' ('.$table_data['mode'].') No Matching record in '.$table.' where values:'.print_r($values,TRUE);
@@ -223,6 +244,13 @@ function tripal_bulk_loader_load_data($nid) {
           $data[$priority]['error'] = TRUE;        
         }
       }// end of if/not record exists
+
+      if ($data[$priority]['error']) {
+        // print debugging information
+        print 'Table Data: '.print_r($table_data,TRUE)."\n";
+        print 'Default Table Data: '.print_r($default_data[$priority],TRUE)."\n";
+        print 'Supplemented Values: '.print_r($values,TRUE)."\n";
+      }
     } // end of foreach table in default data array
 
   } //end of foreach line of file
@@ -247,6 +275,8 @@ function tripal_bulk_loader_add_spreadsheetdata_to_values ($values, $line, $fiel
     if (is_array($value)) { continue; }
     
     $column = $field2column[$field] - 1;
+    if ($column < 0) { continue; }
+    
     if (preg_match('/\w+/',$line[$column])) {
       $values[$field] = $line[$column];
     } else {
@@ -297,7 +327,7 @@ function tripal_bulk_loader_add_foreignkey_to_values($values, $data, $record2pri
 function tripal_bulk_loader_regex_tranform_values ($values, $table_data, $line) {
 
   foreach ($table_data['regex_transform'] as $field => $regex_array) {
-    if (!is_array($regex_array)) { continue; }
+    if (!is_array($regex_array['replace'])) { continue; }
     
     // Check for <#column:\d+#> notation
     // if present replace with that column in the current line