commit 7319c52815c0bc37ccf217710896647ac45cd421
Author: Patrick Plitzner <p.plitzner@bgbm.org>
Date:   Wed Mar 2 15:58:03 2016 +0100

    Refactor name-author mapping

diff --git a/app-import/src/main/java/eu/etaxonomy/cdm/io/redlist/gefaesspflanzen/RedListGefaesspflanzenImportAuthor.java b/app-import/src/main/java/eu/etaxonomy/cdm/io/redlist/gefaesspflanzen/RedListGefaesspflanzenImportAuthor.java
index d32a76d7..403c6b0b 100644
--- a/app-import/src/main/java/eu/etaxonomy/cdm/io/redlist/gefaesspflanzen/RedListGefaesspflanzenImportAuthor.java
+++ b/app-import/src/main/java/eu/etaxonomy/cdm/io/redlist/gefaesspflanzen/RedListGefaesspflanzenImportAuthor.java
@@ -13,13 +13,11 @@ import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.HashMap;
 import java.util.Map;
-import java.util.UUID;
 
 import org.apache.log4j.Logger;
 import org.springframework.stereotype.Component;
 
 import eu.etaxonomy.cdm.common.CdmUtils;
-import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
 import eu.etaxonomy.cdm.io.common.DbImportBase;
 import eu.etaxonomy.cdm.io.common.IPartitionedIO;
 import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
@@ -64,111 +62,47 @@ public class RedListGefaesspflanzenImportAuthor extends DbImportBase<RedListGefa
 
     @Override
     protected void doInvoke(RedListGefaesspflanzenImportState state) {
-        super.doInvoke(state);
+        makeAuthors(state, "AUTOR_KOMB");
+        makeAuthors(state, "AUTOR_BASI");
     }
 
 
     @Override
     public boolean doPartition(ResultSetPartitioner partitioner, RedListGefaesspflanzenImportState state) {
-        ResultSet rs = partitioner.getResultSet();
-        Map<String, TeamOrPersonBase> teamsOrPersonToSave = new HashMap<String, TeamOrPersonBase>();
-        try {
-            while (rs.next()){
-                makeSingleAuthor(state, rs, teamsOrPersonToSave);
-
-            }
-        } catch (SQLException e) {
-            e.printStackTrace();
-        }
-
-//        getAgentService().saveOrUpdate(teamsOrPersonToSave.values());
         return true;
     }
 
-    private void makeSingleAuthor(RedListGefaesspflanzenImportState state, ResultSet rs, Map<String, TeamOrPersonBase> teamsOrPersonToSave)
-            throws SQLException {
-        long id = rs.getLong("NAMNR");
-        String authorName = rs.getString("AUTOR");
-        String authorBasiName = rs.getString("AUTOR_BASI");
-        String authorKombName = rs.getString("AUTOR_KOMB");
-
-        //check null values
-        if(CdmUtils.isBlank(authorName) && CdmUtils.isBlank(authorBasiName) && CdmUtils.isBlank(authorKombName)){
-            logger.info("NAMNR "+id+": No author found!");
-            return;
-        }
-        TeamOrPersonBase authorKomb = null;
-        TeamOrPersonBase authorBasi = null;
-
-        authorKomb = importPerson(state, teamsOrPersonToSave, id, authorKombName);
-        if(authorKomb!=null){
-            state.getAuthorKombMap().put(id, authorKomb.getUuid());
-        }
-        authorBasi = importPerson(state, teamsOrPersonToSave, id, authorBasiName);
-        if(authorKomb!=null){
-            state.getAuthorBasiMap().put(id, authorBasi.getUuid());
-        }
-
-        //check if missapplied name
-        if(authorName.equals("auct.")){
-
-        }
-        //check if pro parte synonym
-        else if(authorName.equals("p .p.")){
-
-        }
-        else if(authorBasi==null && authorKomb==null){
-            logger.warn("NAMNR "+id+": Author not atomised in authorKomb and authorBasi. Author: "+authorName);
-            TeamOrPersonBase team = importPerson(state, teamsOrPersonToSave, id, authorName);
-            state.getAuthorKombMap().put(id, team.getUuid());
-        }
-//        //check author column consistency
-//        String authorCheckString = "";
-//        if(!CdmUtils.isBlank(authorKombName)){
-//            authorCheckString = "("+authorBasiName+")"+" "+authorKombName;
-//        }
-//        else{
-//            authorCheckString = authorBasiName;
-//        }
-//        boolean isAuthorStringCorrect = false;
-//        if(authorName.startsWith(authorCheckString)){
-//            isAuthorStringCorrect = true;
-//        }
-//        if(!isAuthorStringCorrect){
-//            String errorString = "NAMNR "+id+": Author string not consistent! Is \""+authorName+"\" Should start with \""+authorCheckString+"\"";
-//            logger.error(errorString);
-//        }
-
-    }
-
-    private TeamOrPersonBase importPerson(RedListGefaesspflanzenImportState state, Map<String, TeamOrPersonBase> teamsOrPersonToSave,
-            long id, String agentName) {
-        TeamOrPersonBase agent = null;
-        //check if agent already exists
-        TeamOrPersonBase notYetPersistedAgent = teamsOrPersonToSave.get(agentName);
-        UUID existingAgentUuid = state.getAuthorKombMap().get(agentName);
-        if(notYetPersistedAgent!=null){
-            agent = notYetPersistedAgent;
-        }
-        else if(existingAgentUuid!=null){
-            agent = HibernateProxyHelper.deproxy(getAgentService().load(existingAgentUuid), TeamOrPersonBase.class);
-        }
-        else if(!CdmUtils.isBlank(agentName)){
-            //check if it is a team
-            if(agentName.contains("&")){
-                agent = Team.NewInstance();
-                String[] split = agentName.split("&");
-                for (int i = 0; i < split.length; i++) {
-                    ((Team) agent).addTeamMember(Person.NewTitledInstance(split[i].trim()));
+    private void makeAuthors(RedListGefaesspflanzenImportState state, String columnName) {
+
+        //--- combination authors ---
+        String query = "select distinct "+columnName+" from V_TAXATLAS_D20_EXPORT t"
+                + " WHERE TRIM(t."+columnName+") <>''";
+
+        ResultSet rs = state.getConfig().getSource().getResultSet(query);
+
+        try{
+            while(rs.next()){
+                String authorName = rs.getString(columnName);
+                TeamOrPersonBase teamOrPerson = null;
+                if(!CdmUtils.isBlank(authorName)){
+                    //check if it is a team
+                    if(authorName.contains("&")){
+                        teamOrPerson = Team.NewInstance();
+                        String[] split = authorName.split("&");
+                        for (int i = 0; i < split.length; i++) {
+                            ((Team) teamOrPerson).addTeamMember(Person.NewTitledInstance(split[i].trim()));
+                        }
+                    }
+                    else{
+                        teamOrPerson = Person.NewTitledInstance(authorName);
+                    }
+                    getAgentService().saveOrUpdate(teamOrPerson);
+                    state.getAuthorMap().put(authorName, teamOrPerson.getUuid());
                 }
             }
-            else{
-                agent = Person.NewTitledInstance(agentName);
-            }
-            teamsOrPersonToSave.put(agentName, agent);
-//            ImportHelper.setOriginalSource(agent, state.getTransactionalSourceReference(), id, namespace);
+        } catch (SQLException e) {
+            e.printStackTrace();
         }
-        return agent;
     }
 
     @Override
diff --git a/app-import/src/main/java/eu/etaxonomy/cdm/io/redlist/gefaesspflanzen/RedListGefaesspflanzenImportConfigurator.java b/app-import/src/main/java/eu/etaxonomy/cdm/io/redlist/gefaesspflanzen/RedListGefaesspflanzenImportConfigurator.java
index af1c2db0..11807f7f 100644
--- a/app-import/src/main/java/eu/etaxonomy/cdm/io/redlist/gefaesspflanzen/RedListGefaesspflanzenImportConfigurator.java
+++ b/app-import/src/main/java/eu/etaxonomy/cdm/io/redlist/gefaesspflanzen/RedListGefaesspflanzenImportConfigurator.java
@@ -36,7 +36,7 @@ public class RedListGefaesspflanzenImportConfigurator extends
     @Override
     protected void makeIoClassList() {
         ioClassList = new Class[]{
-//                RedListGefaesspflanzenImportAuthor.class,
+                RedListGefaesspflanzenImportAuthor.class,
                 RedListGefaesspflanzenImportNames.class,
 //                RedListGefaesspflanzenImportTaxa.class,
         };
diff --git a/app-import/src/main/java/eu/etaxonomy/cdm/io/redlist/gefaesspflanzen/RedListGefaesspflanzenImportNames.java b/app-import/src/main/java/eu/etaxonomy/cdm/io/redlist/gefaesspflanzen/RedListGefaesspflanzenImportNames.java
index b32f6af1..bbfcb4f2 100644
--- a/app-import/src/main/java/eu/etaxonomy/cdm/io/redlist/gefaesspflanzen/RedListGefaesspflanzenImportNames.java
+++ b/app-import/src/main/java/eu/etaxonomy/cdm/io/redlist/gefaesspflanzen/RedListGefaesspflanzenImportNames.java
@@ -20,12 +20,13 @@ import org.apache.log4j.Logger;
 import org.springframework.stereotype.Component;
 
 import eu.etaxonomy.cdm.common.CdmUtils;
+import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
 import eu.etaxonomy.cdm.io.common.DbImportBase;
 import eu.etaxonomy.cdm.io.common.IPartitionedIO;
 import eu.etaxonomy.cdm.io.common.ImportHelper;
 import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
 import eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException;
-import eu.etaxonomy.cdm.model.agent.AgentBase;
+import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
 import eu.etaxonomy.cdm.model.common.CdmBase;
 import eu.etaxonomy.cdm.model.name.BotanicalName;
 import eu.etaxonomy.cdm.model.name.Rank;
@@ -47,9 +48,7 @@ public class RedListGefaesspflanzenImportNames extends DbImportBase<RedListGefae
 
     private static final String pluralString = "names";
 
-    private static final String TAXON_NAMESPACE = "name";
-    private static final String AUTHOR_KOMB_NAMESPACE = "author_komb";
-    private static final String AUTHOR_BASI_NAMESPACE = "author_basi";
+    private static final String NAME_NAMESPACE = "name";
 
     public RedListGefaesspflanzenImportNames() {
         super(tableName, pluralString);
@@ -80,17 +79,17 @@ public class RedListGefaesspflanzenImportNames extends DbImportBase<RedListGefae
     @Override
     public boolean doPartition(ResultSetPartitioner partitioner, RedListGefaesspflanzenImportState state) {
         ResultSet rs = partitioner.getResultSet();
-        Set<TaxonNameBase> taxaToSave = new HashSet<TaxonNameBase>();
+        Set<TaxonNameBase> namesToSave = new HashSet<TaxonNameBase>();
         try {
             while (rs.next()){
-                makeSingleName(state, rs, taxaToSave);
+                makeSingleName(state, rs, namesToSave);
 
             }
         } catch (SQLException e) {
             e.printStackTrace();
         }
 
-        getNameService().saveOrUpdate(taxaToSave);
+        getNameService().saveOrUpdate(namesToSave);
         return true;
     }
 
@@ -104,6 +103,8 @@ public class RedListGefaesspflanzenImportNames extends DbImportBase<RedListGefae
         String ep3String = rs.getString("EPI3");
         String nomZusatzString = rs.getString("NOM_ZUSATZ");
         String zusatzString = rs.getString("ZUSATZ");
+        String authorKombString = rs.getString("AUTOR_KOMB");
+        String authorBasiString = rs.getString("AUTOR_BASI");
 
         if(CdmUtils.isBlank(taxNameString) && CdmUtils.isBlank(ep1String)){
             logger.error("NAMNR: "+id+" No name found!");
@@ -120,11 +121,13 @@ public class RedListGefaesspflanzenImportNames extends DbImportBase<RedListGefae
         }
 
         //add author
-        AgentBase authorKomb = getAgentService().load(state.getAuthorKombMap().get(id));
-//        name.setCombinationAuthorship(authorKomb);
+        TeamOrPersonBase authorKomb = HibernateProxyHelper.deproxy(getAgentService().load(state.getAuthorMap().get(authorKombString)), TeamOrPersonBase.class);
+        name.setCombinationAuthorship(authorKomb);
 
         //id
-        ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, TAXON_NAMESPACE);
+        ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, NAME_NAMESPACE);
+
+        namesToSave.add(name);
     }
 
     private Rank makeRank(RedListGefaesspflanzenImportState state, String rankStr) {
diff --git a/app-import/src/main/java/eu/etaxonomy/cdm/io/redlist/gefaesspflanzen/RedListGefaesspflanzenImportState.java b/app-import/src/main/java/eu/etaxonomy/cdm/io/redlist/gefaesspflanzen/RedListGefaesspflanzenImportState.java
index 58feb217..09ab6415 100644
--- a/app-import/src/main/java/eu/etaxonomy/cdm/io/redlist/gefaesspflanzen/RedListGefaesspflanzenImportState.java
+++ b/app-import/src/main/java/eu/etaxonomy/cdm/io/redlist/gefaesspflanzen/RedListGefaesspflanzenImportState.java
@@ -28,19 +28,14 @@ public class RedListGefaesspflanzenImportState extends DbImportStateBase<RedList
 	@SuppressWarnings("unused")
 	private static final Logger logger = Logger.getLogger(RedListGefaesspflanzenImportState.class);
 
-	private Map<Long, UUID> authorKombMap = new HashMap<Long, UUID>();
-	private Map<Long, UUID> authorBasiMap = new HashMap<Long, UUID>();
+	private final Map<String, UUID> authorMap = new HashMap<String, UUID>();
 
     protected RedListGefaesspflanzenImportState(RedListGefaesspflanzenImportConfigurator config) {
         super(config);
     }
 
-    public Map<Long, UUID> getAuthorKombMap() {
-        return authorKombMap;
-    }
-
-    public Map<Long, UUID> getAuthorBasiMap() {
-        return authorBasiMap;
+    public Map<String, UUID> getAuthorMap() {
+        return authorMap;
     }
 
 }
