Browse Source

Tripal Bulk Loader: Finished basic Add/Edit/Delete Template; Loader will currently load direct field=>column mappings and file wide constants -not relationships between tables

laceysanderson 14 years ago
parent
commit
5b56642177

+ 7 - 3
theme_tripal/node-tripal_bulk_loader.tpl.php

@@ -1,6 +1,8 @@
 
 <div id="tripal_bulk_loader-base-box" class="tripal_bulk_loader-info-box tripal-info-box">
-  <div class="tripal_bulk_loader-info-box-title tripal-info-box-title"></div>
+  <div class="tripal_bulk_loader-info-box-title tripal-info-box-title">
+  	<?php if ($teaser) { print l($node->title, 'node/'.$node->nid); } ?>
+  </div>
   <div class="tripal_bulk_loader-info-box-desc tripal-info-box-desc"></div>
   
 	<table id="tripal_bulk_loader-base-table" class="tripal_bulk_loader-table tripal-table tripal-table-vert">
@@ -35,5 +37,7 @@
 	</table>  
 </div>
 
-<h3>Template</h3>
-<?php print theme('tripal_bulk_loader_template', $node->template->template_id); ?>
+<?php if (!$teaser) { ?>
+	<?php print drupal_get_form('tripal_bulk_loader_add_loader_job_form', $node); ?>
+	<?php print theme('tripal_bulk_loader_template', $node->template->template_id); ?>
+<?php } ?>

+ 38 - 28
theme_tripal/tripal_bulk_loader/tripal_bulk_loader_template.tpl.php

@@ -21,31 +21,41 @@
 	}
 ?>
 
-<table>
-<caption><b>Constants</b> -These values are applied to all records in the Spreadsheet</caption>
-	<tr><th rowspan="2">Field Name</th><th rowspan="2">Value</th><th colspan="2">Chado Database</th></tr>
-	<tr><th>Table</th><th>Field</th></tr>
-	<?php foreach ($constants as $field) {?>
-		<td><?php print $field['title'];?></td>
-		<td><?php print $field['constant value']; ?></td>
-		<td><?php print $field['table'];?></td>
-		<td><?php print $field['field'];?></td>	
-	<?php } ?>
-</table>
-
-<table>
-<caption><b>Fields</b> -Below is a mapping between Spreadsheet columns and the Chado Database</caption>
-<tr><th rowspan="2">Field Name</th><th colspan="2">Spreadsheet</th><th colspan="2">Chado Datbase</th></tr>
-<tr><th>Worksheet</th><th>Column</th><th>Table</th><th>Field</th></tr>
-<?php foreach ($fields as $column) {?>
-	<?php foreach ($column as $field) {?>
-	<tr>
-		<td><?php print $field['title'];?></td>
-		<td><?php print $field['spreadsheet sheet']; ?></td>
-		<td><?php print $field['spreadsheet column'];?></td>
-		<td><?php print $field['table'];?></td>
-		<td><?php print $field['field'];?></td>
-	<tr>
-<?php }} ?>
-</table>
-
+<div id="tripal_bulk_loader-base-box" class="tripal_bulk_loader-info-box tripal-info-box">
+  <div class="tripal_bulk_loader-info-box-title tripal-info-box-title">Template Description</div>
+  <div class="tripal_bulk_loader-info-box-desc tripal-info-box-desc"></div>
+  
+  <table id="tripal_bulk_loader-template_constant-table" class="tripal_bulk_loader-table tripal-table tripal-table-vert">
+  <caption><b>Constants</b> -These values are applied to all records in the Spreadsheet</caption>
+    <tr><th rowspan="2">Field Name</th><th rowspan="2">Value</th><th colspan="2">Chado Database</th></tr>
+    <tr><th>Table</th><th>Field</th></tr>
+    <?php $row = 'even' ?>
+    <?php foreach ($constants as $field) {?>
+      <tr class="tripal_bulk_loader-table-<?php print $row; ?>-row tripal-table-<?php print $row; ?>-row">
+        <td><?php print $field['title'];?></td>
+        <td><?php print $field['constant value']; ?></td>
+        <td><?php print $field['table'];?></td>
+        <td><?php print $field['field'];?></td>	
+      </tr>
+      <?php $row = ($row == 'odd') ? 'even':'odd' ; ?>
+    <?php } ?>
+  </table>
+  
+  <table id="tripal_bulk_loader-template_fields-table" class="tripal_bulk_loader-table tripal-table tripal-table-vert">
+  <caption><b>Fields</b> -Below is a mapping between Spreadsheet columns and the Chado Database</caption>
+  <tr><th rowspan="2">Field Name</th><th colspan="2">Spreadsheet</th><th colspan="2">Chado Datbase</th></tr>
+  <tr><th>Worksheet</th><th>Column</th><th>Table</th><th>Field</th></tr>
+  <?php $row = 'even' ?>
+  <?php foreach ($fields as $column) {?>
+    <?php foreach ($column as $field) {?>
+    <tr class="tripal_bulk_loader-table-<?php print $row; ?>-row tripal-table-<?php print $row; ?>-row">
+      <td><?php print $field['title'];?></td>
+      <td><?php print $field['spreadsheet sheet']; ?></td>
+      <td><?php print $field['spreadsheet column'];?></td>
+      <td><?php print $field['table'];?></td>
+      <td><?php print $field['field'];?></td>
+    <tr>
+    <?php $row = ($row == 'odd') ? 'even':'odd' ; ?>
+  <?php }} ?>
+  </table>
+</div>

+ 6 - 0
tripal_bulk_loader/tripal_bulk_loader.admin.inc

@@ -1,4 +1,10 @@
 <?php
+
+/**
+ * @file
+ * Handles Create/Edit/Delete Template Admin Forms
+ */
+ 
 /**
  * tripal_bulk_loader_admin_template
  */

+ 1 - 1
tripal_bulk_loader/tripal_bulk_loader.info

@@ -4,4 +4,4 @@ core = 6.x
 project = tripal_bulk_loader
 package = Tripal
 dependencies[] = tripal_core
-version = "0.3a"
+version = "0.4a"

+ 14 - 0
tripal_bulk_loader/tripal_bulk_loader.install

@@ -14,6 +14,20 @@ function tripal_bulk_loader_uninstall(){
    drupal_uninstall_schema('tripal_bulk_loader');
 }
 
+/**
+ * Update
+ * Add status, file_has_header columns to tripal_bulk_loader
+ */
+function tripal_bulk_loader_update_1 () {
+	$update = array();
+	
+	$update[] = update_sql("ALTER TABLE {tripal_bulk_loader} ADD COLUMN job_status varchar(500) DEFAULT 'initialized'");
+	$update[] = update_sql("ALTER TABLE {tripal_bulk_loader} ADD COLUMN file_has_header boolean DEFAULT TRUE");
+	
+	return $update;
+}
+
+
 /*******************************************************************************
 * tripal_bulk_loader_schema
 */

+ 121 - 0
tripal_bulk_loader/tripal_bulk_loader.loader.inc

@@ -0,0 +1,121 @@
+<?php
+
+/**
+ * Add Loader Job Form
+ *
+ * This form is meant to be included on the node page to allow users to submit/re-submit 
+ * loading jobs
+ */
+function tripal_bulk_loader_add_loader_job_form ($form_state, $node) {
+  $form = array();
+  
+  $form['nid'] = array(
+    '#type' => 'hidden',
+    '#value' => $node->nid,
+  ); 
+  
+  $form['file'] = array(
+  	'#type' => 'hidden',
+  	'#value' => $node->file
+  );
+  
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => 'Submit Job'
+  );
+
+  return $form;
+}
+
+/**
+ * Add Loader Job Form (Submit)
+ */
+function tripal_bulk_loader_add_loader_job_form_submit ($form, $form_state) {
+  global $user;
+  
+  if (preg_match('/Submit Job/', $form_state['values']['op'])) {
+    //Submit Tripal Job
+		$job_args[1] = $form_state['values']['nid'];
+		if (is_readable($form_state['values']['file'])) {
+			$fname = basename($form_state['values']['file']);
+			tripal_add_job("Bulk Loading Job: $fname",'tripal_bulk_loader', 'tripal_bulk_loader_load_data', $job_args, $user->uid);
+		} else {
+			drupal_set_message("Can not open ".$form_state['values']['file'].". Job not scheduled.");
+		}    
+  }
+
+}
+
+/**
+ * Tripal Bulk Loader
+ *
+ * This is the function that's run by tripal_launch_jobs to bulk load chado data.
+ * @param $nid
+ *   The Node ID of the bulk loading job node to be loaded. All other needed data is expected to be 
+ *   in the node (ie: template ID and file)
+ *
+ * Note: Instead of returning a value this function updates the tripal_bulk_loader.status and
+ *   Enters errors into tripal_bulk_loader_errors if they are encountered.
+ */
+function tripal_bulk_loader_load_data($nid) {
+  
+  $node = node_load($nid);
+  print "Template: ".$node->template->name." (".$node->template_id.")\n";
+  print "File: ".$node->file."\n";
+  
+  // get a default values array to be passed into tripal_core_chado_insert
+  // and get a mapping between table.field and spreadsheet column
+  $default_values_array = array();
+  $field2column = array();
+  foreach ($node->template->template_array as $table => $table_array) {
+    if (is_array($table_array)) {
+      foreach ($table_array['field'] as $field_array) {
+        if (preg_match('/table field/', $field_array['type'])) {
+          $default_values_array[$table][$field_array['field']] = '';
+          $field2column[$table][$field_array['field']] = $field_array['spreadsheet column'];
+        } elseif (preg_match('/constant/', $field_array['type'])) {
+          $default_values_array[$table][$field_array['field']] = $field_array['constant value'];
+        } else {
+          print 'WARNING: Unsupported type: '. $field_array['type'] . ' for ' . $table . '.' . $field_array['field']."!\n";
+        }
+      }
+    }
+  }
+  
+  //print "\nDefault Values Array: ".print_r($default_values_array, TRUE)."\n";
+  //print "\nField to Column Mapping: ".print_r($field2column, TRUE)."\n";
+  
+  $file_handle = fopen($node->file, 'r');
+  if (preg_match('/(t|true|1)/', $node->file_has_header)) { fgets($file_handle, 4096); }
+  while (!feof($file_handle)) {
+    $line = array();
+    $raw_line = fgets($file_handle, 4096);
+    $line = preg_split("/\t/", $raw_line);
+    
+    $values = $default_values_array;
+    foreach ($values as $table => $table_array) {
+      foreach ($table_array as $field => $value) {
+        $column = $field2column[$table][$field] - 1;
+        if ($line[$column]) {
+        	$values[$table][$field] = $line[$column];
+				}
+      }
+			$has_record = tripal_core_chado_select($table, array_keys($values[$table]), $values[$table], array('has_record' => TRUE));
+			if ($has_record) {
+				$values_string = array();
+				foreach ($values[$table] as $k => $v) { 
+					if (strlen($v) > 20) { $v = substr($v,0,20) . '...'; }
+					$values_string[] = $k.' => '.$v; 
+				}
+				print "\tWARNING: Record already exists in $table where ".implode(', ',$values_string).".\n";
+			} else {
+	      $success = tripal_core_chado_insert($table, $values[$table]);
+  	    if (!$success) {
+    	  	print "ERROR: Unable to insert the following record into $table: ".print_r($values[$table], TRUE)."\n";
+      	}
+      }
+    } //end of tables in $values
+
+  } //end of file
+  
+}

+ 16 - 21
tripal_bulk_loader/tripal_bulk_loader.module

@@ -1,5 +1,8 @@
 <?php
-/*******************************************************************************
+
+include('tripal_bulk_loader.loader.inc');
+
+/**
  * tripal_bulk_loader_init
  */
 function tripal_bulk_loader_init(){
@@ -7,7 +10,7 @@ function tripal_bulk_loader_init(){
 	drupal_add_css(drupal_get_path('theme', 'tripal').'/css/tripal_bulk_loader.css');
 	drupal_add_js(drupal_get_path('theme', 'tripal').'/js/tripal_bulk_loader.js');
 }
-/*******************************************************************************
+/**
  * tripal_bulk_loader_menu
  */
 function tripal_bulk_loader_menu() {
@@ -90,14 +93,14 @@ function tripal_bulk_loader_menu() {
   return $items;
 }
 
-/*******************************************************************************
+/**
  * tripal_bulk_loader_list
  */
 function tripal_bulk_loader_list () {
 	return "Loaders";
 }
 
-/*******************************************************************************
+/**
  *  tripal_bulk_loader_access
  */
 function tripal_bulk_loader_access($op, $node, $account){
@@ -122,7 +125,7 @@ function tripal_bulk_loader_access($op, $node, $account){
 	return FALSE;
 }
 
-/*******************************************************************************
+/**
  * tripal_bulk_loader_perm
  */
 function tripal_bulk_loader_perm(){
@@ -134,7 +137,7 @@ function tripal_bulk_loader_perm(){
 	);
 }
 
-/*******************************************************************************
+/**
  * tripal_bulk_loader_node_info
  */
 function tripal_bulk_loader_node_info() {
@@ -150,7 +153,7 @@ function tripal_bulk_loader_node_info() {
 	return $nodes;
 }
 
-/*******************************************************************************
+/**
  * tripal_bulk_loader_form
  */
 function tripal_bulk_loader_form ($node){
@@ -201,7 +204,7 @@ function tripal_bulk_loader_form ($node){
    return $form;
 }
 
-/*******************************************************************************
+/**
  * tripal_bulk_loader_theme
  */
 function tripal_bulk_loader_theme() {
@@ -220,7 +223,7 @@ function tripal_bulk_loader_theme() {
   );
 }
 
-/*******************************************************************************
+/**
  * theme_tripal_bulk_loader_node_form
  */
 function theme_tripal_bulk_loader_node_form($form) {
@@ -232,7 +235,7 @@ function theme_tripal_bulk_loader_node_form($form) {
 	return drupal_render($form);
 }
 
-/*******************************************************************************
+/**
  * tripal_bulk_loader_load
  */
 function tripal_bulk_loader_load($node){
@@ -251,7 +254,7 @@ function tripal_bulk_loader_load($node){
 	return $node;
 }
 
-/*******************************************************************************
+/**
  * tripal_bulk_loader_insert
  */
 function tripal_bulk_loader_insert ($node) {
@@ -275,7 +278,7 @@ function tripal_bulk_loader_insert ($node) {
 	}
 }
 
-/*******************************************************************************
+/**
  * tripal_bulk_loader_delete
  */
 function tripal_bulk_loader_delete ($node) {
@@ -283,7 +286,7 @@ function tripal_bulk_loader_delete ($node) {
 	db_query($sql, $node->nid);
 }
 
-/*******************************************************************************
+/**
  * tripal_bulk_loader_update
  */
 function tripal_bulk_loader_update ($node) {
@@ -304,14 +307,6 @@ function tripal_bulk_loader_update ($node) {
 	}
 }
 
-/*******************************************************************************
- * tripal_bulk_loader_load_data
- */
-function tripal_bulk_loader_load_data ($loader_name, $template_id, $file) {
-	
-
-}
-
 function tripal_bulk_loader_preprocess_tripal_bulk_loader_template (&$variables) {
 
 	$sql = "SELECT * FROM {tripal_bulk_loader_template} WHERE template_id=%d";