Browse Source

Fixed memory leaks on syncing

Ficklin 10 years ago
parent
commit
846b344b19

+ 11 - 9
tripal_contact/includes/tripal_contact.chado_node.inc

@@ -250,19 +250,19 @@ function chado_contact_validate($node, $form, &$form_state) {
   // Since this validate can be called on AJAX and Deletion of the node
   // we need to make this check to ensure queries are not executed
   // without the proper values.
-  if($node->op != 'Save') {
+  if(property_exists($node, "op") and $node->op != 'Save') {
     return;
   }
 
   // we are syncing if we do not have a node ID but we do have a contact_id. We don't
   // need to validate during syncing so just skip it.
-  if (is_null($node->nid) and property_exists($node, 'contact_id') and $node->contact_id != 0) {
+  if (!property_exists($node, 'nid') and property_exists($node, 'contact_id') and $node->contact_id != 0) {
     return;
   }
 
   // remove surrounding white-space on submitted values
-  $node->contactname    = trim($node->contactname);
-  $node->description    = trim($node->description);
+  $node->contactname = property_exists($node, 'contactname') ? trim($node->contactname) : '';
+  $node->description = property_exists($node, 'description') ? trim($node->description) : '';
 
 
   // Validating for an update
@@ -367,15 +367,17 @@ function chado_contact_node_access($node, $op, $account ) {
  * @ingroup tripal_contact
  */
 function chado_contact_insert($node) {
-
-  // remove surrounding white-space on submitted values
-  $node->contactname    = trim($node->contactname);
-  $node->description    = trim($node->description);
+  $contact_id = '';
 
   // if there is a contact_id in the $node object then this must be a sync so
   // we can skip adding the contact as it is already there, although
-  // we do need to proceed with the rest of the insert
+  // we do need to proceed with insertion into the chado/drupal linking table.
   if (!property_exists($node, 'contact_id')) {
+    // remove surrounding white-space on submitted values
+    $node->contactname    = trim($node->contactname);
+    $node->description    = trim($node->description);
+
+
     // insert and then get the newly inserted contact record
     $values = array(
       'name'           => $node->contactname,

+ 29 - 28
tripal_core/api/tripal_core.chado_nodes.api.inc

@@ -115,7 +115,7 @@ function chado_node_get_base_table($content_type, $module = FALSE) {
   }
   else {
     $node_types = module_invoke_all('node_info');
-  
+
     if (isset($node_types[$content_type])) {
       $node_info = $node_types[$content_type];
     }
@@ -123,7 +123,7 @@ function chado_node_get_base_table($content_type, $module = FALSE) {
       return FALSE;
     }
   }
-  
+
   if (isset($node_info['chado_node_api']['base_table'])) {
     return $node_info['chado_node_api']['base_table'];
   }
@@ -456,18 +456,18 @@ function chado_node_sync_form_submit($form, $form_state) {
 
 /**
  * Generic function for syncing records in Chado with Drupal nodes.
- * 
+ *
  * @param $base_table
  *   The name of the Chado table containing the record that should be synced
  * @param $max_sync
- *   Optional: A numeric value to indicate the maximum number of records to sync. 
+ *   Optional: A numeric value to indicate the maximum number of records to sync.
  * @param $organism_id
  *   Optional: Limit the list of records to be synced to only those that
  *   are associated with this organism_id. If the record is not assocaited
  *   with an organism then this field is not needed.
  * @param $types
  *   Optional: Limit the list of records to be synced to only those that
- *   match the types listed in this array. 
+ *   match the types listed in this array.
  * @param $ids
  *   Optional:  Limit the list of records to bye synced to only those whose
  *   primary key value matches the ID provided in this array.
@@ -480,7 +480,7 @@ function chado_node_sync_form_submit($form, $form_state) {
  * @param $job_id
  *   Optional. Used by the Trpial Jobs system when running this function
  *   as a job. It is not needed othewise.
- * 
+ *
  * @ingroup tripal_chado_node_api
  */
 function chado_node_sync_records($base_table, $max_sync = FALSE, $organism_id = FALSE,
@@ -539,9 +539,9 @@ function chado_node_sync_records($base_table, $max_sync = FALSE, $organism_id =
   $hook_query_alter = $linking_table . '_chado_node_sync_select_query';
   if (function_exists($hook_query_alter)) {
     $update = call_user_func($hook_query_alter, array(
-      'select' => $select, 
-      'joins' => $joins, 
-      'where_clauses' => $where_clauses, 
+      'select' => $select,
+      'joins' => $joins,
+      'where_clauses' => $where_clauses,
       'where_args' => $where_args,
     ));
     // Now add in any new changes
@@ -573,7 +573,7 @@ function chado_node_sync_records($base_table, $max_sync = FALSE, $organism_id =
     $where .= ') ';
     $sql_args = array_merge($sql_args, $where_args[$category]);
   }
-  
+
   if ($where) {
     $query .= $where;
   }
@@ -610,16 +610,17 @@ function chado_node_sync_records($base_table, $max_sync = FALSE, $organism_id =
       "If the sync fails or is terminated prematurely then the entire set of \n" .
       "synced items is rolled back and will not be found in the database\n\n";
   try {
+    $percent = 0;
     foreach ($results as $record) {
 
       //print "\nLoading $base_table " . ($i + 1) . " of $count ($base_table_id=" . $record->{$base_table_id} . ")...";
 
       // update the job status every 1% features
-      if ($job_id and $i % $interval == 0) {
+      //if ($job_id and $i % $interval == 0) {
         $percent = sprintf("%.2f", (($i + 1) / $count) * 100);
         print "Syncing $base_table " . ($i + 1) . " of $count (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes.\r";
         tripal_set_job_progress($job_id, intval(($i/$count)*100));
-      }
+      //}
 
       // Check if it is in the chado linking table (ie: check to see if it is already linked to a node)
       $result = db_select($linking_table, 'lnk')
@@ -652,6 +653,7 @@ function chado_node_sync_records($base_table, $max_sync = FALSE, $organism_id =
         $form = array();
         $form_state = array();
         node_validate($new_node, $form, $form_state);
+
         if (!form_get_errors()) {
           $node = node_submit($new_node);
           node_save($node);
@@ -665,14 +667,13 @@ function chado_node_sync_records($base_table, $max_sync = FALSE, $organism_id =
     }
     print "\n\nComplete!\n";
   }
-  
+
   catch (Exception $e) {
     $transaction->rollback();
     print "\n"; // make sure we start errors on new line
     watchdog_exception('trp-fsync', $e);
     print "FAILED: Rolling back database changes...\n";
   }
-
 }
 
 /**
@@ -883,43 +884,43 @@ function hook_chado_node_sync_form_submit ($form, $form_state) {
  * Note: For your own module, replace hook in the function name with the machine-name of
  * your chado node type (ie: chado_feature).
  *
- * @param $query 
- *   An array containing the following: 
+ * @param $query
+ *   An array containing the following:
  *    'select': An array of select clauses
- *    'joins:  An array of joins (ie: a single join could be 
+ *    'joins:  An array of joins (ie: a single join could be
  *      'LEFT JOIN {chadotable} alias ON base.id=alias.id')
- *    'where_clauses: An array of where clauses which will all be AND'ed 
+ *    'where_clauses: An array of where clauses which will all be AND'ed
  *      together. Use :placeholders for values.
- *    'where_args: An associative array of arguments to be subbed in to the 
+ *    'where_args: An associative array of arguments to be subbed in to the
  *      where clause where the
  *
  * @ingroup tripal_chado_node_api
  */
 function hook_chado_node_sync_select_query($query) {
 
-  // You can add fields to be selected. Be sure to prefix each field with the 
+  // You can add fields to be selected. Be sure to prefix each field with the
   // tale name.
   $query['select'][] = 'example.myfavfield';
 
-  // Provide any join you may need to the joins array. Be sure to wrap the 
+  // Provide any join you may need to the joins array. Be sure to wrap the
   // table name in curly brackets.
   $query['joins'][] = 'LEFT JOIN {exampleprop} PROP ON PROP.example_id=EXAMPLE.example_id';
 
-  // The category should be a unique id for a group of items that will be 
+  // The category should be a unique id for a group of items that will be
   // concatenated together via an SQL 'OR'.  By default the $where_clases
-  // variable will come with categories of 'id', 'organism' and 'type'.  
+  // variable will come with categories of 'id', 'organism' and 'type'.
   // you can add your own unique category or alter the contents of the existing
   // categories.  Be sure to make sure the category doesn't already exist
   // in the $query['where_clauses']
-  $category = 'my_category'; 
-  
+  $category = 'my_category';
+
   // Provide any aditionall where clauses and their necessary arguments.
-  // Be sure to prefix the field with the table name.   Be sure that the 
-  // placeholder is unique across all categories (perhaps add a unique 
+  // Be sure to prefix the field with the table name.   Be sure that the
+  // placeholder is unique across all categories (perhaps add a unique
   // prefix/suffix).
   $query['where_clauses'][$category][] = 'example.myfavfield = :favvalue';
   $query['where_args'][$category][':favvalue'] = 'awesome-ness';
-  
+
   // Must return the updated query
   return $query;
 }

+ 17 - 14
tripal_example/includes/tripal_example.chado_node.inc

@@ -312,19 +312,19 @@ function chado_example_validate($node, $form, &$form_state) {
   // Since this validate can be called on AJAX and Deletion of the node
   // we need to make this check to ensure queries are not executed
   // without the proper values.
-  if($node->op != 'Save') {
+  if(property_exists($node, "op") and $node->op != 'Save') {
     return;
   }
 
   // we are syncing if we do not have a node ID but we do have a example_id. We
   // don't need to validate during syncing so just skip it.
-  if (is_null($node->nid) and property_exists($node, 'example_id') and $node->example_id != 0) {
+  if (!property_exists($node, 'nid') and property_exists($node, 'example_id') and $node->example_id != 0) {
     return;
   }
 
   // be sure to always trim text fields
-  $node->uniquename   = trim($node->uniquename);
-  $node->description  = trim($node->description);
+  $node->uniquename   = property_exists($node, 'uniquename') ? trim($node->uniquename) : '';
+  $node->description  = property_exists($node, 'description') ? trim($node->description) : '';
 
   // Validating for an update. If the 'nid' property is present in the node then
   // this is an update and validation can be different for updates
@@ -375,22 +375,25 @@ function chado_example_validate($node, $form, &$form_state) {
  * @ingroup tripal_example
  */
 function chado_example_insert($node) {
-  // be sure to always trim text fields
-  $node->uniquename   = trim($node->uniquename);
-  $node->description  = trim($node->description);
 
-  // get the example type record
-  $type_cv = tripal_get_default_cv('example', 'type_id');
-  $type = tripal_get_cvterm(array(
-    'name' => $node->example_type,
-    'cv_id' => $type_cv->cv_id,
-  ));
+  $example_id = '';
 
   // if there is an example_id in the $node object then this must be a sync so
   // we can skip adding the example as it is already there, although we do need
-  // to proceed with the rest of the insert
+  // to proceed with insertion into the chado/drupal linking table.
   if (!property_exists($node, 'example_id')) {
 
+    // be sure to always trim text fields
+    $node->uniquename   = trim($node->uniquename);
+    $node->description  = trim($node->description);
+
+    // get the example type record
+    $type_cv = tripal_get_default_cv('example', 'type_id');
+    $type = tripal_get_cvterm(array(
+        'name' => $node->example_type,
+        'cv_id' => $type_cv->cv_id,
+    ));
+
     // perform the insert using the chado_insert_record function();
     $values = array(
       'uniquename' => $node->uniquename,

+ 30 - 28
tripal_feature/includes/tripal_feature.chado_node.inc

@@ -264,25 +264,26 @@ function chado_feature_form($node, &$form_state) {
  */
 function chado_feature_validate($node, $form, &$form_state) {
 
+
   // We only want to validate when the node is saved.
   // Since this validate can be called on AJAX and Deletion of the node
   // we need to make this check to ensure queries are not executed
   // without the proper values.
-  if($node->op != 'Save') {
+  if(property_exists($node, "op") and $node->op != 'Save') {
     return;
   }
 
   // we are syncing if we do not have a node ID but we do have a feature_id. We don't
   // need to validate during syncing so just skip it.
-  if (is_null($node->nid) and property_exists($node, 'feature_id') and $node->feature_id != 0) {
+  if (!property_exists($node, 'nid') and property_exists($node, 'feature_id') and $node->feature_id != 0) {
     return;
   }
 
   // remove surrounding white-space on submitted values
-  $node->uniquename   = trim($node->uniquename);
-  $node->fname        = trim($node->fname);
-  $node->feature_type = trim($node->feature_type);
-  $node->residues     = trim($node->residues);
+  $node->uniquename   = property_exists($node, 'uniquename') ? trim($node->uniquename) : '';
+  $node->fname        = property_exists($node, 'fname') ? trim($node->fname) : '';
+  $node->feature_type = property_exists($node, 'feature_type') ? trim($node->feature_type) : '';
+  $node->residues     = property_exists($node, 'residues') ? trim($node->residues) : '';
 
   // Validating for an update
   if (property_exists($node, 'nid')) {
@@ -342,6 +343,7 @@ function chado_feature_validate($node, $form, &$form_state) {
         CVT.name      = :cvtname
     ";
     $args = array(':name' => $node->uniquename, ':organism_id' => $node->organism_id, ':cvtname' => $node->feature_type);
+
     $result = chado_query($sql, $args)->fetchObject();
     if ($result) {
       form_set_error('uniquename', t("Feature insert cannot proceed. The feature name '$node->uniquename' already exists for this organism. Please provide a unique name for this feature."));
@@ -416,33 +418,33 @@ function chado_feature_node_access($node, $op, $account) {
  */
 function chado_feature_insert($node) {
 
-  $node->uniquename   = trim($node->uniquename);
-  $node->fname        = trim($node->fname);
-  $node->feature_type = trim($node->feature_type);
-  $node->residues     = trim($node->residues);
-
-  // remove spaces, newlines from residues
-  $residues = preg_replace("/[\n\r\s]/", "", $node->residues);
-  $obsolete = 'FALSE';
-  if ($node->is_obsolete) {
-    $obsolete = 'TRUE';
-  }
-
-  // get the feature type id
-  $values = array(
-    'cv_id' => array(
-      'name' => 'sequence'
-    ),
-    'name' => $node->feature_type
-  );
-  $type = chado_select_record('cvterm', array('cvterm_id'), $values);
-
   $feature_id = '';
 
   // if there is a feature_id in the $node object then this must be a sync so
   // we can skip adding the feature as it is already there, although
-  // we do need to proceed with the rest of the insert
+  // we do need to proceed with insertion into the chado/drupal linking table.
   if (!property_exists($node, 'feature_id')) {
+    $node->uniquename   = trim($node->uniquename);
+    $node->fname        = trim($node->fname);
+    $node->feature_type = trim($node->feature_type);
+    $node->residues     = trim($node->residues);
+
+    // remove spaces, newlines from residues
+    $residues = preg_replace("/[\n\r\s]/", "", $node->residues);
+    $obsolete = 'FALSE';
+    if ($node->is_obsolete) {
+      $obsolete = 'TRUE';
+    }
+
+    // get the feature type id
+    $values = array(
+        'cv_id' => array(
+            'name' => 'sequence'
+        ),
+        'name' => $node->feature_type
+    );
+    $type = chado_select_record('cvterm', array('cvterm_id'), $values);
+
     $values = array(
       'organism_id' => $node->organism_id,
       'name' => $node->fname,

+ 9 - 7
tripal_featuremap/includes/tripal_featuremap.chado_node.inc

@@ -159,19 +159,19 @@ function chado_featuremap_validate($node, $form, &$form_state) {
   // Since this validate can be called on AJAX and Deletion of the node
   // we need to make this check to ensure queries are not executed
   // without the proper values.
-  if($node->op != 'Save') {
+  if(property_exists($node, "op") and $node->op != 'Save') {
     return;
   }
 
   // we are syncing if we do not have a node ID but we do have a featuremap_id. We don't
   // need to validate during syncing so just skip it.
-  if (is_null($node->nid) and property_exists($node, 'featuremap_id') and $node->featuremap_id != 0) {
+  if (!property_exists($node, 'nid') and property_exists($node, 'featuremap_id') and $node->featuremap_id != 0) {
     return;
   }
 
   // trim white space from text fields
-  $node->fmapname       = trim($node->fmapname);
-  $node->description    = trim($node->description);
+  $node->fmapname = property_exists($node, 'fmapname') ? trim($node->fmapname) : '';
+  $node->description = property_exists($node, 'description') ? trim($node->description) : '';
 
   $featuremap = 0;
   // check to make sure the unique name on the map is unique
@@ -261,14 +261,16 @@ function chado_featuremap_node_access($node, $op, $account) {
  */
 function chado_featuremap_insert($node) {
 
-  $node->fmapname       = trim($node->fmapname);
-  $node->description    = trim($node->description);
+  $featuremap_id = '';
 
   // if there is an featuremap_id in the $node object then this must be a sync so
   // we can skip adding the featuremap as it is already there, although
-  // we do need to proceed with the rest of the insert
+  // we do need to proceed with insertion into the chado/drupal linking table.
   if (!property_exists($node, 'featuremap_id')) {
 
+    $node->fmapname       = trim($node->fmapname);
+    $node->description    = trim($node->description);
+
     $values = array(
       'name'        => $node->fmapname,
       'description' => $node->description,

+ 11 - 9
tripal_library/includes/tripal_library.chado_node.inc

@@ -222,20 +222,20 @@ function chado_library_validate($node, $form, &$form_state) {
   // Since this validate can be called on AJAX and Deletion of the node
   // we need to make this check to ensure queries are not executed
   // without the proper values.
-  if($node->op != 'Save') {
+  if(property_exists($node, "op") and $node->op != 'Save') {
     return;
   }
 
   // we are syncing if we do not have a node ID but we do have a featuremap_id. We don't
   // need to validate during syncing so just skip it.
-  if (is_null($node->nid) and property_exists($node, 'library_id') and $node->library_id != 0) {
+  if (!property_exists($node, 'nid') and property_exists($node, 'library_id') and $node->library_id != 0) {
     return;
   }
 
   // trim white space from text fields
-  $node->libraryname = trim($node->libraryname);
-  $node->uniquename  = trim($node->uniquename);
-  $node->description = trim($node->description);
+  $node->libraryname = property_exists($node, 'libraryname') ? trim($node->libraryname) : '';
+  $node->uniquename  = property_exists($node, 'uniquename') ? trim($node->uniquename) : '';
+  $node->description = property_exists($node, 'description') ? trim($node->description) : '';
 
   $lib = 0;
   // check to make sure the unique name on the library is unique
@@ -268,14 +268,16 @@ function chado_library_validate($node, $form, &$form_state) {
  */
 function chado_library_insert($node) {
 
-  $node->libraryname = trim($node->libraryname);
-  $node->uniquename  = trim($node->uniquename);
-  $node->description = trim($node->description);
+  $library_id = '';
 
   // if there is an library_id in the $node object then this must be a sync so
   // we can skip adding the library as it is already there, although
-  // we do need to proceed with the rest of the insert
+  // we do need to proceed with insertion into the chado/drupal linking table.
   if (!property_exists($node, 'library_id')) {
+    $node->libraryname = trim($node->libraryname);
+    $node->uniquename  = trim($node->uniquename);
+    $node->description = trim($node->description);
+
     $values = array(
       'name' => $node->libraryname,
       'uniquename' => $node->uniquename,

+ 17 - 14
tripal_organism/includes/tripal_organism.chado_node.inc

@@ -220,22 +220,22 @@ function chado_organism_validate($node, $form, &$form_state) {
   // Since this validate can be called on AJAX and Deletion of the node
   // we need to make this check to ensure queries are not executed
   // without the proper values.
-  if($node->op != 'Save') {
+  if(property_exists($node, "op") and $node->op != 'Save') {
     return;
   }
 
   // we are syncing if we do not have a node ID but we do have a organism_id. We don't
   // need to validate during syncing so just skip it.
-  if (is_null($node->nid) and property_exists($node, 'organism_id') and $node->organism_id != 0) {
+  if (!property_exists($node, 'nid') and property_exists($node, 'organism_id') and $node->organism_id != 0) {
     return;
   }
 
   // remove any white space around values
-  $node->genus        = trim($node->genus);
-  $node->species      = trim($node->species);
-  $node->abbreviation = trim($node->abbreviation);
-  $node->common_name  = trim($node->common_name);
-  $node->description  = trim($node->description);
+  $node->genus        = property_exists($node, 'genus') ? trim($node->genus) : '';
+  $node->species      = property_exists($node, 'species') ? trim($node->species) : '';
+  $node->abbreviation = property_exists($node, 'abbreviation') ? trim($node->abbreviation) : '';
+  $node->common_name  = property_exists($node, 'common_name') ? trim($node->common_name) : '';
+  $node->description  = property_exists($node, 'description') ? trim($node->description) : '';
 
   // Validating for an update
   if (property_exists($node, 'organism_id')) {
@@ -284,17 +284,20 @@ function chado_organism_validate($node, $form, &$form_state) {
  * @ingroup tripal_organism
  */
 function chado_organism_insert($node) {
-  // remove any white space around values
-  $node->genus        = trim($node->genus);
-  $node->species      = trim($node->species);
-  $node->abbreviation = trim($node->abbreviation);
-  $node->common_name  = trim($node->common_name);
-  $node->description  = trim($node->description);
+
+  $organism_id = '';
 
   // if there is an organism_id in the $node object then this must be a sync so
   // we can skip adding the organism as it is already there, although
-  // we do need to proceed with the rest of the insert
+  // we do need to proceed with insertion into the chado/drupal linking table.
   if (!property_exists($node, 'organism_id')) {
+    // remove any white space around values
+    $node->genus        = trim($node->genus);
+    $node->species      = trim($node->species);
+    $node->abbreviation = trim($node->abbreviation);
+    $node->common_name  = trim($node->common_name);
+    $node->description  = trim($node->description);
+
     $values = array(
       'genus'        => $node->genus,
       'species'      => $node->species,

+ 9 - 7
tripal_project/includes/tripal_project.chado_node.inc

@@ -200,19 +200,19 @@ function chado_project_validate($node, $form, &$form_state) {
   // Since this validate can be called on AJAX and Deletion of the node
   // we need to make this check to ensure queries are not executed
   // without the proper values.
-  if($node->op != 'Save') {
+  if(property_exists($node, "op") and $node->op != 'Save') {
     return;
   }
 
   // we are syncing if we do not have a node ID but we do have a project_id. We don't
   // need to validate during syncing so just skip it.
-  if (is_null($node->nid) and property_exists($node, 'project_id') and $node->project_id != 0) {
+  if (!property_exists($node, 'nid') and property_exists($node, 'project_id') and $node->project_id != 0) {
     return;
   }
 
   // trim white space from text fields
-  $node->title = trim($node->title);
-  $node->description = trim($node->description);
+  $node->title = property_exists($node, 'title') ? trim($node->title) : '';
+  $node->description = property_exists($node, 'description') ? trim($node->description) : '';
 
   $project = 0;
   // check to make sure the name on the project is unique
@@ -240,13 +240,15 @@ function chado_project_validate($node, $form, &$form_state) {
  */
 function chado_project_insert($node) {
 
-  $node->title = trim($node->title);
-  $node->description = trim($node->description);
+  $project_id = '';
 
   // if there is an project_id in the $node object then this must be a sync so
   // we can skip adding the project as it is already there, although
-  // we do need to proceed with the rest of the insert
+  // we do need to proceed with insertion into the chado/drupal linking table.
   if (!property_exists($node, 'project_id')) {
+    $node->title = trim($node->title);
+    $node->description = trim($node->description);
+
     $values = array(
       'name' => $node->title,
       'description' => '',

+ 19 - 17
tripal_pub/includes/tripal_pub.chado_node.inc

@@ -337,22 +337,22 @@ function chado_pub_validate($node, $form, &$form_state) {
   // Since this validate can be called on AJAX and Deletion of the node
   // we need to make this check to ensure queries are not executed
   // without the proper values.
-  if($node->op != 'Save') {
+  if(property_exists($node, "op") and $node->op != 'Save') {
     return;
   }
 
   // we are syncing if we do not have a node ID but we do have a pub_id. We don't
   // need to validate during syncing so just skip it.
-  if (is_null($node->nid) and property_exists($node, 'pub_id') and $node->pub_id != 0) {
+  if (!property_exists($node, 'nid') and property_exists($node, 'pub_id') and $node->pub_id != 0) {
     return;
   }
 
   // get the submitted values
-  $title        = trim($node->pubtitle);
-  $pyear        = trim($node->pyear);
-  $uniquename   = trim($node->uniquename);
-  $is_obsolete  = $node->is_obsolete;
-  $type_id      = $node->type_id;
+  $title        = property_exists($node, 'pubtitle') ? trim($node->pubtitle) : '';
+  $pyear        = property_exists($node, 'pyear') ? trim($node->pyear) : '';
+  $uniquename   = property_exists($node, 'uniquename') ? trim($node->uniquename) : '';
+  $is_obsolete  = property_exists($node, 'is_obsolete') ? trim($node->is_obsolete) : 0;
+  $type_id      = property_exists($node, 'type_id') ? trim($node->type_id) : '';
 
   $pub = array();
 
@@ -573,21 +573,23 @@ function chado_pub_node_access($node, $op, $account) {
  */
 function chado_pub_insert($node) {
 
-  $node->pubtitle     = trim($node->pubtitle);
-  $node->pyear        = trim($node->pyear);
-  $node->uniquename   = trim($node->uniquename);
-  $is_obsolete  = $node->is_obsolete;
-  $type_id      = $node->type_id;
-
-  // we need an array suitable for the tripal_pub_create_citation() function
-  // to automatically generate a citation if a uniquename doesn't already exist
-  $pub_arr = array();
+  $pub_id = '';
 
   // if there is an pub_id in the $node object then this must be a sync so
   // we can skip adding the pub as it is already there, although
-  // we do need to proceed with the rest of the insert
+  // we do need to proceed with insertion into the chado/drupal linking table.
   if (!property_exists($node, 'pub_id')) {
 
+    $node->pubtitle     = trim($node->pubtitle);
+    $node->pyear        = trim($node->pyear);
+    $node->uniquename   = trim($node->uniquename);
+    $is_obsolete  = $node->is_obsolete;
+    $type_id      = $node->type_id;
+
+    // we need an array suitable for the tripal_pub_create_citation() function
+    // to automatically generate a citation if a uniquename doesn't already exist
+    $pub_arr = array();
+
     $properties = array(); // stores all of the properties we need to add
     $cross_refs = array(); // stores any cross references for this publication
 

+ 12 - 11
tripal_stock/includes/tripal_stock.chado_node.inc

@@ -323,22 +323,22 @@ function chado_stock_validate(&$node, $form, &$form_state) {
   // Since this validate can be called on AJAX and Deletion of the node
   // we need to make this check to ensure queries are not executed
   // without the proper values.
-  if($node->op != 'Save') {
+  if(property_exists($node, "op") and $node->op != 'Save') {
     return;
   }
 
   // we are syncing if we do not have a node ID but we do have a stock_id. We don't
   // need to validate during syncing so just skip it.
-  if (is_null($node->nid) and property_exists($node, 'stock_id') and $node->stock_id != 0) {
+  if (!property_exists($node, 'nid') and property_exists($node, 'stock_id') and $node->stock_id != 0) {
     return;
   }
 
   // remove surrounding whitespace
-  $node->uniquename = trim($node->uniquename);
-  $node->sname = trim($node->sname);
-  $node->stock_description = trim($node->stock_description);
-  $node->accession = trim($node->accession);
-  $node->db_description = trim($node->db_description);
+  $node->uniquename = property_exists($node, 'uniquename') ? trim($node->uniquename) : '';
+  $node->sname = property_exists($node, 'sname') ? trim($node->sname) : '';
+  $node->stock_description = property_exists($node, 'stock_description') ? trim($node->stock_description) : '';
+  $node->accession = property_exists($node, 'accession') ? trim($node->accession) : '';
+  $node->db_description = property_exists($node, 'db_description') ? trim($node->db_description) : '';
 
   $int_in_chado_sql = "SELECT count(*) as count FROM {:table} WHERE :column = :value";
   $string_in_chado_sql = "SELECT count(*) as count FROM {:table} WHERE :column = :value";
@@ -441,17 +441,18 @@ function chado_stock_validate(&$node, $form, &$form_state) {
  */
 function chado_stock_insert($node) {
 
-  $node->uniquename   = trim($node->uniquename);
-  $node->sname        = trim($node->sname);
-  $node->accession    = trim($node->accession);
 
   $stock_id = '';
 
   // if there is an stock_id in the $node object then this must be a sync so
   // we can skip adding the stock to chado as it is already there, although
-  // we do need to proceed with the rest of the insert
+  // we do need to proceed with insertion into the chado/drupal linking table.
   if (!property_exists($node, 'stock_id')) {
 
+    $node->uniquename   = trim($node->uniquename);
+    $node->sname        = trim($node->sname);
+    $node->accession    = trim($node->accession);
+
     // before we can add the stock, we must add the dbxref if one has been
     // provided by the user.
     $dbxref = NULL;