Project

General

Profile

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

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

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

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

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

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

    
43
    public RedListGefaesspflanzenImportAuthor() {
44
        super(tableName, pluralString);
45
    }
46

    
47
    @Override
48
    protected String getIdQuery(RedListGefaesspflanzenImportState state) {
49
        return "SELECT NAMNR "
50
                + "FROM V_TAXATLAS_D20_EXPORT t "
51
                + " ORDER BY NAMNR";
52
    }
53

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

    
63
    @Override
64
    protected void doInvoke(RedListGefaesspflanzenImportState state) {
65
        makeAuthors(state, "AUTOR_KOMB");
66
        makeAuthors(state, "AUTOR_BASI");
67
    }
68

    
69

    
70
    @Override
71
    public boolean doPartition(ResultSetPartitioner partitioner, RedListGefaesspflanzenImportState state) {
72
        return true;
73
    }
74

    
75
    private void makeAuthors(RedListGefaesspflanzenImportState state, String columnName) {
76

    
77
        //--- combination authors ---
78
        String query = "select distinct "+columnName+" from V_TAXATLAS_D20_EXPORT t"
79
                + " WHERE TRIM(t."+columnName+") <>''";
80

    
81
        ResultSet rs = state.getConfig().getSource().getResultSet(query);
82

    
83
        try{
84
            while(rs.next()){
85
                String authorName = rs.getString(columnName);
86
                TeamOrPersonBase teamOrPerson = null;
87
                if(!CdmUtils.isBlank(authorName)){
88
                    makePerson(state, authorName);
89
                }
90
            }
91
        } catch (SQLException e) {
92
            e.printStackTrace();
93
        }
94
    }
95

    
96
    private void makePerson(RedListGefaesspflanzenImportState state, String authorName) {
97
        TeamOrPersonBase teamOrPerson;
98
        //check if there are ex authors
99
        if(authorName.contains(" ex ")){
100
            String[] split = authorName.split(" ex ");
101
            for (int i = 0; i < split.length; i++) {
102
                makePerson(state, split[i].trim());
103
            }
104
        }
105
        //check if it is a team
106
        if(authorName.contains("&")){
107
            teamOrPerson = Team.NewInstance();
108
            String[] split = authorName.split("&");
109
            for (int i = 0; i < split.length; i++) {
110
                ((Team) teamOrPerson).addTeamMember(Person.NewTitledInstance(split[i].trim()));
111
            }
112
        }
113
        else{
114
            teamOrPerson = Person.NewTitledInstance(authorName);
115
        }
116
        getAgentService().saveOrUpdate(teamOrPerson);
117
        state.getAuthorMap().put(authorName, teamOrPerson.getUuid());
118
    }
119

    
120

    
121
    @Override
122
    public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
123
            RedListGefaesspflanzenImportState state) {
124
        Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
125
        return result;
126
    }
127

    
128
    @Override
129
    protected boolean doCheck(RedListGefaesspflanzenImportState state) {
130
        return false;
131
    }
132

    
133
    @Override
134
    protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
135
        return false;
136
    }
137

    
138
}
(2-2/7)