From 103e8994bf153ca74c09a3c10612f98329739835 Mon Sep 17 00:00:00 2001 From: Nick Ruest Date: Wed, 11 May 2016 15:31:35 -0400 Subject: [PATCH] Message passing between drupal and camel. (#201) * Message passing between drupal and camel updated for sprint-002 --- islandora/include/admin.form.inc | 61 ++++++++++++++ islandora/include/rdf.inc | 21 +++++ islandora/include/triplestore.inc | 70 ++++++++++++++++ islandora/include/utils.inc | 24 ++++++ islandora/islandora.module | 79 +++++++++++++++++++ .../islandora_basic_image.module | 9 ++- .../islandora_collection.module | 9 ++- 7 files changed, 267 insertions(+), 6 deletions(-) create mode 100644 islandora/include/admin.form.inc create mode 100644 islandora/include/rdf.inc create mode 100644 islandora/include/triplestore.inc create mode 100644 islandora/include/utils.inc diff --git a/islandora/include/admin.form.inc b/islandora/include/admin.form.inc new file mode 100644 index 00000000..ce1d6592 --- /dev/null +++ b/islandora/include/admin.form.inc @@ -0,0 +1,61 @@ + array( + '#type' => 'textfield', + '#title' => t('Stomp URL'), + '#default_value' => variable_get('islandora_stomp_url', 'tcp://localhost:61613'), + '#description' => t('URL to the Stomp broker for async messaging'), + '#element_validate' => array('islandora_config_form_validate_stomp_url'), + '#required' => TRUE, + ), + 'islandora_triplestore_index_queue' => array( + '#type' => 'textfield', + '#title' => t('Triplestore Index Queue'), + '#default_value' => variable_get('islandora_triplestore_index_queue', 'islandora/triplestore/index'), + '#description' => t('Queue to send triplestore indexing messages'), + '#required' => TRUE, + ), + ); + + return system_settings_form($form); +} + +/** + * Stomp broker url validator. + * + * @param array $element + * Form element for stomp broker url. + * @param array $form_state + * Drupal form state. + * @param array $form + * Drupal renderable array. + */ +function islandora_config_form_validate_stomp_url(array $element, array &$form_state, array $form) { + // Try to connect to the server. + try { + $url = $element['#value']; + $stomp = new Stomp($url); + unset($stomp); + } + catch (StompException $e) { + form_error($element, t('Connection failed for @url: @message', array('@url' => $url, '@message' => $e->getMessage()))); + } +} diff --git a/islandora/include/rdf.inc b/islandora/include/rdf.inc new file mode 100644 index 00000000..d360d46b --- /dev/null +++ b/islandora/include/rdf.inc @@ -0,0 +1,21 @@ + $rdf_model->ns)); + return $serializer->getSerializedIndex($rdf_model->index); +} diff --git a/islandora/include/triplestore.inc b/islandora/include/triplestore.inc new file mode 100644 index 00000000..8a0b0605 --- /dev/null +++ b/islandora/include/triplestore.inc @@ -0,0 +1,70 @@ + $action, + 'Content-Type' => 'application/n-triples', + 'CamelFcrepoBaseUrl' => $base_url, + 'CamelFcrepoIdentifier' => "/node/{$node->nid}", + ); + + try { + $stomp = new Stomp($broker_url); + $stomp->send($queue, $msg, $headers); + unset($stomp); + return TRUE; + } + catch (StompException $e) { + watchdog('islandora', 'Error publishing triplestore index action to Stomp broker: @err', array('@err' => $e->getMessage()), WATCHDOG_ERROR); + return FALSE; + } +} diff --git a/islandora/include/utils.inc b/islandora/include/utils.inc new file mode 100644 index 00000000..15743c42 --- /dev/null +++ b/islandora/include/utils.inc @@ -0,0 +1,24 @@ + array( + 'title' => 'Islandora', + 'description' => 'Configure settings for Islandora', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('islandora_config_form'), + 'file' => '/include/admin.form.inc', + 'access arguments' => array('administer site configuration'), + 'type' => MENU_NORMAL_ITEM, + ), + ); + return $items; +} + /** * Implements hook_ctools_plugin_api(). */ @@ -67,6 +85,67 @@ function islandora_entity_load($entities, $type) { } } +/** + * Implements hook_node_postinsert(). + */ +function islandora_node_postinsert($node) { + module_load_include('inc', 'islandora', 'include/utils'); + if (islandora_ignore_hooks()) { + return; + } + + $nodes = entity_uuid_load('node', array($node->uuid)); + + // Exit early if the node never was successfully inserted in the database. + if (empty($nodes)) { + return; + } + + $node = array_pop($nodes); + + module_load_include('inc', 'islandora', 'include/triplestore'); + islandora_triplestore_upsert($node); +} + +/** + * Implements hook_node_postupdate(). + */ +function islandora_node_postupdate($node) { + module_load_include('inc', 'islandora', 'include/utils'); + if (islandora_ignore_hooks()) { + return; + } + + // Unfortunately, there's no way to tell if the db transaction got rolled + // back in this hook, so we'll default to doing it anyway so as not to miss + // any updates. Open to suggestions on this one. But we gotta do post_action + // hooks otherwise things can get crazy if the db transaction hasn't closed. + $nodes = entity_uuid_load('node', array($node->uuid)); + + // Exit early if something has gone wrong and there's no node info. + if (empty($nodes)) { + return; + } + + $node = array_pop($nodes); + + module_load_include('inc', 'islandora', 'include/triplestore'); + islandora_triplestore_upsert($node); +} + +/** + * Implements hook_node_postdelete(). + */ +function islandora_node_postdelete($node) { + module_load_include('inc', 'islandora', 'include/utils'); + if (islandora_ignore_hooks()) { + return; + } + + module_load_include('inc', 'islandora', 'include/triplestore'); + islandora_triplestore_delete($node); +} + /** * Implements hook_default_services_endpoint(). */ diff --git a/islandora_basic_image/islandora_basic_image.module b/islandora_basic_image/islandora_basic_image.module index eedabf8e..196856b3 100644 --- a/islandora_basic_image/islandora_basic_image.module +++ b/islandora_basic_image/islandora_basic_image.module @@ -102,7 +102,8 @@ function islandora_basic_image_rdf_mapping() { * Implements hook_node_postinsert(). */ function islandora_basic_image_node_postinsert($node) { - if (isset($_SERVER['HTTP_IGNORE_HOOKS']) && strcmp(strtolower($_SERVER['HTTP_IGNORE_HOOKS']), "true") == 0) { + module_load_include('inc', 'islandora', 'include/utils'); + if (islandora_ignore_hooks()) { return; } @@ -151,7 +152,8 @@ function islandora_basic_image_node_postinsert($node) { * Implements hook_node_postupdate(). */ function islandora_basic_image_node_postupdate($node) { - if (isset($_SERVER['HTTP_IGNORE_HOOKS']) && strcmp(strtolower($_SERVER['HTTP_IGNORE_HOOKS']), "true") == 0) { + module_load_include('inc', 'islandora', 'include/utils'); + if (islandora_ignore_hooks()) { return; } @@ -194,7 +196,8 @@ function islandora_basic_image_node_postupdate($node) { * Implements hook_node_postdelete(). */ function islandora_basic_image_node_postdelete($node) { - if (isset($_SERVER['HTTP_IGNORE_HOOKS']) && strcmp(strtolower($_SERVER['HTTP_IGNORE_HOOKS']), "true") == 0) { + module_load_include('inc', 'islandora', 'include/utils'); + if (islandora_ignore_hooks()) { return; } diff --git a/islandora_collection/islandora_collection.module b/islandora_collection/islandora_collection.module index deeb1921..eb92a651 100644 --- a/islandora_collection/islandora_collection.module +++ b/islandora_collection/islandora_collection.module @@ -106,7 +106,8 @@ function islandora_collection_rdf_mapping() { * Implements hook_node_postinsert(). */ function islandora_collection_node_postinsert($node) { - if (isset($_SERVER['HTTP_IGNORE_HOOKS']) && strcmp(strtolower($_SERVER['HTTP_IGNORE_HOOKS']), "true") == 0) { + module_load_include('inc', 'islandora', 'include/utils'); + if (islandora_ignore_hooks()) { return; } @@ -142,7 +143,8 @@ function islandora_collection_node_postinsert($node) { * Implements hook_node_postupdate(). */ function islandora_collection_node_postupdate($node) { - if (isset($_SERVER['HTTP_IGNORE_HOOKS']) && strcmp(strtolower($_SERVER['HTTP_IGNORE_HOOKS']), "true") == 0) { + module_load_include('inc', 'islandora', 'include/utils'); + if (islandora_ignore_hooks()) { return; } @@ -179,7 +181,8 @@ function islandora_collection_node_postupdate($node) { * Implements hook_node_postdelete(). */ function islandora_collection_node_postdelete($node) { - if (isset($_SERVER['HTTP_IGNORE_HOOKS']) && strcmp(strtolower($_SERVER['HTTP_IGNORE_HOOKS']), "true") == 0) { + module_load_include('inc', 'islandora', 'include/utils'); + if (islandora_ignore_hooks()) { return; }