Project

General

Profile

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

    
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
    public RedListGefaesspflanzenImportAuthor() {
45
        super(tableName, pluralString);
46
    }
47

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

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

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

    
70

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

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

    
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
                if(CdmUtils.isNotBlank(authorName)){
87
                    makePerson(state, authorName);
88
                }
89
            }
90
        } catch (SQLException e) {
91
            e.printStackTrace();
92
        }
93
    }
94

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

    
123

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

    
131
    @Override
132
    protected boolean doCheck(RedListGefaesspflanzenImportState state) {
133
        return false;
134
    }
135

    
136
    @Override
137
    protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
138
        return false;
139
    }
140

    
141
}
(1-1/7)