diff --git a/islandora_herbarium_object.module b/islandora_herbarium_object.module index 6d64041..d503354 100644 --- a/islandora_herbarium_object.module +++ b/islandora_herbarium_object.module @@ -32,17 +32,27 @@ function islandora_herbarium_object_entity_presave(Drupal\Core\Entity\EntityInte { if ($entity->bundle() == 'islandora_herbarium_object') { // Get the geo data. - $geo = \Drupal::service('islandora_herbarium_object.geoparser'); - $geo->updateEntity($entity); + try { + $geo = \Drupal::service('islandora_herbarium_object.geoparser'); + $geo->updateEntity($entity); + } catch (Exception $e) { + drupal_set_message(t('There was an error retrieving Geo data. %msg.', + array('%msg'=> $e->getMessage())), 'error'); + } // Get the taxonomy data. - $taxon = \Drupal::service('islandora_herbarium_object.taxonparser'); - $taxon->updateEntity($entity); - + try { + $taxon = \Drupal::service('islandora_herbarium_object.taxonparser'); + $taxon->updateEntity($entity); + } catch(Exception $e) { + drupal_set_message(t('There was an error retrieving Taxon data. %msg.', + array('%msg'=> $e->getMessage())), 'error'); + } $title = $taxon->getValue('canonicalName') . ' - ' . $geo->getValue('name'); - if(!empty($title)) { + if($title != ' - ') { $entity->setTitle($title); } + } } diff --git a/islandora_herbarium_object.services.yml b/islandora_herbarium_object.services.yml index 2097dce..66aaff8 100644 --- a/islandora_herbarium_object.services.yml +++ b/islandora_herbarium_object.services.yml @@ -2,4 +2,4 @@ services: islandora_herbarium_object.taxonparser: class: Drupal\islandora_herbarium_object\DataParser\GbifParser islandora_herbarium_object.geoparser: - class: Drupal\islandora_herbarium_object\DataParser\GeonamesParser + class: Drupal\islandora_herbarium_object\DataParser\NrcanParser diff --git a/src/DataParser/DataParser.php b/src/DataParser/DataParser.php index cf89bed..bce72c4 100644 --- a/src/DataParser/DataParser.php +++ b/src/DataParser/DataParser.php @@ -14,14 +14,18 @@ abstract class DataParser implements DataParserInterface public $data; public $data_arr = array(); - public function getRemoteData($uri) { + public function getRemoteData($uri, $returnAsVariable = FALSE) { $client = \Drupal::httpClient(); $request = $client->get(trim($uri)); $data = $request->getBody()->getContents(); if(empty($data)) { throw new Exception("Empty dataset"); } - $this->data = $data; + if(!$returnAsVariable) { + $this->data = $data; + } else { + return $data; + } } public function getValue($key) { diff --git a/src/DataParser/DataParserInterface.php b/src/DataParser/DataParserInterface.php index 7270f8c..9ad14be 100644 --- a/src/DataParser/DataParserInterface.php +++ b/src/DataParser/DataParserInterface.php @@ -16,11 +16,6 @@ interface DataParserInterface public function parseData(); - /** - * Gets the value from an array based on it's key, elimnates warnings if key does not exist. - * @param $arr - * @param $key - */ public function getValue($key); public function updateEntity($entity); diff --git a/src/DataParser/NrcanParser.php b/src/DataParser/NrcanParser.php new file mode 100644 index 0000000..c3ab748 --- /dev/null +++ b/src/DataParser/NrcanParser.php @@ -0,0 +1,62 @@ +data, TRUE); + if(empty($arr)) { + throw new \Exception('Error parsing geo json data.'); + } + $this->data_arr = $arr; + } + + public function updateEntity($entity) { + try { + $dwciri = $entity->get("field_dwciri_municipality")->getValue(); + if(empty($dwciri[0]['value'])) { + throw new Exception("No URI provided, cannot look up location data."); + } + $this->getRemoteData($dwciri[0]['value']); + $this->parseData(); + } catch (Exception $e) { + watchdog_exception('islandora_herbarium_object', $e, 'Error getting dwciri'); + drupal_set_message(t('There was an error retrieving Municipality data. %msg. Verify the url is correct %url', + array('%msg'=> $e->getMessage(), '%url' => $dwciri[0]['value'])), 'error'); + return; + } + $entity->set('field_dwc_municipality', $this->getValue('name')); + // This parser assumes we are in canada + $entity->set('field_dwc_countrycode', 'CA'); + $entity->set('field_dwc_country', 'Canada'); + $entity->set('field_dwc_continent', 'North America'); + $entity->set('field_dwc_decimallatitude', $this->getValue('latitude')); + $entity->set('field_dwc_decimallongitude', $this->getValue('longitude')); + $provinceUri = $this->getvalue('province')['links']['self']['href']; + try { + $provinceData = $this->getRemoteData($provinceUri, TRUE); + $p_arr = json_decode($provinceData, TRUE); + $province = $p_arr['description']; + } + catch(Exception $e) { + drupal_set_message(t('There was an error retrieving Province data. %msg.', + array('%msg'=> $e->getMessage())), 'error'); + } + $entity->set('field_dwc_stateprovince', $province); + $point = array( + 'lon' => $this->getValue('longitude'), + 'lat' => $this->getValue('latitude'), + ); + $geo_field_value = \Drupal::service('geofield.wkt_generator')->WktBuildPoint($point); + $entity->set('field_herb_geo_field', $geo_field_value); + } +}