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.pesi.erms;
|
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.io.common.IOValidator;
|
21
|
import eu.etaxonomy.cdm.io.common.Source;
|
22
|
import eu.etaxonomy.cdm.io.common.mapping.DbImportMapping;
|
23
|
import eu.etaxonomy.cdm.io.pesi.erms.validation.ErmsRankImportValidator;
|
24
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
25
|
import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;
|
26
|
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
|
27
|
import eu.etaxonomy.cdm.model.name.Rank;
|
28
|
import eu.etaxonomy.cdm.model.name.RankClass;
|
29
|
import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
|
30
|
|
31
|
/**
|
32
|
* @author a.mueller
|
33
|
* @since 01.03.2010
|
34
|
*/
|
35
|
@Component
|
36
|
public class ErmsImportRankMap extends ErmsImportBase<Rank>{
|
37
|
|
38
|
private static final long serialVersionUID = -3956071827341860580L;
|
39
|
private static final Logger logger = Logger.getLogger(ErmsImportRankMap.class);
|
40
|
|
41
|
private Map<Integer, Map<Integer,Rank>> rankMap;
|
42
|
|
43
|
/**
|
44
|
* @param pluralString
|
45
|
* @param dbTableName
|
46
|
*/
|
47
|
public ErmsImportRankMap() {
|
48
|
super(null, null, null);
|
49
|
}
|
50
|
|
51
|
|
52
|
@Override
|
53
|
public void invoke (ErmsImportState state){
|
54
|
rankMap = new HashMap<>();
|
55
|
Source source = state.getConfig().getSource() ;
|
56
|
String strSQL = " SELECT * FROM ranks ";
|
57
|
ResultSet rs = source.getResultSet(strSQL);
|
58
|
try {
|
59
|
while (rs.next()){
|
60
|
Integer kingdomId = rs.getInt("kingdom_id");
|
61
|
Integer rankId = rs.getInt("rank_id");
|
62
|
String rankName = rs.getString("rank_name");
|
63
|
NomenclaturalCode nc = ErmsTransformer.kingdomId2NomCode(kingdomId);
|
64
|
|
65
|
Map<Integer, Rank> kingdomMap = makeKingdomMap(rankMap, rankId);
|
66
|
rankName = rankName.replace("Forma", "Form").replace("Subforma", "Subform");
|
67
|
Rank rank = null;
|
68
|
@SuppressWarnings("unchecked")
|
69
|
OrderedTermVocabulary<Rank> voc = CdmBase.deproxy(Rank.GENUS().getVocabulary(), OrderedTermVocabulary.class);
|
70
|
if (nc == null && kingdomId == 1){
|
71
|
rank = Rank.DOMAIN();
|
72
|
}else{
|
73
|
try {
|
74
|
rank = Rank.getRankByEnglishName(rankName, nc, false);
|
75
|
} catch (UnknownCdmTypeException e) {
|
76
|
}
|
77
|
if (rank == null){
|
78
|
if (kingdomId == 2){
|
79
|
if (rankId == 85){
|
80
|
rank = getRank(state, ErmsTransformer.uuidRankSubterclass, "Subterclass", "Subterclass", null, voc, Rank.INFRACLASS(), RankClass.Suprageneric);
|
81
|
}else if (rankId == 122){
|
82
|
rank = getRank(state, ErmsTransformer.uuidRankParvorder, "Parvorder", "Parvorder", null, voc, Rank.INFRACLASS(), RankClass.Suprageneric);
|
83
|
}
|
84
|
}
|
85
|
|
86
|
if (kingdomId == 3){
|
87
|
if (rankId == 214){
|
88
|
rank = Rank.SPECIESAGGREGATE();
|
89
|
}else if (rankId == 216){
|
90
|
rank = getRank(state, ErmsTransformer.uuidRankCollectiveSpecies, "Collective Species", "Collective Species", "Coll. sp.", voc, Rank.INFRACLASS(), RankClass.Suprageneric);
|
91
|
}
|
92
|
}
|
93
|
}
|
94
|
if (rank == null){
|
95
|
logger.warn("Rank could not be defined: " + rankName + "; nomcode = " + nc + ", kingdom_id = " + kingdomId);
|
96
|
}
|
97
|
kingdomMap.put(kingdomId, rank);
|
98
|
}
|
99
|
}
|
100
|
} catch (SQLException e) {
|
101
|
logger.error(e.getMessage());
|
102
|
throw new RuntimeException(e);
|
103
|
}
|
104
|
state.setRankMap(rankMap);
|
105
|
return ; //state.getResult();
|
106
|
}
|
107
|
|
108
|
/**
|
109
|
* Retrieves or creates the kingdom map (mapping kingdom to rank for a defined rank_id) and
|
110
|
* adds it to the rank map.
|
111
|
* @param rankMap
|
112
|
* @param rankId
|
113
|
* @return
|
114
|
*/
|
115
|
private Map<Integer, Rank> makeKingdomMap(Map<Integer, Map<Integer, Rank>> rankMap, Integer rankId) {
|
116
|
Map<Integer, Rank> result = rankMap.get(rankId);
|
117
|
if (result == null){
|
118
|
result = new HashMap<>();
|
119
|
rankMap.put(rankId, result);
|
120
|
}
|
121
|
return result;
|
122
|
}
|
123
|
|
124
|
@Override
|
125
|
protected String getRecordQuery(ErmsImportConfigurator config) {
|
126
|
return null; // not needed
|
127
|
}
|
128
|
|
129
|
@Override
|
130
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, ErmsImportState state) {
|
131
|
return null; // not needed
|
132
|
}
|
133
|
|
134
|
@Override
|
135
|
protected DbImportMapping<?, ?> getMapping() {
|
136
|
return null; //not needed
|
137
|
}
|
138
|
|
139
|
@Override
|
140
|
protected boolean doCheck(ErmsImportState state) {
|
141
|
IOValidator<ErmsImportState> rankImport = new ErmsRankImportValidator();
|
142
|
return rankImport.validate(state);
|
143
|
}
|
144
|
|
145
|
@Override
|
146
|
protected boolean isIgnore(ErmsImportState state) {
|
147
|
return false; //should always be called
|
148
|
}
|
149
|
}
|