Project

General

Profile

Download (6.96 KB) Statistics
| Branch: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2016 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.io.redlist.gefaesspflanzen;
11

    
12
import java.sql.ResultSet;
13
import java.sql.SQLException;
14
import java.util.HashMap;
15
import java.util.Map;
16
import java.util.Map.Entry;
17
import java.util.UUID;
18

    
19
import org.apache.log4j.Logger;
20
import org.springframework.stereotype.Component;
21

    
22
import eu.etaxonomy.cdm.common.CdmUtils;
23
import eu.etaxonomy.cdm.io.common.DbImportBase;
24
import eu.etaxonomy.cdm.io.common.IPartitionedIO;
25
import eu.etaxonomy.cdm.io.common.ImportHelper;
26
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
27
import eu.etaxonomy.cdm.model.agent.AgentBase;
28
import eu.etaxonomy.cdm.model.agent.Person;
29
import eu.etaxonomy.cdm.model.agent.Team;
30
import eu.etaxonomy.cdm.model.common.CdmBase;
31

    
32
/**
33
 * @author pplitzner
34
 * @date Feb 29, 2016
35
 *
36
 */
37
@Component
38
@SuppressWarnings("serial")
39
public class RedListGefaesspflanzenImportAuthor extends DbImportBase<RedListGefaesspflanzenImportState, RedListGefaesspflanzenImportConfigurator> {
40
    private static final Logger logger = Logger.getLogger(RedListGefaesspflanzenImportAuthor.class);
41

    
42
    private static final String tableName = "Rote Liste Gefäßpflanzen";
43

    
44
    private static final String pluralString = "authors";
45

    
46
    private static final String AUTHOR_KOMB_NAMESPACE = "author_komb";
47
    private static final String AUTHOR_BASI_NAMESPACE = "author_basi";
48

    
49
    public RedListGefaesspflanzenImportAuthor() {
50
        super(tableName, pluralString);
51
    }
52

    
53
    @Override
54
    protected String getIdQuery(RedListGefaesspflanzenImportState state) {
55
        return "SELECT NAMNR "
56
                + "FROM V_TAXATLAS_D20_EXPORT t "
57
                + " ORDER BY NAMNR";
58
    }
59

    
60
    @Override
61
    protected String getRecordQuery(RedListGefaesspflanzenImportConfigurator config) {
62
        String result = " SELECT * "
63
                + " FROM V_TAXATLAS_D20_EXPORT t "
64
                + " WHERE t.NAMNR IN (@IDSET)";
65
        result = result.replace("@IDSET", IPartitionedIO.ID_LIST_TOKEN);
66
        return result;
67
    }
68

    
69
    @Override
70
    protected void doInvoke(RedListGefaesspflanzenImportState state) {
71
        super.doInvoke(state);
72
    }
73

    
74

    
75
    @Override
76
    public boolean doPartition(ResultSetPartitioner partitioner, RedListGefaesspflanzenImportState state) {
77
        ResultSet rs = partitioner.getResultSet();
78
        Map<String, AgentBase> teamsOrPersonToSave = new HashMap<String, AgentBase>();
79
        try {
80
            while (rs.next()){
81
                makeSingleAuthor(state, rs, teamsOrPersonToSave);
82

    
83
            }
84
        } catch (SQLException e) {
85
            e.printStackTrace();
86
        }
87

    
88
        getAgentService().saveOrUpdate(teamsOrPersonToSave.values());
89
        //add partition to state map
90
        for (Entry<String, AgentBase> entry: teamsOrPersonToSave.entrySet()) {
91
            state.getAgentMap().put(entry.getKey(), entry.getValue().getUuid());
92
        }
93
        return true;
94
    }
95

    
96
    private void makeSingleAuthor(RedListGefaesspflanzenImportState state, ResultSet rs, Map<String, AgentBase> teamsOrPersonToSave)
97
            throws SQLException {
98
        long id = rs.getLong("NAMNR");
99
        String authorName = rs.getString("AUTOR");
100
        String authorBasiName = rs.getString("AUTOR_BASI");
101
        String authorKombName = rs.getString("AUTOR_KOMB");
102

    
103
        //check null values
104
        if(CdmUtils.isBlank(authorName) && CdmUtils.isBlank(authorBasiName) && CdmUtils.isBlank(authorKombName)){
105
            logger.info("NAMNR "+id+": No author found for NAMNR "+id);
106
            return;
107
        }
108
        AgentBase authorKomb = null;
109
        AgentBase authorBasi = null;
110
        AgentBase author = null;
111

    
112
        authorKomb = importPerson(state, teamsOrPersonToSave, id, authorKombName, AUTHOR_KOMB_NAMESPACE);
113
        authorBasi = importPerson(state, teamsOrPersonToSave, id, authorBasiName, AUTHOR_BASI_NAMESPACE);
114

    
115
        //check if missapplied name
116
        if(authorName.equals("auct.")){
117

    
118
        }
119
        //check if pro parte synonym
120
        else if(authorName.equals("p .p.")){
121

    
122
        }
123
        else if(authorBasi==null && authorKomb==null){
124
            logger.warn("NAMNR "+id+": Author not atomised in authorKomb and authorBasi. Author: "+authorName);
125
            importPerson(state, teamsOrPersonToSave, id, authorName, AUTHOR_KOMB_NAMESPACE);
126
        }
127
//        //check author column consistency
128
//        String authorCheckString = "";
129
//        if(!CdmUtils.isBlank(authorKombName)){
130
//            authorCheckString = "("+authorBasiName+")"+" "+authorKombName;
131
//        }
132
//        else{
133
//            authorCheckString = authorBasiName;
134
//        }
135
//        boolean isAuthorStringCorrect = false;
136
//        if(authorName.startsWith(authorCheckString)){
137
//            isAuthorStringCorrect = true;
138
//        }
139
//        if(!isAuthorStringCorrect){
140
//            String errorString = "NAMNR "+id+": Author string not consistent! Is \""+authorName+"\" Should start with \""+authorCheckString+"\"";
141
//            logger.error(errorString);
142
//        }
143

    
144
    }
145

    
146
    private AgentBase importPerson(RedListGefaesspflanzenImportState state, Map<String, AgentBase> teamsOrPersonToSave,
147
            long id, String agentName, String namespace) {
148
        AgentBase agent = null;
149
        //check if agent already exists
150
        AgentBase notYetPersistedAgent = teamsOrPersonToSave.get(agentName);
151
        UUID existingAgentUuid = state.getAgentMap().get(agentName);
152
        if(notYetPersistedAgent!=null){
153
            agent = notYetPersistedAgent;
154
        }
155
        else if(existingAgentUuid!=null){
156
            agent = getAgentService().load(existingAgentUuid);
157
        }
158
        else if(!CdmUtils.isBlank(agentName)){
159
            //check if it is a team
160
            if(agentName.contains("&")){
161
                agent = Team.NewInstance();
162
                String[] split = agentName.split("&");
163
                for (int i = 0; i < split.length; i++) {
164
                    ((Team) agent).addTeamMember(Person.NewTitledInstance(split[i].trim()));
165
                }
166
            }
167
            else{
168
                agent = Person.NewTitledInstance(agentName);
169
            }
170
            teamsOrPersonToSave.put(agentName, agent);
171
            state.getAgentMap().put(agentName, agent.getUuid());
172
            ImportHelper.setOriginalSource(agent, state.getTransactionalSourceReference(), id, namespace);
173
        }
174
        return agent;
175
    }
176

    
177
    @Override
178
    public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
179
            RedListGefaesspflanzenImportState state) {
180
        Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
181
        return result;
182
    }
183

    
184
    @Override
185
    protected boolean doCheck(RedListGefaesspflanzenImportState state) {
186
        return false;
187
    }
188

    
189
    @Override
190
    protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
191
        return false;
192
    }
193

    
194
}
(1-1/5)