ref #5448 Add new rank "forme"
[cdmlib-apps.git] / app-import / src / main / java / eu / etaxonomy / cdm / io / redlist / gefaesspflanzen / RedListGefaesspflanzenImportNames.java
index 71f86576bad817ca75246b1adca729d4a57be425..b3100204a04de95b6c3501ae1717f71e300b847b 100644 (file)
@@ -33,20 +33,23 @@ import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
 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.ExtensionType;
 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;
+import eu.etaxonomy.cdm.model.name.CultivarPlantName;
+import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
 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;
 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
-import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
-import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
 import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
 
 /**
@@ -68,32 +71,60 @@ public class RedListGefaesspflanzenImportNames extends DbImportBase<RedListGefae
 
     private static final boolean STRICT_TITLE_CHECK = false;
 
+    private ExtensionType extensionTypeFlor;
+
+    private ExtensionType extensionTypeAtlasIdx;
+
+    private ExtensionType extensionTypeKart;
+
+    private ExtensionType extensionTypeRl2015;
+
+    private ExtensionType extensionTypeEhrd;
+
+    private ExtensionType extensionTypeWissk;
+
     public RedListGefaesspflanzenImportNames() {
         super(tableName, pluralString);
     }
 
     @Override
     protected String getIdQuery(RedListGefaesspflanzenImportState state) {
-        return "SELECT NAMNR "
+        return "SELECT SEQNUM "
                 + "FROM V_TAXATLAS_D20_EXPORT t "
-                + " ORDER BY NAMNR";
+                + " ORDER BY SEQNUM";
     }
 
     @Override
     protected String getRecordQuery(RedListGefaesspflanzenImportConfigurator config) {
         String result = " SELECT * "
                 + " FROM V_TAXATLAS_D20_EXPORT t "
-                + " WHERE t.NAMNR IN (@IDSET)";
+                + " WHERE t.SEQNUM IN (@IDSET)";
         result = result.replace("@IDSET", IPartitionedIO.ID_LIST_TOKEN);
         return result;
     }
 
     @Override
     protected void doInvoke(RedListGefaesspflanzenImportState state) {
+        makeExtensionTypes();
         super.doInvoke(state);
     }
 
 
+    private void makeExtensionTypes() {
+        extensionTypeFlor = ExtensionType.NewInstance(RedListUtil.FLOR, RedListUtil.FLOR, "");
+        extensionTypeAtlasIdx = ExtensionType.NewInstance(RedListUtil.ATLAS_IDX, RedListUtil.ATLAS_IDX, "");
+        extensionTypeKart = ExtensionType.NewInstance(RedListUtil.KART, RedListUtil.KART, "");
+        extensionTypeRl2015 = ExtensionType.NewInstance(RedListUtil.RL2015, RedListUtil.RL2015, "");
+        extensionTypeEhrd = ExtensionType.NewInstance(RedListUtil.EHRD, RedListUtil.EHRD, "");
+        extensionTypeWissk = ExtensionType.NewInstance(RedListUtil.WISSK, RedListUtil.WISSK, "");
+        getTermService().saveOrUpdate(extensionTypeFlor);
+        getTermService().saveOrUpdate(extensionTypeAtlasIdx);
+        getTermService().saveOrUpdate(extensionTypeKart);
+        getTermService().saveOrUpdate(extensionTypeRl2015);
+        getTermService().saveOrUpdate(extensionTypeEhrd);
+        getTermService().saveOrUpdate(extensionTypeWissk);
+    }
+
     @Override
     public boolean doPartition(ResultSetPartitioner partitioner, RedListGefaesspflanzenImportState state) {
         ResultSet rs = partitioner.getResultSet();
@@ -116,7 +147,14 @@ public class RedListGefaesspflanzenImportNames extends DbImportBase<RedListGefae
     private void makeSingleNameAndTaxon(RedListGefaesspflanzenImportState state, ResultSet rs, Set<TaxonNameBase> namesToSave, Set<TaxonBase> taxaToSave)
             throws SQLException {
         long id = rs.getLong(RedListUtil.NAMNR);
-        String clTaxonString = rs.getString(RedListUtil.CL_TAXON);
+        String relationE = rs.getString(RedListUtil.E);
+        String relationW = rs.getString(RedListUtil.W);
+        String relationK = rs.getString(RedListUtil.K);
+        String relationAW = rs.getString(RedListUtil.AW);
+        String relationAO = rs.getString(RedListUtil.AO);
+        String relationR = rs.getString(RedListUtil.R);
+        String relationO = rs.getString(RedListUtil.O);
+        String relationS = rs.getString(RedListUtil.S);
 
         //---NAME---
         NonViralName<?> name = importName(state, rs, namesToSave);
@@ -126,63 +164,54 @@ 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);
+            RedListUtil.logMessage(id, "!SERIOUS ERROR! Taxon for name "+name+" could not be created!", logger);
             return;
         }
 
         //---CONCEPT RELATIONSHIPS---
-        //checklist
-        TaxonBase<?> checklistTaxon = null;
-        if(CdmUtils.isNotBlank(clTaxonString) && !clTaxonString.trim().equals("-")){
-            checklistTaxon = (TaxonBase<?>) taxonBase.clone();
-            if(checklistTaxon.isInstanceOf(Taxon.class)){
-                TaxonRelationship relation = HibernateProxyHelper.deproxy(checklistTaxon, Taxon.class).addTaxonRelation(HibernateProxyHelper.deproxy(taxonBase, Taxon.class), TaxonRelationshipType.CONGRUENT_TO(), null, null);
-                relation.setDoubtful(true);
-            }
-
-            ImportHelper.setOriginalSource(checklistTaxon, state.getTransactionalSourceReference(), id, RedListUtil.TAXON_CHECKLISTE_NAMESPACE);
-            taxaToSave.add(checklistTaxon);
-        }
         //E, W, K, AW, AO, R, O, S
-        cloneTaxon(taxonBase, RedListUtil.CLASSIFICATION_NAMESPACE_E, taxaToSave, id, state);
-        cloneTaxon(taxonBase, RedListUtil.CLASSIFICATION_NAMESPACE_W, taxaToSave, id, state);
-        cloneTaxon(taxonBase, RedListUtil.CLASSIFICATION_NAMESPACE_K, taxaToSave, id, state);
-        cloneTaxon(taxonBase, RedListUtil.CLASSIFICATION_NAMESPACE_AW, taxaToSave, id, state);
-        cloneTaxon(taxonBase, RedListUtil.CLASSIFICATION_NAMESPACE_AO, taxaToSave, id, state);
-        cloneTaxon(taxonBase, RedListUtil.CLASSIFICATION_NAMESPACE_R, taxaToSave, id, state);
-        cloneTaxon(taxonBase, RedListUtil.CLASSIFICATION_NAMESPACE_O, taxaToSave, id, state);
-        cloneTaxon(taxonBase, RedListUtil.CLASSIFICATION_NAMESPACE_S, taxaToSave, id, state);
+        cloneTaxon(taxonBase, relationE, RedListUtil.CLASSIFICATION_NAMESPACE_E, taxaToSave, id, state);
+        cloneTaxon(taxonBase, relationW, RedListUtil.CLASSIFICATION_NAMESPACE_W, taxaToSave, id, state);
+        cloneTaxon(taxonBase, relationK, RedListUtil.CLASSIFICATION_NAMESPACE_K, taxaToSave, id, state);
+        cloneTaxon(taxonBase, relationAW, RedListUtil.CLASSIFICATION_NAMESPACE_AW, taxaToSave, id, state);
+        cloneTaxon(taxonBase, relationAO, RedListUtil.CLASSIFICATION_NAMESPACE_AO, taxaToSave, id, state);
+        cloneTaxon(taxonBase, relationR, RedListUtil.CLASSIFICATION_NAMESPACE_R, taxaToSave, id, state);
+        cloneTaxon(taxonBase, relationO, RedListUtil.CLASSIFICATION_NAMESPACE_O, taxaToSave, id, state);
+        cloneTaxon(taxonBase, relationS, RedListUtil.CLASSIFICATION_NAMESPACE_S, taxaToSave, id, state);
 
         //NOTE: the source has to be added after cloning or otherwise the clone would also get the source
         ImportHelper.setOriginalSource(taxonBase, state.getTransactionalSourceReference(), id, RedListUtil.TAXON_GESAMTLISTE_NAMESPACE);
         taxaToSave.add(taxonBase);
     }
 
-    private Taxon cloneTaxon(final TaxonBase<?> gesamtListeTaxon, String sourceNameSpace, Set<TaxonBase> taxaToSave, long id, RedListGefaesspflanzenImportState state){
-        Taxon clonedTaxon = null;
+    private void cloneTaxon(final TaxonBase<?> gesamtListeTaxon, String relationString, String sourceNameSpace, Set<TaxonBase> taxaToSave, long id, RedListGefaesspflanzenImportState state){
+        if(CdmUtils.isNotBlank(relationString) && !relationString.equals(".")){
+            Taxon clonedTaxon = null;
 
-        if(gesamtListeTaxon.isInstanceOf(Taxon.class)){
-            clonedTaxon = HibernateProxyHelper.deproxy(gesamtListeTaxon.clone(), Taxon.class);
-        }
-        else if(gesamtListeTaxon.isInstanceOf(Synonym.class)){
-            clonedTaxon = Taxon.NewInstance(gesamtListeTaxon.getName(), gesamtListeTaxon.getSec());
-        }
-        else{
-            RedListUtil.logMessage(id, "Taxon base "+gesamtListeTaxon+" is neither taxon nor synonym! Taxon could not be cloned", logger);
-            return null;
+            if(gesamtListeTaxon.isInstanceOf(Taxon.class)){
+                clonedTaxon = HibernateProxyHelper.deproxy(gesamtListeTaxon.clone(), Taxon.class);
+            }
+            else if(gesamtListeTaxon.isInstanceOf(Synonym.class)){
+                clonedTaxon = Taxon.NewInstance(gesamtListeTaxon.getName(), gesamtListeTaxon.getSec());
+            }
+            else{
+                RedListUtil.logMessage(id, "Taxon base "+gesamtListeTaxon+" is neither taxon nor synonym! Taxon could not be cloned", logger);
+                return;
+            }
+            ImportHelper.setOriginalSource(clonedTaxon, state.getTransactionalSourceReference(), id, sourceNameSpace);
+            taxaToSave.add(clonedTaxon);
         }
-
-        ImportHelper.setOriginalSource(clonedTaxon, state.getTransactionalSourceReference(), id, sourceNameSpace);
-        taxaToSave.add(clonedTaxon);
-        return clonedTaxon;
     }
 
-    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);
+        String epi1String = rs.getString(RedListUtil.EPI1);
+        String epi2String = rs.getString(RedListUtil.EPI2);
+        String epi3String = rs.getString(RedListUtil.EPI3);
         String gueltString = rs.getString(RedListUtil.GUELT);
         String trivialString = rs.getString(RedListUtil.TRIVIAL);
         String authorBasiString = rs.getString(RedListUtil.AUTOR_BASI);
@@ -206,6 +235,7 @@ public class RedListGefaesspflanzenImportNames extends DbImportBase<RedListGefae
             taxonBase = Synonym.NewInstance(name, null);
         }
         else{
+            RedListUtil.logMessage(id, "Taxon was not created!! Unknown value for "+RedListUtil.GUELT+"!", logger);
             return null;
         }
 
@@ -217,24 +247,18 @@ public class RedListGefaesspflanzenImportNames extends DbImportBase<RedListGefae
         }
 
         //add annotations
-        addAnnotation(RedListUtil.FLOR+": "+florString, taxonBase);
-        addAnnotation(RedListUtil.ATLAS_IDX+": "+atlasIdxString, taxonBase);
-        addAnnotation(RedListUtil.KART+": "+kartString, taxonBase);
-        addAnnotation(RedListUtil.RL2015+": "+rl2015String, taxonBase);
-        addAnnotation(RedListUtil.EHRD+": "+ehrdString, taxonBase);
-        addAnnotation(RedListUtil.WISSK+": "+wisskString, taxonBase);
+        taxonBase.addExtension(florString, extensionTypeFlor);
+        taxonBase.addExtension(atlasIdxString, extensionTypeAtlasIdx);
+        taxonBase.addExtension(kartString, extensionTypeKart);
+        taxonBase.addExtension(rl2015String, extensionTypeRl2015);
+        taxonBase.addExtension(ehrdString, extensionTypeEhrd);
+        taxonBase.addExtension(wisskString, extensionTypeWissk);
 
         //check taxon name consistency
-        checkTaxonNameConsistency(id, taxNameString, hybString, taxonBase);
+        checkTaxonConsistency(id, taxNameString, hybString, epi1String, epi2String, epi3String, taxonBase, state);
         return taxonBase;
     }
 
-    private void addAnnotation(String string, TaxonBase<?> taxonBase) {
-        if(CdmUtils.isNotBlank(string)){
-            taxonBase.addAnnotation(Annotation.NewInstance(string, AnnotationType.TECHNICAL(), Language.GERMAN()));
-        }
-    }
-
     private void importAuthors(RedListGefaesspflanzenImportState state, ResultSet rs, NonViralName<?> name) throws SQLException {
 
         long id = rs.getLong(RedListUtil.NAMNR);
@@ -243,25 +267,20 @@ public class RedListGefaesspflanzenImportNames extends DbImportBase<RedListGefae
         String zusatzString = rs.getString(RedListUtil.ZUSATZ);
         String authorKombString = rs.getString(RedListUtil.AUTOR_KOMB);
         String authorBasiString = rs.getString(RedListUtil.AUTOR_BASI);
+        String hybString = rs.getString(RedListUtil.HYB);
 
         //combination author
         if(authorKombString.contains(RedListUtil.EX)){
-            //TODO: what happens with multiple ex authors??
-            String[] kombSplit = authorKombString.split(RedListUtil.EX);
-            if(kombSplit.length!=2){
-                RedListUtil.logMessage(id, "Multiple ex combination authors found", logger);
-            }
-            for (int i = 0; i < kombSplit.length; i++) {
-                if(i==0){
-                    //first author is ex author
-                    TeamOrPersonBase<?> authorKomb = (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, kombSplit[i]);
-                    name.setExCombinationAuthorship(authorKomb);
-                }
-                else{
-                    TeamOrPersonBase<?> authorKomb = (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, kombSplit[i]);
-                    name.setCombinationAuthorship(authorKomb);
-                }
-            }
+            // multiple ex authors will be reduced to only the last one
+            // e.g. Almq. ex Sternström ex Dahlst. -> Almq. ex Dahlst.
+            //first author is ex combination author
+            String exAuthorString = RedListUtil.getExAuthorOfExAuthorshipString(authorKombString);
+            TeamOrPersonBase<?> exAuthor = (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, exAuthorString);
+            name.setExCombinationAuthorship(exAuthor);
+            //the last author is the combination author
+            String authorString = RedListUtil.getAuthorOfExAuthorshipString(authorKombString);
+            TeamOrPersonBase<?> combAuthor = (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, authorString);
+            name.setCombinationAuthorship(combAuthor);
         }
         else if(authorKombString.trim().contains(RedListUtil.AUCT)){
             RedListUtil.logMessage(id, "AUCT information in "+RedListUtil.AUTOR_KOMB+" column", logger);
@@ -272,29 +291,19 @@ public class RedListGefaesspflanzenImportNames extends DbImportBase<RedListGefae
         }
         //basionym author
         if(authorBasiString.contains(RedListUtil.EX)){
-            String[] basiSplit = authorBasiString.split(RedListUtil.EX);
-            for (int i = 0; i < basiSplit.length; i++) {
-                if(basiSplit.length!=2){
-                    RedListUtil.logMessage(id, "Multiple ex basionymn authors found", logger);
-                }
-                if(i==0){
-                    TeamOrPersonBase<?> authorBasi= (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, basiSplit[i]);
-                    if(CdmUtils.isBlank(authorKombString)){
-                        name.setExCombinationAuthorship(authorBasi);
-                    }
-                    else{
-                        name.setExBasionymAuthorship(authorBasi);
-                    }
-                }
-                else{
-                    TeamOrPersonBase<?> authorBasi= (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, basiSplit[i]);
-                    if(CdmUtils.isBlank(authorKombString)){
-                        name.setCombinationAuthorship(authorBasi);
-                    }
-                    else{
-                        name.setBasionymAuthorship(authorBasi);
-                    }
-                }
+            TeamOrPersonBase<?> authorExBasi= (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, RedListUtil.getExAuthorOfExAuthorshipString(authorBasiString));
+            if(CdmUtils.isBlank(authorKombString)){
+                name.setExCombinationAuthorship(authorExBasi);
+            }
+            else{
+                name.setExBasionymAuthorship(authorExBasi);
+            }
+            TeamOrPersonBase<?> authorBasi= (TeamOrPersonBase<?>) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, RedListUtil.getAuthorOfExAuthorshipString(authorBasiString));
+            if(CdmUtils.isBlank(authorKombString)){
+                name.setCombinationAuthorship(authorBasi);
+            }
+            else{
+                name.setBasionymAuthorship(authorBasi);
             }
         }
         else if(CdmUtils.isNotBlank(authorBasiString)){
@@ -310,8 +319,7 @@ public class RedListGefaesspflanzenImportNames extends DbImportBase<RedListGefae
 
         //check authorship consistency
         String authorString = rs.getString(RedListUtil.AUTOR);
-        String authorshipCache = name.getAuthorshipCache();
-        checkAuthorShipConsistency(id, nomZusatzString, taxZusatzString, zusatzString, authorString, authorshipCache);
+        checkNameConsistency(id, nomZusatzString, taxZusatzString, zusatzString, authorString, hybString, name);
     }
 
     private NonViralName<?> importName(RedListGefaesspflanzenImportState state, ResultSet rs, Set<TaxonNameBase> namesToSave) throws SQLException {
@@ -330,105 +338,100 @@ public class RedListGefaesspflanzenImportNames extends DbImportBase<RedListGefae
             RedListUtil.logMessage(id, "No name found!", logger);
         }
 
-        Rank rank = makeRank(id, state, rangString, ep3String!=null);
-        NonViralName<?> name = BotanicalName.NewInstance(rank);
-
-        //ep1 should always be present
-        if(CdmUtils.isBlank(ep1String)){
-            RedListUtil.logMessage(id, RedListUtil.EPI1+" is empty!", logger);
+        NonViralName<?> name = null;
+        Rank rank = makeRank(id, state, rangString, CdmUtils.isNotBlank(ep3String));
+        //cultivar
+        if(rank!= null && rank.equals(Rank.CULTIVAR())){
+            CultivarPlantName cultivar = CultivarPlantName.NewInstance(rank);
+            cultivar.setGenusOrUninomial(ep1String);
+            cultivar.setSpecificEpithet(ep2String);
+            cultivar.setCultivarName(ep3String);
+            name = cultivar;
         }
-        name.setGenusOrUninomial(ep1String);
-        if(CdmUtils.isNotBlank(ep2String)){
-            if(rank!=null && rank.isInfraGeneric()){
-                name.setInfraGenericEpithet(ep2String);
-            }
-            else{
-                name.setSpecificEpithet(ep2String);
+        //botanical names
+        else{
+            name = BotanicalName.NewInstance(rank);
+
+            //ep1 should always be present
+            if(CdmUtils.isBlank(ep1String)){
+                RedListUtil.logMessage(id, RedListUtil.EPI1+" is empty!", logger);
             }
-        }
-        if(CdmUtils.isNotBlank(ep3String)){
-            name.setInfraSpecificEpithet(ep3String);
-        }
-        //nomenclatural status
-        if(CdmUtils.isNotBlank(nomZusatzString)){
-            NomenclaturalStatusType statusType = makeNomenclaturalStatus(id, state, nomZusatzString);
-            if(statusType!=null){
-                NomenclaturalStatus status = NomenclaturalStatus.NewInstance(statusType);
-                //special case for invalid names where the DB entry contains
-                //additional information in brackets e.g. "nom. inval. (sine basion.)"
-                if(statusType.equals(NomenclaturalStatusType.INVALID())){
-                    Pattern pattern = Pattern.compile("\\((.*?)\\)");
-                    Matcher matcher = pattern.matcher(nomZusatzString);
-                    if (matcher.find()){
-                        status.setRuleConsidered(matcher.group(1));
-                    }
+            name.setGenusOrUninomial(ep1String);
+            if(CdmUtils.isNotBlank(ep2String)){
+                if(rank!=null && rank.isInfraGenericButNotSpeciesGroup()){
+                    name.setInfraGenericEpithet(ep2String);
+                }
+                else{
+                    name.setSpecificEpithet(ep2String);
                 }
-                name.addStatus(status);
             }
-        }
-        //hybrid
-        if(CdmUtils.isNotBlank(hybString)){
-            //save hybrid formula
-            if(CdmUtils.isNotBlank(formelString)){
-                Annotation annotation = Annotation.NewDefaultLanguageInstance(formelString);
-                annotation.setAnnotationType(AnnotationType.TECHNICAL());
-                name.addAnnotation(annotation);
+            if(CdmUtils.isNotBlank(ep3String)){
+                name.setInfraSpecificEpithet(ep3String);
             }
 
-            if(hybString.equals(RedListUtil.HYB_X)){
-                name.setBinomHybrid(true);
-            }
-            else if(hybString.equals(RedListUtil.HYB_G)){
-                name.setMonomHybrid(true);
+
+            //nomenclatural status
+            if(CdmUtils.isNotBlank(nomZusatzString)){
+                NomenclaturalStatusType statusType = makeNomenclaturalStatus(id, state, nomZusatzString);
+                if(statusType!=null){
+                    NomenclaturalStatus status = NomenclaturalStatus.NewInstance(statusType);
+                    //special case for invalid names where the DB entry contains
+                    //additional information in brackets e.g. "nom. inval. (sine basion.)"
+                    if(statusType.equals(NomenclaturalStatusType.INVALID()) || statusType.equals(NomenclaturalStatusType.REJECTED()) ){
+                        Pattern pattern = Pattern.compile("\\((.*?)\\)");
+                        Matcher matcher = pattern.matcher(nomZusatzString);
+                        if (matcher.find()){
+                            status.setRuleConsidered(matcher.group(1));
+                        }
+                    }
+                    name.addStatus(status);
+                }
             }
-            else if(hybString.equals(RedListUtil.HYB_XF)){
-                name.setHybridFormula(true);
-                if(ep1String.contains(RedListUtil.HYB_SIGN)){
-                    RedListUtil.logMessage(id, "EPI1 has hybrid signs but with flag: "+RedListUtil.HYB_XF, logger);
+            //hybrid
+            if(CdmUtils.isNotBlank(hybString)){
+                //more than two hybrids not yet handled by name parser
+                //TODO: use parser when implemented to fully support hybrids
+                if(taxNameString.split(RedListUtil.HYB_SIGN).length>2){
+                    name = BotanicalName.NewInstance(rank);
+                    name.setTitleCache(taxNameString, true);
+                }
+                else if(hybString.equals(RedListUtil.HYB_X)){
+                    name.setBinomHybrid(true);
                 }
-                else if(ep2String.contains(RedListUtil.HYB_SIGN)){
-                    String[] split = ep2String.split(RedListUtil.HYB_SIGN);
-                    if(split.length!=2){
-                        RedListUtil.logMessage(id, "Multiple hybrid signs found in "+ep2String, logger);
+                else if(hybString.equals(RedListUtil.HYB_G)){
+                    name.setMonomHybrid(true);
+                }
+                else if(hybString.equals(RedListUtil.HYB_XF) || hybString.equals(RedListUtil.HYB_XU)){
+                    name.setHybridFormula(true);
+                    String fullFormula = buildHybridFormula(ep1String, ep2String, ep3String, rank);
+                    name = NonViralNameParserImpl.NewInstance().parseFullName(fullFormula, NomenclaturalCode.ICNAFP, rank);
+                }
+                else if(hybString.equals(RedListUtil.HYB_N)){
+                    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, NomenclaturalCode.ICNAFP, rank);
                     }
-                    String hybridFormula1 = ep1String+" "+split[0].trim();
-                    String hybridFormula2 = ep1String+" "+split[1].trim();
-                    if(CdmUtils.isNotBlank(ep3String)){
-                        hybridFormula1 += " "+ep3String;
-                        hybridFormula2 += " "+ep3String;
+                    else{
+                        RedListUtil.logMessage(id, "HYB is "+hybString+" but "+RedListUtil.HYB+" does not contain "+RedListUtil.HYB_SIGN, logger);
                     }
-                    String fullFormula = hybridFormula1+" "+RedListUtil.HYB_SIGN+" "+hybridFormula2;
-                    name = NonViralNameParserImpl.NewInstance().parseFullName(fullFormula);
                 }
-                else if(ep3String.contains(RedListUtil.HYB_SIGN)){
-                    String[] split = ep3String.split(RedListUtil.HYB_SIGN);
-                    if(split.length!=2){
-                        RedListUtil.logMessage(id, "Multiple hybrid signs found in "+ep3String, logger);
-                    }
-                    String hybridFormula1 = ep1String+" "+ep2String+" "+split[0];
-                    String hybridFormula2 = ep1String+" "+ep2String+" "+split[1];
-                    String fullFormula = hybridFormula1+" "+RedListUtil.HYB_SIGN+" "+hybridFormula2;
-                    name = NonViralNameParserImpl.NewInstance().parseFullName(fullFormula);
-                }
-            }
-            else if(hybString.equals(RedListUtil.HYB_N)){
-                name = NonViralNameParserImpl.NewInstance().parseFullName(ep1String+" "+ep2String+" nothosubsp. "+ep3String);
-            }
-            else if(hybString.equals(RedListUtil.HYB_GF)){
-                if(ep1String.contains(RedListUtil.HYB_SIGN)){
-                    name = NonViralNameParserImpl.NewInstance().parseFullName(ep1String);
+                else if(hybString.equals(RedListUtil.HYB_XS)){
+                    //nothing to do
                 }
                 else{
-                    RedListUtil.logMessage(id, "HYB is "+hybString+" but "+RedListUtil.HYB+" does not contain "+RedListUtil.HYB_SIGN, logger);
+                    logger.error("HYB value "+hybString+" not yet handled");
+                }
+                //save hybrid formula
+                if(CdmUtils.isNotBlank(formelString)){
+                    Annotation annotation = Annotation.NewDefaultLanguageInstance(formelString);
+                    annotation.setAnnotationType(AnnotationType.TECHNICAL());
+                    name.addAnnotation(annotation);
                 }
-            }
-            else if(hybString.equals(RedListUtil.HYB_XS)){
-                //nothing to do
-            }
-            else{
-                logger.error("HYB value "+hybString+" not yet handled");
             }
         }
+
         //add source
         ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, RedListUtil.NAME_NAMESPACE);
 
@@ -436,8 +439,60 @@ public class RedListGefaesspflanzenImportNames extends DbImportBase<RedListGefae
         return name;
     }
 
-    private void checkAuthorShipConsistency(long id, String nomZusatzString, String taxZusatzString,
-            String zusatzString, String authorString, String authorshipCache) {
+    private String buildHybridFormula(String ep1String, String ep2String, String ep3String, Rank rank) {
+        String fullFormula = null;
+        if(ep1String.contains(RedListUtil.HYB_SIGN)){
+            fullFormula = ep1String;
+        }
+        else if(ep2String.contains(RedListUtil.HYB_SIGN)){
+            String[] split = ep2String.split(RedListUtil.HYB_SIGN);
+            String hybridFormula1 = ep1String+" "+split[0].trim();
+            String hybridFormula2 = ep1String+" "+split[1].trim();
+            //check if the genus is mentioned in EP2 or not
+            String[] secondHybrid = split[1].trim().split(" ");
+            //check if the genus is abbreviated like e.g. Centaurea jacea × C. decipiens
+            if(secondHybrid.length>1 && secondHybrid[0].matches("[A-Z]\\.")){
+                hybridFormula2 = ep1String+" "+split[1].trim().substring(3);
+            }
+            else if(secondHybrid.length>1 && secondHybrid[0].matches("[A-Z].*")){
+                hybridFormula2 = split[1].trim();
+            }
+            if(CdmUtils.isNotBlank(ep3String)){
+                hybridFormula1 += " "+rank.getAbbreviation()+" "+ep3String;
+                hybridFormula2 += " "+rank.getAbbreviation()+" "+ep3String;
+            }
+            fullFormula = hybridFormula1+" "+RedListUtil.HYB_SIGN+" "+hybridFormula2;
+        }
+        else if(ep3String.contains(RedListUtil.HYB_SIGN)){
+            String[] split = ep3String.split(RedListUtil.HYB_SIGN);
+            String hybridFormula1 = ep1String+" "+ep2String+" "+rank.getAbbreviation()+" "+split[0].trim();
+            String hybridFormula2 = ep1String+" "+ep2String+" "+rank.getAbbreviation()+" "+split[1].trim();
+            //check if the genus is mentioned in EP3 or not
+            String[] secondHybrid = split[1].trim().split(" ");
+            //check if the genus is abbreviated like e.g. Centaurea jacea jacea × C. jacea subsp. decipiens
+            if(secondHybrid.length>1 && secondHybrid[0].matches("[A-Z]\\.")){
+                hybridFormula2 = ep1String+" "+split[1].trim().substring(3);
+            }
+            else if(secondHybrid.length>1 && secondHybrid[0].matches("[A-Z].*")){
+                hybridFormula2 = split[1].trim();
+            }
+            fullFormula = hybridFormula1+" "+RedListUtil.HYB_SIGN+" "+hybridFormula2;
+        }
+        return fullFormula;
+    }
+
+    private void checkNameConsistency(long id, String nomZusatzString, String taxZusatzString,
+            String zusatzString, String authorString, String hybString, NonViralName<?> name) {
+        String authorshipCache = name.getAuthorshipCache();
+        //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;
+            }
+            return;
+        }
+
         if(CdmUtils.isNotBlank(zusatzString)){
             authorString = authorString.replace(", "+zusatzString, "");
         }
@@ -450,6 +505,9 @@ public class RedListGefaesspflanzenImportNames extends DbImportBase<RedListGefae
         if(authorString.equals(RedListUtil.AUCT)){
             authorString = "";
         }
+        if(!STRICT_TITLE_CHECK && authorString.matches(".*ex.*ex.*")){
+            return;
+        }
         if(STRICT_TITLE_CHECK){
             if(!authorString.equals(authorshipCache)){
                 RedListUtil.logMessage(id, "Authorship inconsistent! name.authorhshipCache <-> Column "+RedListUtil.AUTOR+": "+authorshipCache+" <-> "+authorString, logger);
@@ -462,29 +520,19 @@ public class RedListGefaesspflanzenImportNames extends DbImportBase<RedListGefae
         }
     }
 
-    private void checkTaxonNameConsistency(long id, String taxNameString, String hybString, TaxonBase<?> taxonBase) {
-        if(hybString.equals(RedListUtil.HYB_XF)){
-            if(HibernateProxyHelper.deproxy(taxonBase.getName(),NonViralName.class).getHybridChildRelations().isEmpty()){
-                RedListUtil.logMessage(id, "Hybrid name but no hybrid child relations", logger);
-                return;
-            }
+    private void checkTaxonConsistency(long id, String taxNameString, String hybString, String epi1String, String epi2String, String epi3String, TaxonBase<?> taxonBase, RedListGefaesspflanzenImportState state) {
+        if(taxNameString.split(RedListUtil.HYB_SIGN).length>2){
+            RedListUtil.logInfoMessage(id, "multiple hybrid signs. No name check for "+taxNameString, logger);
             return;
         }
 
-
         String nameCache = HibernateProxyHelper.deproxy(taxonBase.getName(), NonViralName.class).getNameCache().trim();
         taxNameString = taxNameString.trim();
-        taxNameString.replaceAll(" +", " ");
+        taxNameString = taxNameString.replaceAll(" +", " ");
 
-        if(taxNameString.endsWith("agg.")){
-            taxNameString = taxNameString.replace("agg.", "aggr.");
-        }
-        if(taxNameString.endsWith("aggr.")){
-            taxNameString = taxNameString.replaceFirst(" ", " (");
-            taxNameString = taxNameString.replace(" aggr.", ") aggr.");
-        }
 
-        if(hybString.equals(RedListUtil.HYB_X)){
+        if((hybString.equals(RedListUtil.HYB_X) || hybString.equals(RedListUtil.HYB_N))
+                && nameCache.matches(".*\\s"+RedListUtil.HYB_SIGN+"\\w.*")){
             taxNameString = taxNameString.replace(" "+RedListUtil.HYB_SIGN+" ", " "+RedListUtil.HYB_SIGN);//hybrid sign has no space after it in titleCache for binomial hybrids
             taxNameString = taxNameString.replace(" x ", " "+RedListUtil.HYB_SIGN);//in some cases a standard 'x' is used
         }
@@ -492,29 +540,47 @@ public class RedListGefaesspflanzenImportNames extends DbImportBase<RedListGefae
             taxNameString = taxNameString.replace("X ", RedListUtil.HYB_SIGN);
         }
         else if(hybString.equals(RedListUtil.HYB_GF)){
-            taxNameString = taxNameString.replace(" "+RedListUtil.HYB_SIGN, " x");
+            taxNameString = taxNameString.replace(" "+RedListUtil.HYB_SIGN+" ", " "+RedListUtil.HYB_SIGN);
+        }
+        else if(hybString.equals(RedListUtil.HYB_XF)){
+            nameCache = taxonBase.getName().getTitleCache();
+            if(nameCache.contains("sec")){
+                nameCache = nameCache.substring(0, nameCache.indexOf("sec"));
+            }
+            if(!STRICT_TITLE_CHECK){
+                taxNameString = buildHybridFormula(epi1String, epi2String, epi3String, taxonBase.getName().getRank());
+            }
+            if(taxNameString.split(RedListUtil.HYB_SIGN).length==1){
+                taxNameString = taxNameString.replace(RedListUtil.HYB_SIGN+" ", RedListUtil.HYB_SIGN);
+            }
         }
 
-        if(taxNameString.endsWith("- Gruppe")){String a ="Festuca ×xx Lolium <-> Festuca ×× Lolium";
+        if(taxNameString.endsWith("- Gruppe")){
             taxNameString = taxNameString.replaceAll("- Gruppe", "species group");
         }
         if(taxNameString.endsWith("- group")){
             taxNameString = taxNameString.replaceAll("- group", "species group");
         }
-        if(taxNameString.endsWith("species group")){
-            taxNameString = taxNameString.replaceFirst(" ", " (");
-            taxNameString = taxNameString.replace(" species group", ") species group");
-        }
 
+        taxNameString = taxNameString.replace("agg.", "aggr.");
         taxNameString = taxNameString.replace("[ranglos]", "[unranked]");
+
+        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)){
-                RedListUtil.logMessage(id, "Taxon name inconsistent! taxon.titleCache <-> Column "+RedListUtil.TAXNAME+": "+nameCache+" <-> "+taxNameString, logger);
+                RedListUtil.logMessage(id, "Taxon name inconsistent! taxon.nameCache <-> Column "+RedListUtil.TAXNAME+": "+nameCache+" <-> "+taxNameString, logger);
             }
         }
         else{
             if(!taxNameString.startsWith(nameCache)){
-                RedListUtil.logMessage(id, "Taxon name inconsistent! taxon.titleCache <-> Column "+RedListUtil.TAXNAME+": "+nameCache+" <-> "+taxNameString, logger);
+                RedListUtil.logMessage(id, "Taxon name inconsistent! taxon.nameCache <-> Column "+RedListUtil.TAXNAME+": "+nameCache+" <-> "+taxNameString, logger);
             }
         }
     }
@@ -525,12 +591,34 @@ public class RedListGefaesspflanzenImportNames extends DbImportBase<RedListGefae
             if(rankStr.equals("ORA")){
                 //special handling for ORA because of two possibilities
                 if(hasSpecificEpithet){
-                    return Rank.UNRANKED_INFRASPECIFIC();
+                    //re-load term because the representation was changed before
+                    return (Rank) getTermService().load(Rank.uuidInfraspecificTaxon);
                 }
                 else{
                     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 if(rankStr.equals("SPR")){
+                return getRank(state, RedListUtil.uuidRankSubproles, "Subproles", "Subproles", "subproles", (OrderedTermVocabulary<Rank>) Rank.GENUS().getVocabulary(), null, RankClass.Infraspecific);
+            }
+            else if(rankStr.equals("MOD")){
+                return getRank(state, RedListUtil.uuidRankModification, "Modification", "Modification", "modificatio", (OrderedTermVocabulary<Rank>) Rank.GENUS().getVocabulary(), null, RankClass.Infraspecific);
+            }
+            else if(rankStr.equals("LUS")){
+                return getRank(state, RedListUtil.uuidRankLusus, "Lusus", "Lusus", "lusus", (OrderedTermVocabulary<Rank>) Rank.GENUS().getVocabulary(), null, RankClass.Infraspecific);
+            }
+            else if(rankStr.equals("SPI")){
+                return getRank(state, RedListUtil.uuidRankSubspeciesPrincipes, "Subspecies principes", "Subspecies principes", "subsp. princ.", (OrderedTermVocabulary<Rank>) Rank.GENUS().getVocabulary(), null, RankClass.Infraspecific);
+            }
+            else if(rankStr.equals("KMB")){
+                return getRank(state, RedListUtil.uuidRankCombination, "Combination", "Combination", "", (OrderedTermVocabulary<Rank>) Rank.GENUS().getVocabulary(), null, RankClass.Infraspecific);
+            }
+            else if(rankStr.equals("'FO")){
+                return getRank(state, RedListUtil.uuidRankForme, "Forme'", "Forme'", "", (OrderedTermVocabulary<Rank>) Rank.GENUS().getVocabulary(), null, RankClass.Infraspecific);
+            }
             else{
                 rank = state.getTransformer().getRankByKey(rankStr);
             }