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.faunaEuropaea;
|
11
|
|
12
|
import static eu.etaxonomy.cdm.io.faunaEuropaea.FaunaEuropaeaTransformer.A_AUCT;
|
13
|
import static eu.etaxonomy.cdm.io.faunaEuropaea.FaunaEuropaeaTransformer.P_PARENTHESIS;
|
14
|
import static eu.etaxonomy.cdm.io.faunaEuropaea.FaunaEuropaeaTransformer.Q_NO_RESTRICTION;
|
15
|
import static eu.etaxonomy.cdm.io.faunaEuropaea.FaunaEuropaeaTransformer.R_GENUS;
|
16
|
import static eu.etaxonomy.cdm.io.faunaEuropaea.FaunaEuropaeaTransformer.R_SUBGENUS;
|
17
|
import static eu.etaxonomy.cdm.io.faunaEuropaea.FaunaEuropaeaTransformer.R_SPECIES;
|
18
|
import static eu.etaxonomy.cdm.io.faunaEuropaea.FaunaEuropaeaTransformer.R_SUBSPECIES;
|
19
|
import static eu.etaxonomy.cdm.io.faunaEuropaea.FaunaEuropaeaTransformer.T_STATUS_ACCEPTED;
|
20
|
import static eu.etaxonomy.cdm.io.faunaEuropaea.FaunaEuropaeaTransformer.T_STATUS_NOT_ACCEPTED;
|
21
|
|
22
|
import java.sql.ResultSet;
|
23
|
import java.sql.SQLException;
|
24
|
import java.util.Collection;
|
25
|
import java.util.HashMap;
|
26
|
import java.util.List;
|
27
|
import java.util.Map;
|
28
|
import java.util.Set;
|
29
|
import java.util.UUID;
|
30
|
|
31
|
import org.apache.log4j.Logger;
|
32
|
import org.springframework.stereotype.Component;
|
33
|
import org.springframework.transaction.TransactionStatus;
|
34
|
|
35
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
36
|
import eu.etaxonomy.cdm.io.berlinModel.CdmOneToManyMapper;
|
37
|
import eu.etaxonomy.cdm.io.berlinModel.CdmStringMapper;
|
38
|
import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportState;
|
39
|
import eu.etaxonomy.cdm.io.common.CdmAttributeMapperBase;
|
40
|
import eu.etaxonomy.cdm.io.common.CdmSingleAttributeMapperBase;
|
41
|
import eu.etaxonomy.cdm.io.common.ICdmIO;
|
42
|
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
|
43
|
import eu.etaxonomy.cdm.io.common.ImportHelper;
|
44
|
import eu.etaxonomy.cdm.io.common.MapWrapper;
|
45
|
import eu.etaxonomy.cdm.io.common.Source;
|
46
|
import eu.etaxonomy.cdm.io.tcsxml.in.TcsXmlImportState;
|
47
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
48
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
49
|
import eu.etaxonomy.cdm.model.common.ISourceable;
|
50
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
51
|
import eu.etaxonomy.cdm.model.common.OriginalSource;
|
52
|
import eu.etaxonomy.cdm.model.name.Rank;
|
53
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
54
|
import eu.etaxonomy.cdm.model.name.ZoologicalName;
|
55
|
import eu.etaxonomy.cdm.model.reference.Database;
|
56
|
import eu.etaxonomy.cdm.model.reference.Generic;
|
57
|
import eu.etaxonomy.cdm.model.reference.PublicationBase;
|
58
|
import eu.etaxonomy.cdm.model.reference.Publisher;
|
59
|
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
|
60
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
61
|
import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
|
62
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
63
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
64
|
import eu.etaxonomy.cdm.model.taxon.TaxonomicTree;
|
65
|
import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
|
66
|
|
67
|
|
68
|
/**
|
69
|
* @author a.babadshanjan
|
70
|
* @created 12.05.2009
|
71
|
* @version 1.0
|
72
|
*/
|
73
|
@Component
|
74
|
public class FaunaEuropaeaRelShipImport extends FaunaEuropaeaImportBase {
|
75
|
|
76
|
public static final String OS_NAMESPACE_TAXON = "Taxon";
|
77
|
private static final Logger logger = Logger.getLogger(FaunaEuropaeaRelShipImport.class);
|
78
|
|
79
|
/* (non-Javadoc)
|
80
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
81
|
*/
|
82
|
@Override
|
83
|
protected boolean doCheck(FaunaEuropaeaImportState state) {
|
84
|
boolean result = true;
|
85
|
FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
|
86
|
logger.warn("Checking for Taxa not yet fully implemented");
|
87
|
result &= checkTaxonStatus(fauEuConfig);
|
88
|
|
89
|
return result;
|
90
|
}
|
91
|
|
92
|
/* (non-Javadoc)
|
93
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
94
|
*/
|
95
|
protected boolean isIgnore(FaunaEuropaeaImportState state) {
|
96
|
return ! state.getConfig().isDoTaxa();
|
97
|
}
|
98
|
|
99
|
private boolean checkTaxonStatus(FaunaEuropaeaImportConfigurator fauEuConfig) {
|
100
|
boolean result = true;
|
101
|
// try {
|
102
|
Source source = fauEuConfig.getSource();
|
103
|
String sqlStr = "";
|
104
|
ResultSet rs = source.getResultSet(sqlStr);
|
105
|
return result;
|
106
|
// } catch (SQLException e) {
|
107
|
// e.printStackTrace();
|
108
|
// return false;
|
109
|
// }
|
110
|
}
|
111
|
|
112
|
|
113
|
/*
|
114
|
* Import with complete taxon store
|
115
|
*/
|
116
|
protected boolean doInvoke(FaunaEuropaeaImportState state) {
|
117
|
|
118
|
Map<String, MapWrapper<? extends CdmBase>> stores = state.getStores();
|
119
|
MapWrapper<TeamOrPersonBase> authorStore = (MapWrapper<TeamOrPersonBase>)stores.get(ICdmIO.TEAM_STORE);
|
120
|
authorStore.makeEmpty();
|
121
|
Map<Integer, FaunaEuropaeaTaxon> fauEuTaxonMap = state.getFauEuTaxonMap();
|
122
|
TransactionStatus txStatus = null;
|
123
|
boolean success = true;
|
124
|
|
125
|
if(logger.isInfoEnabled()) { logger.info("Start making taxa..."); }
|
126
|
|
127
|
if (state.getConfig().isUseTransactions()) {
|
128
|
txStatus = startTransaction();
|
129
|
}
|
130
|
|
131
|
success = processTaxa(state, fauEuTaxonMap);
|
132
|
success = saveTaxa(state, state.getHighestTaxonIndex(), state.getConfig().getLimitSave());
|
133
|
|
134
|
if (state.getConfig().isUseTransactions()) {
|
135
|
commitTransaction(txStatus);
|
136
|
}
|
137
|
|
138
|
logger.info("End making taxa...");
|
139
|
return success;
|
140
|
}
|
141
|
|
142
|
|
143
|
/*
|
144
|
* Processes taxa from complete taxon store
|
145
|
*/
|
146
|
private boolean processTaxa(FaunaEuropaeaImportState state,
|
147
|
Map<Integer, FaunaEuropaeaTaxon> fauEuTaxonMap) {
|
148
|
|
149
|
if(logger.isInfoEnabled()) { logger.info("Processing taxa second pass..."); }
|
150
|
|
151
|
Map<String, MapWrapper<? extends CdmBase>> stores = state.getStores();
|
152
|
MapWrapper<TaxonBase> taxonStore = (MapWrapper<TaxonBase>)stores.get(ICdmIO.TAXON_STORE);
|
153
|
FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
|
154
|
ReferenceBase<?> sourceRef = fauEuConfig.getSourceReference();
|
155
|
|
156
|
boolean success = true;
|
157
|
|
158
|
for (int id : fauEuTaxonMap.keySet())
|
159
|
//for (int id : taxonStore.keySet())
|
160
|
{
|
161
|
TaxonBase<?> taxonBase = taxonStore.get(id);
|
162
|
TaxonNameBase<?,?> taxonName = taxonBase.getName();
|
163
|
FaunaEuropaeaTaxon fauEuTaxon = fauEuTaxonMap.get(id);
|
164
|
|
165
|
if (logger.isDebugEnabled()) { logger.debug("Taxon # " + id); }
|
166
|
createRelationships(fauEuTaxon, taxonBase, taxonName, fauEuTaxonMap, state);
|
167
|
}
|
168
|
return success;
|
169
|
}
|
170
|
|
171
|
|
172
|
/** Creates relationships for existing taxon store in memory */
|
173
|
private boolean createRelationships(FaunaEuropaeaTaxon fauEuTaxon,
|
174
|
TaxonBase<?> taxonBase, TaxonNameBase<?,?> taxonName,
|
175
|
Map<Integer, FaunaEuropaeaTaxon> fauEuTaxonMap, FaunaEuropaeaImportState state) {
|
176
|
|
177
|
int parentId = fauEuTaxon.getParentId();
|
178
|
int taxonId = fauEuTaxon.getId();
|
179
|
Map<String, MapWrapper<? extends CdmBase>> stores = state.getStores();
|
180
|
MapWrapper<TaxonBase> taxonStore = (MapWrapper<TaxonBase>)stores.get(ICdmIO.TAXON_STORE);
|
181
|
ReferenceBase<?> sourceRef = state.getConfig().getSourceReference();
|
182
|
TaxonBase<?> parentTaxonBase = taxonStore.get(parentId);
|
183
|
Taxon parentTaxon = parentTaxonBase.deproxy(parentTaxonBase, Taxon.class);
|
184
|
// FaunaEuropaeaTaxon parent = fauEuTaxonMap.get(parentId);
|
185
|
boolean success = true;
|
186
|
|
187
|
if (!fauEuTaxon.isValid()) { // FauEu Synonym
|
188
|
|
189
|
if (fauEuTaxon.getAuthor() != null && fauEuTaxon.getAuthor().equals("A_AUCT_NAME")) {
|
190
|
try {
|
191
|
// add misapplied name relationship from this taxon to parent
|
192
|
Taxon taxon = taxonBase.deproxy(taxonBase, Taxon.class);
|
193
|
taxon.addMisappliedName(parentTaxon, sourceRef, null);
|
194
|
if (logger.isInfoEnabled()) {
|
195
|
logger.info("Misapplied name created " + taxon.getUuid());
|
196
|
}
|
197
|
|
198
|
} catch (Exception e) {
|
199
|
logger.error("Error creating misapplied name relationship for taxon (" +
|
200
|
parentId + ")");
|
201
|
}
|
202
|
}
|
203
|
|
204
|
else if((fauEuTaxon.getAuthor() == null)
|
205
|
|| (fauEuTaxon.getAuthor() != null && !fauEuTaxon.getAuthor().equals("A_AUCT_NAME"))) {
|
206
|
try {
|
207
|
// add this synonym as heterotypic synonym to parent
|
208
|
Synonym synonym = taxonBase.deproxy(taxonBase, Synonym.class);
|
209
|
parentTaxon.addSynonym(synonym, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF());
|
210
|
if (logger.isDebugEnabled()) {
|
211
|
logger.debug("Heterotypic synonym created " + synonym.getUuid());
|
212
|
}
|
213
|
|
214
|
} catch (Exception e) {
|
215
|
logger.error("Error creating heterotypic synonym for taxon (" + parentId + ")");
|
216
|
// e.printStackTrace();
|
217
|
}
|
218
|
}
|
219
|
|
220
|
} else if (fauEuTaxon.isValid()) { // FauEu Taxon
|
221
|
|
222
|
Taxon taxon = taxonBase.deproxy(taxonBase, Taxon.class);
|
223
|
|
224
|
try {
|
225
|
// add this taxon as child to parent
|
226
|
if (parentTaxon != null) {
|
227
|
makeTaxonomicallyIncluded(state, parentTaxon, taxon, sourceRef, null);
|
228
|
if (logger.isDebugEnabled()) {
|
229
|
logger.debug("Parent-child (" + parentId + "-" + taxonId +
|
230
|
") relationship created");
|
231
|
}
|
232
|
}
|
233
|
|
234
|
} catch (Exception e) {
|
235
|
logger.error("Error creating taxonomically included relationship Parent-child (" +
|
236
|
parentId + "-" + taxonId + ")");
|
237
|
}
|
238
|
}
|
239
|
|
240
|
return success;
|
241
|
}
|
242
|
|
243
|
|
244
|
private boolean makeTaxonomicallyIncluded(FaunaEuropaeaImportState state, Taxon toTaxon, Taxon fromTaxon, ReferenceBase citation, String microCitation){
|
245
|
boolean success = true;
|
246
|
ReferenceBase sec = toTaxon.getSec();
|
247
|
TaxonomicTree tree = state.getTree(sec);
|
248
|
if (tree == null){
|
249
|
tree = makeTree(state, sec);
|
250
|
}
|
251
|
success = tree.addParentChild(toTaxon, fromTaxon, citation, microCitation);
|
252
|
return success;
|
253
|
}
|
254
|
|
255
|
}
|