#5448 Remove non-exisiting hybrid category
[cdmlib-apps.git] / app-import / src / main / java / eu / etaxonomy / cdm / io / redlist / gefaesspflanzen / RedListGefaesspflanzenImportNames.java
index d8c6f0b1a49b3061162d052282ebcd1400ef044a..3be4cfd60e6dfc5cec4d9a27ebba86ec081ce62f 100644 (file)
@@ -34,6 +34,7 @@ import eu.etaxonomy.cdm.model.common.Annotation;
 import eu.etaxonomy.cdm.model.common.AnnotationType;
 import eu.etaxonomy.cdm.model.common.CdmBase;
 import eu.etaxonomy.cdm.model.common.Language;
+import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;
 import eu.etaxonomy.cdm.model.description.CommonTaxonName;
 import eu.etaxonomy.cdm.model.description.TaxonDescription;
 import eu.etaxonomy.cdm.model.name.BotanicalName;
@@ -43,6 +44,7 @@ import eu.etaxonomy.cdm.model.name.NomenclaturalStatus;
 import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
 import eu.etaxonomy.cdm.model.name.NonViralName;
 import eu.etaxonomy.cdm.model.name.Rank;
+import eu.etaxonomy.cdm.model.name.RankClass;
 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
 import eu.etaxonomy.cdm.model.taxon.Synonym;
 import eu.etaxonomy.cdm.model.taxon.Taxon;
@@ -136,7 +138,7 @@ public class RedListGefaesspflanzenImportNames extends DbImportBase<RedListGefae
         importAuthors(state, rs, name);
 
         //---TAXON---
-        TaxonBase<?> taxonBase = importTaxon(rs, name);
+        TaxonBase<?> taxonBase = importTaxon(rs, name, state);
         if(taxonBase==null){
             RedListUtil.logMessage(id, "Taxon for name "+name+" could not be created.", logger);
             return;
@@ -189,7 +191,7 @@ public class RedListGefaesspflanzenImportNames extends DbImportBase<RedListGefae
         }
     }
 
-    private TaxonBase<?> importTaxon(ResultSet rs, NonViralName<?> name) throws SQLException {
+    private TaxonBase<?> importTaxon(ResultSet rs, NonViralName<?> name, RedListGefaesspflanzenImportState state) throws SQLException {
 
         long id = rs.getLong(RedListUtil.NAMNR);
         String taxNameString = rs.getString(RedListUtil.TAXNAME);
@@ -235,7 +237,7 @@ public class RedListGefaesspflanzenImportNames extends DbImportBase<RedListGefae
         addAnnotation(RedListUtil.WISSK+": "+wisskString, taxonBase);
 
         //check taxon name consistency
-        checkTaxonConsistency(id, taxNameString, hybString, taxonBase);
+        checkTaxonConsistency(id, taxNameString, hybString, taxonBase, state);
         return taxonBase;
     }
 
@@ -418,6 +420,11 @@ public class RedListGefaesspflanzenImportNames extends DbImportBase<RedListGefae
                         String[] split = ep2String.split(RedListUtil.HYB_SIGN);
                         String hybridFormula1 = ep1String+" "+split[0].trim();
                         String hybridFormula2 = ep1String+" "+split[1].trim();
+                        //check if the specific epithets are from the same genus or not like e.g. EPI2 = pratensis × Lolium multiflorum
+                        String[] secondHybrid = split[1].trim().split(" ");
+                        if(secondHybrid.length>1 && secondHybrid[0].matches("[A-Z].*")){
+                            hybridFormula2 = split[1];
+                        }
                         if(CdmUtils.isNotBlank(ep3String)){
                             hybridFormula1 += " "+ep3String;
                             hybridFormula2 += " "+ep3String;
@@ -430,15 +437,15 @@ public class RedListGefaesspflanzenImportNames extends DbImportBase<RedListGefae
                         String hybridFormula1 = ep1String+" "+ep2String+" "+split[0];
                         String hybridFormula2 = ep1String+" "+ep2String+" "+split[1];
                         String fullFormula = hybridFormula1+" "+RedListUtil.HYB_SIGN+" "+hybridFormula2;
-                        name = NonViralNameParserImpl.NewInstance().parseFullName(fullFormula);
+                        name = NonViralNameParserImpl.NewInstance().parseFullName(fullFormula, NomenclaturalCode.ICNAFP, rank);
                     }
                 }
                 else if(hybString.equals(RedListUtil.HYB_N)){
-                    name = NonViralNameParserImpl.NewInstance().parseFullName(taxNameString);
+                    name = NonViralNameParserImpl.NewInstance().parseFullName(taxNameString, NomenclaturalCode.ICNAFP, rank);
                 }
                 else if(hybString.equals(RedListUtil.HYB_GF)){
                     if(ep1String.contains(RedListUtil.HYB_SIGN)){
-                        name = NonViralNameParserImpl.NewInstance().parseFullName(ep1String);
+                        name = NonViralNameParserImpl.NewInstance().parseFullName(ep1String, NomenclaturalCode.ICNAFP, rank);
                     }
                     else{
                         RedListUtil.logMessage(id, "HYB is "+hybString+" but "+RedListUtil.HYB+" does not contain "+RedListUtil.HYB_SIGN, logger);
@@ -462,7 +469,8 @@ public class RedListGefaesspflanzenImportNames extends DbImportBase<RedListGefae
     private void checkNameConsistency(long id, String nomZusatzString, String taxZusatzString,
             String zusatzString, String authorString, String hybString, NonViralName<?> name) {
         String authorshipCache = name.getAuthorshipCache();
-        if(hybString.equals(RedListUtil.HYB_XF)){
+        //FIXME: remove split length check when name parser can parse multiple hybrid parents
+        if(hybString.equals(RedListUtil.HYB_XF) && name.getTitleCache().split(RedListUtil.HYB_SIGN).length==2){
             if(name.getHybridChildRelations().isEmpty()){
                 RedListUtil.logMessage(id, "Hybrid formula but no hybrid child relations: "+name.getTitleCache(), logger);
                 return;
@@ -494,7 +502,7 @@ public class RedListGefaesspflanzenImportNames extends DbImportBase<RedListGefae
         }
     }
 
-    private void checkTaxonConsistency(long id, String taxNameString, String hybString, TaxonBase<?> taxonBase) {
+    private void checkTaxonConsistency(long id, String taxNameString, String hybString, TaxonBase<?> taxonBase, RedListGefaesspflanzenImportState state) {
         String nameCache = HibernateProxyHelper.deproxy(taxonBase.getName(), NonViralName.class).getNameCache().trim();
         taxNameString = taxNameString.trim();
         taxNameString = taxNameString.replaceAll(" +", " ");
@@ -522,8 +530,13 @@ public class RedListGefaesspflanzenImportNames extends DbImportBase<RedListGefae
         }
 
         taxNameString = taxNameString.replace("[ranglos]", "[unranked]");
-        if(taxonBase.getName().getRank()!=null && taxonBase.getName().getRank().equals(Rank.PROLES())){
-            taxNameString = taxNameString.replace("proles", "prol.");
+        if(taxonBase.getName().getRank()!=null){
+            if(taxonBase.getName().getRank().equals(Rank.PROLES())){
+                taxNameString = taxNameString.replace("proles", "prol.");
+            }
+            else if(taxonBase.getName().getRank().equals(state.getRank(RedListUtil.uuidRankCollectionSpecies))){
+                taxNameString = taxNameString.replace("\"Sammelart\"", "\"Coll. Species\"");
+            }
         }
         if(STRICT_TITLE_CHECK){
             if(!taxNameString.trim().equals(nameCache)){
@@ -549,6 +562,9 @@ public class RedListGefaesspflanzenImportNames extends DbImportBase<RedListGefae
                     return Rank.UNRANKED_INFRAGENERIC();
                 }
             }
+            else if(rankStr.equals("SAM")){
+                return getRank(state, RedListUtil.uuidRankCollectionSpecies, "Collective Species", "Collective Species", "\"Coll. Species\"", (OrderedTermVocabulary<Rank>) Rank.GENUS().getVocabulary(), null, RankClass.SpeciesGroup);
+            }
             else{
                 rank = state.getTransformer().getRankByKey(rankStr);
             }