Browse Source

tripal api select now returns an array of objects; tripal select foreign key records can return 1+; both previous updates work with tripal api insert/update/select; also began adding natural diversity views integration -currently not ready for use

laceysanderson 14 years ago
parent
commit
b2a58886a6

+ 14 - 0
tripal_core/chado_tables.schema.inc

@@ -3398,6 +3398,20 @@ function tripal_core_get_chado_schema (){
        'stock_idx4' => array('uniquename'),
        'stock_name_ind1' => array('name'),
      ),
+    'foreign keys' => array(
+       'organism' => array(
+         'table' => 'organism',
+         'columns' => array('organism_id' => 'organism_id'),
+       ),
+       'dbxref' => array(
+         'table' => 'dbxref',
+         'columns' => array('dbxref_id' => 'dbxref_id'),
+       ),
+       'cvterm' => array(
+         'table' => 'cvterm',
+         'columns' => array('type_id' => 'cvterm_id'),
+       ),
+     ),
    );
    $schema['stock_cvterm'] = array(
      'description' => t('TODO: please describe this table!'),

+ 86 - 25
tripal_core/tripal_core.api.inc

@@ -13,11 +13,11 @@ require_once "chado_tables.schema.inc";
 function tripal_core_chado_insert_test(){
    $ivalues =  array(
      'organism_id' => array(
-         'genus' => 'Citrus',
-         'species' => 'sinensis',
+         'genus' => 'Lens',
+         'species' => 'culinaris',
       ),
      'name' => 'orange1.1g000034m.g',
-     'uniquename' => 'orange1.1g000034m.g7',
+     'uniquename' => 'orange1.1g000034m.g8',
      'type_id' => array (
          'cv_id' => array (
             'name' => 'sequence',
@@ -28,8 +28,8 @@ function tripal_core_chado_insert_test(){
    );
    $umatch = array(
      'organism_id' => array(
-         'genus' => 'Citrus',
-         'species' => 'sinensis',
+         'genus' => 'Lens',
+         'species' => 'culinaris',
       ),
      'uniquename' => 'orange1.1g000034m.g7',
      'type_id' => array (
@@ -50,9 +50,15 @@ function tripal_core_chado_insert_test(){
          'is_obsolete' => 0
       ),
    );
-//   $result = tripal_core_chado_insert('feature',$ivalues);
-//   return "$result->feature_id";
-   $result = tripal_core_chado_update('feature',$umatch,$uvalues);
+   $select_multiple = array(
+     'dbxref_id' => array(
+       'db_id' => 2,
+     )
+   );
+   //$result = tripal_core_chado_insert('feature',$ivalues);
+   //return "$result->feature_id";
+   //$result = tripal_core_chado_update('feature',$umatch,$uvalues);
+   //$result = tripal_core_chado_select('feature',array('type_id', 'uniquename'),$select_multiple);
    return $result;
 
 }
@@ -110,8 +116,15 @@ function tripal_core_chado_insert($table,$values){
    foreach($values as $field => $value){
       if(is_array($value)){
          // select the value from the foreign key relationship for this value
-         $insert_values[$field] = tripal_core_chado_get_foreign_key($table_desc,$field,$value);
-      } 
+         $results = tripal_core_chado_get_foreign_key($table_desc,$field,$value);
+         if (sizeof($results) > 1) {
+           watchdog('tripal_core', 'tripal_core_chado_insert: Too many records match the criteria supplied for !foreign_key foreign key constraint (!criteria)', array('!foreign_key' => $field, '!criteria' => print_r($value,TRUE)), WATCHDOG_ERROR);         
+         } elseif (sizeof($results) < 1) {
+           watchdog('tripal_core', 'tripal_core_chado_insert: no record matches criteria supplied for !foreign_key foreign key constraint (!criteria)', array('!foreign_key' => $field, '!criteria' => print_r($value,TRUE)), WATCHDOG_ERROR);
+         } else {
+           $insert_values[$field] = $results[0];
+         }
+      }  
       else {
          $insert_values[$field] = $value;
       }
@@ -128,8 +141,8 @@ function tripal_core_chado_insert($table,$values){
          $ukselect_vals[$field] = $insert_values[$field];
       }
       // now check the constraint
-      if(db_fetch_object(tripal_core_chado_select($table,$ukselect_cols,$ukselect_vals))){
-         watchdog('tripal_core',"Cannot insert duplicate record into $table table: " . print_r($values,1),array(),'WATCHDOG_ERROR');
+      if(tripal_core_chado_select($table,$ukselect_cols,$ukselect_vals)){
+         watchdog('tripal_core',"tripal_core_chado_insert: Cannot insert duplicate record into $table table: " . print_r($values,1),array(),'WATCHDOG_ERROR');
          return false;
       }
    }
@@ -137,8 +150,8 @@ function tripal_core_chado_insert($table,$values){
    // if trying to insert a field that is the primary key, make sure it also is unique
    $pkey = $table_desc['primary key'][0];
    if($insert_values[$pkey]){
-      if(db_fetch_object(tripal_core_chado_select($table,array($pkey),array($pkey => $insert_values[$pkey])))){
-         watchdog('tripal_core',"Cannot insert duplicate primary key into $table table: " . print_r($values,1),array(),'WATCHDOG_ERROR');
+      if(tripal_core_chado_select($table,array($pkey),array($pkey => $insert_values[$pkey]))){
+         watchdog('tripal_core',"tripal_core_chado_insert: Cannot insert duplicate primary key into $table table: " . print_r($values,1),array(),'WATCHDOG_ERROR');
          return false;
       }
    }
@@ -149,7 +162,7 @@ function tripal_core_chado_insert($table,$values){
       // a field is considered missing if it cannot be null and there is no default
       // value for it or it is of type 'serial'
       if($def['not null'] == 1 and !$insert_values[$field] and !$def['default'] and strcmp($def['type'],serial)!=0){
-         watchdog('tripal_core',"Field $field cannot be null: " . print_r($values,1),array(),'WATCHDOG_ERROR');
+         watchdog('tripal_core',"tripal_core_chado_insert: Field $field cannot be null: " . print_r($values,1),array(),'WATCHDOG_ERROR');
          return false;
       }
    }
@@ -175,7 +188,7 @@ function tripal_core_chado_insert($table,$values){
       return true;
    } 
    else {
-      watchdog('tripal_core',"Cannot insert record into $table table: " . print_r($values,1),array(),'WATCHDOG_ERROR');
+      watchdog('tripal_core',"tripal_core_chado_insert: Cannot insert record into $table table: " . print_r($values,1),array(),'WATCHDOG_ERROR');
       return false;
    }
    return false;
@@ -246,7 +259,14 @@ function tripal_core_chado_update($table,$match,$values){
    // get the values needed for matching in the SQL statement
    foreach ($match as $field => $value){
       if(is_array($value)){
-         $update_matches[$field] = tripal_core_chado_get_foreign_key($table_desc,$field,$value);
+         $results = tripal_core_chado_get_foreign_key($table_desc,$field,$value);
+         if (sizeof($results) > 1) {
+           watchdog('tripal_core', 'tripal_core_chado_update: When trying to find record to update, too many records match the criteria supplied for !foreign_key foreign key constraint (!criteria)', array('!foreign_key' => $field, '!criteria' => print_r($value,TRUE)), WATCHDOG_ERROR);         
+         } elseif (sizeof($results) < 1) {
+           watchdog('tripal_core', 'tripal_core_chado_update: When trying to find record to update, no record matches criteria supplied for !foreign_key foreign key constraint (!criteria)', array('!foreign_key' => $field, '!criteria' => print_r($value,TRUE)), WATCHDOG_ERROR);
+         } else {
+           $update_matches[$field] = $results[0];
+         }
       }
       else {
          $update_matches[$field] = $value;
@@ -256,7 +276,14 @@ function tripal_core_chado_update($table,$match,$values){
    // get the values used for updating
    foreach ($values as $field => $value){
       if(is_array($value)){
-         $update_values[$field] = tripal_core_chado_get_foreign_key($table_desc,$field,$value);
+         $results = tripal_core_chado_get_foreign_key($table_desc,$field,$value);
+         if (sizeof($results) > 1) {
+           watchdog('tripal_core', 'tripal_core_chado_update: When trying to find update values, too many records match the criteria supplied for !foreign_key foreign key constraint (!criteria)', array('!foreign_key' => $field, '!criteria' => print_r($value,TRUE)), WATCHDOG_ERROR);         
+         } elseif (sizeof($results) < 1) {
+           watchdog('tripal_core', 'tripal_core_chado_update: When trying to find update values, no record matches criteria supplied for !foreign_key foreign key constraint (!criteria)', array('!foreign_key' => $field, '!criteria' => print_r($value,TRUE)), WATCHDOG_ERROR);
+         } else {
+           $update_values[$field] = $results[0];
+         }         
       }
       else {
          $update_values[$field] = $value;
@@ -288,7 +315,7 @@ function tripal_core_chado_update($table,$match,$values){
       array_push($uargs,$value);
    }
    $sql = substr($sql,0,-4);  // get rid of the trailing 'AND'
-
+   
    // finally perform the update.  If successful, return the updated record
    if(db_query($sql,$uargs)){
       return true;
@@ -355,7 +382,25 @@ function tripal_core_chado_select($table,$columns,$values){
       $select[] = $field;
       if(is_array($value)){
          // select the value from the foreign key relationship for this value
-         $where[$field] = tripal_core_chado_get_foreign_key($table_desc,$field,$value);
+         $results = tripal_core_chado_get_foreign_key($table_desc,$field,$value);
+         if (sizeof($results) < 1) {
+           // foreign key records are required
+           // thus if none matched then return false and alert the admin through watchdog
+           watchdog('tripal_core', 
+            'tripal_core_chado_select: no record in the table referenced by the foreign key (!field)   exists. tripal_core_chado_select table=!table, columns=!columns, values=!values', 
+            array('!table' => $table, 
+              '!columns' => '<pre>' . print_r($columns, TRUE) . '</pre>', 
+              '!values' => '<pre>' . print_r($values, TRUE) . '</pre>',
+              '!field' => $field,
+            ), 
+            WATCHDOG_WARNING);
+           return false;
+           
+         } elseif (sizeof($results) == 1) {
+           $where[$field] = $results[0];
+         } else {
+           $where[$field] = $results;
+         }
       } 
       else {
          $where[$field] = $value;
@@ -367,11 +412,23 @@ function tripal_core_chado_select($table,$columns,$values){
    $sql .= "FROM {$table} ";
    $sql .= "WHERE ";
    foreach($where as $field => $value){
-      $sql .= "$field = '%s' AND ";
-      $args[] = $value;
+     if (is_array($value)) {
+       $sql .= "$field IN (".db_placeholders($value,'varchar').") AND ";
+       foreach ($value as $v) { $args[] = $v; }
+     } else {
+       $sql .= "$field = '%s' AND ";
+       $args[] = $value;
+     }
    }
    $sql = substr($sql,0,-4);  // get rid of the trailing 'AND'
-   return db_query($sql,$args);
+   
+   $resource = db_query($sql,$args);
+   $results = array();
+   while ($r = db_fetch_object($resource)) {
+     $results[] = $r;    
+   }
+   
+   return $results;
 }
 /**
 * Gets the value of a foreign key relationship
@@ -428,8 +485,12 @@ function tripal_core_chado_get_foreign_key($table_desc,$field,$values){
                // the column name of the foreign key matches the field we want 
                // so this is the right relationship.  Now we want to select
                $select_cols = array($right);
-               $result = db_fetch_object(tripal_core_chado_select($table,$select_cols,$values));
-               return $result->$right;
+               $result = tripal_core_chado_select($table,$select_cols,$values);
+               $fields = array();
+               foreach ($result as $obj) {
+                 $fields[] = $obj->$right;
+               }
+               return $fields;
             }
          }
       } 

+ 3 - 0
tripal_core/tripal_core.views.inc

@@ -17,6 +17,9 @@ function tripal_core_views_handlers() {
      'views_handler_field_node_optional' => array(
        'parent' => 'views_handler_field_node',
      ),
+     'views_handler_field_cvterm_name' => array(
+       'parent' => 'views_handler_field',
+     ),
    ),
  );
 }

+ 59 - 0
tripal_core/views/handlers/views_handler_field_cvterm_name.inc

@@ -0,0 +1,59 @@
+<?php 
+// $Id$
+
+class views_handler_field_cvterm_name extends views_handler_field {
+
+  /**
+   * Add join to the cvterm table and cvterm.name field to the query
+   */
+  function query() {
+  
+    // if this table isn't a base table
+    // if it is this can be done by describing a join between base table and cvterm
+    if (!preg_match('/^'.$this->view->base_table.'$/', $this->table)) {
+
+      //Want to add join: LEFT JOIN cvterm nd_experimentprop_cvterm ON nd_experimentprop.type_id = nd_experimentprop_cvterm.cvterm_id
+      //===============================================================
+      // add to table_queue--------------------------------------------
+      $cvterm_join['table'] = 'cvterm';
+      $cvterm_join['num'] = 1;
+      $cvterm_join['alias'] = $this->table . '_cvterm';
+      $cvterm_join['relationship'] = $this->table;
+
+      $cvterm_join['join'] = clone($this->query->table_queue[$this->table]['join']);
+      $cvterm_join['join']->table = 'cvterm';
+      $cvterm_join['join']->field = 'cvterm_id';
+      $cvterm_join['join']->left_table = $this->table;
+      $cvterm_join['join']->left_field = 'type_id';
+      
+      $cvterm_join['join']->definition['table'] = 'cvterm';
+      $cvterm_join['join']->definition['field'] = 'cvterm_id';
+      $cvterm_join['join']->definition['left_table'] = $this->table;
+      $cvterm_join['join']->definition['left_field'] = 'type_id';
+
+      $this->query->table_queue['cvterm'] = $cvterm_join;
+
+      // add to table--------------------------------------------------
+      $this->query->tables[$this->view->base_table][$this->table.'_cvterm'] = array(
+        'count' => 1,
+        'alias' => $this->table.'_cvterm',
+      );
+      
+      //Want to add field: nd_experimentprop_cvterm.name as nd_experimentprop_type
+      //===============================================================
+      $field_alias = $this->table.'_cvterm_name';
+      $this->query->add_field($this->table.'_cvterm', 'name');
+      $this->add_additional_fields();
+      $this->aliases['name'] = $field_alias;  
+      
+    }
+  }
+  
+  /**
+   * Ensure that the type/cvterm name is rendered
+   */
+ function render($values) { 
+   return $values->{$this->aliases['name']}; 
+ }
+  
+}

+ 7 - 0
tripal_natural_diversity/tripal_natural_diversity.views.inc

@@ -20,9 +20,16 @@
  *   includes basic details about the table, fields in that table and
  *   relationships between that table and others (joins)
  */
+require_once('views/nd_experiment.views.inc');
+require_once('views/nd_experimentprop.views.inc');
+require_once('views/nd_geolocation.views.inc');
 function tripal_natural_diversity_views_data()  {
   $data = array();
   
+  $data = array_merge($data, retrieve_nd_experiment_views_data());
+  $data = array_merge($data, retrieve_nd_experimentprop_views_data());
+  $data = array_merge($data, retrieve_nd_geolocation_views_data());
+  
   return $data;
 }
 

+ 89 - 0
tripal_natural_diversity/views/nd_experiment.views.inc

@@ -0,0 +1,89 @@
+<?php
+ 
+/**
+ *  @file
+ *  This file defines the data array for a given chado table. This array
+ *  is merged into a larger array containing definitions of all tables associated
+ *  with this module in:
+ *  @see tripal_natural_diversity.views.inc --in tripal_natural_diversity_views_data()
+ *
+ *  Documentation on views integration can be found at 
+ *  http://views2.logrus.com/doc/html/index.html.
+ */
+
+/*************************************************************************
+ * Purpose: this function returns the portion of the data array 
+ *   which describes the nd_experiment table, it's fields and any joins between it and other tables
+ * @see tripal_natural_diversity_views_data() --in tripal_natural_diversity.views.inc
+ *
+ * @todo Add relationship to nd_geolocations.views.inc
+ *
+ * Table: nd_experiment
+ * @code
+  CREATE TABLE nd_experiment (
+    nd_experiment_id serial PRIMARY KEY NOT NULL,
+    nd_geolocation_id integer NOT NULL references nd_geolocation (nd_geolocation_id) on delete cascade INITIALLY DEFERRED,
+    type_id integer NOT NULL references cvterm (cvterm_id) on delete cascade INITIALLY DEFERRED 
+  );
+ * @endcode
+ */
+ function retrieve_nd_experiment_views_data() {
+  global $db_url;
+  $data = array();
+
+  // if the chado database is not local to the drupal database
+  // then we need to set the database name.  This should always
+  // be 'chado'.
+  if(is_array($db_url) and array_key_exists('chado',$db_url)){
+    $database = 'chado';
+  }
+   
+  //Basic table definition-----------------------------------
+  $data['nd_experiment']['table']['group'] = t('Chado ND Experiment');
+  
+  $data['nd_experiment']['table']['base'] = array(
+    'field' => 'nd_experiment_id',
+    'title' => t('Chado Natural Diversity Experiment'),
+    'help' => t('Represents one data point in a natural diversity project.'),
+  );
+  if($database){
+     $data['nd_experiment']['table']['database'] = $database;
+  }
+
+  
+  //Relationship Definitions---------------------------------
+  //Join: nd_experiment => nd_experiment_contact => contact
+  $data['nd_experiment']['table']['join']['nd_experiment_contact'] = array(
+    'left_field' => 'nd_experiment_id',
+    'field' => 'nd_experiment_id',
+  );  
+  $data['nd_experiment']['table']['join']['contact'] = array(
+    'left_table' => 'nd_experiment_contact',
+    'left_field' => 'nd_experiment_id',
+    'field' => 'nd_experiment_id',
+  );
+  $data['nd_experiment_contact']['table']['join']['contact'] = array(
+    'left_field' => 'contact_id',
+    'field' => 'contact_id',
+  );
+  
+  //Table Field Definitions----------------------------------
+      
+  //Field: nd_experiment_id (primary key)
+  $data['nd_experiment']['nd_experiment_id'] = array(
+    'title' => t('ND Experiment Primary Key'),
+    'help' => t('A unique index for every nd_experiment.'),
+    'field' => array(
+      'handler' => 'views_handler_field_numeric',
+      'click sortable' => TRUE,
+     ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_numeric',
+    ),
+  );
+    
+  return $data;
+}

+ 126 - 0
tripal_natural_diversity/views/nd_experimentprop.views.inc

@@ -0,0 +1,126 @@
+<?php
+ 
+/**
+ *  @file
+ *  This file defines the data array for a given chado table. This array
+ *  is merged into a larger array containing definitions of all tables associated
+ *  with this module in:
+ *  @see tripal_natural_diversity.views.inc --in tripal_natural_diversity_views_data()
+ *
+ *  Documentation on views integration can be found at 
+ *  http://views2.logrus.com/doc/html/index.html.
+ */
+
+/*************************************************************************
+ * Purpose: this function returns the portion of the data array 
+ *   which describes the nd_experimentprop table, it's fields and any joins between it and other tables
+ * @see tripal_natural_diversity_views_data() --in tripal_natural_diversity.views.inc
+ *
+ * Table: nd_experimentprop
+ * @code
+ * nd_experimentprop-Copy/Paste Table SQL code here-nd_experimentprop
+ * @endcode
+ */
+ function retrieve_nd_experimentprop_views_data() {
+  global $db_url;
+  $data = array();
+
+  // if the chado database is not local to the drupal database
+  // then we need to set the database name.  This should always
+  // be 'chado'.
+  if(is_array($db_url) and array_key_exists('chado',$db_url)){
+    $database = 'chado';
+  }
+   
+  //Basic table definition-----------------------------------
+  $data['nd_experimentprop']['table'] = array(
+    'group' => t('Chado ND Experiment Properties'),
+    'field' => 'nd_experimentprop_id',
+    'title' => t('Chado ND Experiment Property'),
+    'help' => t('Properties of a given Natural Diversity Experiment.'),
+  );
+  if($database){
+     $data['nd_experimentprop']['table']['database'] = $database;
+  }
+
+  
+  //Relationship Definitions---------------------------------
+  //Join: nd_experimentprop => nd_experiment
+  $data['nd_experimentprop']['table']['join']['nd_experiment'] = array(
+    'left_field' => 'nd_experiment_id',
+    'field' => 'nd_experiment_id',
+  );
+  
+  //Table Field Definitions----------------------------------
+      
+  //Field: nd_experimentprop_id (primary key)
+  $data['nd_experimentprop']['nd_experimentprop_id'] = array(
+    'title' => t('ND Experiment Property Primary Key'),
+    'help' => t('A unique index for every nd_experimentprop.'),
+    'field' => array(
+      'handler' => 'views_handler_field_numeric',
+      'click sortable' => TRUE,
+     ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_numeric',
+    ),
+  );
+
+  //Field: value (varchar 255)   
+  $data['nd_experimentprop']['value'] = array(
+    'title' => t('Property Value'),
+    'help' => t('The value of a given property.'),
+    'field' => array(
+      'handler' => 'views_handler_field',
+      'click sortable' => TRUE,
+     ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_string',
+    ),
+    'argument' => array(
+      'handler' => 'views_handler_argument_string',
+    ),
+  );
+
+  //Field: rank (int)   
+  $data['nd_experimentprop']['rank'] = array(
+    'title' => t('Property Rank'),
+    'help' => t('The rank of a given propery.'),
+    'field' => array(
+      'handler' => 'views_handler_field_numeric',
+      'click sortable' => TRUE,
+     ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_numeric',
+    ),
+  );
+
+  //Calculated Field: type/cvterm name (varchar 255)   
+  $data['nd_experimentprop']['nd_experimentprop_cvterm_name'] = array(
+    'title' => t('Property Type'),
+    'help' => t('The type of property.'),
+    'field' => array(
+      'handler' => 'views_handler_field_cvterm_name',
+      'click sortable' => TRUE,
+     ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_string',
+    ),
+    'argument' => array(
+      'handler' => 'views_handler_argument_string',
+    ),
+  );    
+  return $data;
+}

+ 161 - 0
tripal_natural_diversity/views/nd_geolocation.views.inc

@@ -0,0 +1,161 @@
+<?php
+ 
+/**
+ *  @file
+ *  This file defines the data array for a given chado table. This array
+ *  is merged into a larger array containing definitions of all tables associated
+ *  with this module in:
+ *  @see tripal_natural_diversity.views.inc --in tripal_natural_diversity_views_data()
+ *
+ *  Documentation on views integration can be found at 
+ *  http://views2.logrus.com/doc/html/index.html.
+ */
+
+/*************************************************************************
+ * Purpose: this function returns the portion of the data array 
+ *   which describes the nd_geolocation table, it's fields and any joins between it and other tables
+ * @see tripal_natural_diversity_views_data() --in tripal_natural_diversity.views.inc
+ *
+ * Table: nd_geolocation
+ * @code
+ * nd_geolocation-Copy/Paste Table SQL code here-nd_geolocation
+ * @endcode
+ */
+ function retrieve_nd_geolocation_views_data() {
+  global $db_url;
+  $data = array();
+
+  // if the chado database is not local to the drupal database
+  // then we need to set the database name.  This should always
+  // be 'chado'.
+  if(is_array($db_url) and array_key_exists('chado',$db_url)){
+    $database = 'chado';
+  }
+   
+  //Basic table definition-----------------------------------
+  $data['nd_geolocation']['table']['group'] = t('Chado ND geolocation');
+  
+  $data['nd_geolocation']['table']['base'] = array(
+    'field' => 'nd_geolocation_id',
+    'title' => t('Chado Natural Diversity Geolocation'),
+    'help' => t('A listing of locations where Natural Diversity Experiments can be performed.'),
+  );
+  if($database){
+     $data['nd_geolocation']['table']['database'] = $database;
+  }
+
+  
+  //Relationship Definitions---------------------------------
+  //Join: nd_geolocation => nd_experiment
+  $data['nd_geolocation']['table']['join']['nd_experiment'] = array(
+    'left_field' => 'nd_geolocation_id',
+    'field' => 'nd_geolocation_id',
+  );  
+   
+  //Table Field Definitions----------------------------------
+      
+  //Field: nd_geolocation_id (primary key)
+  $data['nd_geolocation']['nd_geolocation_id'] = array(
+    'title' => t('ND Geolocation Primary Key'),
+    'help' => t('A unique index for every nd_geolocation.'),
+    'field' => array(
+      'handler' => 'views_handler_field_numeric',
+      'click sortable' => TRUE,
+     ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_numeric',
+    ),
+  );
+
+  //Field: description (varchar 255)   
+  $data['nd_geolocation']['description'] = array(
+    'title' => t('Description'),
+    'help' => t('A short description of a given geolocation.'),
+    'field' => array(
+      'handler' => 'views_handler_field',
+      'click sortable' => TRUE,
+     ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_string',
+    ),
+    'argument' => array(
+      'handler' => 'views_handler_argument_string',
+    ),
+  );
+
+  //Field: latitude (real)   
+  $data['nd_geolocation']['latitude'] = array(
+    'title' => t('Latitude'),
+    'help' => t('The decimal latitude coordinate of the georeference, using positive and negative sign to indicate N and S, respectively.'),
+    'field' => array(
+      'handler' => 'views_handler_field_numeric',
+      'click sortable' => TRUE,
+     ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_numeric',
+    ),
+  );
+
+  //Field: longitude (real)   
+  $data['nd_geolocation']['longitude'] = array(
+    'title' => t('Longitude'),
+    'help' => t('The decimal longitude coordinate of the georeference, using positive and negative sign to indicate E and W, respectively.'),
+    'field' => array(
+      'handler' => 'views_handler_field_numeric',
+      'click sortable' => TRUE,
+     ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_numeric',
+    ),
+  );
+
+  //Field: altitude (real)   
+  $data['nd_geolocation']['altitude'] = array(
+    'title' => t('Altitude'),
+    'help' => t('The altitude (elevation) of the location in meters. If the altitude is only known as a range, this is the average, and altitude_dev will hold half of the width of the range.'),
+    'field' => array(
+      'handler' => 'views_handler_field_numeric',
+      'click sortable' => TRUE,
+     ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_numeric',
+    ),
+  );
+
+  //Field: geodetic_datum (varchar 255)   
+  $data['nd_geolocation']['geodetic_datum'] = array(
+    'title' => t('Deodetic Datum'),
+    'help' => t('The geodetic system on which the geo-reference coordinates are based. For geo-references measured between 1984 and 2010, this will typically be WGS84.'),
+    'field' => array(
+      'handler' => 'views_handler_field',
+      'click sortable' => TRUE,
+     ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_string',
+    ),
+    'argument' => array(
+      'handler' => 'views_handler_argument_string',
+    ),
+  );
+  
+    
+  return $data;
+}

+ 210 - 0
tripal_natural_diversity/views/nd_reagent.views.inc

@@ -0,0 +1,210 @@
+<?php
+
+/*************************************************************************
+ * @file: THIS IS A TEMPLATE AND SHOULD NOT BE INCLUDED IN THE MODULE CODE
+ *
+ *   - Every instance of nd_reagent should be replaced with the name of your table
+ *   - If this is a base table (you want a view where every row is a row from this table)
+ *     then change $data['nd_reagent']['table'] to $data['nd_reagent']['table']['base'] 
+ *     and $data['nd_reagent']['table']['database'] to $data['nd_reagent']['table']['base']['database']
+ *   - Relationships between this table and others: YYY is the table you are trying to join to this
+ *     one. You want to join a table to this one if this table contains a foreign key to the other
+ *     table. If the join between this table and another is through a linking table
+ *     (ie: library-nd_reagent/YYY => library_feature-XY => feature-nd_reagent/YYY) then make the join in both
+ *     directions (ie: in the file nd_reagent.views.inc and the file YYY.views.inc
+ *   - Create a field definition for each field in this table using the example fields already
+ *     listed. Match the type of the database field to the field definition listed below.
+ *     (ie: for a text/varchar field from the database use plain_text_field below)
+ * 
+ *  NOTE: Creating the table definition file is not enough. You also need to call the 
+ *        retrieve_nd_reagent_views_data() function from ../tripal_natural_diversity.views.inc:tripal_natural_diversity_views_data()
+ *        by adding the following line:
+ *           $data = array_merge($data, retrieve_nd_reagent_views_data());
+ *        to the function and including the file directly above the function (blow the function 
+ *        header by adding:
+ *           require_once('views/nd_reagent.views.inc');
+ *
+ *  REMOVE THIS COMMENT IN THE COPY!
+ */ 
+ 
+/**
+ *  @file
+ *  This file defines the data array for a given chado table. This array
+ *  is merged into a larger array containing definitions of all tables associated
+ *  with this module in:
+ *  @see tripal_natural_diversity.views.inc --in tripal_natural_diversity_views_data()
+ *
+ *  Documentation on views integration can be found at 
+ *  http://views2.logrus.com/doc/html/index.html.
+ */
+
+/*************************************************************************
+ * Purpose: this function returns the portion of the data array 
+ *   which describes the nd_reagent table, it's fields and any joins between it and other tables
+ * @see tripal_natural_diversity_views_data() --in tripal_natural_diversity.views.inc
+ *
+ * Table: nd_reagent
+ * @code
+  CREATE TABLE nd_reagent (
+    nd_reagent_id serial PRIMARY KEY NOT NULL,
+    name character varying(80) NOT NULL,
+    type_id integer NOT NULL references cvterm (cvterm_id) on delete cascade INITIALLY DEFERRED,
+    feature_id integer
+  );
+ * @endcode
+ */
+ function retrieve_nd_reagent_views_data() {
+  global $db_url;
+  $data = array();
+
+  // if the chado database is not local to the drupal database
+  // then we need to set the database name.  This should always
+  // be 'chado'.
+  if(is_array($db_url) and array_key_exists('chado',$db_url)){
+    $database = 'chado';
+  }
+   
+  //Basic table definition-----------------------------------
+  $data['nd_reagent']['table']['group'] = t('Chado ND Reagent');
+  
+  $data['nd_reagent']['table'] = array(
+    'field' => 'nd_reagent_id',
+    'title' => t('Chado ND Reagent'),
+    'help' => t('Enter some user-friendly description of this tables purpose to the user.'),
+  );
+  if($database){
+     $data['nd_reagent']['table']['database'] = $database;
+  }
+
+  
+  //Relationship Definitions---------------------------------
+  //Join: nd_reagent => YYY
+  $data['nd_reagent']['table']['join']['YYY'] = array(
+    'left_field' => 'primary key in YYY table',
+    'field' => 'foreign key in nd_reagent table',
+  );  
+  
+  //Join: nd_reagent => XY => YYY
+  $data['nd_reagent']['table']['join']['XY'] = array(
+    'left_field' => 'matching nd_reagent key in the XY table',
+    'field' => 'foreign key in nd_reagent table',
+  );  
+  $data['nd_reagent']['table']['join']['YYY'] => array(
+    'left_table' => 'XY',
+    'left_field' => 'matching nd_reagent key in the XY table',
+    'field' => 'foreign key in nd_reagent table',
+  );
+  $data['XY']['table']['join']['YYY'] = array(
+    'left_field' => 'foreign key in YYY table',
+    'field' => 'matching YYY key in the XY table',
+  );
+   
+  //Table Field Definitions----------------------------------
+      
+  //Field: nd_reagent_id (primary key)
+  $data['nd_reagent']['field_name'] = array(
+    'title' => t('nd_reagent Primary Key'),
+    'help' => t('A unique index for every nd_reagent.'),
+    'field' => array(
+      'handler' => 'views_handler_field_numeric',
+      'click sortable' => TRUE,
+     ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_numeric',
+    ),
+  );
+
+  /*.......................................................
+   * Beginning of Example Field definitions
+   * Remove this section when done
+   */
+
+  //Field: plain_text_field (chado datatype)   
+  $data['nd_reagent']['plain_text_field'] = array(
+    'title' => t('Human-Readable Name'),
+    'help' => t('Description of this field.'),
+    'field' => array(
+      'handler' => 'views_handler_field',
+      'click sortable' => TRUE,
+     ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_string',
+    ),
+    'argument' => array(
+      'handler' => 'views_handler_argument_string',
+    ),
+  );
+
+  //Field: numeric_field (chado datatype)   
+  $data['nd_reagent']['numeric_field'] = array(
+    'title' => t('Human-Readable Name'),
+    'help' => t('Description of this field.'),
+    'field' => array(
+      'handler' => 'views_handler_field_numeric',
+      'click sortable' => TRUE,
+     ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_numeric',
+    ),
+  );
+
+  //Field: boolean_field (chado datatype)   
+  $data['nd_reagent']['boolean_field'] = array(
+    'title' => t('Human-Readable Name'),
+    'help' => t('Description of this field.'),
+    'field' => array(
+      'handler' => 'views_handler_field_boolean',
+      'click sortable' => TRUE,
+     ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_boolean_operator',
+    ),
+  );
+
+  //Field: unix_timestamp (chado datatype)   
+  $data['nd_reagent']['unix_timestamp'] = array(
+    'title' => t('Human-Readable Name'),
+    'help' => t('Description of this field.'),
+    'field' => array(
+      'handler' => 'views_handler_field_date',
+      'click sortable' => TRUE,
+     ),
+    'sort' => array(
+      'handler' => 'views_handler_sort_date',
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_date',
+    ),
+  );
+
+  //Field: human_readable_date (chado datatype)   
+  $data['nd_reagent']['human_readable_date'] = array(
+    'title' => t('Human-Readable Name'),
+    'help' => t('Description of this field.'),
+    'field' => array(
+      'handler' => 'views_handler_field_readble_date',
+      'click sortable' => TRUE,
+     ),
+    'sort' => array(
+      'handler' => 'views_handler_sort_date',
+    ),
+  );
+   
+   /*
+    * End of Example Field definitions
+    *......................................................./
+    
+  return $data;
+}

+ 205 - 0
tripal_natural_diversity/views/nd_reagent_relationship.views.inc

@@ -0,0 +1,205 @@
+<?php
+
+/*************************************************************************
+ * @file: THIS IS A TEMPLATE AND SHOULD NOT BE INCLUDED IN THE MODULE CODE
+ *
+ *   - Every instance of nd_reagent_relationship should be replaced with the name of your table
+ *   - If this is a base table (you want a view where every row is a row from this table)
+ *     then change $data['nd_reagent_relationship']['table'] to $data['nd_reagent_relationship']['table']['base'] 
+ *     and $data['nd_reagent_relationship']['table']['database'] to $data['nd_reagent_relationship']['table']['base']['database']
+ *   - Relationships between this table and others: YYY is the table you are trying to join to this
+ *     one. You want to join a table to this one if this table contains a foreign key to the other
+ *     table. If the join between this table and another is through a linking table
+ *     (ie: library-nd_reagent_relationship/YYY => library_feature-XY => feature-nd_reagent_relationship/YYY) then make the join in both
+ *     directions (ie: in the file nd_reagent_relationship.views.inc and the file YYY.views.inc
+ *   - Create a field definition for each field in this table using the example fields already
+ *     listed. Match the type of the database field to the field definition listed below.
+ *     (ie: for a text/varchar field from the database use plain_text_field below)
+ * 
+ *  NOTE: Creating the table definition file is not enough. You also need to call the 
+ *        retrieve_nd_reagent_relationship_views_data() function from ../tripal_natural_diversity.views.inc:tripal_natural_diversity_views_data()
+ *        by adding the following line:
+ *           $data = array_merge($data, retrieve_nd_reagent_relationship_views_data());
+ *        to the function and including the file directly above the function (blow the function 
+ *        header by adding:
+ *           require_once('views/nd_reagent_relationship.views.inc');
+ *
+ *  REMOVE THIS COMMENT IN THE COPY!
+ */ 
+ 
+/**
+ *  @file
+ *  This file defines the data array for a given chado table. This array
+ *  is merged into a larger array containing definitions of all tables associated
+ *  with this module in:
+ *  @see tripal_natural_diversity.views.inc --in tripal_natural_diversity_views_data()
+ *
+ *  Documentation on views integration can be found at 
+ *  http://views2.logrus.com/doc/html/index.html.
+ */
+
+/*************************************************************************
+ * Purpose: this function returns the portion of the data array 
+ *   which describes the nd_reagent_relationship table, it's fields and any joins between it and other tables
+ * @see tripal_natural_diversity_views_data() --in tripal_natural_diversity.views.inc
+ *
+ * Table: nd_reagent_relationship
+ * @code
+ * nd_reagent_relationship-Copy/Paste Table SQL code here-nd_reagent_relationship
+ * @endcode
+ */
+ function retrieve_nd_reagent_relationship_views_data() {
+  global $db_url;
+  $data = array();
+
+  // if the chado database is not local to the drupal database
+  // then we need to set the database name.  This should always
+  // be 'chado'.
+  if(is_array($db_url) and array_key_exists('chado',$db_url)){
+    $database = 'chado';
+  }
+   
+  //Basic table definition-----------------------------------
+  $data['nd_reagent_relationship']['table']['group'] = t('Chado nd_reagent_relationship');
+  
+  $data['nd_reagent_relationship']['table'] = array(
+    'field' => 'primary_id',
+    'title' => t('Chado nd_reagent_relationship'),
+    'help' => t('Enter some user-friendly description of this tables purpose to the user.'),
+  );
+  if($database){
+     $data['nd_reagent_relationship']['table']['database'] = $database;
+  }
+
+  
+  //Relationship Definitions---------------------------------
+  //Join: nd_reagent_relationship => YYY
+  $data['nd_reagent_relationship']['table']['join']['YYY'] = array(
+    'left_field' => 'primary key in YYY table',
+    'field' => 'foreign key in nd_reagent_relationship table',
+  );  
+  
+  //Join: nd_reagent_relationship => XY => YYY
+  $data['nd_reagent_relationship']['table']['join']['XY'] = array(
+    'left_field' => 'matching nd_reagent_relationship key in the XY table',
+    'field' => 'foreign key in nd_reagent_relationship table',
+  );  
+  $data['nd_reagent_relationship']['table']['join']['YYY'] => array(
+    'left_table' => 'XY',
+    'left_field' => 'matching nd_reagent_relationship key in the XY table',
+    'field' => 'foreign key in nd_reagent_relationship table',
+  );
+  $data['XY']['table']['join']['YYY'] = array(
+    'left_field' => 'foreign key in YYY table',
+    'field' => 'matching YYY key in the XY table',
+  );
+   
+  //Table Field Definitions----------------------------------
+      
+  //Field: nd_reagent_relationship_id (primary key)
+  $data['nd_reagent_relationship']['field_name'] = array(
+    'title' => t('nd_reagent_relationship Primary Key'),
+    'help' => t('A unique index for every nd_reagent_relationship.'),
+    'field' => array(
+      'handler' => 'views_handler_field_numeric',
+      'click sortable' => TRUE,
+     ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_numeric',
+    ),
+  );
+
+  /*.......................................................
+   * Beginning of Example Field definitions
+   * Remove this section when done
+   */
+
+  //Field: plain_text_field (chado datatype)   
+  $data['nd_reagent_relationship']['plain_text_field'] = array(
+    'title' => t('Human-Readable Name'),
+    'help' => t('Description of this field.'),
+    'field' => array(
+      'handler' => 'views_handler_field',
+      'click sortable' => TRUE,
+     ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_string',
+    ),
+    'argument' => array(
+      'handler' => 'views_handler_argument_string',
+    ),
+  );
+
+  //Field: numeric_field (chado datatype)   
+  $data['nd_reagent_relationship']['numeric_field'] = array(
+    'title' => t('Human-Readable Name'),
+    'help' => t('Description of this field.'),
+    'field' => array(
+      'handler' => 'views_handler_field_numeric',
+      'click sortable' => TRUE,
+     ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_numeric',
+    ),
+  );
+
+  //Field: boolean_field (chado datatype)   
+  $data['nd_reagent_relationship']['boolean_field'] = array(
+    'title' => t('Human-Readable Name'),
+    'help' => t('Description of this field.'),
+    'field' => array(
+      'handler' => 'views_handler_field_boolean',
+      'click sortable' => TRUE,
+     ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_boolean_operator',
+    ),
+  );
+
+  //Field: unix_timestamp (chado datatype)   
+  $data['nd_reagent_relationship']['unix_timestamp'] = array(
+    'title' => t('Human-Readable Name'),
+    'help' => t('Description of this field.'),
+    'field' => array(
+      'handler' => 'views_handler_field_date',
+      'click sortable' => TRUE,
+     ),
+    'sort' => array(
+      'handler' => 'views_handler_sort_date',
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_date',
+    ),
+  );
+
+  //Field: human_readable_date (chado datatype)   
+  $data['nd_reagent_relationship']['human_readable_date'] = array(
+    'title' => t('Human-Readable Name'),
+    'help' => t('Description of this field.'),
+    'field' => array(
+      'handler' => 'views_handler_field_readble_date',
+      'click sortable' => TRUE,
+     ),
+    'sort' => array(
+      'handler' => 'views_handler_sort_date',
+    ),
+  );
+   
+   /*
+    * End of Example Field definitions
+    *......................................................./
+    
+  return $data;
+}

+ 205 - 0
tripal_natural_diversity/views/nd_reagentprop.views.inc

@@ -0,0 +1,205 @@
+<?php
+
+/*************************************************************************
+ * @file: THIS IS A TEMPLATE AND SHOULD NOT BE INCLUDED IN THE MODULE CODE
+ *
+ *   - Every instance of nd_reagentprop should be replaced with the name of your table
+ *   - If this is a base table (you want a view where every row is a row from this table)
+ *     then change $data['nd_reagentprop']['table'] to $data['nd_reagentprop']['table']['base'] 
+ *     and $data['nd_reagentprop']['table']['database'] to $data['nd_reagentprop']['table']['base']['database']
+ *   - Relationships between this table and others: YYY is the table you are trying to join to this
+ *     one. You want to join a table to this one if this table contains a foreign key to the other
+ *     table. If the join between this table and another is through a linking table
+ *     (ie: library-nd_reagentprop/YYY => library_feature-XY => feature-nd_reagentprop/YYY) then make the join in both
+ *     directions (ie: in the file nd_reagentprop.views.inc and the file YYY.views.inc
+ *   - Create a field definition for each field in this table using the example fields already
+ *     listed. Match the type of the database field to the field definition listed below.
+ *     (ie: for a text/varchar field from the database use plain_text_field below)
+ * 
+ *  NOTE: Creating the table definition file is not enough. You also need to call the 
+ *        retrieve_nd_reagentprop_views_data() function from ../tripal_natural_diversity.views.inc:tripal_natural_diversity_views_data()
+ *        by adding the following line:
+ *           $data = array_merge($data, retrieve_nd_reagentprop_views_data());
+ *        to the function and including the file directly above the function (blow the function 
+ *        header by adding:
+ *           require_once('views/nd_reagentprop.views.inc');
+ *
+ *  REMOVE THIS COMMENT IN THE COPY!
+ */ 
+ 
+/**
+ *  @file
+ *  This file defines the data array for a given chado table. This array
+ *  is merged into a larger array containing definitions of all tables associated
+ *  with this module in:
+ *  @see tripal_natural_diversity.views.inc --in tripal_natural_diversity_views_data()
+ *
+ *  Documentation on views integration can be found at 
+ *  http://views2.logrus.com/doc/html/index.html.
+ */
+
+/*************************************************************************
+ * Purpose: this function returns the portion of the data array 
+ *   which describes the nd_reagentprop table, it's fields and any joins between it and other tables
+ * @see tripal_natural_diversity_views_data() --in tripal_natural_diversity.views.inc
+ *
+ * Table: nd_reagentprop
+ * @code
+ * nd_reagentprop-Copy/Paste Table SQL code here-nd_reagentprop
+ * @endcode
+ */
+ function retrieve_nd_reagentprop_views_data() {
+  global $db_url;
+  $data = array();
+
+  // if the chado database is not local to the drupal database
+  // then we need to set the database name.  This should always
+  // be 'chado'.
+  if(is_array($db_url) and array_key_exists('chado',$db_url)){
+    $database = 'chado';
+  }
+   
+  //Basic table definition-----------------------------------
+  $data['nd_reagentprop']['table']['group'] = t('Chado nd_reagentprop');
+  
+  $data['nd_reagentprop']['table'] = array(
+    'field' => 'primary_id',
+    'title' => t('Chado nd_reagentprop'),
+    'help' => t('Enter some user-friendly description of this tables purpose to the user.'),
+  );
+  if($database){
+     $data['nd_reagentprop']['table']['database'] = $database;
+  }
+
+  
+  //Relationship Definitions---------------------------------
+  //Join: nd_reagentprop => YYY
+  $data['nd_reagentprop']['table']['join']['YYY'] = array(
+    'left_field' => 'primary key in YYY table',
+    'field' => 'foreign key in nd_reagentprop table',
+  );  
+  
+  //Join: nd_reagentprop => XY => YYY
+  $data['nd_reagentprop']['table']['join']['XY'] = array(
+    'left_field' => 'matching nd_reagentprop key in the XY table',
+    'field' => 'foreign key in nd_reagentprop table',
+  );  
+  $data['nd_reagentprop']['table']['join']['YYY'] => array(
+    'left_table' => 'XY',
+    'left_field' => 'matching nd_reagentprop key in the XY table',
+    'field' => 'foreign key in nd_reagentprop table',
+  );
+  $data['XY']['table']['join']['YYY'] = array(
+    'left_field' => 'foreign key in YYY table',
+    'field' => 'matching YYY key in the XY table',
+  );
+   
+  //Table Field Definitions----------------------------------
+      
+  //Field: nd_reagentprop_id (primary key)
+  $data['nd_reagentprop']['field_name'] = array(
+    'title' => t('nd_reagentprop Primary Key'),
+    'help' => t('A unique index for every nd_reagentprop.'),
+    'field' => array(
+      'handler' => 'views_handler_field_numeric',
+      'click sortable' => TRUE,
+     ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_numeric',
+    ),
+  );
+
+  /*.......................................................
+   * Beginning of Example Field definitions
+   * Remove this section when done
+   */
+
+  //Field: plain_text_field (chado datatype)   
+  $data['nd_reagentprop']['plain_text_field'] = array(
+    'title' => t('Human-Readable Name'),
+    'help' => t('Description of this field.'),
+    'field' => array(
+      'handler' => 'views_handler_field',
+      'click sortable' => TRUE,
+     ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_string',
+    ),
+    'argument' => array(
+      'handler' => 'views_handler_argument_string',
+    ),
+  );
+
+  //Field: numeric_field (chado datatype)   
+  $data['nd_reagentprop']['numeric_field'] = array(
+    'title' => t('Human-Readable Name'),
+    'help' => t('Description of this field.'),
+    'field' => array(
+      'handler' => 'views_handler_field_numeric',
+      'click sortable' => TRUE,
+     ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_numeric',
+    ),
+  );
+
+  //Field: boolean_field (chado datatype)   
+  $data['nd_reagentprop']['boolean_field'] = array(
+    'title' => t('Human-Readable Name'),
+    'help' => t('Description of this field.'),
+    'field' => array(
+      'handler' => 'views_handler_field_boolean',
+      'click sortable' => TRUE,
+     ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_boolean_operator',
+    ),
+  );
+
+  //Field: unix_timestamp (chado datatype)   
+  $data['nd_reagentprop']['unix_timestamp'] = array(
+    'title' => t('Human-Readable Name'),
+    'help' => t('Description of this field.'),
+    'field' => array(
+      'handler' => 'views_handler_field_date',
+      'click sortable' => TRUE,
+     ),
+    'sort' => array(
+      'handler' => 'views_handler_sort_date',
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_date',
+    ),
+  );
+
+  //Field: human_readable_date (chado datatype)   
+  $data['nd_reagentprop']['human_readable_date'] = array(
+    'title' => t('Human-Readable Name'),
+    'help' => t('Description of this field.'),
+    'field' => array(
+      'handler' => 'views_handler_field_readble_date',
+      'click sortable' => TRUE,
+     ),
+    'sort' => array(
+      'handler' => 'views_handler_sort_date',
+    ),
+  );
+   
+   /*
+    * End of Example Field definitions
+    *......................................................./
+    
+  return $data;
+}

+ 1 - 1
tripal_project/tripal_project.info

@@ -6,4 +6,4 @@ description = A module for interfacing the GMOD chado database with Drupal, prov
 core = 6.x
 
 dependencies[] = tripal_core
-dependencies[] = cv
+dependencies[] = tripal_cv

+ 112 - 151
tripal_stock/tripal_stock.api.inc

@@ -9,7 +9,6 @@
  *
  * @return stock object created by node load
  */
-//function get_chado_stock($nid=0, $stock_id=0) {
 function triapl_stock_get_stock_by_nid ($nid) {
 	
 	return node_load($nid);
@@ -21,15 +20,14 @@ function triapl_stock_get_stock_by_nid ($nid) {
  *
  * @return stock object created by node load
  */
-//function get_chado_stock($nid=0, $stock_id=0) {
-function triapl_stock_get_stock_by_stock_id ($stock_id) {
+function tripal_stock_get_stock_by_stock_id ($stock_id) {
 
   $sql = "SELECT nid FROM {chado_stock} WHERE stock_id=%d";
   $r = db_fetch_object(db_query($sql, $stock_id));
   if (!empty($r->nid)) {
     return node_load($r->nid);
   } else {
-    drupal_set_message("Function: triapl_stock_get_stock_by_stock_id() -no stock with that stock id sync'd with drupal", 'error');
+    watchdog('tripal_stock', 'tripal_stock_get_stock_by_stock_id(!stock_id): no stock with that stock_id is sync\'d with drupal', array('!stock_id' => $stock_id), WATCHDOG_WARNING);
 	}
 
 	return 0;
@@ -43,11 +41,9 @@ function triapl_stock_get_stock_by_stock_id ($stock_id) {
 /*************************************************************************
  * Purpose: Returns all stocks currently sync'd with drupal
  *
- * @return array(
- *			<stock_id> => <stock object created by node load>
- *		)
+ * @return 
+     An array of node objects keyed by stock_id
  */
-//function get_all_chado_stocks() {
 function tripal_stock_get_all_stocks() {
 	$sql = "SELECT stock_id, nid from {chado_stock}";
 	$resource = db_query($sql);
@@ -61,152 +57,117 @@ function tripal_stock_get_all_stocks() {
 /*************************************************************************
  * Purpose: Return all stocks that match a given criteria
  *
- * @params Criteria = array(
- *						<column name> => array( 
- *							'value'=> <value of column>, 
- *							'regex' => <False if exact value, TRUE otherwise>,
- *							'type' => <INT or STRING>
- *						)
- *         )
- *			column name can be any of those for the stock table
- *			additional column names include: accession, synonym
- *	if you don't know which column the value is from and want to match
- *		the value against any of name, uniquename, dbxref accessions, and synonyms
- *   	use 'unknown' => array(
- *												'value' => <value to search for>,
- *												'columns' => array of columns to search 
- *											)
- *		where columns can be any combination of 'name', 'uniquename', 'stock','accession', or 'synonym'
- * @params match_type: can be either ALL or ANY
- *	 where ALL= mathcing stocks must match all criteria supplied
- *				 ANY= matching stock need only match ONE of the supplied criteria
- * @return an array of matching stock objects (produced using node_load)
+ * @params $values
+ *   An associative array containing the values for filtering the results.
+ * @return 
+ *   An array of matching stock objects (produced using node_load)
  *   matching the given criteria
+ *
+ * Example usage:
+ * @code
+ *   $values =  array(
+ *     'organism_id' => array(
+ *         'genus' => 'Lens',
+ *         'species' => 'culinaris',
+ *      ),
+ *     'name' => 'CDC Redberry',
+ *     'type_id' => array (
+ *         'cv_id' => array (
+ *            'name' => 'germplasm',
+ *         ),
+ *         'name' => 'registered_cultivar',
+ *         'is_obsolete' => 0
+ *      ),
+ *   );
+ *   $result = tripal_stock_get_stocks($values);
+ * @endcode
+ * The above code selects a record from the chado stock table using three fields with values which
+ * identify a stock or multiple stocks. Then the node for each stock identified is returned, if it
+ * exists. The $values array is nested such that the organism is identified by way of the 
+ * organism_id foreign key constraint by specifying the genus and species.  The cvterm is also 
+ * specified using its foreign key and the cv_id for the cvterm is nested as well.
  */
-//function get_chado_stocks($criteria, $match_type, $organism_id = NULL, $error_checking = FALSE) {
-function tripal_stock_get_stocks($criteria, $match_type, $organism_id = NULL, $error_checking = FALSE) {
+function tripal_stock_get_stocks($values) {
 
-	//Deal with unknown column----------------------------
-	if (!empty($criteria['unknown'])) {
-		if ($error_checking) { drupal_set_message('Uknown provided','warning');}
-		$unknown_provided = TRUE;
-		$new_criteria = array();
-		foreach ($criteria['unknown']['columns'] as $column_name) {
-			if (in_array($column_name, array('stock_id','dbxref_id','organism_id','type_id') )) {
-				if (preg_match('/^\d+$/',$criteria['unknown']['value'])) {
-					$new_criteria[$column_name] = array('type'=>'INT','value' => $criteria['unknown']['value']);
-				}
-			} else {
-				$new_criteria[$column_name] = array('type'=>'STRING','value' => $criteria['unknown']['value'], 'regex'=>TRUE);
-			}
-		}
-		if ($error_checking) { drupal_set_message('Re-calling tripal_stock_get_stocks(<pre>'.print_r($new_criteria,TRUE).'</pre>, ANY, '.$organism_id.')','warning'); }
-		$unknown_stocks = tripal_stock_get_stocks($new_criteria, 'ANY', $organism_id, $error_checking);
-		if ($error_checking) { drupal_set_message(sizeof($unknown_stocks).' Unknown Stocks', 'warning'); }
-	}
-	unset($criteria['unknown']); //so it's not mistaken as a column in the stock table
-	
-	// Determine all criteria------------------------------
-	$where = array(); // parts of the where portion of the SQL query
-	$joins = array(); //joins between the stock table and other tables
-	foreach ($criteria as $column_name => $v) {
-		if (preg_match("/accession/i",$column_name)) {
-			if ($v['regex']) { $operator = '~'; }
-			else { $operator = '='; }
-			
-			$where[] = 'dbxref.accession'.$operator."'".$v['value']."'";
-			$joins[] = 'LEFT JOIN stock_dbxref stock_dbxref ON stock_dbxref.stock_id=stock.stock_id';
-			$joins[] = 'LEFT JOIN dbxref dbxref ON dbxref.dbxref_id=stock_dbxref.dbxref_id';
-			
-		} elseif (preg_match("/synonym/i",$column_name)) {
-			if ($v['regex']) { $operator = '~'; }
-			else { $operator = '='; }
-			
-			$synonym_cvterm = tripal_cv_get_cvterm_by_name('synonym', variable_get('chado_stock_prop_types_cv', 'null'));
-			$where[] = '(stockprop.type_id='.$synonym_cvterm->cvterm_id.' AND stockprop.value'.$operator."'".$v['value']."')";
-			$joins[] = 'LEFT JOIN stockprop stockprop ON stockprop.stock_id=stock.stock_id';
-	
-		} else {
-			if ($v['regex']) { $operator = '~'; }
-			else { $operator = '='; }
-			
-			if (preg_match('/INT/', $v['type'])) {
-				$where[] = 'stock.'.$column_name.'='.$v['value'];
-			} else {
-				$where[] = 'stock.'.$column_name.$operator."'".$v['value']."'";
-			}		
-		}
-	}
-	
-	if ($error_checking) { 
-		drupal_set_message('Where Conditions: <pre>'.print_r($where,TRUE).'</pre>', 'warning'); 
-		drupal_set_message('Left Joins: <pre>'.print_r($joins,TRUE).'</pre>', 'warning');
-	}
-	
-	//Build query-----------------------------------------
-	if (preg_match('/ANY/', $match_type)) {
-		$where_string = implode(' OR ',$where);
-		if ($organism_id) {
-			$where_string = '('.$where_string.') AND organism_id='.$organism_id;
-		}
-	} else {
-		$where_string = implode(' AND ',$where);
-		if ($organism_id) {
-			$where_string .= ' AND organism_id='.$organism_id; }
-	}
-	
-	if (sizeof($where) >= 1) {
-		$execute_query = TRUE;
-		$sql_query = 'SELECT stock.stock_id FROM stock '.implode(' ',$joins).' WHERE '.$where_string;
-		//drupal_set_message('Query='.$sql_query);
-	} elseif (!$unknown_provided) {	
-		$execute_query = TRUE;
-		$sql_query = 'SELECT stock.stock_id FROM stock';
-		drupal_set_message('You did not enter any criteria during the stock selection process. All stocks will be returned.','warning');
-	} else {
-		$execute_query = FALSE;
-	}
-	
-	if ($error_checking) { drupal_set_message('Query: '.$sql_query, 'warning'); }
-	
-	if ($execute_query) {
-		//Get stock_ids---------------------------------------
-		$previous_db = tripal_db_set_active('chado');
-		$resource = db_query($sql_query);
-		tripal_db_set_active($previous_db);
-	
-		$stock_ids = array();	
-		while ($r = db_fetch_object($resource)) {
-			$stock_ids[] = $r->stock_id;
-		}
-		$stock_ids = array_unique($stock_ids);
-		
-		if ($error_checking) { drupal_set_message('Stock IDs: <pre>'.print_r($stock_ids,TRUE).'</pre>', 'warning'); }
-	
-		//Get Stocks------------------------------------------
-		if (!empty($stock_ids)) {
-			$resource = db_query("SELECT nid FROM {chado_stock} WHERE stock_id IN (%s)",implode(',',$stock_ids));
-			$main_stocks = array();
-			while ($r = db_fetch_object($resource)) {
-				$main_stocks[] = node_load($r->nid);
-			}
-		}
-	}
-	
-	if (!empty($main_stocks)) {
-		if(!empty($unknown_stocks)){
-			return array_merge($unknown_stocks,$main_stocks);
-		} else {
-			return $main_stocks;
-		}
-	} else {
-		if(!empty($unknown_stocks)){
-			return $unknown_stocks;
-		} else {
-			//drupal_set_message('No Stocks matched the given criteria','warning');
-			return array();
-		}
-	}
+  $stock_ids = tripal_core_chado_select('stock',array('stock_id'),$values);
 
+  // Change from stock_ids to nodes-----------------------------------
+  $stock_ids = array_filter($stock_ids);
+  $stock_ids = array_unique($stock_ids);
+  
+  $stocks = array();
+  foreach ($stock_ids as $stock_id) {
+    $stocks[] = tripal_stock_get_stock_by_stock_id($stock_id->stock_id);
+  }
+  
+  return $stocks;
 }
 
+/*************************************************************************
+ * Purpose: Return all stocks with a given name identifier
+ *  which might match stock.name, stock.uniquename, dbxref.accession, 
+ *  stockprop.value where stockprop.type='synonym'
+ *
+ * @param $name
+ *   The name identfier to be used
+ * @params $organism_id
+ *   The stock.organism_id of the stock to be selected
+ * @return
+ *   An array of stock node objects
+ */
+function tripal_stock_get_stock_by_name_identifier($name, $organism_id) {
+  $stock_ids = array();
+  
+  // where name_identifier = stock.name-------------------------------
+  $current_stocks = tripal_core_chado_select('stock',array('stock_id'),
+    array(
+      'name' => $name,
+      'organism_id' => $organism_id,
+    )
+  );
+  if (!empty($current_stocks)) {
+    $stock_ids = array_merge($stock_ids, $current_stocks);
+  }
+
+  // where name_identifier = stock.uniquename-------------------------------
+  $current_stocks = tripal_core_chado_select('stock',array('stock_id'),
+    array(
+      'uniquename' => $name,
+      'organism_id' => $organism_id,
+    )
+  );
+  if (!empty($current_stocks)) {
+    $stock_ids = array_merge($stock_ids, $current_stocks);
+  }
+  
+  // where name_identifier = dbxref.accession-------------------------------
+  // linked to stock through stock.dbxref
+  $current_stocks = tripal_core_chado_select('stock',array('stock_id'),
+    array(
+      'dbxref_id' => array(
+        'accession' => $name,
+      ),
+      'organism_id' => $organism_id,
+    )
+  );
+  if (!empty($current_stocks)) {
+    $stock_ids = array_merge($stock_ids, $current_stocks);  
+  }
+  
+  // linked to stock through stock_dbxref?
+  
+  // where name_identifier = stockprop.value-------------------------------
+  // where type='synonym' 
+  
+  
+  // Change from stock_ids to nodes-----------------------------------
+  $stock_ids = array_filter($stock_ids);
+  $stock_ids = array_unique($stock_ids);
+  
+  $stocks = array();
+  foreach ($stock_ids as $stock_id) {
+    $stocks[] = tripal_stock_get_stock_by_stock_id($stock_id->stock_id);
+  }
+  
+  return $stocks;
+}