Browse Source

Added 'Select Once' option to bulk loader

spficklin 12 years ago
parent
commit
26d7b30b9a

+ 8 - 2
tripal_bulk_loader/tripal_bulk_loader.admin.templates.inc

@@ -149,6 +149,9 @@ function tripal_bulk_loader_modify_template_base_form($form_state = NULL, $mode)
         } 
         elseif (strcmp($table_array['mode'],'insert_once')==0) {
           $mode_value .= 'insert once';
+        } 
+         elseif (strcmp($table_array['mode'],'select_once')==0) {
+          $mode_value .= 'select once';
         } 
         elseif ($table_array['mode']) {
           $mode_value .= $table_array['mode'];
@@ -707,7 +710,7 @@ function tripal_bulk_loader_edit_template_record_form(&$form_state = NULL) {
   // get default for the select optional
   $select_optional = $form_state['storage']['template_array'][$form_state['storage']['original_priority']]['select_optional'];
   if(!isset($select_optional)){
-    $select_optional = 1;
+    $select_optional = 0;
   }
   
   // get default for the select if duplicate
@@ -797,6 +800,7 @@ function tripal_bulk_loader_edit_template_record_form(&$form_state = NULL) {
     '#title' => 'Action to take when Loading Record',
     '#options' => array(
       'select' => 'SELECT: Don\'t insert this record: it\'s used to define a foreign key in another record',
+      'select_once' => 'SELECT ONCE: Select the record only once for the entire file.',  
       'insert' => 'INSERT: Insert the record',
       'insert_once' => 'INSERT ONCE: Record will be inserted once for the entire file',
     ),
@@ -826,6 +830,8 @@ function tripal_bulk_loader_edit_template_record_form(&$form_state = NULL) {
      '#description' => t('If this is not the first time this record has been added then perform a select rather than an insert.'),
      '#default_value' => $select_if_duplicate
   );
+  
+  
 // TODO: finish coding up the update_if_duplicate functionality  
 /*  $form['edit_record']['update_if_duplicate'] = array(
      '#type' => 'checkbox',
@@ -837,7 +843,7 @@ function tripal_bulk_loader_edit_template_record_form(&$form_state = NULL) {
   $form['edit_record']['optional'] = array(
      '#type' => 'checkbox',
      '#title' => t('Optional'),
-     '#description' => t('The insert, update or select will only be performed if all required data are present'),
+     '#description' => t('The insert, update or select will only be performed only if all required data are present'),
      '#default_value' => $optional
   );  
   $form['edit_record']['disable'] = array(

+ 38 - 18
tripal_bulk_loader/tripal_bulk_loader.loader.inc

@@ -483,7 +483,7 @@ function process_data_array_for_line($priority, &$data, &$default_data, $addt) {
     if (array_key_exists($priority, $addt->field2column)) {
       $values = tripal_bulk_loader_add_spreadsheetdata_to_values($values, $addt->line, $addt->field2column[$priority]);
     } 
-    $values = tripal_bulk_loader_add_foreignkey_to_values($table_data, $values, $data, $addt->record2priority, $addt->nid, $priority);    
+    $values = tripal_bulk_loader_add_foreignkey_to_values($table_data, $values, $data, $addt->record2priority, $addt->nid, $priority, $default_data);    
   }
 
   $values = tripal_bulk_loader_regex_tranform_values($values, $table_data, $addt->line);
@@ -560,8 +560,14 @@ function process_data_array_for_line($priority, &$data, &$default_data, $addt) {
   }
   
   // check if it is already inserted
-  if (array_key_exists('inserted',$table_data) and $table_data['inserted']) {
-    //watchdog('T_bulk_loader','Already Inserted:'.print_r($values,TRUE),array(),WATCHDOG_NOTICE);
+  if (array_key_exists('inserted', $table_data) and $table_data['inserted']) {
+    return $no_errors;
+  }
+  
+  // check if it is already selected, if so, just get the value stored in
+  // the default_data array
+  if (array_key_exists('selected', $table_data) and $table_data['selected']) {
+  	$data[$priority]['values_array'] = $default_data[$priority]['values_array'];
     return $no_errors;
   }
          
@@ -575,8 +581,7 @@ function process_data_array_for_line($priority, &$data, &$default_data, $addt) {
     $options = array('is_duplicate' => 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
-    // that for future records that my depend on this one, they can get the values 
-    // needed
+    // that for future records that my depend on this one, they can get the values needed
     if ($duplicate and is_array($duplicate) and count($duplicate) > 0) {
 
       // if all we have is one field then we will just use the value returned
@@ -685,22 +690,14 @@ function process_data_array_for_line($priority, &$data, &$default_data, $addt) {
       }
     } //end of if insert was successful
   }
+  // perform a select
   else {
     // 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);
     }
-//$time_end = microtime(true);
-//$time = $time_end - $time_start;
-//printf("time: %04f seconds\n",$time);
-//if($time > 0.05){
-//  print "$table\n";
-//  print_r(array_keys($table_desc['fields']));
-//  print_r($values);
-//  exit;
-//}
-    // if the record doesn't exists and it's not optional then generate an error
+    // 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) {        
@@ -729,6 +726,19 @@ function process_data_array_for_line($priority, &$data, &$default_data, $addt) {
         $data[$priority]['values_array'] = NULL;
       }
     } 
+    // if mode=select_once then ensure we only select it once
+    if (preg_match('/select_once/', $table_data['mode'])) {
+      $default_data[$priority]['selected'] = TRUE;
+      
+      // save the pkey
+      if(array_key_exists('primary key',$table_desc)){
+      	$new_values = array();
+        foreach($matches[0] as $key => $value){ 
+          $new_values[$key] = $value;
+        } 
+        $default_data[$priority]['values_default'] = $new_values;   
+      } 
+    }
   }
   return $no_errors;
 }
@@ -775,7 +785,8 @@ function tripal_bulk_loader_add_spreadsheetdata_to_values($values, $line, $field
  * record is looked up and the values array is substituted in.
  *
  */
-function tripal_bulk_loader_add_foreignkey_to_values($table_array, $values, $data, $record2priority, $nid, $priority) {
+function tripal_bulk_loader_add_foreignkey_to_values($table_array, $values, $data, $record2priority, $nid, 
+  $priority, $default_data) {
 
   // iterate through each field in the $values arrray and
   // substitute any values for FK / referring fields
@@ -790,7 +801,16 @@ function tripal_bulk_loader_add_foreignkey_to_values($table_array, $values, $dat
       $foreign_field    = $value['foreign record']['field'];
       
       // get the values of the foreign record and substitute those for the values
-      $foreign_values   = $data[$foreign_priority]['values_array'];
+      $foreign_values   = $data[$foreign_priority]['values_array'];      
+      
+      // check to see if we have any default values in the $default_data array
+      // these were populated from select statements that only need to run once
+      // so we can reuse the values from those previous selects.
+      if (array_key_exists('values_default', $default_data[$foreign_priority]) and
+          array_key_exists($foreign_field, $default_data[$foreign_priority]['values_default'])) {
+         $values[$field] = $default_data[$foreign_priority]['values_default'][$foreign_field];
+         continue;
+      }
             
       // if the field in the Referral records is in a FK relationship with
       // this field then we can simply keep the value we have
@@ -800,7 +820,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]){
-         $values[$field] = $foreign_values;      
+         $values[$field] = $foreign_values; 
       } 
       // if the field in the Referral records is not in an FK relationship
       // with this field then we we have to get the requested value, we must