Class: DatasetRecord::DarwinCore::Occurrence::ImportProtonym

Inherits:
Object
  • Object
show all
Defined in:
app/models/dataset_record/darwin_core/occurrence.rb

Direct Known Subclasses

CreateIfNotExists, MatchExisting

Defined Under Namespace

Classes: CreateIfNotExists, MatchExisting

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_if_not_existsObject



27
28
29
# File 'app/models/dataset_record/darwin_core/occurrence.rb', line 27

def self.create_if_not_exists
  @create_if_not_exists ||= CreateIfNotExists.new
end

.match_existingObject



31
32
33
# File 'app/models/dataset_record/darwin_core/occurrence.rb', line 31

def self.match_existing
  @match_existing ||= MatchExisting.new
end

Instance Method Details

#execute(origins, parent, name) ⇒ Object



35
36
37
38
39
# File 'app/models/dataset_record/darwin_core/occurrence.rb', line 35

def execute(origins, parent, name)
  protonym = get_protonym(parent, name)
  raise DatasetRecord::DarwinCore::InvalidData.new(exception_args(origins, parent, name, protonym)) unless protonym&.persisted?
  protonym
end

#get_protonym(parent, name) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/dataset_record/darwin_core/occurrence.rb', line 41

def get_protonym(parent, name)
  name = name.except(:rank_class) if name[:rank_class].nil?

  %I(name masculine_name feminine_name neuter_name).inject(nil) do |protonym, field|
    break protonym unless protonym.nil?

    p = Protonym.find_by(name.slice(:rank_class).merge({field => name[:name], :parent => parent}))

    # Protonym might not exist, or might have intermediate parent not listed in file
    # if it exists, run more expensive query to see if it has an ancestor matching parent name and rank
    if p.nil? && Protonym.where(name.slice(:rank_class).merge({field => name[:name]})).where(project_id: parent.project_id).exists?
      p = Protonym.where(name.slice(:rank_class).merge!({field => name[:name]})).with_ancestor(parent).first

      # check parent.cached_valid_taxon_name_id if not valid, can have obsolete subgenus Aus (Aus) bus -> Aus bus, bus won't have ancestor (Aus)
      if p.nil? && !parent.cached_is_valid
        p = Protonym.where(name.slice(:rank_class).merge!({field => name[:name]})).with_ancestor(parent.valid_taxon_name).first
    end

    end
    p
  end
end