Project

General

Profile

« Previous | Next » 

Revision 76dce350

Added by Patrick Plitzner about 8 years ago

Unifiy name and taxon creation

View differences:

app-import/src/main/java/eu/etaxonomy/cdm/io/redlist/gefaesspflanzen/RedListGefaesspflanzenImportConfigurator.java
36 36
    @Override
37 37
    protected void makeIoClassList() {
38 38
        ioClassList = new Class[]{
39
//                RedListGefaesspflanzenImportAuthor.class,
40
//                RedListGefaesspflanzenImportNames.class,
41
                RedListGefaesspflanzenImportTaxa.class,
39
                RedListGefaesspflanzenImportAuthor.class,
40
                RedListGefaesspflanzenImportNames.class,
42 41
                RedListGefaesspflanzenImportClassification.class,
43 42
        };
44 43
    }
app-import/src/main/java/eu/etaxonomy/cdm/io/redlist/gefaesspflanzen/RedListGefaesspflanzenImportNames.java
31 31
import eu.etaxonomy.cdm.model.name.BotanicalName;
32 32
import eu.etaxonomy.cdm.model.name.Rank;
33 33
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
34
import eu.etaxonomy.cdm.model.taxon.Synonym;
35
import eu.etaxonomy.cdm.model.taxon.Taxon;
36
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
34 37

  
35 38
/**
36 39
 *
......
83 86
    public boolean doPartition(ResultSetPartitioner partitioner, RedListGefaesspflanzenImportState state) {
84 87
        ResultSet rs = partitioner.getResultSet();
85 88
        Set<TaxonNameBase> namesToSave = new HashSet<TaxonNameBase>();
89
        Set<TaxonBase> taxaToSave = new HashSet<TaxonBase>();
86 90
        try {
87 91
            while (rs.next()){
88
                makeSingleName(state, rs, namesToSave);
92
                makeSingleNameAndTaxon(state, rs, namesToSave, taxaToSave);
89 93

  
90 94
            }
91 95
        } catch (SQLException e) {
......
93 97
        }
94 98

  
95 99
        getNameService().saveOrUpdate(namesToSave);
100
        getTaxonService().saveOrUpdate(taxaToSave);
96 101
        return true;
97 102
    }
98 103

  
99
    private void makeSingleName(RedListGefaesspflanzenImportState state, ResultSet rs, Set<TaxonNameBase> namesToSave)
104
    private void makeSingleNameAndTaxon(RedListGefaesspflanzenImportState state, ResultSet rs, Set<TaxonNameBase> namesToSave, Set<TaxonBase> taxaToSave)
100 105
            throws SQLException {
101 106
        long id = rs.getLong("NAMNR");
102 107
        String taxNameString = rs.getString("TAXNAME");
108
        String gueltString = rs.getString("GUELT");
103 109
        String rangString = rs.getString("RANG");
104 110
        String ep1String = rs.getString("EPI1");
105 111
        String ep2String = rs.getString("EPI2");
......
116 122
        Rank rank = makeRank(state, rangString);
117 123
        if(rank==null){
118 124
            logger.error("NAMNR: "+id+" Rank could not be resolved.");
119
            return;
120 125
        }
121 126
        BotanicalName name = BotanicalName.NewInstance(rank);
122 127

  
......
194 199
        state.getNameMap().put(id, name.getUuid());
195 200

  
196 201
        namesToSave.add(name);
202

  
203
        //---TAXON---
204
        TaxonBase taxonBase = null;
205
        if(gueltString.equals("1")){
206
            taxonBase = Taxon.NewInstance(name, null);
207
        }
208
        else if(gueltString.equals("x")){
209
            taxonBase = Synonym.NewInstance(name, null);
210
        }
211
        else if(gueltString.equals("b")){
212
            taxonBase = Synonym.NewInstance(name, null);
213
        }
214
        if(taxonBase==null){
215
            logger.error("NAMNR: "+id+" Taxon for name "+name+" could not be created.");
216
            return;
217
        }
218

  
219
        taxaToSave.add(taxonBase);
220

  
221
        //id
222
        ImportHelper.setOriginalSource(taxonBase, state.getTransactionalSourceReference(), id, Namespace.TAXON_NAMESPACE);
223
        state.getTaxonMap().put(id, taxonBase.getUuid());
197 224
    }
198 225

  
199 226
    private Rank makeRank(RedListGefaesspflanzenImportState state, String rankStr) {
app-import/src/main/java/eu/etaxonomy/cdm/io/redlist/gefaesspflanzen/RedListGefaesspflanzenImportTaxa.java
1
/**
2
 * Copyright (C) 2007 EDIT
3
 * European Distributed Institute of Taxonomy
4
 * http://www.e-taxonomy.eu
5
 *
6
 * The contents of this file are subject to the Mozilla Public License Version 1.1
7
 * See LICENSE.TXT at the top of this package for the full license terms.
8
 */
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.HashSet;
16
import java.util.Map;
17
import java.util.Set;
18

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

  
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.common.CdmBase;
27
import eu.etaxonomy.cdm.model.name.BotanicalName;
28
import eu.etaxonomy.cdm.model.name.Rank;
29
import eu.etaxonomy.cdm.model.taxon.Synonym;
30
import eu.etaxonomy.cdm.model.taxon.Taxon;
31
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
32

  
33
/**
34
 *
35
 * @author pplitzner
36
 * @date Mar 1, 2016
37
 *
38
 */
39

  
40
@Component
41
@SuppressWarnings("serial")
42
public class RedListGefaesspflanzenImportTaxa extends DbImportBase<RedListGefaesspflanzenImportState, RedListGefaesspflanzenImportConfigurator> {
43

  
44
    private static final Logger logger = Logger.getLogger(RedListGefaesspflanzenImportTaxa.class);
45

  
46
    private static final String tableName = "Rote Liste Gefäßpflanzen";
47

  
48
    private static final String pluralString = "taxa";
49

  
50
    public RedListGefaesspflanzenImportTaxa() {
51
        super(tableName, pluralString);
52
    }
53

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

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

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

  
75
    @Override
76
    public boolean doPartition(ResultSetPartitioner partitioner, RedListGefaesspflanzenImportState state) {
77
        ResultSet rs = partitioner.getResultSet();
78
        Set<TaxonBase> taxaToSave = new HashSet<>();
79
        try {
80
            while (rs.next()){
81
                makeSingleTaxon(state, rs, taxaToSave);
82

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

  
88
        getTaxonService().saveOrUpdate(taxaToSave);
89
        return true;
90
    }
91

  
92
    private void makeSingleTaxon(RedListGefaesspflanzenImportState state, ResultSet rs, Set<TaxonBase> taxaToSave)
93
            throws SQLException {
94
        long id = rs.getLong("NAMNR");
95
        String gueltString = rs.getString("GUELT");
96

  
97
//        BotanicalName name = state.getRelatedObject(Namespace.NAME_NAMESPACE,String.valueOf(id), BotanicalName.class);
98
        BotanicalName name = BotanicalName.NewInstance(Rank.GENUS());
99
        name.setGenusOrUninomial(String.valueOf(id));
100

  
101
        TaxonBase taxonBase = null;
102
        if(gueltString.equals("1")){
103
            taxonBase = Taxon.NewInstance(name, null);
104
        }
105
        else if(gueltString.equals("x")){
106
            taxonBase = Synonym.NewInstance(name, null);
107
        }
108
        else if(gueltString.equals("b")){
109
            taxonBase = Synonym.NewInstance(name, null);
110
        }
111
        if(taxonBase==null){
112
            logger.error("NAMNR: "+id+" Taxon for name "+name+" could not be created.");
113
            return;
114
        }
115

  
116
        taxaToSave.add(taxonBase);
117

  
118
        //id
119
        ImportHelper.setOriginalSource(taxonBase, state.getTransactionalSourceReference(), id, Namespace.TAXON_NAMESPACE);
120
        state.getTaxonMap().put(id, taxonBase.getUuid());
121
    }
122

  
123
    @Override
124
    public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
125
            RedListGefaesspflanzenImportState state) {
126
        Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
127
//        Map<String, BotanicalName> nameMap = new HashMap<String, BotanicalName>();
128
//        try {
129
//            while (rs.next()){
130
//                long id = rs.getLong("NAMNR");
131
//                nameMap.put(String.valueOf(id), (BotanicalName) getNameService().load(state.getNameMap().get(id)));
132
//            }
133
//        } catch (SQLException e) {
134
//            e.printStackTrace();
135
//        }
136
//        result.put(Namespace.NAME_NAMESPACE, nameMap);
137

  
138
        return result;
139
    }
140

  
141
    @Override
142
    protected boolean doCheck(RedListGefaesspflanzenImportState state) {
143
        return false;
144
    }
145

  
146
    @Override
147
    protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
148
        return false;
149
    }
150

  
151
}

Also available in: Unified diff