tripal_chado.api.inc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * Retrieves an entity that matches the given table and record id.
  4. *
  5. * @param $table
  6. * The name of the Chado table.
  7. * @param $record_id
  8. * The record's primary key in the table specified by $table.
  9. *
  10. * @return
  11. * A chado_entity object.
  12. */
  13. function tripal_load_chado_entity($table, $record_id) {
  14. $entity_id = db_select('chado_entity', 'ce')
  15. ->fields('ce', array('entity_id'))
  16. ->condition('ce.record_id', $record_id)
  17. ->condition('ce.data_table', $table)
  18. ->execute()
  19. ->fetchField();
  20. if ($entity_id) {
  21. $entity = entity_load('TripalEntity', array($entity_id));
  22. return reset($entity);
  23. }
  24. return NULL;
  25. }
  26. /**
  27. * Publishes content in Chado as a new TripalEntity entity.
  28. *
  29. * @param $values
  30. * A key/value associative array that supports the following keys:
  31. * - bundle_name: The name of the the TripalBundle (e.g. bio_data-12345).
  32. * @param $job_id
  33. * (Optional) The numeric job ID as provided by the Tripal jobs system. There
  34. * is no need to specify this argument if this function is being called
  35. * outside of the jobs systems.
  36. *
  37. * @return boolean
  38. * TRUE if all of the records of the given bundle type were published, and
  39. * FALSE if a failure occured.
  40. */
  41. function tripal_chado_publish_records($values, $job_id = NULL) {
  42. $bundle_name = $values['bundle_name'];
  43. $sync_node = array_key_exists('sync_node', $values) ? $values['sync_node'] : '';
  44. if (!array_key_exists('bundle_name', $values) or !$values['bundle_name']) {
  45. tripal_report_error('tripal_chado', TRIPAL_ERROR, "Could not publish record: @error", array('@error' => 'The bundle name was not provided'));
  46. return FALSE;
  47. }
  48. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  49. $bundle_id = $bundle->id;
  50. $table = tripal_get_bundle_variable('chado_table', $bundle_id);
  51. $column = tripal_get_bundle_variable('chado_column', $bundle_id);
  52. $cvterm_id = tripal_get_bundle_variable('chado_cvterm_id', $bundle_id);
  53. // Get the table information
  54. $table_schema = chado_get_schema($table);
  55. $pkey_field = $table_schema['primary key'][0];
  56. $select = "SELECT $pkey_field as record_id ";
  57. $from = "FROM {" . $table . "} T
  58. LEFT JOIN public.chado_entity CE on CE.record_id = T.$pkey_field
  59. AND CE.data_table = '$table'
  60. ";
  61. if ($sync_node && db_table_exists('chado_' . $table)) {
  62. $select = "SELECT T.$pkey_field as record_id, CT.nid ";
  63. $from .= "INNER JOIN public.chado_$table CT ON CT.$pkey_field = T.$pkey_field";
  64. }
  65. $where = " WHERE CE.record_id IS NULL ";
  66. if ($table != 'analysis' and $table != 'organism') {
  67. $where .= "AND $column = $cvterm_id";
  68. }
  69. $sql = $select . $from . $where;
  70. $records = chado_query($sql);
  71. $num_published = 0;
  72. $transaction = db_transaction();
  73. try {
  74. while($record = $records->fetchObject()) {
  75. // First save the tripal_entity record.
  76. $record_id = $record->record_id;
  77. $ec = entity_get_controller('TripalEntity');
  78. $entity = $ec->create(array(
  79. 'bundle' => $bundle_name,
  80. 'term_id' => $bundle->term_id,
  81. ));
  82. if (!$entity->save()) {
  83. throw new Exception('Could not create entity.');
  84. }
  85. // Next save the chado_entity record.
  86. $entity_record = array(
  87. 'entity_id' => $entity->id,
  88. 'record_id' => $record_id,
  89. 'data_table' => $table,
  90. 'type_table' => $table,
  91. 'field' => $column,
  92. );
  93. // For the Tv2 to Tv3 migration we want to add the nid to the
  94. // entity so we can associate the node with the entity.
  95. if (property_exists($record, 'nid')) {
  96. $entity_record['nid'] = $record->nid;
  97. }
  98. $success = drupal_write_record('chado_entity', $entity_record);
  99. $entity = entity_load('TripalEntity', array($entity->id));
  100. $entity = reset($entity);
  101. $title_format = tripal_get_title_format($bundle);
  102. $title = tripal_replace_tokens($title_format, $entity, $bundle);
  103. $ec->setTitle($entity, $title);
  104. $num_published++;
  105. }
  106. }
  107. catch (Exception $e) {
  108. $transaction->rollback();
  109. $error = $e->getMessage();
  110. tripal_report_error('tripal_chado', TRIPAL_ERROR, "Could not publish record: @error", array('@error' => $error));
  111. drupal_set_message('Failed publishing record. See recent logs for more details.', 'error');
  112. return FALSE;
  113. }
  114. drupal_set_message("Succesfully published $num_published " . $bundle->label . " record(s).");
  115. return TRUE;
  116. }