diff --git a/islandora_herbarium_object.module b/islandora_herbarium_object.module index 263c5cb..6d64041 100644 --- a/islandora_herbarium_object.module +++ b/islandora_herbarium_object.module @@ -31,51 +31,12 @@ function islandora_herbarium_object_help($route_name, RouteMatchInterface $route function islandora_herbarium_object_entity_presave(Drupal\Core\Entity\EntityInterface $entity) { if ($entity->bundle() == 'islandora_herbarium_object') { - try { - $dwciri = $entity->get("field_dwciri_municipality")->getValue(); - $geo = \Drupal::service('islandora_herbarium_object.geoparser'); - $geo->getRemoteData($dwciri[0]['value']); - $geo->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 %ulr', - array('%msg'=> $e->getMessage(), '%url' => $dwciri[0]['value'])), 'error'); - return; - } - $entity->set('field_dwc_municipality', $geo->getValue('name')); - $entity->set('field_dwc_countrycode', $geo->getValue('countryCode')); - $entity->set('field_dwc_country', $geo->getValue('countryName')); - $entity->set('field_dwc_continent', $geo->getValue('countinentCode')); - $entity->set('field_dwc_decimallatitude', $geo->getValue('lat')); - $entity->set('field_dwc_decimallongitude', $geo->getValue('lng')); - $entity->set('field_dwc_stateprovince', $geo->getValue('adminName1')); - $point = array( - 'lon' => $geo->data_arr['lng'], - 'lat' => $geo->data_arr['lat'], - ); - $geo_field_value = \Drupal::service('geofield.wkt_generator')->WktBuildPoint($point); - $entity->set('field_herb_geo_field', $geo_field_value); - - try { - $taxoniri = $entity->get("field_dwciri_scientificname")->getValue(); - $taxon = \Drupal::service('islandora_herbarium_object.taxonparser'); - $taxon->getRemoteData($taxoniri[0]['value']); - $taxon->parseData(); - } catch (Exception $e) { - watchdog_exception('islandora_herbarium_object', $e, 'Error getting dwciri'); - drupal_set_message(t('There was an error retrieving Taxon data. %msg. Verify the url is correct %url', - array('%msg'=> $e->getMessage(), '%url' => $taxoniri[0]['value'])),'error'); - return; - } - $entity->set('field_dwc_scientificname', $taxon->getValue('canonicalName')); - $name_arr = explode(' ', $taxon->getValue('canonicalName')); - $entity->set('field_dwc_specificepithet', $name_arr[1]); - $entity->set('field_dwc_kingdom', $taxon->getValue('kingdom')); - $entity->set('field_dwc_genus', $taxon->getValue('genus')); - $entity->set('field_dwc_phylum', $taxon->getValue('phylum')); - $entity->set('field_dwc_order', $taxon->getValue('order')); - $entity->set('field_dwc_family', $taxon->getValue('family')); - $entity->set('field_dwc_vernacularname', $taxon->getValue('vernacularName')); + // Get the geo data. + $geo = \Drupal::service('islandora_herbarium_object.geoparser'); + $geo->updateEntity($entity); + // Get the taxonomy data. + $taxon = \Drupal::service('islandora_herbarium_object.taxonparser'); + $taxon->updateEntity($entity); $title = $taxon->getValue('canonicalName') . ' - ' . $geo->getValue('name'); if(!empty($title)) { diff --git a/src/DataParser/DataParserInterface.php b/src/DataParser/DataParserInterface.php index f39ace0..7270f8c 100644 --- a/src/DataParser/DataParserInterface.php +++ b/src/DataParser/DataParserInterface.php @@ -23,4 +23,6 @@ interface DataParserInterface */ public function getValue($key); + public function updateEntity($entity); + } diff --git a/src/DataParser/GbifParser.php b/src/DataParser/GbifParser.php index 27fcefd..33d7ecd 100644 --- a/src/DataParser/GbifParser.php +++ b/src/DataParser/GbifParser.php @@ -19,4 +19,30 @@ class GbifParser extends DataParser $this->data_arr = reset($arr['results']); } + public function updateEntity($entity) { + try { + $taxoniri = $entity->get("field_dwciri_scientificname")->getValue(); + if(empty($taxoniri[0]['value'])) { + throw new Exception("No URI provided, cannot look up taxon data."); + } + + $this->getRemoteData($taxoniri[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 Taxon data. %msg. Verify the url is correct %url', + array('%msg'=> $e->getMessage(), '%url' => $taxoniri[0]['value'])),'error'); + return; + } + $entity->set('field_dwc_scientificname', $this->getValue('canonicalName')); + $name_arr = explode(' ', $this->getValue('canonicalName')); + $entity->set('field_dwc_specificepithet', $name_arr[1]); + $entity->set('field_dwc_kingdom', $this->getValue('kingdom')); + $entity->set('field_dwc_genus', $this->getValue('genus')); + $entity->set('field_dwc_phylum', $this->getValue('phylum')); + $entity->set('field_dwc_order', $this->getValue('order')); + $entity->set('field_dwc_family', $this->getValue('family')); + $entity->set('field_dwc_vernacularname', $this->getValue('vernacularName')); + } + } diff --git a/src/DataParser/GeonamesParser.php b/src/DataParser/GeonamesParser.php index b0594d7..a0d3998 100644 --- a/src/DataParser/GeonamesParser.php +++ b/src/DataParser/GeonamesParser.php @@ -38,4 +38,33 @@ class GeonamesParser extends XmlDataParser $this->getXmlNodeValue($xml_doc, 'east'); $this->getXmlNodeValue($xml_doc, 'south'); } + + 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')); + $entity->set('field_dwc_countrycode', $this->getValue('countryCode')); + $entity->set('field_dwc_country', $this->getValue('countryName')); + $entity->set('field_dwc_continent', $this->getValue('countinentCode')); + $entity->set('field_dwc_decimallatitude', $this->getValue('lat')); + $entity->set('field_dwc_decimallongitude', $this->getValue('lng')); + $entity->set('field_dwc_stateprovince', $this->getValue('adminName1')); + $point = array( + 'lon' => $this->getValue('lng'), + 'lat' => $this->getValue('lat'), + ); + $geo_field_value = \Drupal::service('geofield.wkt_generator')->WktBuildPoint($point); + $entity->set('field_herb_geo_field', $geo_field_value); + } }