tripal_chado.bundle.inc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /**
  3. * Implements hook_chado_bundle_create().
  4. *
  5. * This is a Tripal hook. It allows any module to perform tasks after
  6. * a bundle has been created.
  7. *
  8. * @param $bundle
  9. * The TripalBundle object.
  10. */
  11. function tripal_chado_bundle_create($bundle, $storage_args) {
  12. $entity_type = $bundle->type;
  13. if (!array_key_exists('data_table', $storage_args)) {
  14. throw new Exception('Cannot create content type. Missing the "data_table" for this bundle.');
  15. }
  16. $type_id = '';
  17. if (array_key_exists('type_id', $storage_args)) {
  18. $type_id = $storage_args['type_id'];
  19. }
  20. else {
  21. $term = tripal_load_term_entity(['term_id' => $bundle->term_id]);
  22. $vocab = tripal_load_vocab_entity(['vocab_id' => $term->vocab_id]);
  23. $cvterm = chado_generate_var('cvterm', [
  24. 'dbxref_id' => [
  25. 'db_id' => [
  26. 'name' => $vocab->vocabulary,
  27. ],
  28. 'accession' => $term->accession,
  29. ],
  30. ]);
  31. $type_id = $cvterm->cvterm_id;
  32. }
  33. // Before adding fields to this bundle, let's make sure we associate
  34. // what table in Chado this bundle is mapped to
  35. $chado_bundle = db_select('chado_bundle', 'cb')
  36. ->fields('cb')
  37. ->condition('bundle_id', $bundle->id)
  38. ->execute()
  39. ->fetchObject();
  40. if (!$chado_bundle) {
  41. $record = [
  42. 'bundle_id' => $bundle->id,
  43. 'data_table' => $storage_args['data_table'],
  44. 'type_id' => $type_id,
  45. ];
  46. if (array_key_exists('type_linker_table', $storage_args)) {
  47. $record['type_linker_table'] = $storage_args['type_linker_table'];
  48. }
  49. if (array_key_exists('type_column', $storage_args)) {
  50. $record['type_column'] = $storage_args['type_column'];
  51. }
  52. if (array_key_exists('type_value', $storage_args)) {
  53. $record['type_value'] = $storage_args['type_value'];
  54. }
  55. if (array_key_exists('base_type_id', $storage_args)) {
  56. $record['base_type_id'] = $storage_args['base_type_id'];
  57. }
  58. $success = drupal_write_record('chado_bundle', $record);
  59. if (!$success) {
  60. throw new Exception('Cannot create content type. Problem associating type with Chado.');
  61. }
  62. }
  63. tripal_chado_create_bundle_table($bundle);
  64. }
  65. /**
  66. * Creates the table that tripal_chado uses to link Chado records with entities.
  67. *
  68. * @param $bundle
  69. * A bundle object (as retrieved from tripal_load_bundle_entity().
  70. */
  71. function tripal_chado_create_bundle_table($bundle) {
  72. // Now create the table where the bundle's records will go
  73. $schema = [
  74. 'description' => 'The linker table that associates TripalEntities with Chado records for entities of type ' . $bundle->name . '.',
  75. 'fields' => [
  76. 'mapping_id' => [
  77. 'type' => 'serial',
  78. 'not null' => TRUE,
  79. ],
  80. 'entity_id' => [
  81. 'description' => 'The unique entity id.',
  82. 'type' => 'int',
  83. 'not null' => TRUE,
  84. ],
  85. 'record_id' => [
  86. 'description' => 'The unique numerical identifier for the record that this entity is associated with (e.g. feature_id, stock_id, library_id, etc.).',
  87. 'type' => 'int',
  88. 'not null' => TRUE,
  89. ],
  90. 'nid' => [
  91. 'description' => 'Optional. For linking nid to the entity when migrating Tripal v2 content',
  92. 'type' => 'int',
  93. ],
  94. ],
  95. 'primary key' => [
  96. 'mapping_id',
  97. ],
  98. 'indexes' => [
  99. 'record_id' => ['record_id'],
  100. 'entity_id' => ['entity_id'],
  101. 'nid' => ['nid'],
  102. ],
  103. 'unique keys' => [
  104. 'table_record' => ['record_id'],
  105. 'entity_id' => ['entity_id'],
  106. ],
  107. ];
  108. $chado_entity_table = chado_get_bundle_entity_table($bundle);
  109. if (!$chado_entity_table) {
  110. throw new Exception('Cannot create entity linker table for chado.');
  111. }
  112. db_create_table($chado_entity_table, $schema);
  113. // Ensure we clear the drupal schema cache after creating this table.
  114. // This is needed so we can publish records directly after creating a new
  115. // content type.
  116. drupal_get_complete_schema(TRUE);
  117. }
  118. /**
  119. * Implements hook_bundle_delete().
  120. *
  121. */
  122. function tripal_chado_bundle_delete($bundle) {
  123. // Remove the entries in the appropriate chado entity table
  124. // and tripal_entity
  125. $chado_entity_table = chado_get_bundle_entity_table($bundle);
  126. $sql = "DROP TABLE {$chado_entity_table}";
  127. db_query($sql);
  128. // Remove the entry from the chado_bundle table.
  129. db_delete('chado_bundle')
  130. ->condition('bundle_id', $bundle->id)
  131. ->execute();
  132. }
  133. /**
  134. * Implements hook_bundle_find_orphans().
  135. *
  136. */
  137. function tripal_chado_bundle_find_orphans($bundle, $count = FALSE, $offset = 0, $limit = 10) {
  138. $chado_bundle_table = chado_get_bundle_entity_table($bundle);
  139. $schema = chado_get_schema($bundle->data_table);
  140. $primary_key = $schema['primary key'][0];
  141. $select = "CT.entity_id";
  142. if ($count) {
  143. $select = "count(*) as count";
  144. }
  145. $qlimit = '';
  146. if (!$count and $limit) {
  147. $qlimit = "LIMIT $limit OFFSET $offset ";
  148. }
  149. // Get the count
  150. $query = "
  151. SELECT $select
  152. FROM [$chado_bundle_table] CT
  153. LEFT JOIN {" . $bundle->data_table . "} BT ON CT.record_id = BT.$primary_key
  154. WHERE BT.$primary_key IS NULL
  155. $qlimit
  156. ";
  157. $results = chado_query($query);
  158. if ($count) {
  159. $num_orphans = (int) $results->fetchField();
  160. return $num_orphans;
  161. }
  162. else {
  163. $ids = [];
  164. while ($entity_id = $results->fetchField()) {
  165. $ids[] = $entity_id;
  166. }
  167. return $ids;
  168. }
  169. }
  170. /**
  171. * Implements hook_bundle_delete_orphans().
  172. */
  173. function tripal_chado_bundle_delete_orphans(TripalBundle $bundle, array $ids, TripalJob $job = NULL) {
  174. $chado_bundle_table = chado_get_bundle_entity_table($bundle);
  175. $schema = chado_get_schema($bundle->data_table);
  176. $primary_key = $schema['primary key'][0];
  177. $num_deleted = db_delete($chado_bundle_table)
  178. ->condition('entity_id', $ids, 'IN')
  179. ->execute();
  180. if ($job) {
  181. $job->addItemsHandled($num_deleted);
  182. $job->logMessage("Removed !num orphaned entities", ['!num' => $num_deleted]);
  183. }
  184. return $num_deleted;
  185. }