app/models/bplmodels/mods_desc_metadata.rb
def insert_tgn(tgn_id)
      puts 'TGN ID is: ' + tgn_id
      return false if self.subject.valueURI.include?(tgn_id) # check for dupe TGN value

      api_result = Geomash::TGN.get_tgn_data(tgn_id)
      puts 'API Result is: ' + api_result.to_s

      #FIXME: Only works for hier_geo places....
      if api_result[:hier_geo].present? && self.subject.hierarchical_geographic.present?
        existing_subjects = []
        self.mods(0).subject.each_with_index do |_subject, subject_index|
          if self.mods(0).subject(subject_index).authority == ['tgn']
            existing_subjects << { index: subject_index,
                                   geo_hash: Bplmodels::GeographicDataFuncs.hiergeo_hash(self.mods(0).subject(subject_index)) }
          end
        end
        # now we check existing subjects, delete if we're adding more specific geo entity
        # lots of edge cases with non-trad geo subjects (areas, mountains, rivers)
        existing_subjects.each do |esub|
          remove_esub = false
          esub_geo = esub[:geo_hash]
          api_hier_geo = api_result[:hier_geo]

          if api_hier_geo[:city_section].present?
            if esub_geo[:city_section].blank? && esub_geo[:city] == api_hier_geo[:city]
              remove_esub = true
            end
          elsif api_hier_geo[:city].present?
            if esub_geo[:city].blank? && esub_geo[:state] == api_hier_geo[:state]
              remove_esub = true
            elsif esub_geo[:city] == api_hier_geo[:city]
              return false unless api_result[:non_hier_geo].present?

              remove_esub = true
            end
          elsif api_hier_geo[:area].present?
            if esub_geo[:area] == api_hier_geo[:area]
              return false unless api_result[:non_hier_geo].present?

              remove_esub = true
            end
          elsif api_hier_geo[:county].present?
            if esub_geo[:county].blank? && esub_geo[:state] == api_hier_geo[:state]
              remove_esub = true
            elsif esub_geo[:county] == api_hier_geo[:county]
              return false unless api_result[:non_hier_geo].present?

              remove_esub = true
            end
          elsif api_hier_geo[:state].present?
            if esub_geo[:state].blank? && esub_geo[:country] == api_hier_geo[:country]
              remove_esub = true
            elsif esub_geo[:state] == api_hier_geo[:state]
              return false unless api_result[:non_hier_geo].present? || api_hier_geo[:area].present?

              remove_esub = true if self.mods(0).subject(esub[:index]).hierarchical_geographic(0).area.blank?
            end
          elsif api_hier_geo[:country].present?
            if esub_geo[:country] == api_hier_geo[:country]
              return false unless api_result[:non_hier_geo].present?

              unless self.mods(0).subject(esub[:index]).hierarchical_geographic(0).city.present? ||
                     self.mods(0).subject(esub[:index]).hierarchical_geographic(0).state.present?
                remove_esub = true
              end
            end
          end
          # but don't remove existing subject if it has non-hier-geo elements
          if remove_esub && self.mods(0).subject(esub[:index]).geographic(0).blank?
            self.mods(0).subject(esub[:index], nil)
          end
        end
      end

      if api_result[:non_hier_geo].present?
        #Exit if same place match currently....
        self.mods(0).subject.each_with_index do |_ignored, subject_index|
          if self.mods(0).subject(subject_index).authority == ['tgn']
            if self.mods(0).subject(subject_index).geographic == [api_result[:non_hier_geo][:value]]
              return false
            end
          end
        end
      end

      subject_index = self.mods(0).subject.count

      self.mods(0).subject(subject_index).authority = "tgn"
      self.mods(0).subject(subject_index).valueURI = "http://vocab.getty.edu/tgn/#{tgn_id}"
      self.mods(0).subject(subject_index).authorityURI = 'http://vocab.getty.edu/tgn/'

      #Insert geographic text
      if api_result[:non_hier_geo].present?
        self.mods(0).subject(subject_index).geographic = api_result[:non_hier_geo][:value]
        self.mods(0).subject(subject_index).geographic.display_label = api_result[:non_hier_geo][:qualifier] unless api_result[:non_hier_geo][:qualifier].blank?
      end

      #Insert hierarchicalGeographic text
      if api_result[:hier_geo] != nil
        api_result[:hier_geo].keys.reverse.each do |key|
          key_with_equal = key.to_s + "="
          self.mods(0).subject(subject_index).hierarchical_geographic.send(key_with_equal.to_sym, Bplmodels::DatastreamInputFuncs.utf8Encode(api_result[:hier_geo][key]))
        end
      end

      #Insert Coordinates
      if api_result[:coords] != nil
        self.mods(0).subject(subject_index).cartographics.coordinates = api_result[:coords][:latitude] + "," + api_result[:coords][:longitude]
      end
    end
#include Hydra::Datastream::CommonModsIndexMethods
# MODS XML constants.
include Hydra::Datastream::CommonModsIndexMethods
MODS XML constants.
include Hydra :: Datastream :: CommonModsIndexMethods MODS XML constants .
def insert_tgn(tgn_id)
      puts 'TGN ID is: ' + tgn_id
      return false if self.subject.valueURI.include?(tgn_id) 
      api_result = Geomash::TGN.get_tgn_data(tgn_id)
      puts 'API Result is: ' + api_result.to_s
      if api_result[:hier_geo].present? && self.subject.hierarchical_geographic.present?
        existing_subjects = []
        self.mods(0).subject.each_with_index do |_subject, subject_index|
          if self.mods(0).subject(subject_index).authority == ['tgn']
            existing_subjects << { index: subject_index,
                                   geo_hash: Bplmodels::GeographicDataFuncs.hiergeo_hash(self.mods(0).subject(subject_index)) }
          end
        end
        existing_subjects.each do |esub|
          remove_esub = false
          esub_geo = esub[:geo_hash]
          api_hier_geo = api_result[:hier_geo]
          if api_hier_geo[:city_section].present?
            if esub_geo[:city_section].blank? && esub_geo[:city] == api_hier_geo[:city]
              remove_esub = true
            end
          elsif api_hier_geo[:city].present?
            if esub_geo[:city].blank? && esub_geo[:state] == api_hier_geo[:state]
              remove_esub = true
            elsif esub_geo[:city] == api_hier_geo[:city]
              return false unless api_result[:non_hier_geo].present?
              remove_esub = true
            end
          elsif api_hier_geo[:area].present?
            if esub_geo[:area] == api_hier_geo[:area]
              return false unless api_result[:non_hier_geo].present?
              remove_esub = true
            end
          elsif api_hier_geo[:county].present?
            if esub_geo[:county].blank? && esub_geo[:state] == api_hier_geo[:state]
              remove_esub = true
            elsif esub_geo[:county] == api_hier_geo[:county]
              return false unless api_result[:non_hier_geo].present?
              remove_esub = true
            end
          elsif api_hier_geo[:state].present?
            if esub_geo[:state].blank? && esub_geo[:country] == api_hier_geo[:country]
              remove_esub = true
            elsif esub_geo[:state] == api_hier_geo[:state]
              return false unless api_result[:non_hier_geo].present? || api_hier_geo[:area].present?
              remove_esub = true if self.mods(0).subject(esub[:index]).hierarchical_geographic(0).area.blank?
            end
          elsif api_hier_geo[:country].present?
            if esub_geo[:country] == api_hier_geo[:country]
              return false unless api_result[:non_hier_geo].present?
              unless self.mods(0).subject(esub[:index]).hierarchical_geographic(0).city.present? ||
                     self.mods(0).subject(esub[:index]).hierarchical_geographic(0).state.present?
                remove_esub = true
              end
            end
          end
          if remove_esub && self.mods(0).subject(esub[:index]).geographic(0).blank?
            self.mods(0).subject(esub[:index], nil)
          end
        end
      end
      if api_result[:non_hier_geo].present?
        self.mods(0).subject.each_with_index do |_ignored, subject_index|
          if self.mods(0).subject(subject_index).authority == ['tgn']
            if self.mods(0).subject(subject_index).geographic == [api_result[:non_hier_geo][:value]]
              return false
            end
          end
        end
      end
      subject_index = self.mods(0).subject.count
      self.mods(0).subject(subject_index).authority = "tgn"
      self.mods(0).subject(subject_index).valueURI = "http://vocab.getty.edu/tgn/#{tgn_id}"
      self.mods(0).subject(subject_index).authorityURI = 'http://vocab.getty.edu/tgn/'
      if api_result[:non_hier_geo].present?
        self.mods(0).subject(subject_index).geographic = api_result[:non_hier_geo][:value]
        self.mods(0).subject(subject_index).geographic.display_label = api_result[:non_hier_geo][:qualifier] unless api_result[:non_hier_geo][:qualifier].blank?
      end
      if api_result[:hier_geo] != nil
        api_result[:hier_geo].keys.reverse.each do |key|
          key_with_equal = key.to_s + "="
          self.mods(0).subject(subject_index).hierarchical_geographic.send(key_with_equal.to_sym, Bplmodels::DatastreamInputFuncs.utf8Encode(api_result[:hier_geo][key]))
        end
      end
      if api_result[:coords] != nil
        self.mods(0).subject(subject_index).cartographics.coordinates = api_result[:coords][:latitude] + "," + api_result[:coords][:longitude]
      end
    end
def insert_tgn ( tgn_id ) puts 'TGN ID is: ' + tgn_id return false if self . subject . valueURI . include? ( tgn_id ) api_result = Geomash :: TGN . get_tgn_data ( tgn_id ) puts 'API Result is: ' + api_result . to_s if api_result [ :hier_geo ] . present? && self . subject . hierarchical_geographic . present? existing_subjects = [ ] self . mods ( 0 ) . subject . each_with_index do | _subject , subject_index | if self . mods ( 0 ) . subject ( subject_index ) . authority == [ 'tgn' ] existing_subjects << { index : subject_index , geo_hash : Bplmodels :: GeographicDataFuncs . hiergeo_hash ( self . mods ( 0 ) . subject ( subject_index ) ) } end end existing_subjects . each do | esub | remove_esub = false esub_geo = esub [ :geo_hash ] api_hier_geo = api_result [ :hier_geo ] if api_hier_geo [ :city_section ] . present? if esub_geo [ :city_section ] . blank? && esub_geo [ :city ] == api_hier_geo [ :city ] remove_esub = true end elsif api_hier_geo [ :city ] . present? if esub_geo [ :city ] . blank? && esub_geo [ :state ] == api_hier_geo [ :state ] remove_esub = true elsif esub_geo [ :city ] == api_hier_geo [ :city ] return false unless api_result [ :non_hier_geo ] . present? remove_esub = true end elsif api_hier_geo [ :area ] . present? if esub_geo [ :area ] == api_hier_geo [ :area ] return false unless api_result [ :non_hier_geo ] . present? remove_esub = true end elsif api_hier_geo [ :county ] . present? if esub_geo [ :county ] . blank? && esub_geo [ :state ] == api_hier_geo [ :state ] remove_esub = true elsif esub_geo [ :county ] == api_hier_geo [ :county ] return false unless api_result [ :non_hier_geo ] . present? remove_esub = true end elsif api_hier_geo [ :state ] . present? if esub_geo [ :state ] . blank? && esub_geo [ :country ] == api_hier_geo [ :country ] remove_esub = true elsif esub_geo [ :state ] == api_hier_geo [ :state ] return false unless api_result [ :non_hier_geo ] . present? || api_hier_geo [ :area ] . present? remove_esub = true if self . mods ( 0 ) . subject ( esub [ :index ] ) . hierarchical_geographic ( 0 ) . area . blank? end elsif api_hier_geo [ :country ] . present? if esub_geo [ :country ] == api_hier_geo [ :country ] return false unless api_result [ :non_hier_geo ] . present? unless self . mods ( 0 ) . subject ( esub [ :index ] ) . hierarchical_geographic ( 0 ) . city . present? || self . mods ( 0 ) . subject ( esub [ :index ] ) . hierarchical_geographic ( 0 ) . state . present? remove_esub = true end end end if remove_esub && self . mods ( 0 ) . subject ( esub [ :index ] ) . geographic ( 0 ) . blank? self . mods ( 0 ) . subject ( esub [ :index ] , nil ) end end end if api_result [ :non_hier_geo ] . present? self . mods ( 0 ) . subject . each_with_index do | _ignored , subject_index | if self . mods ( 0 ) . subject ( subject_index ) . authority == [ 'tgn' ] if self . mods ( 0 ) . subject ( subject_index ) . geographic == [ api_result [ :non_hier_geo ] [ :value ] ] return false end end end end subject_index = self . mods ( 0 ) . subject . count self . mods ( 0 ) . subject ( subject_index ) . authority = "tgn" self . mods ( 0 ) . subject ( subject_index ) . valueURI = "http://vocab.getty.edu/tgn/#{tgn_id}" self . mods ( 0 ) . subject ( subject_index ) . authorityURI = 'http://vocab.getty.edu/tgn/' if api_result [ :non_hier_geo ] . present? self . mods ( 0 ) . subject ( subject_index ) . geographic = api_result [ :non_hier_geo ] [ :value ] self . mods ( 0 ) . subject ( subject_index ) . geographic . display_label = api_result [ :non_hier_geo ] [ :qualifier ] unless api_result [ :non_hier_geo ] [ :qualifier ] . blank? end if api_result [ :hier_geo ] != nil api_result [ :hier_geo ] . keys . reverse . each do | key | key_with_equal = key . to_s + "=" self . mods ( 0 ) . subject ( subject_index ) . hierarchical_geographic . send ( key_with_equal . to_sym , Bplmodels :: DatastreamInputFuncs . utf8Encode ( api_result [ :hier_geo ] [ key ] ) ) end end if api_result [ :coords ] != nil self . mods ( 0 ) . subject ( subject_index ) . cartographics . coordinates = api_result [ :coords ] [ :latitude ] + "," + api_result [ :coords ] [ :longitude ] end end
include Hydra::Datastream::CommonModsIndexMethods
MODS XML constants.
include Hydra :: Datastream :: CommonModsIndexMethods MODS XML constants .
# check for dupe TGN value #FIXME: Only works for hier_geo places.... # now we check existing subjects, delete if we're adding more specific geo entity # lots of edge cases with non-trad geo subjects (areas, mountains, rivers) # but don't remove existing subject if it has non-hier-geo elements #Exit if same place match currently.... #Insert geographic text #Insert hierarchicalGeographic text #Insert Coordinates
tgn_id