Module: Protonym::SoftValidationExtensions::Instance

Included in:
Protonym
Defined in:
app/models/protonym/soft_validation_extensions.rb

Instance Method Summary collapse

Instance Method Details

#sv_author_is_not_requiredObject



1582
1583
1584
1585
1586
1587
1588
1589
# File 'app/models/protonym/soft_validation_extensions.rb', line 1582

def sv_author_is_not_required
  if self.verbatim_author && (!self.taxon_name_author_roles.empty? || (self.source && self.verbatim_author == self.source.authority_name))
    soft_validations.add(
      :verbatim_author, 'Verbatim author is not required, it is derived from the source and taxon name author roles',
      success_message: 'Verbatim author was deleted',
      failure_message:  'Failed to delete verbatim author')
  end
end

#sv_duplicate_nomen_nudumObject



1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
# File 'app/models/protonym/soft_validation_extensions.rb', line 1667

def sv_duplicate_nomen_nudum
  if self.id == self.cached_valid_taxon_name_id && self.cached_is_valid == false
    if !self.cached_secondary_homonym_alternative_spelling.nil?
      possible_available = Protonym.where(cached_secondary_homonym_alternative_spelling: self.cached_secondary_homonym_alternative_spelling).not_self(self).with_project(self.project_id).any?
      soft_validations.add(:base, "An available protonym with the same original combination,  #{self.cached_html}, exits.") if possible_available
    elsif !self.cached_primary_homonym_alternative_spelling.nil?
      possible_available = Protonym.where(cached_primary_homonym_alternative_spelling: self.cached_primary_homonym_alternative_spelling).not_self(self).with_project(self.project_id).any?
      soft_validations.add(:base, "An available protonym with the same name,  #{self.cached_html}, exits.") if possible_available
    end
  end
end

#sv_extant_childrenObject



1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
# File 'app/models/protonym/soft_validation_extensions.rb', line 1523

def sv_extant_children
  unless self.parent_id.blank?
    if self.is_fossil?
      taxa = Protonym.where(parent_id: self.id)
      z = 0
      unless taxa.empty?
        taxa.each do |t|
          soft_validations.add(:base, "Extinct taxon #{self.cached_html} has extant children") if !t.is_fossil? && t.id == t.cached_valid_taxon_name_id && z == 0
          z = 1
        end
      end
    end
  end
end

#sv_family_is_invalidObject



1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
# File 'app/models/protonym/soft_validation_extensions.rb', line 1383

def sv_family_is_invalid
  if persisted? && is_family_rank? && is_available?
    tg = type_genus
    if tg && (!TaxonNameRelationship.where_subject_is_taxon_name(tg).homonym_or_suppressed.empty? ||
        !TaxonNameClassification.where_taxon_name(tg).with_type_string('TaxonNameClassification::Iczn::Available::Invalid::Homonym').empty? )
      if self.id == self.lowest_rank_coordinated_taxon.id
        if TaxonNameClassification.where_taxon_name(self).with_type_base('TaxonNameClassification::Iczn::Available::Invalid').empty?
          soft_validations.add(:base, "Missing relationship: #{self.cached_html_name_and_author_year} is invalid due to the homonymy or suppression of its type genus")
        end
      end
    end
  end
end

#sv_family_is_invalid_no_substituteObject



1397
1398
1399
1400
1401
1402
1403
1404
1405
# File 'app/models/protonym/soft_validation_extensions.rb', line 1397

def sv_family_is_invalid_no_substitute
  if persisted? && is_family_rank? && self.id == self.lowest_rank_coordinated_taxon.id
    if !TaxonNameClassification.where_taxon_name(self).with_type_base('TaxonNameClassification::Iczn::Available::Invalid').empty?
      if self.iczn_set_as_synonym_of.nil?
        soft_validations.add(:base, 'Missing relationship: The name is invalid, but a substitute name is not selected')
      end
    end
  end
end

#sv_fix_add_nominotypical_subObject



1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
# File 'app/models/protonym/soft_validation_extensions.rb', line 1286

def sv_fix_add_nominotypical_sub
  return false if list_of_coordinated_names.collect{|r| r.id}.include?(parent_id)
  rank  = rank_string
  p     = self.parent
  prank = p.rank_string
  if (rank =~ /Family/ && prank =~ /Family/) || (rank =~ /Genus/ && prank =~ /Genus/) || (rank =~ /Species/ && prank =~ /Species/)
    begin
      Protonym.transaction do
        if rank =~ /Family/ && prank =~ /Family/
          name = Protonym.family_group_base(self.parent.name)
          case self.rank_class.rank_name
          when 'subfamily'
            name += 'inae'
          when 'tribe'
            name += 'ini'
          when 'subtribe'
            name += 'ina'
          end
        else
          name = p.name
        end

        t = Protonym.new(name: name, rank_class: rank, verbatim_author: p.verbatim_author, year_of_publication: p.year_of_publication, source: p.source, parent: p)
        t.save
        t.soft_validate
        t.fix_soft_validations
      end
    rescue ActiveRecord::RecordInvalid # naked rescue is very bad
      return false
    end
    return true
  end
end

#sv_fix_author_is_not_requiredObject



1591
1592
1593
1594
# File 'app/models/protonym/soft_validation_extensions.rb', line 1591

def sv_fix_author_is_not_required
  self.update_column(:verbatim_author, nil)
  return true
end

#sv_fix_coordinated_names_authorObject



659
660
661
662
663
664
665
666
667
668
# File 'app/models/protonym/soft_validation_extensions.rb', line 659

def sv_fix_coordinated_names_author
  return false if !self.verbatim_author.nil? || !self.taxon_name_author_roles.empty?
  list_of_coordinated_names.each do |t|
    if self.verbatim_author.nil? && !t.verbatim_author.nil?
      self.update_column(:verbatim_author, t.verbatim_author)
      return true
    end
  end
  return false
end

#sv_fix_coordinated_names_etymologyObject



952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
# File 'app/models/protonym/soft_validation_extensions.rb', line 952

def sv_fix_coordinated_names_etymology
  fixed = false
  return false unless self.etymology.blank?
  list_of_coordinated_names.each do |t|
    if !t.etymology.blank?
      self.etymology = t.etymology
      fixed = true
    end
  end
  if fixed
    begin
      Protonym.transaction do
        self.save
      end
      return true
    rescue
      return false
    end
  end
end

#sv_fix_coordinated_names_genderObject



699
700
701
702
703
704
705
706
707
708
# File 'app/models/protonym/soft_validation_extensions.rb', line 699

def sv_fix_coordinated_names_gender
  return false unless self.gender_class.nil?
  list_of_coordinated_names.each do |t|
    unless t.gender_class.nil?
      c = self.taxon_name_classifications.create(type: t.gender_class.to_s)
      return true if c.id
    end
  end
  return false
end

#sv_fix_coordinated_names_original_formObject



890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
# File 'app/models/protonym/soft_validation_extensions.rb', line 890

def sv_fix_coordinated_names_original_form
  fixed = false
  return false unless self.original_form.nil?
  list_of_coordinated_names.each do |t|
    if !t.original_form.nil?
      self.original_form = t.original_form
      fixed = true
    end
  end
  if fixed
    begin
      Protonym.transaction do
        self.save
      end
      return true
    rescue
      return false
    end
  end
end

#sv_fix_coordinated_names_original_genusObject



740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
# File 'app/models/protonym/soft_validation_extensions.rb', line 740

def sv_fix_coordinated_names_original_genus
  fixed = false
  return false unless self.original_genus.nil?
  list_of_coordinated_names.each do |t|
    if !t.original_genus.nil?
      self.original_genus = t.original_genus
      fixed = true
    end
  end
  if fixed
    begin
      Protonym.transaction do
        self.save
      end
      return true
    rescue
      return false
    end
  end
end

#sv_fix_coordinated_names_original_speciesObject



800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
# File 'app/models/protonym/soft_validation_extensions.rb', line 800

def sv_fix_coordinated_names_original_species
  fixed = false
  return false unless self.original_species.nil?
  list_of_coordinated_names.each do |t|
    if !t.original_species.nil?
      self.original_species = t.original_species
      fixed = true
    end
  end
  if fixed
    begin
      Protonym.transaction do
        self.save
      end
      return true
    rescue
      return false
    end
  end
end

#sv_fix_coordinated_names_original_subgenusObject



770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
# File 'app/models/protonym/soft_validation_extensions.rb', line 770

def sv_fix_coordinated_names_original_subgenus
  fixed = false
  return false unless self.original_subgenus.nil?
  list_of_coordinated_names.each do |t|
    if !t.original_subgenus.nil?
      self.original_subgenus = t.original_subgenus
      fixed = true
    end
  end
  if fixed
    begin
      Protonym.transaction do
        self.save
      end
      return true
    rescue
      return false
    end
  end
end

#sv_fix_coordinated_names_original_subspeciesObject



830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
# File 'app/models/protonym/soft_validation_extensions.rb', line 830

def sv_fix_coordinated_names_original_subspecies
  fixed = false
  return false unless self.original_subspecies.nil?
  list_of_coordinated_names.each do |t|
    if !t.original_subspecies.nil?
      self.original_subspecies = t.original_subspecies
      fixed = true
    end
  end
  if fixed
    begin
      Protonym.transaction do
        self.save
      end
      return true
    rescue
      return false
    end
  end
end

#sv_fix_coordinated_names_original_varietyObject



860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
# File 'app/models/protonym/soft_validation_extensions.rb', line 860

def sv_fix_coordinated_names_original_variety
  fixed = false
  return false unless self.original_variety.nil?
  list_of_coordinated_names.each do |t|
    if !t.original_variety.nil?
      self.original_variety = t.original_variety
      fixed = true
    end
  end
  if fixed
    begin
      Protonym.transaction do
        self.save
      end
      return true
    rescue
      return false
    end
  end
end

#sv_fix_coordinated_names_pageObject



631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
# File 'app/models/protonym/soft_validation_extensions.rb', line 631

def sv_fix_coordinated_names_page
  fixed = false
  return false if self.origin_citation.nil? || !self.origin_citation.pages.nil?
  list_of_coordinated_names.each do |t|
    if !t.origin_citation.nil? && !t.origin_citation.pages.nil? && self.origin_citation.source_id == t.origin_citation.source_id
      self.origin_citation.pages = t.origin_citation.pages
      fixed = true
    end
  end
  if fixed
    begin
      Protonym.transaction do
        self.origin_citation.save
      end
      return true
    rescue
      return false
    end
  end
end

#sv_fix_coordinated_names_part_of_speechObject



720
721
722
723
724
725
726
727
728
729
# File 'app/models/protonym/soft_validation_extensions.rb', line 720

def sv_fix_coordinated_names_part_of_speech
  return false unless self.part_of_speech_class.nil?
  list_of_coordinated_names.each do |t|
    unless t.part_of_speech_class.nil?
      c = self.taxon_name_classifications.create(type: t.part_of_speech_class.to_s)
      return true if c.id
    end
  end
  return false
end

#sv_fix_coordinated_names_rolesObject



982
983
984
985
986
987
988
989
990
991
992
993
# File 'app/models/protonym/soft_validation_extensions.rb', line 982

def sv_fix_coordinated_names_roles
  return false unless self.taxon_name_author_roles.empty?
  list_of_coordinated_names.each do |t|
    if !t.taxon_name_author_roles.empty?
      t.taxon_name_author_roles.each do |r|
        TaxonNameAuthor.create(person_id: r.person_id, role_object: self, position: r.position)
      end
      return true
    end
  end
  return false
end

#sv_fix_coordinated_names_sourceObject



596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
# File 'app/models/protonym/soft_validation_extensions.rb', line 596

def sv_fix_coordinated_names_source
  fixed = false
  pg = nil
  return false unless self.source.nil?
    list_of_coordinated_names.each do |t|
      if !t.source.nil?
        self.source = t.source
        pg = t.origin_citation.pages
        fixed = true
      end
    end
    if fixed
    begin
      Protonym.transaction do
        self.source.save
        self.origin_citation.update_column(:pages, pg)
      end
      return true
    rescue
      return false
    end
  end
end

#sv_fix_coordinated_names_type_genusObject



1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
# File 'app/models/protonym/soft_validation_extensions.rb', line 1040

def sv_fix_coordinated_names_type_genus
  fixed = false
  return false unless self.type_genus.nil?
  list_of_coordinated_names.each do |t|
    if !t.type_genus.nil?
      self.type_genus = t.type_genus
      fixed = true
    end
  end
  if fixed
    begin
      Protonym.transaction do
        self.type_genus.save
      end
      return true
    rescue
      return false
    end
  end
end

#sv_fix_coordinated_names_type_speciesObject



921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
# File 'app/models/protonym/soft_validation_extensions.rb', line 921

def sv_fix_coordinated_names_type_species
  fixed = false
  tr = nil
  return false unless self.type_species.nil?
  list_of_coordinated_names.each do |t|
    if !t.type_species.nil? && fixed == false
      tr = TaxonNameRelationship.new(type: t.type_species_relationship.type, subject_taxon_name_id: t.type_species_relationship.subject_taxon_name_id, object_taxon_name_id: self.id)
      fixed = true
    end
  end
  if fixed
    begin
      TaxonNameRelationship.transaction do
        tr.save
      end
      return true
    rescue
      return false
    end
  end
end

#sv_fix_coordinated_names_type_species_typeObject



1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
# File 'app/models/protonym/soft_validation_extensions.rb', line 1007

def sv_fix_coordinated_names_type_species_type
  sttnr = self.type_taxon_name_relationship
  return false if sttnr.nil?
  fixed = false
  list_of_coordinated_names.each do |t|
    tttnr = t.type_taxon_name_relationship
    if !tttnr.nil? && sttnr.type != tttnr.type && sttnr.type.safe_constantize.descendants.collect{|i| i.to_s}.include?(tttnr.type.to_s)
      self.type_taxon_name_relationship.type = t.type_taxon_name_relationship.type
      fixed = true
    end
  end
  if fixed
    begin
      Protonym.transaction do
        self.type_taxon_name_relationship.save
      end
      return true
    rescue
      return false
    end
  end
end

#sv_fix_coordinated_names_type_specimenObject



1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
# File 'app/models/protonym/soft_validation_extensions.rb', line 1070

def sv_fix_coordinated_names_type_specimen
  fixed = false
  return false unless self.get_primary_type.empty?
  list_of_coordinated_names.each do |t1|
    types2 = t1.get_primary_type
    if !types2.empty?
      new_type_material = []
      types2.each do |t|
        new_type_material.push({type_type: t.type_type, protonym_id: self.id, collection_object_id: t.collection_object_id, source: t.source})
      end
      self.type_materials.build(new_type_material)
      fixed = true
    end
  end
  if fixed
    begin
      Protonym.transaction do
        self.save
      end
      return true
    rescue
      return false
    end
  end
end

#sv_fix_coordinated_names_yearObject



677
678
679
680
681
682
683
684
685
686
# File 'app/models/protonym/soft_validation_extensions.rb', line 677

def sv_fix_coordinated_names_year
  return false if !self.year_of_publication.nil? || !self.source.try(:year).nil?
  list_of_coordinated_names.each do |t|
    if self.year_of_publication.nil? && !t.year_of_publication.nil?
      self.update_column(:year_of_publication, t.year_of_publication)
      return true
    end
  end
  return false
end

#sv_fix_missing_otuObject



1651
1652
1653
1654
1655
1656
1657
# File 'app/models/protonym/soft_validation_extensions.rb', line 1651

def sv_fix_missing_otu
  if is_available? && otus.empty?
    otus.create(taxon_name_id: id)
    return true
  end
  return false
end

#sv_fix_misspelling_author_is_not_requiredObject



1623
1624
1625
1626
# File 'app/models/protonym/soft_validation_extensions.rb', line 1623

def sv_fix_misspelling_author_is_not_required
  self.update_column(:verbatim_author, nil)
  return true
end

#sv_fix_misspelling_roles_are_not_requiredObject



1607
1608
1609
1610
1611
1612
# File 'app/models/protonym/soft_validation_extensions.rb', line 1607

def sv_fix_misspelling_roles_are_not_required
  self.taxon_name_author_roles.each do |r|
    r.destroy
  end
  return true
end

#sv_fix_misspelling_year_is_not_requiredObject



1637
1638
1639
1640
# File 'app/models/protonym/soft_validation_extensions.rb', line 1637

def sv_fix_misspelling_year_is_not_required
  self.update_column(:year_of_publication, nil)
  return true
end

#sv_fix_presence_of_combinationObject



1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
# File 'app/models/protonym/soft_validation_extensions.rb', line 1690

def sv_fix_presence_of_combination
  begin
    TaxonName.transaction do
      c = Combination.new
      safe_self_and_ancestors.each do |i|
        case i.rank
          when 'genus'
            c.genus = i
          when 'subgenus'
            c.subgenus = i
          when 'species'
            c.species = i
          when 'subspecies'
            c.subspecies = i
        end
      end
      c.save
    end
  rescue
  end
end

#sv_fix_type_placement1Object



1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
# File 'app/models/protonym/soft_validation_extensions.rb', line 1218

def sv_fix_type_placement1
  self.type_of_taxon_names.each do |t|
    coordinated = t.lowest_rank_coordinated_taxon
    if self.parent_id != coordinated.id
      #          if t.id != coordinated.id && self.parent_id != coordinated.id
      begin
        Protonym.transaction do
          self.parent_id = coordinated.id
          self.save
        end
        return true
      rescue
        return false
      end
    end
  end
end

#sv_fix_year_is_not_requiredObject



1577
1578
1579
1580
# File 'app/models/protonym/soft_validation_extensions.rb', line 1577

def sv_fix_year_is_not_required
  self.update_column(:year_of_publication, nil)
  return true
end

#sv_homotypic_synonymsObject



1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
# File 'app/models/protonym/soft_validation_extensions.rb', line 1339

def sv_homotypic_synonyms
  unless !is_valid? # unavailable_or_invalid?
    if self.id == self.lowest_rank_coordinated_taxon.id
      possible_synonyms = []
      if rank_string =~ /Species/
        primary_types = self.get_primary_type
        unless primary_types.empty?
          p                 = primary_types.collect {|t| t.collection_object_id}
          possible_synonyms = Protonym.with_type_material_array(p).without_taxon_name_classification_array(TAXON_NAME_CLASS_NAMES_UNAVAILABLE_AND_INVALID).not_self(self).with_project(self.project_id)
        end
      elsif rank_string =~ /Family/ && self.name == Protonym.family_group_base(self.name)
        # do nothing
      else
        type = self.type_taxon_name
        unless type.nil?
          possible_synonyms = Protonym.with_type_of_taxon_names(type.id).not_self(self).with_project(self.project_id)
        end
      end

      possible_synonyms = reduce_list_of_synonyms(possible_synonyms)
      possible_synonyms.each do |s|
        soft_validations.add(:base, "Missing relationship: #{self.rank_class.rank_name} #{self.cached_html} should be a synonym of #{s.cached_html} #{s.cached_author_year} since they share the same type")
      end
    end
  end
end

#sv_missing_etymologyObject



1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
# File 'app/models/protonym/soft_validation_extensions.rb', line 1499

def sv_missing_etymology
  if self.etymology.nil? && self.rank_string =~ /(Genus|Species)/ && is_available?
    z = TaxonName.
        where(name: name, project_id: project_id).where.not(etymology: nil).
        group(:etymology).
        count(:etymology)

    if z.empty?
      z = TaxonName.where(name: name).where.not(etymology: nil).group(:etymology).count(:etymology)
      other_project = ' in different projects'
    else
      other_project = ''
    end

    if z.empty?
      soft_validations.add(:etymology, 'Etymology is missing')
    else
      z1 = z.sort_by {|k, v| -v}
      t = z1[0][1] == 1 ? 'time' : 'times'
      soft_validations.add(:etymology, "Etymology is missing. Previously used etymology for similar name#{other_project}: '#{z1[0][0]}' (#{z1[0][1]} #{t})")
    end
  end
end

#sv_missing_genderObject



495
496
497
498
499
500
# File 'app/models/protonym/soft_validation_extensions.rb', line 495

def sv_missing_gender
  if is_genus_rank? && self.gender_class.nil? && !self.cached_misspelling && is_available?
    g = genus_suggested_gender
    soft_validations.add(:base, "Gender is not specified#{ g.nil? ? '' : ' (possible gender is ' + g + ')'}")
  end
end

#sv_missing_infrasubspecific_statusObject



1478
1479
1480
1481
1482
# File 'app/models/protonym/soft_validation_extensions.rb', line 1478

def sv_missing_infrasubspecific_status
  if nomenclatural_code == :iczn && (self.cached_original_combination&.include?(' var. ') || self.cached_original_combination&.include?(' f. ')) && self.cached_nomenclature_date&.year > 1960 && is_available?(refresh = true)
    soft_validations.add(:base, 'Missing status. The name described as variety or form after 1960 should be treated as infrasubspecific.')
  end
end

#sv_missing_original_genusObject



1472
1473
1474
1475
1476
# File 'app/models/protonym/soft_validation_extensions.rb', line 1472

def sv_missing_original_genus
  if is_genus_or_species_rank? && self.original_genus.nil? && !not_binominal?
    soft_validations.add(:base, 'Missing relationship: Original genus is not selected')
  end
end

#sv_missing_otuObject



1642
1643
1644
1645
1646
1647
1648
1649
# File 'app/models/protonym/soft_validation_extensions.rb', line 1642

def sv_missing_otu
  if is_available? && otus.empty? && rank_string != 'NomenclaturalRank'
    soft_validations.add(
      :year_of_publication, 'Missing OTU',
      success_message: 'OTU was created',
      failure_message:  'Failed to create OTU')
  end
end

#sv_missing_part_of_speechObject



461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'app/models/protonym/soft_validation_extensions.rb', line 461

def sv_missing_part_of_speech
  if is_species_rank? && self.part_of_speech_class.nil? && !has_misspelling_relationship? && is_available?

    z = TaxonNameClassification.
        joins(:taxon_name).
        where(taxon_names: { name: name, project_id: project_id }).
        where("taxon_name_classifications.type LIKE 'TaxonNameClassification::Latinized::PartOfSpeech%'").
        group(:type).
        count(:type)

    if z.empty?
      z = TaxonNameClassification.
          joins(:taxon_name).
          where(taxon_names: { name: name}).
          where("taxon_name_classifications.type LIKE 'TaxonNameClassification::Latinized::PartOfSpeech%'").
          group(:type).
          count(:type)
      other_project = ' in different projects'
    else
      other_project = ''
    end

    if z.empty?
      soft_validations.add(:base, 'Part of speech is not specified. Please select if the name is a noun or an adjective.')
    else
      l = []
      z.each do |key, value|
        l << (value == 1 ? " as '#{key.constantize.label}' #{value.to_s} time" : " as '#{key.constantize.label}' #{value.to_s} times")
      end
      soft_validations.add(:base, "Part of speech is not specified. The name was previously used#{other_project}" + l.join('; '))
    end
  end
end

#sv_missing_rolesObject



1546
1547
1548
1549
1550
# File 'app/models/protonym/soft_validation_extensions.rb', line 1546

def sv_missing_roles
  if self.taxon_name_author_roles.empty? && !has_misspelling_relationship? && !name_is_misapplied? && is_family_or_genus_or_species_rank?
    soft_validations.add(:base, 'Taxon name author role is not selected')
  end
end

#sv_missing_substitute_nameObject



453
454
455
456
457
458
459
# File 'app/models/protonym/soft_validation_extensions.rb', line 453

def sv_missing_substitute_name
  if !self.iczn_set_as_homonym_of.nil? || !TaxonNameClassification.where_taxon_name(self).with_type_string('TaxonNameClassification::Iczn::Available::Invalid::Homonym').empty?
    if self.iczn_set_as_synonym_of.nil? && is_available?
      soft_validations.add(:base, 'Missing relationship: The name is a homonym, but a substitute name is not selected')
    end
  end
end

#sv_missing_type_genusObject



447
448
449
450
451
# File 'app/models/protonym/soft_validation_extensions.rb', line 447

def sv_missing_type_genus
  if is_family_rank? && self.type_genus.nil? && is_available?
    soft_validations.add(:base, 'Missing relationship: Type genus is not selected')
  end
end

#sv_missing_type_speciesObject



441
442
443
444
445
# File 'app/models/protonym/soft_validation_extensions.rb', line 441

def sv_missing_type_species
  if is_genus_rank? && self.type_species.nil? && is_available?
    soft_validations.add(:base, 'Missing relationship: Type species is not selected')
  end
end

#sv_misspelling_author_is_not_requiredObject



1614
1615
1616
1617
1618
1619
1620
1621
# File 'app/models/protonym/soft_validation_extensions.rb', line 1614

def sv_misspelling_author_is_not_required
  if self.verbatim_author && self.source && taxon_name_relationships.with_type_array(TAXON_NAME_RELATIONSHIP_NAMES_MISSPELLING_AND_MISAPPLICATION).any?
    soft_validations.add(
      :verbatim_author, 'Verbatim author is not required for misspellings and misapplications',
      success_message: 'Verbatim author was deleted',
      failure_message:  'Failed to delete verbatim author')
  end
end

#sv_misspelling_roles_are_not_requiredObject



1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
# File 'app/models/protonym/soft_validation_extensions.rb', line 1596

def sv_misspelling_roles_are_not_required
  #DD: do not use .has_misspelling_relationship?
  misspellings = taxon_name_relationships.with_type_array(TAXON_NAME_RELATIONSHIP_NAMES_MISSPELLING_AND_MISAPPLICATION).any?
  if !self.taxon_name_author_roles.empty? && self.source && misspellings
    soft_validations.add(
      :base, 'Taxon name author role is not required for misspellings and misapplications',
      success_message: 'Roles were deleted',
      failure_message:  'Fail to delete roles')
  end
end

#sv_misspelling_year_is_not_requiredObject



1628
1629
1630
1631
1632
1633
1634
1635
# File 'app/models/protonym/soft_validation_extensions.rb', line 1628

def sv_misspelling_year_is_not_required
  if self.year_of_publication && taxon_name_relationships.with_type_array(TAXON_NAME_RELATIONSHIP_NAMES_MISSPELLING_AND_MISAPPLICATION).any?
    soft_validations.add(
      :year_of_publication, 'Year is not required for misspellings and misapplications',
      success_message: 'Year was deleted',
      failure_message:  'Failed to delete year')
  end
end

#sv_original_combination_relationshipsObject



1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
# File 'app/models/protonym/soft_validation_extensions.rb', line 1484

def sv_original_combination_relationships
  relationships = self.original_combination_relationships
  unless relationships.empty?
    relationships = relationships.sort_by{|r| r.type_class.order_index }
    ids = relationships.collect{|r| r.subject_taxon_name_id}
    if !ids.include?(self.id)
      if (list_of_coordinated_names.collect{|r| r.id} & ids).empty?
        soft_validations.add(:base, "Missing relationship: The original rank of #{self.cached_html} is not specified in the original combination.")
      end
    elsif ids.last != self.id
      soft_validations.add(:base, "Invalid original combination relationship: #{self.cached_html} should be moved to the lowest rank")
    end
  end
end

#sv_parent_priorityObject



1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
# File 'app/models/protonym/soft_validation_extensions.rb', line 1320

def sv_parent_priority
  if self.rank_class
    rank_group = self.rank_class.parent
    parent = self.parent

    if !is_higher_rank? && parent && rank_group == parent.rank_class.parent
      unless !is_valid? #  unavailable_or_invalid?
        date1 = self.cached_nomenclature_date
        date2 = parent.cached_nomenclature_date
        unless date1.nil? || date2.nil?
          if date1 < date2
            soft_validations.add(:base, "#{self.rank_class.rank_name.capitalize} #{self.cached_html_name_and_author_year} should not be older than parent #{parent.rank_class.rank_name} #{parent.cached_html_name_and_author_year}")
          end
        end
      end
    end
  end
end

#sv_person_vs_year_of_publicationObject



1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
# File 'app/models/protonym/soft_validation_extensions.rb', line 1552

def sv_person_vs_year_of_publication
  if self.cached_nomenclature_date
    year = self.cached_nomenclature_date&.year
    self.taxon_name_author_roles.each do |r|
      person = r.person
      if person.year_died && year > person.year_died + 2
        soft_validations.add(:base, "The year of description does not fit the years of #{person.last_name}'s life (#{person.year_born}#{person.year_died})")
      elsif person.year_born && year > person.year_born + 105
        soft_validations.add(:base, "The year of description does not fit the years of #{person.last_name}'s life (#{person.year_born}#{person.year_died})")
      elsif person.year_born && year < person.year_born + 10
        soft_validations.add(:base, "The year of description does not fit the years of #{person.last_name}'s life (#{person.year_born}#{person.year_died})")
      end
    end
  end
end

#sv_potential_family_homonymsObject



1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
# File 'app/models/protonym/soft_validation_extensions.rb', line 1366

def sv_potential_family_homonyms
  if persisted? && is_family_rank? && is_available?
    if TaxonNameRelationship.where_subject_is_taxon_name(self).homonym_or_suppressed.empty?
      if self.id == self.lowest_rank_coordinated_taxon.id
        name2 = self.cached_primary_homonym_alternative_spelling ? self.cached_primary_homonym_alternative_spelling : nil
        possible_primary_homonyms_alternative_spelling = name2 ? Protonym.with_primary_homonym_alternative_spelling(name2).without_homonym_or_suppressed.without_taxon_name_classification_array(TAXON_NAME_CLASS_NAMES_UNAVAILABLE_AND_INVALID).not_self(self).with_base_of_rank_class('NomenclaturalRank::Iczn::FamilyGroup').with_project(self.project_id) : []
        list2 = reduce_list_of_synonyms(possible_primary_homonyms_alternative_spelling)
        if !list2.empty?
          list2.each do |s|
            soft_validations.add(:base, "Missing relationship: #{self.cached_html_name_and_author_year} should be a homonym or duplicate of #{s.cached_html_name_and_author_year}")
          end
        end
      end
    end
  end
end

#sv_potential_genus_homonymsObject



1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
# File 'app/models/protonym/soft_validation_extensions.rb', line 1407

def sv_potential_genus_homonyms
  if persisted? && is_genus_rank? && is_available?
    if TaxonNameRelationship.where_subject_is_taxon_name(self).homonym_or_suppressed.empty?
      if self.id == self.lowest_rank_coordinated_taxon.id
        name1 = self.cached_primary_homonym ? self.cached_primary_homonym : nil
        possible_primary_homonyms = name1 ? Protonym.with_primary_homonym(name1).without_taxon_name_classification_array(TAXON_NAME_CLASS_NAMES_UNAVAILABLE_AND_INVALID).without_homonym_or_suppressed.not_self(self).with_base_of_rank_class('NomenclaturalRank::Iczn::GenusGroup').with_project(self.project_id) : []
        list1 = reduce_list_of_synonyms(possible_primary_homonyms)
        if !list1.empty?
          list1.each do |s|
            soft_validations.add(:base, "Missing relationship: #{self.cached_html_name_and_author_year} should be a homonym or duplicate of #{s.cached_html_name_and_author_year}") if s.nomenclatural_code == self.nomenclatural_code
          end
        end
      end
    end
  end
end

#sv_potential_species_homonymsObject



1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
# File 'app/models/protonym/soft_validation_extensions.rb', line 1424

def sv_potential_species_homonyms
  if persisted? && is_species_rank? && is_available?
    if TaxonNameRelationship.where_subject_is_taxon_name(self).homonym_or_suppressed.empty?
      if self.id == self.lowest_rank_coordinated_taxon.id
        name1 = self.cached_primary_homonym ? self.cached_primary_homonym : nil
        possible_primary_homonyms = name1 ? Protonym.with_primary_homonym(name1).without_taxon_name_classification_array(TAXON_NAME_CLASS_NAMES_UNAVAILABLE_AND_INVALID).without_homonym_or_suppressed.not_self(self).with_base_of_rank_class('NomenclaturalRank::Iczn::SpeciesGroup').with_project(self.project_id) : []
        list1 = reduce_list_of_synonyms(possible_primary_homonyms)
        if !list1.empty?
          list1.each do |s|
            soft_validations.add(
              :base, "Missing relationship: #{self.cached_html_name_and_author_year} should be a primary homonym or duplicate of #{s.cached_html_name_and_author_year}")
            #  fix: :sv_fix_add_relationship('iczn_set_as_primary_homonym_of'.to_sym, s.id),
            #  success_message: 'Primary homonym relationship was added',
            #  failure_message: 'Fail to add a relationship')
          end
        else
          name2 = self.cached_primary_homonym_alternative_spelling ? self.cached_primary_homonym_alternative_spelling : nil
          possible_primary_homonyms_alternative_spelling = name2 ? Protonym.with_primary_homonym_alternative_spelling(name2).without_homonym_or_suppressed.without_taxon_name_classification_array(TAXON_NAME_CLASS_NAMES_UNAVAILABLE_AND_INVALID).not_self(self).with_base_of_rank_class('NomenclaturalRank::Iczn::SpeciesGroup').with_project(self.project_id) : []
          list2 = reduce_list_of_synonyms(possible_primary_homonyms_alternative_spelling)
          if !list2.empty?
            list2.each do |s|
              soft_validations.add(:base, "Missing relationship: #{self.cached_html_name_and_author_year} could be a primary homonym of #{s.cached_html_name_and_author_year} (alternative spelling)")
            end
          else
            name3 = self.cached_secondary_homonym ? self.cached_secondary_homonym : nil
            possible_secondary_homonyms = name3 ? Protonym.with_secondary_homonym(name3).without_homonym_or_suppressed.without_taxon_name_classification_array(TAXON_NAME_CLASS_NAMES_UNAVAILABLE_AND_INVALID).not_self(self).with_base_of_rank_class('NomenclaturalRank::Iczn::SpeciesGroup').with_project(self.project_id) : []
            list3 = reduce_list_of_synonyms(possible_secondary_homonyms)
            if !list3.empty?
              list3.each do |s|
                soft_validations.add(:base, "Missing relationship: #{self.cached_html_name_and_author_year} should be a secondary homonym or duplicate of #{s.cached_html_name_and_author_year}")
              end
            else
              name4 = self.cached_secondary_homonym ? self.cached_secondary_homonym_alternative_spelling : nil
              possible_secondary_homonyms_alternative_spelling = name4 ? Protonym.with_secondary_homonym_alternative_spelling(name4).without_homonym_or_suppressed.without_taxon_name_classification_array(TAXON_NAME_CLASS_NAMES_UNAVAILABLE_AND_INVALID).not_self(self).with_base_of_rank_class('NomenclaturalRank::Iczn::SpeciesGroup').with_project(self.project_id) : []
              list4 = reduce_list_of_synonyms(possible_secondary_homonyms_alternative_spelling)
              if !list4.empty?
                list4.each do |s|
                  soft_validations.add(:base, "Missing relationship: #{self.cached_html_name_and_author_year} could be a secondary homonym of #{s.cached_html_name_and_author_year} (alternative spelling)")
                end
              end
            end
          end
        end
      end
    end
  end
end

#sv_potential_usage_duplicatesObject



1659
1660
1661
1662
1663
1664
1665
# File 'app/models/protonym/soft_validation_extensions.rb', line 1659

def sv_potential_usage_duplicates
  usage = TaxonNameRelationship::Iczn::Invalidating::Usage.where(subject_taxon_name_id: self.id).first
  return true if usage.nil?
  TaxonNameRelationship::Iczn::Invalidating::Usage.where(object_taxon_name_id: usage.object_taxon_name_id).not_self(usage).each do |tr|
    soft_validations.add(:base, "'#{name}' is a duplicate #{usage.subject_status + ' ' + usage.subject_status_connector_to_object} #{tr.object_taxon_name.cached_html}",) if usage.subject_taxon_name.name == tr.subject_taxon_name.name
  end
end

#sv_presence_of_combinationObject



1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
# File 'app/models/protonym/soft_validation_extensions.rb', line 1679

def sv_presence_of_combination
  if is_genus_or_species_rank? && is_valid? && self.id == self.lowest_rank_coordinated_taxon.id && !cached_original_combination.nil? && cached != cached_original_combination
    unless Combination.where("cached = ? AND cached_valid_taxon_name_id = ?", cached, cached_valid_taxon_name_id).any?
      soft_validations.add(
        :base, "Protonym #{self.cached_html} missing corresponding subsequent combination. Current classification of the taxon is different from original combination. (Fix will try to create a new combination if possible)",
        success_message: "Combination #{self.cached_html} was successfully create",
        failure_message:  'Failed to create a new combination')
    end
  end
end

#sv_primary_typesObject



1236
1237
1238
1239
1240
1241
1242
1243
1244
# File 'app/models/protonym/soft_validation_extensions.rb', line 1236

def sv_primary_types
  if is_species_rank? && is_available?
    if self.type_materials.primary.empty? && self.type_materials.syntypes.empty?
      soft_validations.add(:base, 'Primary type is not selected')
    elsif self.type_materials.primary.count > 1 || (!self.type_materials.primary.empty? && !self.type_materials.syntypes.empty?)
      soft_validations.add(:base, 'More than one of primary types are selected. Uncheck the specimens which are not primary types for this taxon')
    end
  end
end

#sv_primary_types_repositoryObject



1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
# File 'app/models/protonym/soft_validation_extensions.rb', line 1246

def sv_primary_types_repository
  if is_species_rank?
    s = self.type_materials
    unless s.empty?
      s.primary.each do |t|
        soft_validations.add(:base, 'Primary type repository is not set') if t.collection_object.try(:repository).nil?
      end
      s.syntypes.each do |t|
        soft_validations.add(:base, 'Syntype repository is not set') if t.collection_object.try(:repository).nil?
      end
    end
  end
end

#sv_protonym_to_combinationObject



1538
1539
1540
1541
1542
1543
1544
# File 'app/models/protonym/soft_validation_extensions.rb', line 1538

def sv_protonym_to_combination
  if convertable_to_combination?
    soft_validations.add(
      :base, "Unavailable or Invalid #{self.cached_original_combination_html} could potentially be converted into a combination (Fix will try to convert protonym into combination)",
      success_message: "Protonym #{self.cached_original_combination_html} was successfully converted into a combination", failure_message:  'Failed to convert protonym into combination')
  end
end

#sv_single_sub_taxonObject



1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
# File 'app/models/protonym/soft_validation_extensions.rb', line 1260

def sv_single_sub_taxon
  if self.rank_class
    rank = rank_string
    if rank != 'potentially_validating rank' && self.rank_class.nomenclatural_code == :iczn && %w(subspecies subgenus subtribe tribe subfamily).include?(self.rank_class.rank_name)
      sisters = self.parent.descendants.with_rank_class(rank).select{|t| t.id == t.cached_valid_taxon_name_id}
      if rank =~ /Family/
        z = Protonym.family_group_base(self.name)
        search_name = z.nil? ? nil : Protonym::FAMILY_GROUP_ENDINGS.collect{|i| z+i}
        a = sisters.collect{|i| Protonym.family_group_base(i.name) }
        sister_names = a.collect{|z| Protonym::FAMILY_GROUP_ENDINGS.collect{|i| z+i} }.flatten
      else
        search_name = [self.name]
        sister_names = sisters.collect{|i| i.name }
      end
      if search_name.include?(self.parent.name) && sisters.count == 1
        soft_validations.add(:base, "#{self.cached_html} is a single #{self.rank_class.rank_name} in the nominal #{self.parent.rank_class.rank_name} #{self.parent.cached_html}")
      elsif !sister_names.include?(self.parent.name) && !sisters.empty? && self.parent.name == Protonym.family_group_base(self.parent.name) && rank =~ /Family/
        # do nothing
      elsif !sister_names.include?(self.parent.name) && !sisters.empty?
        soft_validations.add(:base, "The parent #{self.parent.rank_class.rank_name} #{self.parent.cached_html} of this #{self.rank_class.rank_name} does not contain nominotypical #{self.rank_class.rank_name} #{Protonym.family_group_name_at_rank(self.parent.name, self.rank_class.rank_name)}",
                             success_message: "Nominotypical #{self.rank_class.rank_name} was added to nominal #{self.parent.rank_class.rank_name} #{self.parent.cached_html}", failure_message:  'Failed to create nomynotypical taxon')
      end
    end
  end
end

#sv_source_not_older_then_descriptionObject



399
400
401
402
403
# File 'app/models/protonym/soft_validation_extensions.rb', line 399

def sv_source_not_older_then_description
  if self.source && self.year_of_publication
    soft_validations.add(:base, 'The year of publication of the taxon and the year in the original reference do not match') if self.try(:source).try(:year) != self.year_of_publication
  end
end

#sv_species_gender_agreementObject



502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
# File 'app/models/protonym/soft_validation_extensions.rb', line 502

def sv_species_gender_agreement
  if is_species_rank?
    s = part_of_speech_name
    if !s.nil? && is_available?
      if %w{adjective participle}.include?(s)
        if !feminine_name.blank? && !masculine_name.blank? && !neuter_name.blank? && name != masculine_name && name != feminine_name && name != neuter_name
          soft_validations.add(:base, 'Species name does not match with either of three alternative forms')
        else
          forms = predict_three_forms
          if feminine_name.blank?
            soft_validations.add(:feminine_name, "The species name is marked as #{part_of_speech_name}, but the name spelling in feminine is not provided")
          else
            # e = species_questionable_ending(TaxonNameClassification::Latinized::Gender::Feminine, feminine_name)
            # soft_validations.add(:feminine_name, "Name has a non feminine ending: -#{e}") unless e.nil?
            soft_validations.add(:feminine_name, "Feminine form does not match with predicted: #{forms[:feminine_name]}") if feminine_name != forms[:feminine_name]
          end

          if masculine_name.blank?
            soft_validations.add(:masculine_name, "The species name is marked as #{part_of_speech_name}, but the name spelling in masculine is not provided")
          else
            # e = species_questionable_ending(TaxonNameClassification::Latinized::Gender::Masculine, masculine_name)
            # soft_validations.add(:masculine_name, "Name has a non masculine ending: -#{e}") unless e.nil?
            soft_validations.add(:masculine_name, "Masculine form does not match with predicted: #{forms[:masculine_name]}") if masculine_name != forms[:masculine_name]
          end

          if neuter_name.blank?
            soft_validations.add(:neuter_name, "The species name is marked as #{part_of_speech_name}, but the name spelling in neuter is not provided")
          else
            # e = species_questionable_ending(TaxonNameClassification::Latinized::Gender::Neuter, neuter_name)
            # soft_validations.add(:neuter_name, "Name has a non neuter ending: -#{e}") unless e.nil?
            soft_validations.add(:neuter_name, "Neuter form does not match with predicted: #{forms[:neuter_name]}") if neuter_name != forms[:neuter_name]
          end
        end
      end
    end
  end
end

#sv_species_gender_agreement_not_requiredObject



540
541
542
543
544
545
546
547
548
549
# File 'app/models/protonym/soft_validation_extensions.rb', line 540

def sv_species_gender_agreement_not_required
  if is_species_rank? && ((!feminine_name.blank? || !masculine_name.blank? || !neuter_name.blank?)) && is_available?
    s = part_of_speech_name
    if !s.nil? && !%w{adjective participle}.include?(s)
      soft_validations.add(:feminine_name, 'Alternative spelling is not required for the name which is not adjective or participle.') unless feminine_name.blank?
      soft_validations.add(:masculine_name, 'Alternative spelling is not required for the name which is not adjective or participle.')  unless masculine_name.blank?
      soft_validations.add(:neuter_name, 'Alternative spelling is not required for the name which is not adjective or participle.')  unless neuter_name.blank?
    end
  end
end

#sv_type_placementObject

def sv_fix_coordinated_names

  fixed = false
  gender = self.gender_class
  speech = self.part_of_speech_class

  list_of_coordinated_names.each do |t|
    if t.source.nil? && !t.source.nil?
      self.source = t.source
      fixed = true
    end

    if self.verbatim_author.nil? && !t.verbatim_author.nil?
      self.verbatim_author = t.verbatim_author
      fixed = true
    end

    if self.year_of_publication.nil? && !t.year_of_publication.nil?
      self.year_of_publication = t.year_of_publication
      fixed = true
    end

    t_gender = t.gender_class

    if gender.nil? && !t_gender.nil?
      self.taxon_name_classifications.build(type: t_gender.to_s)
      fixed = true
    end

    t_speech = t.part_of_speech_class

    if speech.nil? && speech != t_speech
      self.taxon_name_classifications.build(type: t_speech.to_s)
      fixed = true
    end

    if self.gender_class.nil? && !t.gender_class.nil?
      self.taxon_name_classifications.build(type: t.gender_class.to_s)
      fixed = true
    end

    if self.original_genus.nil? && !t.original_genus.nil?
      self.original_genus = t.original_genus
      fixed = true
    end

    if self.original_subgenus.nil? && !t.original_subgenus.nil?
      self.original_subgenus = t.original_subgenus
      fixed = true
    end

    if self.original_species.nil? && !t.original_species.nil?
      self.original_species = t.original_species
      fixed = true
    end

    if self.original_subspecies.nil? && !t.original_subspecies.nil?
      self.original_subspecies = t.original_subspecies
      fixed = true
    end

    if self.type_species.nil? && !t.type_species.nil?
      self.type_species = t.type_species
      fixed = true
    end

    if self.type_genus.nil? && !t.type_genus.nil?
      self.type_genus = t.type_genus
      fixed = true
    end

    types1 = self.get_primary_type
    types2 = t.get_primary_type
    if types1.empty? && !types2.empty?
      new_type_material = []
      types2.each do |t|
        new_type_material.push({type_type: t.type_type, protonym_id: t.protonym_id, collection_object_id: t.collection_object_id, source: t.source})
      end
      self.type_materials.build(new_type_material)
      fixed = true
    end

    sttnr = self.type_taxon_name_relationship
    tttnr = t.type_taxon_name_relationship
    unless sttnr.nil? || tttnr.nil?
      if sttnr.type != tttnr.type && sttnr.type.safe_constantize.descendants.collect{|i| i.to_s}.include?(tttnr.type.to_s)
        self.type_taxon_name_relationship.type = t.type_taxon_name_relationship.type
        fixed = true
      end
    end
  end

  if fixed
    begin
      Protonym.transaction do
        self.save
      end
    rescue
      return false
    end
  end
  return fixed
end


1201
1202
1203
1204
1205
1206
# File 'app/models/protonym/soft_validation_extensions.rb', line 1201

def sv_type_placement
  # type of this taxon is not included in this taxon
  if !!self.type_taxon_name
    soft_validations.add(:base, "#{self.rank_class.rank_name} #{self.cached_html} has the type #{self.type_taxon_name.rank_class.rank_name} #{self.type_taxon_name.cached_html} classified outside of this taxon") unless self.type_taxon_name.get_valid_taxon_name.ancestors.include?(TaxonName.find(self.cached_valid_taxon_name_id))
  end
end

#sv_type_placement1Object



1208
1209
1210
1211
1212
1213
1214
1215
1216
# File 'app/models/protonym/soft_validation_extensions.rb', line 1208

def sv_type_placement1
  # this taxon is a type, but not included in nominal taxon
  if !!self.type_of_taxon_names
    self.type_of_taxon_names.each do |t|
      soft_validations.add(:base, "#{self.rank_class.rank_name.capitalize} #{self.cached_html} is the type of #{t.rank_class.rank_name} #{t.cached_html} but it has a parent outside of #{t.cached_html}",
                           success_message: 'Parent for type species was updated', failure_message: 'Parent for type species was not updated') unless self.get_valid_taxon_name.ancestors.include?(TaxonName.find(t.cached_valid_taxon_name_id))
    end
  end
end

#sv_validate_coordinated_names_authorObject



652
653
654
655
656
657
# File 'app/models/protonym/soft_validation_extensions.rb', line 652

def sv_validate_coordinated_names_author
  s = self.verbatim_author
  list_of_coordinated_names.each do |t|
    soft_validations.add(:verbatim_author, "The author does not match with the author of the coordinate #{t.rank_class.rank_name}", success_message: 'Author was updated', failure_message:  'Failed to update author') unless s == t.verbatim_author
  end
end

#sv_validate_coordinated_names_etymologyObject



943
944
945
946
947
948
949
950
# File 'app/models/protonym/soft_validation_extensions.rb', line 943

def sv_validate_coordinated_names_etymology
  return true unless is_available?
  list_of_coordinated_names.each do |t|
    if self.etymology != t.etymology
      soft_validations.add(:etymology, "The etymology does not match with the etymology of the coordinate #{t.rank_class.rank_name}", success_message: 'Etymology was updated', failure_message:  'Etymology was not updated')
    end
  end
end

#sv_validate_coordinated_names_genderObject



688
689
690
691
692
693
694
695
696
697
# File 'app/models/protonym/soft_validation_extensions.rb', line 688

def sv_validate_coordinated_names_gender
  return true unless is_available?
  return true unless is_genus_rank?
  s = self.gender_class
  list_of_coordinated_names.each do |t|
    if s != t.gender_class
      soft_validations.add(:base, "The gender status does not match with that of the coordinate #{t.rank_class.rank_name}", success_message: 'Gender was updated', failure_message:  'Failed to update gender')
    end
  end
end

#sv_validate_coordinated_names_original_formObject



881
882
883
884
885
886
887
888
# File 'app/models/protonym/soft_validation_extensions.rb', line 881

def sv_validate_coordinated_names_original_form
  return true if !is_species_rank? || has_misspelling_relationship?
  list_of_coordinated_names.each do |t|
    if self.original_form.try(:name) != t.original_form.try(:name)
      soft_validations.add(:base, "The original form does not match with the original form of coordinate #{t.rank_class.rank_name}", success_message: 'Original form was updated', failure_message:  'Failed to update original form')
    end
  end
end

#sv_validate_coordinated_names_original_genusObject



731
732
733
734
735
736
737
738
# File 'app/models/protonym/soft_validation_extensions.rb', line 731

def sv_validate_coordinated_names_original_genus
  return true if !is_genus_or_species_rank? || !is_valid?
  list_of_coordinated_names.each do |t|
    if self.original_genus.try(:name) != t.original_genus.try(:name)
      soft_validations.add(:base, "The original genus does not match with the original genus of coordinate #{t.rank_class.rank_name}", success_message: 'Original genus was updated', failure_message:  'Failed to update original genus')
    end
  end
end

#sv_validate_coordinated_names_original_speciesObject



791
792
793
794
795
796
797
798
# File 'app/models/protonym/soft_validation_extensions.rb', line 791

def sv_validate_coordinated_names_original_species
  return true if !is_species_rank? || has_misspelling_relationship?
  list_of_coordinated_names.each do |t|
    if self.original_species.try(:name) != t.original_species.try(:name)
      soft_validations.add(:base, "The original species does not match with the original species of coordinate #{t.rank_class.rank_name}", success_message: 'Original species was updated', failure_message:  'Failed to update original species')
    end
  end
end

#sv_validate_coordinated_names_original_subgenusObject



761
762
763
764
765
766
767
768
# File 'app/models/protonym/soft_validation_extensions.rb', line 761

def sv_validate_coordinated_names_original_subgenus
  return true if !is_genus_or_species_rank? || has_misspelling_relationship?
  list_of_coordinated_names.each do |t|
    if self.original_subgenus.try(:name) != t.original_subgenus.try(:name)
      soft_validations.add(:base, "The original subgenus does not match with the original subgenus of coordinate #{t.rank_class.rank_name}", success_message: 'Original subgenus was updated', failure_message:  'Failed to update original subgenus')
    end
  end
end

#sv_validate_coordinated_names_original_subspeciesObject



821
822
823
824
825
826
827
828
# File 'app/models/protonym/soft_validation_extensions.rb', line 821

def sv_validate_coordinated_names_original_subspecies
  return true if !is_species_rank? || has_misspelling_relationship?
  list_of_coordinated_names.each do |t|
    if self.original_subspecies.try(:name) != t.original_subspecies.try(:name)
      soft_validations.add(:base, "The original subspecies does not match with the original subspecies of coordinate #{t.rank_class.rank_name}", success_message: 'Original subspecies was updated', failure_message:  'Failed to update original subspecies')
    end
  end
end

#sv_validate_coordinated_names_original_varietyObject



851
852
853
854
855
856
857
858
# File 'app/models/protonym/soft_validation_extensions.rb', line 851

def sv_validate_coordinated_names_original_variety
  return true if !is_species_rank? || has_misspelling_relationship?
  list_of_coordinated_names.each do |t|
    if self.original_variety.try(:name) != t.original_variety.try(:name)
      soft_validations.add(:base, "The original variety does not match with the original variety of coordinate #{t.rank_class.rank_name}", success_message: 'Original variety was updated', failure_message:  'Failed to update original variety')
    end
  end
end

#sv_validate_coordinated_names_pageObject



620
621
622
623
624
625
626
627
628
629
# File 'app/models/protonym/soft_validation_extensions.rb', line 620

def sv_validate_coordinated_names_page
  return true unless is_valid?
  s = self.origin_citation
  list_of_coordinated_names.each do |t|
    ts = t.origin_citation
    if s && ts && s.source_id == ts.source_id && (s.pages != ts.pages || (s.pages.nil? && !ts.pages.nil?))
      soft_validations.add(:base, "The original publication page does not match with the original publication page of the coordinate #{t.rank_class.rank_name}", success_message: 'Original publication pages were updated', failure_message: 'Original publication pages were not updated')
    end
  end
end

#sv_validate_coordinated_names_part_of_speechObject



710
711
712
713
714
715
716
717
718
# File 'app/models/protonym/soft_validation_extensions.rb', line 710

def sv_validate_coordinated_names_part_of_speech
  return true unless is_available?
  return true unless is_species_rank?
  list_of_coordinated_names.each do |t|
    if self.part_of_speech_class != t.part_of_speech_class
      soft_validations.add(:base, "The part of speech status does not match with that of the coordinate #{t.rank_class.rank_name}", success_message: 'Part of speech was updated', failure_message:  'Failed to update part of speech')
    end
  end
end

#sv_validate_coordinated_names_rolesObject



973
974
975
976
977
978
979
980
# File 'app/models/protonym/soft_validation_extensions.rb', line 973

def sv_validate_coordinated_names_roles
  return true unless is_valid?
  list_of_coordinated_names.each do |t|
    if self.taxon_name_author_roles.collect{|i| i.person_id} != t.taxon_name_author_roles.collect{|i| i.person_id}
      soft_validations.add(:base, "The author roles do not match with the author roles of the coordinate #{t.rank_class.rank_name}", success_message: 'Author roles were updated', failure_message:  'Author roles were not updated')
    end
  end
end

#sv_validate_coordinated_names_sourceObject

def sv_validate_coordinated_names

  return true unless is_available?
  r = self.iczn_set_as_incorrect_original_spelling_of_relationship
  list_of_coordinated_names.each do |t|
    soft_validations.add(:base, "The original publication does not match with the original publication of the coordinated #{t.rank_class.rank_name}",
                         fix: :sv_fix_coordinated_names, success_message: 'Original publication was updated') if self.source && t.source && self.source.id != t.source.id
    soft_validations.add(:verbatim_author, "The author does not match with the author of the coordinated #{t.rank_class.rank_name}",
                         fix: :sv_fix_coordinated_names, success_message: 'Author was updated') unless self.verbatim_author == t.verbatim_author
    soft_validations.add(:year_of_publication, "The year of publication does not match with the year of the coordinated #{t.rank_class.rank_name}",
                         fix: :sv_fix_coordinated_names, success_message: 'Year was updated') unless self.year_of_publication == t.year_of_publication
    soft_validations.add(:base, "The gender status does not match with the gender of the coordinated #{t.rank_class.rank_name}",
                         fix: :sv_fix_coordinated_names, success_message: 'Gender was updated') if rank_string =~ /Genus/ && self.gender_class != t.gender_class && !has_misspelling_relationship?
    soft_validations.add(:base, "The part of speech status does not match with the part of speech of the coordinated #{t.rank_class.rank_name}",
                         fix: :sv_fix_coordinated_names, success_message: 'Gender was updated') if rank_string =~ /Species/ && self.part_of_speech_class != t.part_of_speech_class && !has_misspelling_relationship?
    soft_validations.add(:base, "The original genus does not match with the original genus of coordinated #{t.rank_class.rank_name}",
                         fix: :sv_fix_coordinated_names, success_message: 'Original genus was updated') if self.original_genus != t.original_genus && r.blank?
    soft_validations.add(:base, "The original subgenus does not match with the original subgenus of the coordinated #{t.rank_class.rank_name}",
                         fix: :sv_fix_coordinated_names, success_message: 'Original subgenus was updated') if self.original_subgenus != t.original_subgenus && r.blank?
    soft_validations.add(:base, "The original species does not match with the original species of the coordinated #{t.rank_class.rank_name}",
                         fix: :sv_fix_coordinated_names, success_message: 'Original species was updated') if self.original_species != t.original_species && r.blank?
    soft_validations.add(:base, "The type species does not match with the type species of the coordinated #{t.rank_class.rank_name}",
                         fix: :sv_fix_coordinated_names, success_message: 'Type species was updated') if self.type_species != t.type_species && !has_misspelling_relationship?
    soft_validations.add(:base, "The type genus does not match with the type genus of the coordinated #{t.rank_class.rank_name}",
                         fix: :sv_fix_coordinated_names, success_message: 'Type genus was updated') if self.type_genus != t.type_genus && !has_misspelling_relationship?
    soft_validations.add(:base, "The type specimen does not match with the type specimen of the coordinated #{t.rank_class.rank_name}",
                         fix: :sv_fix_coordinated_names, success_message: 'Type specimen was updated') if !self.has_same_primary_type(t) && !has_misspelling_relationship?
    sttnr = self.type_taxon_name_relationship
    tttnr = t.type_taxon_name_relationship
    unless sttnr.nil? || tttnr.nil?
      soft_validations.add(:base, "The type species relationship does not match with the type species relationship of the coordinated #{t.rank_class.rank_name}",
                           fix: :sv_fix_coordinated_names, success_message: 'Type species relationship was updated') unless sttnr.type == tttnr.type
    end
  end
end


586
587
588
589
590
591
592
593
594
# File 'app/models/protonym/soft_validation_extensions.rb', line 586

def sv_validate_coordinated_names_source
  return true unless is_valid?
  s = self.source
  list_of_coordinated_names.each do |t|
    if ((s && t.source && s.id != t.source.id) || (s.nil? && t.source))
      soft_validations.add(:base, "The original publication does not match with the original publication of the coordinate #{t.rank_class.rank_name}", success_message: 'Original publication was updated', failure_message:  'Failed to update original publication')
    end
  end
end

#sv_validate_coordinated_names_type_genusObject



1030
1031
1032
1033
1034
1035
1036
1037
1038
# File 'app/models/protonym/soft_validation_extensions.rb', line 1030

def sv_validate_coordinated_names_type_genus
  return true unless is_valid?
  return true unless is_family_rank?
  list_of_coordinated_names.each do |t|
    if self.type_genus != t.type_genus
      soft_validations.add(:base, "The type genus does not match with the type genus of the coordinate #{t.rank_class.rank_name}", success_message: 'Type genus was updated', failure_message:  'Failed to update type genus')
    end
  end
end

#sv_validate_coordinated_names_type_speciesObject



911
912
913
914
915
916
917
918
919
# File 'app/models/protonym/soft_validation_extensions.rb', line 911

def sv_validate_coordinated_names_type_species
  return true unless is_valid?
  return true unless is_genus_rank?
  list_of_coordinated_names.each do |t|
    if self.type_species != t.type_species
      soft_validations.add(:base, "The type species does not match with the type species of the coordinate #{t.rank_class.rank_name}", success_message: 'Type species was updated', failure_message:  'Failed to update type species')
    end
  end
end

#sv_validate_coordinated_names_type_species_typeObject



995
996
997
998
999
1000
1001
1002
1003
1004
1005
# File 'app/models/protonym/soft_validation_extensions.rb', line 995

def sv_validate_coordinated_names_type_species_type
  return true unless is_genus_rank?
  sttnr = self.type_taxon_name_relationship
  return true if sttnr.nil?
  list_of_coordinated_names.each do |t|
    tttnr = t.type_taxon_name_relationship
    if !tttnr.nil? && sttnr.type != tttnr.type
      soft_validations.add(:base, "The type species relationship does not match with the type species relationship of the coordinate #{t.rank_class.rank_name}", success_message: 'Type species relationship was updated', failure_message:  'Failed to update type species relationship')
    end
  end
end

#sv_validate_coordinated_names_type_specimenObject



1061
1062
1063
1064
1065
1066
1067
1068
# File 'app/models/protonym/soft_validation_extensions.rb', line 1061

def sv_validate_coordinated_names_type_specimen
  return true if !is_species_rank? || !is_valid?
  list_of_coordinated_names.each do |t|
    if !self.has_same_primary_type(t)
      soft_validations.add(:base, "The type specimen does not match with the type specimen of the coordinate #{t.rank_class.rank_name}", success_message: 'Type specimen was updated', failure_message:  'Failed to update type specimen')
    end
  end
end

#sv_validate_coordinated_names_yearObject



670
671
672
673
674
675
# File 'app/models/protonym/soft_validation_extensions.rb', line 670

def sv_validate_coordinated_names_year
  s = self.year_of_publication
  list_of_coordinated_names.each do |t|
    soft_validations.add(:year_of_publication, "The year of publication does not match with the year of the coordinate #{t.rank_class.rank_name}", success_message: 'Year was updated', failure_message:  'Failed to update year') unless s == t.year_of_publication
  end
end

#sv_validate_nameObject



405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'app/models/protonym/soft_validation_extensions.rb', line 405

def sv_validate_name
  correct_name_format = false

  if rank_class
    # TODO: name these Regexp somewhere
    # TODO: move pertinent checks to base of nomenclature Classes to manage them there
    if (name =~ /^[a-zA-Z]*$/) || # !! should reference NOT_LATIN
        (nomenclatural_code == :iczn && name =~ /^[a-zA-Z]-[a-zA-Z]*$/) ||
        (nomenclatural_code == :icnp && name =~ /^[a-zA-Z]-[a-zA-Z]*$/) ||
        (nomenclatural_code == :icn && name =~  /^[a-zA-Z]*-[a-zA-Z]*$/) ||
        (nomenclatural_code == :icn && name =~  /^[a-zA-Z]*\s×\s[a-zA-Z]*$/) ||
        (nomenclatural_code == :icn && name =~  /^[a-zA-Z]*\s×[a-zA-Z]*$/) ||
        (nomenclatural_code == :icn && name =~  /^×[a-zA-Z]*$/) ||
        (nomenclatural_code == :icvcn)
      correct_name_format = true
    end

    unless correct_name_format
      icvcn_species = (nomenclatural_code == :icvcn && self.rank_string =~ /Species/) ? true : nil
      if is_available? && icvcn_species.nil?
        soft_validations.add(:name, 'Name should not have spaces or special characters, unless it has a status of misspelling or original misspelling')
      end
    end
  end
end

#sv_validate_parent_rankObject



431
432
433
434
435
436
437
438
439
# File 'app/models/protonym/soft_validation_extensions.rb', line 431

def sv_validate_parent_rank
  if self.rank_class && self.id == self.cached_valid_taxon_name_id
    if rank_string == 'NomenclaturalRank' || self.parent&.rank_string == 'NomenclaturalRank' || !!self.iczn_uncertain_placement_relationship
      true
    elsif !self.rank_class&.valid_parents.include?(self.parent&.rank_string)
      soft_validations.add(:rank_class, "The rank #{self.rank_class&.rank_name} is not compatible with the rank of parent (#{self.parent&.rank_class&.rank_name}). The name should be marked as 'Incertae sedis'")
    end
  end
end

#sv_year_is_not_requiredObject



1568
1569
1570
1571
1572
1573
1574
1575
# File 'app/models/protonym/soft_validation_extensions.rb', line 1568

def sv_year_is_not_required
  if !self.year_of_publication.nil? && self.source && self.year_of_publication == self.source.year
    soft_validations.add(
      :year_of_publication, 'Year of publication is not required, it is derived from the source',
      success_message: 'Year was deleted',
      failure_message: 'Failed to delete year')
  end
end