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.T_STATUS_ACCEPTED;
|
13
|
import static eu.etaxonomy.cdm.io.faunaEuropaea.FaunaEuropaeaTransformer.T_STATUS_NOT_ACCEPTED;
|
14
|
|
15
|
import java.sql.ResultSet;
|
16
|
import java.sql.SQLException;
|
17
|
import java.util.HashMap;
|
18
|
import java.util.Map;
|
19
|
import java.util.Set;
|
20
|
import java.util.UUID;
|
21
|
|
22
|
import org.apache.log4j.Logger;
|
23
|
import org.springframework.stereotype.Component;
|
24
|
import org.springframework.transaction.TransactionStatus;
|
25
|
|
26
|
import eu.etaxonomy.cdm.io.berlinModel.CdmOneToManyMapper;
|
27
|
import eu.etaxonomy.cdm.io.berlinModel.CdmStringMapper;
|
28
|
import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator;
|
29
|
import eu.etaxonomy.cdm.io.common.CdmAttributeMapperBase;
|
30
|
import eu.etaxonomy.cdm.io.common.CdmSingleAttributeMapperBase;
|
31
|
import eu.etaxonomy.cdm.io.common.ICdmIO;
|
32
|
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
|
33
|
import eu.etaxonomy.cdm.io.common.ImportHelper;
|
34
|
import eu.etaxonomy.cdm.io.common.MapWrapper;
|
35
|
import eu.etaxonomy.cdm.io.common.Source;
|
36
|
import eu.etaxonomy.cdm.model.agent.Team;
|
37
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
38
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
39
|
import eu.etaxonomy.cdm.model.description.Feature;
|
40
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
41
|
import eu.etaxonomy.cdm.model.description.TextData;
|
42
|
import eu.etaxonomy.cdm.model.name.Rank;
|
43
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
44
|
import eu.etaxonomy.cdm.model.name.ZoologicalName;
|
45
|
import eu.etaxonomy.cdm.model.reference.Generic;
|
46
|
import eu.etaxonomy.cdm.model.reference.PublicationBase;
|
47
|
import eu.etaxonomy.cdm.model.reference.Publisher;
|
48
|
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
|
49
|
import eu.etaxonomy.cdm.model.reference.StrictReferenceBase;
|
50
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
51
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
52
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
53
|
import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
|
54
|
|
55
|
|
56
|
/**
|
57
|
* @author a.babadshanjan
|
58
|
* @created 12.05.2009
|
59
|
* @version 1.0
|
60
|
*/
|
61
|
@Component
|
62
|
public class FaunaEuropaeaRefImport extends FaunaEuropaeaImportBase {
|
63
|
private static final Logger logger = Logger.getLogger(FaunaEuropaeaRefImport.class);
|
64
|
|
65
|
/* Interval for progress info message when retrieving taxa */
|
66
|
private int modCount = 10000;
|
67
|
|
68
|
|
69
|
/* (non-Javadoc)
|
70
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
71
|
*/
|
72
|
@Override
|
73
|
protected boolean doCheck(FaunaEuropaeaImportState state) {
|
74
|
boolean result = true;
|
75
|
FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
|
76
|
logger.warn("Checking for References not yet fully implemented");
|
77
|
result &= checkReferenceStatus(fauEuConfig);
|
78
|
|
79
|
return result;
|
80
|
}
|
81
|
|
82
|
private boolean checkReferenceStatus(FaunaEuropaeaImportConfigurator fauEuConfig) {
|
83
|
boolean result = true;
|
84
|
// try {
|
85
|
Source source = fauEuConfig.getSource();
|
86
|
String sqlStr = "";
|
87
|
ResultSet rs = source.getResultSet(sqlStr);
|
88
|
return result;
|
89
|
// } catch (SQLException e) {
|
90
|
// e.printStackTrace();
|
91
|
// return false;
|
92
|
// }
|
93
|
}
|
94
|
|
95
|
/* (non-Javadoc)
|
96
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IImportConfigurator, eu.etaxonomy.cdm.api.application.CdmApplicationController, java.util.Map)
|
97
|
*/
|
98
|
@Override
|
99
|
protected boolean doInvoke(FaunaEuropaeaImportState state) {
|
100
|
|
101
|
Map<String, MapWrapper<? extends CdmBase>> stores = state.getStores();
|
102
|
MapWrapper<TeamOrPersonBase> authorStore = (MapWrapper<TeamOrPersonBase>)stores.get(ICdmIO.TEAM_STORE);
|
103
|
MapWrapper<TaxonBase> taxonStore = (MapWrapper<TaxonBase>)stores.get(ICdmIO.TAXON_STORE);
|
104
|
MapWrapper<ReferenceBase> refStore = (MapWrapper<ReferenceBase>)stores.get(ICdmIO.REFERENCE_STORE);
|
105
|
TransactionStatus txStatus = null;
|
106
|
|
107
|
FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
|
108
|
Source source = fauEuConfig.getSource();
|
109
|
|
110
|
String namespace = "Reference";
|
111
|
boolean success = true;
|
112
|
|
113
|
if(logger.isInfoEnabled()) { logger.info("Start making References..."); }
|
114
|
|
115
|
try {
|
116
|
String strQuery =
|
117
|
" SELECT MAX(TAX_ID) AS TAX_ID FROM dbo.Taxon ";
|
118
|
|
119
|
ResultSet rs = source.getResultSet(strQuery);
|
120
|
while (rs.next()) {
|
121
|
int maxTaxonId = rs.getInt("TAX_ID");
|
122
|
// highestTaxonIndex = maxTaxonId;
|
123
|
}
|
124
|
|
125
|
strQuery =
|
126
|
" SELECT Reference.*, TaxRefs.* " +
|
127
|
" FROM Reference INNER JOIN TaxRefs ON Reference.ref_id = TaxRefs.trf_ref_id " +
|
128
|
" WHERE (1=1)" +
|
129
|
" ORDER BY TaxRefs.trf_tax_id";
|
130
|
|
131
|
rs = source.getResultSet(strQuery) ;
|
132
|
|
133
|
int i = 0;
|
134
|
while (rs.next()) {
|
135
|
|
136
|
if ((i++ % modCount) == 0 && i!= 1 ) {
|
137
|
if(logger.isInfoEnabled()) {
|
138
|
logger.info("References handled: " + (i-1));
|
139
|
}
|
140
|
}
|
141
|
|
142
|
int taxonId = rs.getInt("trf_tax_id");
|
143
|
int refId = rs.getInt("ref_id");
|
144
|
String refAuthor = rs.getString("ref_author");
|
145
|
String year = rs.getString("ref_year");
|
146
|
String title = rs.getString("ref_title");
|
147
|
String refSource = rs.getString("ref_source");
|
148
|
String page = rs.getString("trf_page");
|
149
|
|
150
|
StrictReferenceBase<?> reference = null;
|
151
|
TeamOrPersonBase<Team> author = null;
|
152
|
|
153
|
try {
|
154
|
reference = Generic.NewInstance();
|
155
|
reference.setTitleCache(title);
|
156
|
reference.setDatePublished(ImportHelper.getDatePublished(year));
|
157
|
author = Team.NewInstance();
|
158
|
author.setTitleCache(refAuthor);
|
159
|
|
160
|
ImportHelper.setOriginalSource(reference, fauEuConfig.getSourceReference(), refId, namespace);
|
161
|
ImportHelper.setOriginalSource(author, fauEuConfig.getSourceReference(), refId, namespace);
|
162
|
|
163
|
// Create references
|
164
|
|
165
|
if (!refStore.containsId(refId)) {
|
166
|
if (reference == null) {
|
167
|
logger.warn("Reference is null");
|
168
|
}
|
169
|
refStore.put(refId, reference);
|
170
|
if (logger.isDebugEnabled()) {
|
171
|
logger.debug("Stored reference (" + refId + ") " + refAuthor);
|
172
|
}
|
173
|
} else {
|
174
|
if (logger.isDebugEnabled()) {
|
175
|
logger.debug("Not imported reference with duplicated ref_id (" + refId +
|
176
|
") " + refAuthor);
|
177
|
}
|
178
|
continue;
|
179
|
}
|
180
|
|
181
|
// Create authors
|
182
|
|
183
|
if (!authorStore.containsId(refId)) { // TODO: Don't insert identical author names
|
184
|
if (refAuthor == null) {
|
185
|
logger.warn("Reference author is null");
|
186
|
}
|
187
|
authorStore.put(refId, author);
|
188
|
if (logger.isDebugEnabled()) {
|
189
|
logger.debug("Stored author (" + refId + ") " + refAuthor);
|
190
|
}
|
191
|
} else {
|
192
|
if (logger.isDebugEnabled()) {
|
193
|
logger.debug("Not imported author with duplicated aut_id (" + refId +
|
194
|
") " + refAuthor);
|
195
|
}
|
196
|
}
|
197
|
|
198
|
} catch (Exception e) {
|
199
|
logger.warn("An exception occurred when creating reference with id " + refId +
|
200
|
". Reference could not be saved.");
|
201
|
}
|
202
|
|
203
|
try {
|
204
|
// Create descriptions
|
205
|
|
206
|
Taxon taxon;
|
207
|
TaxonBase taxonBase = taxonStore.get(taxonId);
|
208
|
if (taxonBase == null) { continue; }
|
209
|
boolean isSynonym = taxonBase.isInstanceOf(Synonym.class);
|
210
|
if (isSynonym){
|
211
|
Synonym syn = CdmBase.deproxy(taxonBase, Synonym.class);
|
212
|
taxon = syn.getAcceptedTaxa().iterator().next();
|
213
|
}else{
|
214
|
taxon = CdmBase.deproxy(taxonBase, Taxon.class);
|
215
|
}
|
216
|
|
217
|
TaxonDescription taxonDescription = null;
|
218
|
Set<TaxonDescription> descriptions = taxon.getDescriptions();
|
219
|
if (descriptions.size() > 0) {
|
220
|
taxonDescription = descriptions.iterator().next();
|
221
|
} else {
|
222
|
taxonDescription = TaxonDescription.NewInstance();
|
223
|
taxon.addDescription(taxonDescription);
|
224
|
}
|
225
|
|
226
|
TextData textData = TextData.NewInstance(Feature.CITATION());
|
227
|
if (isSynonym){
|
228
|
Synonym syn = CdmBase.deproxy(taxonBase, Synonym.class);
|
229
|
textData.setNameUsedInReference(syn.getName());
|
230
|
}
|
231
|
textData.setCitation(reference);
|
232
|
textData.setCitationMicroReference(page);
|
233
|
taxonDescription.addElement(textData);
|
234
|
|
235
|
} catch (Exception e) {
|
236
|
logger.warn("An exception occurred when creating description for reference " + refId +
|
237
|
". Taxon description could not be saved.");
|
238
|
e.printStackTrace();
|
239
|
}
|
240
|
|
241
|
}
|
242
|
|
243
|
if(logger.isInfoEnabled()) { logger.info("Saving references ..."); }
|
244
|
|
245
|
if (state.getConfig().isUseTransactions()) {
|
246
|
txStatus = startTransaction();
|
247
|
}
|
248
|
|
249
|
// save taxa, references, and authors
|
250
|
success = saveTaxa(state, state.getHighestTaxonIndex(), state.getConfig().getLimitSave());
|
251
|
getReferenceService().saveReferenceAll(refStore.objects());
|
252
|
getAgentService().saveAgentAll(authorStore.objects());
|
253
|
|
254
|
if (state.getConfig().isUseTransactions()) {
|
255
|
commitTransaction(txStatus);
|
256
|
}
|
257
|
|
258
|
if(logger.isInfoEnabled()) { logger.info("End making references ..."); }
|
259
|
|
260
|
} catch (SQLException e) {
|
261
|
logger.error("SQLException:" + e);
|
262
|
success = false;
|
263
|
}
|
264
|
return success;
|
265
|
}
|
266
|
|
267
|
|
268
|
/* (non-Javadoc)
|
269
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
270
|
*/
|
271
|
protected boolean isIgnore(FaunaEuropaeaImportState state){
|
272
|
return (state.getConfig().getDoReferences() == IImportConfigurator.DO_REFERENCES.NONE);
|
273
|
}
|
274
|
|
275
|
}
|