Project

General

Profile

Download (6.69 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

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

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

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

    
40
    private static final String tableName = "Rote Liste Gefäßpflanzen";
41

    
42
    private static final String pluralString = "authors";
43

    
44
    private static final String AUTHOR_KOMB_NAMESPACE = "author_komb";
45
    private static final String AUTHOR_BASI_NAMESPACE = "author_basi";
46

    
47
    public RedListGefaesspflanzenImportAuthor() {
48
        super(tableName, pluralString);
49
    }
50

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

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

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

    
72

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

    
81
            }
82
        } catch (SQLException e) {
83
            e.printStackTrace();
84
        }
85

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

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

    
102
        //check null values
103
        if(CdmUtils.isBlank(authorName) && CdmUtils.isBlank(authorBasiName) && CdmUtils.isBlank(authorKombName)){
104
            logger.error("No author found for NAMNR "+id);
105
            return;
106
        }
107
        Person authorKomb = null;
108
        Person authorBasi = null;
109
        Person author = null;
110
        authorKomb = importPerson(state, teamsOrPersonToSave, id, authorKombName, AUTHOR_KOMB_NAMESPACE);
111

    
112
        authorBasi = importPerson(state, teamsOrPersonToSave, id, authorBasiName, AUTHOR_BASI_NAMESPACE);
113

    
114
//        if(authorBasi!=null && authorKomb!=null){
115
//            Team team = Team.NewInstance();
116
//            team.addTeamMember(authorBasi);
117
//            team.addTeamMember(authorKomb);
118
//            teamsOrPersonToSave.add(team);
119
//            ImportHelper.setOriginalSource(team, state.getTransactionalSourceReference(), id, AUTHOR_NAMESPACE);
120
//        }
121
        if(authorBasi==null && authorKomb==null){
122
            logger.warn("Author not atomised in authorKomb and authorBasi");
123
            author = Person.NewTitledInstance(authorName);
124
            teamsOrPersonToSave.put(authorName, author);
125
            ImportHelper.setOriginalSource(author, state.getTransactionalSourceReference(), id, 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
            if(!CdmUtils.isBlank(zusatz) && !authorName.contains(zusatz)){
139
                isAuthorStringCorrect = false;
140
            }
141
        }
142
        if(!isAuthorStringCorrect){
143
            String errorString = "ID: "+id+", Author string not consistent! Is \""+authorName+"\" Should start with \""+authorCheckString+"\"";
144
            if(!CdmUtils.isBlank(zusatz)){
145
                errorString +=" and contain \""+zusatz+"\"";
146
            }
147
            logger.error(errorString);
148
        }
149

    
150
    }
151

    
152
    private Person importPerson(RedListGefaesspflanzenImportState state, Map<String, AgentBase> teamsOrPersonToSave,
153
            long id, String agentName, String namespace) {
154
        Person person = null;
155
        if(!CdmUtils.isBlank(agentName) && !state.getAgentMap().containsKey(agentName)){
156
            person = Person.NewTitledInstance(agentName);
157
            teamsOrPersonToSave.put(agentName, person);
158
            state.getAgentMap().put(agentName, person.getUuid());
159
            ImportHelper.setOriginalSource(person, state.getTransactionalSourceReference(), id, namespace);
160
        }
161
        return person;
162
    }
163

    
164
    @Override
165
    public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
166
            RedListGefaesspflanzenImportState state) {
167
        Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
168
        return result;
169
    }
170

    
171
    @Override
172
    protected boolean doCheck(RedListGefaesspflanzenImportState state) {
173
        return false;
174
    }
175

    
176
    @Override
177
    protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
178
        return false;
179
    }
180

    
181
}
(1-1/5)