commit f6f52f73cb81a380103edc6f453c9e8acfe5c679
Author: Andreas Müller <a.mueller@bgbm.org>
Date:   Thu Jan 25 11:27:23 2018 +0100

    ref #7216 adapt Cuba import for pteridophytae import

diff --git a/app-import/src/main/java/eu/etaxonomy/cdm/app/common/CdmDestinations.java b/app-import/src/main/java/eu/etaxonomy/cdm/app/common/CdmDestinations.java
index 62580193..8d85aba3 100644
--- a/app-import/src/main/java/eu/etaxonomy/cdm/app/common/CdmDestinations.java
+++ b/app-import/src/main/java/eu/etaxonomy/cdm/app/common/CdmDestinations.java
@@ -358,6 +358,14 @@ public class CdmDestinations {
 		return makeDestination(dbType, cdmServer, cdmDB, -1, cdmUserName, null);
 	}
 
+    public static ICdmDataSource cdm_local_cuba(){
+        DatabaseTypeEnum dbType = DatabaseTypeEnum.MySQL;
+        String cdmServer = "127.0.0.1";
+        String cdmDB = "cdm_local_flora_cuba";
+        String cdmUserName = "edit";
+        return makeDestination(dbType, cdmServer, cdmDB, -1, cdmUserName, null);
+    }
+
 	public static ICdmDataSource cdm_local_caryo(){
 		DatabaseTypeEnum dbType = DatabaseTypeEnum.MySQL;
 		String cdmServer = "127.0.0.1";
diff --git a/app-import/src/main/java/eu/etaxonomy/cdm/app/cuba/CubaActivator.java b/app-import/src/main/java/eu/etaxonomy/cdm/app/cuba/CubaActivator.java
index ff38ee55..62fadac7 100644
--- a/app-import/src/main/java/eu/etaxonomy/cdm/app/cuba/CubaActivator.java
+++ b/app-import/src/main/java/eu/etaxonomy/cdm/app/cuba/CubaActivator.java
@@ -41,9 +41,10 @@ public class CubaActivator {
 	//database validation status (create, update, validate ...)
 	static DbSchemaValidation hbm2dll = DbSchemaValidation.VALIDATE;
 
-    static final ICdmDataSource cdmDestination = CdmDestinations.localH2();
+//  static final ICdmDataSource cdmDestination = CdmDestinations.localH2();
 //	static final ICdmDataSource cdmDestination = CdmDestinations.cdm_test_local_mysql_test();
-//    static final ICdmDataSource cdmDestination = CdmDestinations.cdm_cuba_production();
+    static final ICdmDataSource cdmDestination = CdmDestinations.cdm_local_cuba();
+//  static final ICdmDataSource cdmDestination = CdmDestinations.cdm_cuba_production();
 
 	static boolean invers = true;
 
@@ -64,6 +65,7 @@ public class CubaActivator {
     boolean doOrchidaceae = include;
     boolean doRubiaceae = include;
     boolean doUrticaceae = include;
+    boolean doPteridophyta = !include;
 
     static boolean include = !invers;
 
@@ -81,8 +83,13 @@ public class CubaActivator {
 	static final CHECK check = CHECK.IMPORT_WITHOUT_CHECK;
 
 	boolean doVocabularies = (hbm2dll == DbSchemaValidation.CREATE);
-	static final boolean doTaxa = false;
-	static final boolean doDeduplicate = true;
+	static final boolean doTaxa = true;
+	static final boolean doDeduplicate = false;
+
+	static final boolean doAltFlorasSanchez2017 = true;
+	static final boolean doAltFlorasFRC = true;
+	static final boolean doAltFlorasFC = false;
+	static final boolean doAltFlorasAS = false;
 
 
 	private void doImport(ICdmDataSource cdmDestination){
@@ -99,8 +106,12 @@ public class CubaActivator {
 		config.setDbSchemaValidation(hbm2dll);
 		config.setSourceReferenceTitle(sourceReferenceTitle);
 		config.setDoVocabularies(doVocabularies);
+		config.setDoAltFlorasAS(doAltFlorasAS);
+		config.setDoAltFlorasFC(doAltFlorasFC);
+		config.setDoAltFlorasFRC(doAltFlorasFRC);
+		config.setDoAltFlorasSanchez2017(doAltFlorasSanchez2017);
 
-		CdmDefaultImport<CubaImportConfigurator> myImport = new CdmDefaultImport<CubaImportConfigurator>();
+		CdmDefaultImport<CubaImportConfigurator> myImport = new CdmDefaultImport<>();
 
 
 		//...
@@ -155,6 +166,9 @@ public class CubaActivator {
         if (doUrticaceae){
             doSingleSource(urticaceae(), config, myImport, doVocabularies);
         }
+        if (doPteridophyta){
+            doSingleSource(pteridophyta(), config, myImport, doVocabularies);
+        }
 
 
 		//deduplicate
@@ -303,6 +317,10 @@ public class CubaActivator {
     public static URI rubiaceae() {
         return URI.create("file:////BGBM-PESIHPC/Cuba/Rubiaceae.xlsx");
     }
+    //Rubiaceae
+    public static URI pteridophyta() {
+        return URI.create("file:////BGBM-PESIHPC/Cuba/Pteridophyta.xlsx");
+    }
 
 	/**
 	 * @param args
diff --git a/app-import/src/main/java/eu/etaxonomy/cdm/io/cuba/CubaExcelImport.java b/app-import/src/main/java/eu/etaxonomy/cdm/io/cuba/CubaExcelImport.java
index 4890eec4..c38fba10 100644
--- a/app-import/src/main/java/eu/etaxonomy/cdm/io/cuba/CubaExcelImport.java
+++ b/app-import/src/main/java/eu/etaxonomy/cdm/io/cuba/CubaExcelImport.java
@@ -90,8 +90,8 @@ public class CubaExcelImport
     private static INonViralNameParser<?> nameParser = NonViralNameParserImpl.NewInstance();
     private static NomenclaturalCode nc = NomenclaturalCode.ICNAFP;
 
-    private  static List<String> expectedKeys= Arrays.asList(new String[]{
-            "Fam. default","Fam. FRC","Fam. A&S","Fam. FC",
+    private  static List<String> expectedKeys = Arrays.asList(new String[]{
+            "Fam. default","Fam. FRC","Fam. A&S","Fam. FC","Fam. Sánchez 2017",
             "Taxón","(Notas)","Syn.","End","Ind","Ind? D","Nat","Dud P","Adv","Cult C","CuW","PR PR*","Art","Hab(*)","May","Mat","IJ","CuC","VC","Ci","SS","CA","Cam","LT","CuE","Gr","Ho","SC","Gu","Esp","Ja","PR","Men","Bah","Cay","AmN","AmC","AmS","VM"});
 
 	@Override
@@ -407,12 +407,21 @@ public class CubaExcelImport
             misappliedNameTaxon.setDoubtful(doubtful);
             if (secondPart.startsWith("sensu")){
                 secondPart = secondPart.substring(5).trim();
-                if (secondPart.contains(" ")){
-                    logger.warn(line + "CHECK: Second part contains more than 1 word. Check if this is correct: " + secondPart);
+                Reference sensu;
+                if (isConcreteReference(secondPart)) {
+                    List<Reference> sensuRefs = getConcreteReferences(secondPart, state);
+                    if (sensuRefs.size() > 1){
+                        logger.warn(line + "MAs > 1 not yet handled: " + secondPart);
+                    }
+                    sensu = sensuRefs.get(0);
+                }else{
+                    if (secondPart.contains(" ")){
+                        logger.warn(line + "CHECK: Second part contains more than 1 word. Check if this is correct: " + secondPart);
+                    }
+                    sensu = ReferenceFactory.newGeneric();
+                    Team team = Team.NewTitledInstance(secondPart, null);
+                    sensu.setAuthorship(team);
                 }
-                Reference sensu = ReferenceFactory.newGeneric();
-                Team team = Team.NewTitledInstance(secondPart, null);
-                sensu.setAuthorship(team);
                 misappliedNameTaxon.setSec(sensu);
             }else if (secondPart.matches(auctRegExStr)){
                 secondPart = secondPart.replace("p. p.", "p.p.");
@@ -483,6 +492,45 @@ public class CubaExcelImport
         }
     }
 
+    /**
+     * @param secondPart
+     * @return
+     */
+    private boolean isConcreteReference(String secondPart) {
+        boolean result = false;
+        String[] splits = secondPart.split(",");
+        for (String split : splits){
+            split = split.trim();
+            result = split.equals("Griseb. 2") || split.equals("Sauv. 3")
+                    || split.equals("Grisebach 5") || split.equals("Griseb. 78") ;
+            if (result == false){
+                return result;
+            }
+        }
+        return result;
+    }
+
+    private List<Reference> getConcreteReferences(String secondPart, CubaImportState state) {
+        List<Reference> result = new ArrayList<>();
+        String[] splits = secondPart.split(",");
+        for (String split : splits){
+            split = split.trim();
+            if (split.equals("Griseb. 2")){
+                result.add(getSourceByNumber("2", state));
+            }else if (split.equals("Sauv. 3")){
+                result.add(getSourceByNumber("3", state));
+            }else if (split.equals("Grisebach 5")){
+                result.add(getSourceByNumber("5", state));
+            }else if (split.equals("Griseb. 78")){
+                result.add(getSourceByNumber("78", state));
+            }else{
+                logger.warn("Concrete reference does not match: " + split);
+            }
+        }
+        return result;
+    }
+
+
     /**
      * @param state
      * @param list
@@ -565,12 +613,18 @@ public class CubaExcelImport
             }
             TaxonDescription desc = getTaxonDescription(state.getCurrentTaxon(), false, true);
             PresenceAbsenceTerm status =  makeProvinceStatus(areaKey, record, state);
-            if (status == null){
+            Reference source = getAreaSourceByNumber(areaKey, record, state);
+            if (status == null && source == null){
                 logger.warn(state.getCurrentLine() + ": Province distribution status could not be defined: " + record.get(areaKey));
+            }else if (status == null){
+                status = PresenceAbsenceTerm.NATIVE();
             }
             Distribution distribution = Distribution.NewInstance(area, status);
             desc.addElement(distribution);
             distribution.addSource(makeDescriptionSource(state));
+            if (source != null){
+                distribution.addPrimaryTaxonomicSource(source, null);
+            }
         } catch (UndefinedTransformerMethodException e) {
             e.printStackTrace();
         }
@@ -578,6 +632,46 @@ public class CubaExcelImport
     }
 
 
+    /**
+     * @param areaKey
+     * @param record
+     * @param state
+     */
+    private Reference getAreaSourceByNumber(String areaKey, HashMap<String, String> record, CubaImportState state) {
+        String statusStr = record.get(areaKey);
+        if (statusStr == null){
+            return null;
+        }else{
+            statusStr = statusStr.trim();
+        }
+        if ("p 78".equals(statusStr)){
+            statusStr = "78";
+        }else if ("– 7".equals(statusStr)){
+            statusStr = "7";
+        }
+        return getSourceByNumber(statusStr, state);
+    }
+
+    private Reference getSourceByNumber(String number, CubaImportState state) {
+        if ("78".equals(number)){
+            return makeReference(state, CubaTransformer.uuidRefPteridophyta78);
+        }else if ("1".equals(number)){
+            return makeReference(state, CubaTransformer.uuidRefPteridophyta2);
+        }else if ("2".equals(number)){
+            return makeReference(state, CubaTransformer.uuidRefPteridophyta2);
+        }else if ("3".equals(number)){
+            return makeReference(state, CubaTransformer.uuidRefPteridophyta3);
+        }else if ("5".equals(number)){
+            return makeReference(state, CubaTransformer.uuidRefPteridophyta5);
+        }else if ("8".equals(number)){
+            return makeReference(state, CubaTransformer.uuidRefPteridophyta8_82);
+        }else if ("7".equals(number)){
+            return makeReference(state, CubaTransformer.uuidRefPteridophyta7);
+        }
+        return null;
+    }
+
+
     /**
      * @param synonymStr
      * @param state
@@ -898,12 +992,14 @@ public class CubaExcelImport
                 genus = Taxon.NewInstance(name, sec);
                 genus.addSource(makeOriginalSource(state));
                 higherNode = familyNode.addChildTaxon(genus, null, null);
+                getTaxonNodeService().saveOrUpdate(higherNode);
                 state.putHigherTaxon(genusStr, genus);
             }
         }
         taxon.addSource(makeOriginalSource(state));
 
         TaxonNode newNode = higherNode.addChildTaxon(taxon, null, null);
+        getTaxonNodeService().saveOrUpdate(newNode);
         if(isAbsent){
             botanicalName.setTitleCache(taxonStrOrig, true);
             newNode.setExcluded(true);
@@ -1083,6 +1179,7 @@ public class CubaExcelImport
             family = Taxon.NewInstance(name, sec);
             ITaxonTreeNode rootNode = getClassification(state);
             familyNode = rootNode.addChildTaxon(family, sec, null);
+            this.getTaxonNodeService().saveOrUpdate(familyNode);
             state.putHigherTaxon(familyStr, family);
 
         }
@@ -1154,7 +1251,7 @@ public class CubaExcelImport
                             hasExternalPresence = true;
                             if (state.isEndemic()){
                                 String name = taxon.getName().getTitleCache();
-                                logger.error(state.getCurrentLine() +": Taxon ("+name+")is endemic but has non-cuban distribution: " + area.getIdInVocabulary() + "-" + status.getIdInVocabulary());
+                                logger.warn(state.getCurrentLine() +": Taxon ("+name+")is endemic but has non-cuban distribution: " + area.getIdInVocabulary() + "-" + status.getIdInVocabulary());
                                 return;
                             }
                         }
@@ -1164,7 +1261,7 @@ public class CubaExcelImport
         }
         if (!state.isEndemic() && ! hasExternalPresence){
             String name = taxon.getName().getTitleCache();
-            logger.error(state.getCurrentLine() +": Taxon ("+name+")is not endemic but has no non-cuban distribution" );
+            logger.error(state.getCurrentLine() +": Taxon ("+name+") is not endemic but has no non-cuban distribution" );
         }
     }
 
@@ -1406,17 +1503,32 @@ public class CubaExcelImport
             TaxonNode familyTaxon,
             Taxon taxon) {
 
+        CubaImportConfigurator config = state.getConfig();
+
         String famFRC = record.get("Fam. FRC");
         String famAS = record.get("Fam. A&S");
         String famFC = record.get("Fam. FC");
+        String famSanchez2017 = record.get("Fam. Sánchez 2017");
+
+        if (config.isDoAltFlorasFRC()){
+            Reference refFRC = makeReference(state, CubaTransformer.uuidRefFRC);
+            makeSingleAlternativeFamily(state, taxon, famFRC, refFRC);
+        }
+
+        if (config.isDoAltFlorasAS()){
+            Reference refAS = makeReference(state, CubaTransformer.uuidRefAS);
+            makeSingleAlternativeFamily(state, taxon, famAS, refAS);
+        }
 
-        Reference refFRC = makeReference(state, CubaTransformer.uuidRefFRC);
-        Reference refAS = makeReference(state, CubaTransformer.uuidRefAS);
-        Reference refFC = makeReference(state, CubaTransformer.uuidRefFC);
+        if (config.isDoAltFlorasFC()){
+            Reference refFC = makeReference(state, CubaTransformer.uuidRefFC);
+            makeSingleAlternativeFamily(state, taxon, famFC, refFC);
+        }
 
-        makeSingleAlternativeFamily(state, taxon, famFRC, refFRC);
-        makeSingleAlternativeFamily(state, taxon, famAS, refAS);
-        makeSingleAlternativeFamily(state, taxon, famFC, refFC);
+        if (config.isDoAltFlorasSanchez2017()){
+            Reference refSanchez2017 = makeReference(state, CubaTransformer.uuidRefSanchez);
+            makeSingleAlternativeFamily(state, taxon, famSanchez2017, refSanchez2017);
+        }
     }
 
 
@@ -1545,6 +1657,11 @@ public class CubaExcelImport
         PresenceAbsenceTerm status = state.getTransformer().getPresenceTermByKey(statusStr);
         if (status == null){
 //            PresenceAbsenceTerm highestStatus = state.getHighestStatusForTaxon();
+            if ("– 7".equals(statusStr)){
+                statusStr = "–";
+            }else if ("p 78".equals(statusStr)){
+                statusStr = "p";
+            }
             if (state.isCubanProvince() && isMinus(statusStr)){
 //                getAbsenceTermForStatus(state, highestStatus);
                 //we now handle cuban provinces same as external regions
@@ -1553,9 +1670,12 @@ public class CubaExcelImport
                 status = state.getTransformer().getPresenceTermByKey("--");
             }else{
 //                logger.warn("Unhandled status str for provinces / external regions: " + statusStr);
+
                 UUID statusUuid = state.getTransformer().getPresenceTermUuid(statusStr);
                 if (statusUuid == null){
-                    logger.error(state.getCurrentLine() + ": Undefined status str for provinces / external regions. No UUID given: '" + statusStr + "'");
+                    if (! ("78".equals(statusStr)|| "1".equals(statusStr)||"8".equals(statusStr))){
+                        logger.error(state.getCurrentLine() + ": Undefined status str for provinces / external regions. No UUID given: '" + statusStr + "'");
+                    }
                 }else{
                     status = getPresenceTerm(state, statusUuid, statusStr, statusStr, statusStr, false);
                 }
diff --git a/app-import/src/main/java/eu/etaxonomy/cdm/io/cuba/CubaImportConfigurator.java b/app-import/src/main/java/eu/etaxonomy/cdm/io/cuba/CubaImportConfigurator.java
index 72e1c700..d6be2ffd 100644
--- a/app-import/src/main/java/eu/etaxonomy/cdm/io/cuba/CubaImportConfigurator.java
+++ b/app-import/src/main/java/eu/etaxonomy/cdm/io/cuba/CubaImportConfigurator.java
@@ -10,7 +10,6 @@
 package eu.etaxonomy.cdm.io.cuba;
 
 import java.net.URI;
-import java.util.UUID;
 
 import org.apache.log4j.Logger;
 
@@ -36,10 +35,14 @@ public class CubaImportConfigurator extends ExcelImportConfiguratorBase {
 
     private String cubaReferenceTitle;
 
-    private UUID uuidCyprusReference = UUID.fromString("b5281cd3-9d5d-4ae2-8d55-b62a592ce846");
-
 	private boolean isDoTaxa;
 
+	private boolean doAltFlorasSanchez2017;
+    private boolean doAltFlorasFRC;
+    private boolean doAltFlorasFC;
+    private boolean doAltFlorasAS;
+
+
     private boolean doVocabularies;
     public void setDoVocabularies(boolean doVocabularies) {this.doVocabularies = doVocabularies;}
     public boolean isDoVocabularies() {return doVocabularies;}
@@ -99,16 +102,6 @@ public class CubaImportConfigurator extends ExcelImportConfiguratorBase {
 	}
 
 
-	public void setUuidCyprusReference(UUID uuidCyprusReference) {
-		this.uuidCyprusReference = uuidCyprusReference;
-	}
-
-
-	public UUID getUuidCyprusReference() {
-		return uuidCyprusReference;
-	}
-
-
 	public void setCubaReferenceTitle(String cyprusReferenceTitle) {
 		this.cubaReferenceTitle = cyprusReferenceTitle;
 	}
@@ -127,5 +120,33 @@ public class CubaImportConfigurator extends ExcelImportConfiguratorBase {
 	}
 
 
+	//************* ALT FLORAS ********************/
+
+	public boolean isDoAltFlorasSanchez2017() {
+        return doAltFlorasSanchez2017;
+    }
+    public void setDoAltFlorasSanchez2017(boolean doAltFlorasSanchez2017) {
+        this.doAltFlorasSanchez2017 = doAltFlorasSanchez2017;
+    }
+    public boolean isDoAltFlorasFRC() {
+        return doAltFlorasFRC;
+    }
+    public void setDoAltFlorasFRC(boolean doAltFlorasFRC) {
+        this.doAltFlorasFRC = doAltFlorasFRC;
+    }
+    public boolean isDoAltFlorasFC() {
+        return doAltFlorasFC;
+    }
+    public void setDoAltFlorasFC(boolean doAltFlorasFC) {
+        this.doAltFlorasFC = doAltFlorasFC;
+    }
+    public boolean isDoAltFlorasAS() {
+        return doAltFlorasAS;
+    }
+    public void setDoAltFlorasAS(boolean doAltFlorasAS) {
+        this.doAltFlorasAS = doAltFlorasAS;
+    }
+
+
 
 }
diff --git a/app-import/src/main/java/eu/etaxonomy/cdm/io/cuba/CubaTransformer.java b/app-import/src/main/java/eu/etaxonomy/cdm/io/cuba/CubaTransformer.java
index 1836649c..238006ee 100644
--- a/app-import/src/main/java/eu/etaxonomy/cdm/io/cuba/CubaTransformer.java
+++ b/app-import/src/main/java/eu/etaxonomy/cdm/io/cuba/CubaTransformer.java
@@ -32,6 +32,16 @@ public final class CubaTransformer extends InputTransformerBase {
     public static final UUID uuidRefFRC = UUID.fromString("c1caf6a2-5083-4f44-8f97-9abe23a84cd8");
     public static final UUID uuidRefAS = UUID.fromString("1f15291a-b4c5-4e15-960f-d0145a250539");
     public static final UUID uuidRefFC = UUID.fromString("c5a0bfb8-85b2-422d-babe-423aa2e24c35");
+    public static final UUID uuidRefSanchez = UUID.fromString("d861aa65-9c56-4405-84c8-cdaa65b01f0b");
+
+    //refPteridophyta
+    protected static final UUID uuidRefPteridophyta78 = UUID.fromString("e9f68d98-c706-4bb6-8cbf-2be1f3897469");
+    protected static final UUID uuidRefPteridophyta2 = UUID.fromString("910ae303-bd67-4dce-bb12-aebf889ca3fd");
+    protected static final UUID uuidRefPteridophyta3 = UUID.fromString("80f83b9a-2aa4-4dfb-bbcd-457d3ed9e836");
+    protected static final UUID uuidRefPteridophyta5 = UUID.fromString("bf41be7c-5832-4659-be47-66b94bf393f4");
+    protected static final UUID uuidRefPteridophyta8_82 = UUID.fromString("21ca49a2-4243-4041-b876-13dfbcc7abb8");
+    protected static final UUID uuidRefPteridophyta7 = UUID.fromString("251a51e2-8371-42e2-b042-063ecaaef241");
+
 
     //featureUUID
     public static final UUID uuidAlternativeFamily = UUID.fromString("a005f8a1-6377-4641-a826-185f67136860");
