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.UUID;
|
20
|
|
21
|
import org.apache.log4j.Logger;
|
22
|
import org.springframework.stereotype.Component;
|
23
|
|
24
|
import eu.etaxonomy.cdm.io.berlinModel.CdmOneToManyMapper;
|
25
|
import eu.etaxonomy.cdm.io.berlinModel.CdmStringMapper;
|
26
|
import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator;
|
27
|
import eu.etaxonomy.cdm.io.common.CdmAttributeMapperBase;
|
28
|
import eu.etaxonomy.cdm.io.common.CdmSingleAttributeMapperBase;
|
29
|
import eu.etaxonomy.cdm.io.common.ICdmIO;
|
30
|
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
|
31
|
import eu.etaxonomy.cdm.io.common.ImportHelper;
|
32
|
import eu.etaxonomy.cdm.io.common.MapWrapper;
|
33
|
import eu.etaxonomy.cdm.io.common.Source;
|
34
|
import eu.etaxonomy.cdm.model.agent.Team;
|
35
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
36
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
37
|
import eu.etaxonomy.cdm.model.name.Rank;
|
38
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
39
|
import eu.etaxonomy.cdm.model.name.ZoologicalName;
|
40
|
import eu.etaxonomy.cdm.model.reference.Generic;
|
41
|
import eu.etaxonomy.cdm.model.reference.PublicationBase;
|
42
|
import eu.etaxonomy.cdm.model.reference.Publisher;
|
43
|
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
|
44
|
import eu.etaxonomy.cdm.model.reference.StrictReferenceBase;
|
45
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
46
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
47
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
48
|
import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
|
49
|
|
50
|
|
51
|
/**
|
52
|
* @author a.babadshanjan
|
53
|
* @created 12.05.2009
|
54
|
* @version 1.0
|
55
|
*/
|
56
|
@Component
|
57
|
public class FaunaEuropaeaReferenceImport extends FaunaEuropaeaImportBase {
|
58
|
private static final Logger logger = Logger.getLogger(FaunaEuropaeaReferenceImport.class);
|
59
|
|
60
|
private int modCount = 10000;
|
61
|
/* Max number of references to be saved with one service call */
|
62
|
private int limit = 20000; // TODO: Make configurable
|
63
|
|
64
|
|
65
|
/* (non-Javadoc)
|
66
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
67
|
*/
|
68
|
@Override
|
69
|
protected boolean doCheck(FaunaEuropaeaImportState state) {
|
70
|
boolean result = true;
|
71
|
FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
|
72
|
logger.warn("Checking for Taxa not yet fully implemented");
|
73
|
result &= checkReferenceStatus(fauEuConfig);
|
74
|
|
75
|
return result;
|
76
|
}
|
77
|
|
78
|
private boolean checkReferenceStatus(FaunaEuropaeaImportConfigurator fauEuConfig) {
|
79
|
boolean result = true;
|
80
|
// try {
|
81
|
Source source = fauEuConfig.getSource();
|
82
|
String sqlStr = "";
|
83
|
ResultSet rs = source.getResultSet(sqlStr);
|
84
|
return result;
|
85
|
// } catch (SQLException e) {
|
86
|
// e.printStackTrace();
|
87
|
// return false;
|
88
|
// }
|
89
|
}
|
90
|
|
91
|
/* (non-Javadoc)
|
92
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IImportConfigurator, eu.etaxonomy.cdm.api.application.CdmApplicationController, java.util.Map)
|
93
|
*/
|
94
|
@Override
|
95
|
protected boolean doInvoke(FaunaEuropaeaImportState state) {
|
96
|
|
97
|
Map<String, MapWrapper<? extends CdmBase>> stores = state.getStores();
|
98
|
MapWrapper<TeamOrPersonBase> authorStore = (MapWrapper<TeamOrPersonBase>)stores.get(ICdmIO.TEAM_STORE);
|
99
|
authorStore.makeEmpty();
|
100
|
MapWrapper<ReferenceBase> refStore = (MapWrapper<ReferenceBase>)stores.get(ICdmIO.REFERENCE_STORE);
|
101
|
|
102
|
FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
|
103
|
Source source = fauEuConfig.getSource();
|
104
|
|
105
|
String namespace = "Reference";
|
106
|
boolean success = true;
|
107
|
|
108
|
if(logger.isInfoEnabled()) { logger.info("Start making References..."); }
|
109
|
|
110
|
try {
|
111
|
String strQuery =
|
112
|
" SELECT Reference.*, TaxRefs.* " +
|
113
|
" FROM Reference INNER JOIN TaxRefs ON Reference.ref_id = TaxRefs.trf_ref_id " +
|
114
|
" WHERE (1=1)";
|
115
|
|
116
|
// String strQuery =
|
117
|
// " SELECT Reference.*, TaxRefs.*, author.aut_id " +
|
118
|
// " FROM Reference INNER JOIN TaxRefs ON Reference.ref_id = TaxRefs.trf_ref_id " +
|
119
|
// " INNER JOIN author ON Reference.ref_author = author.aut_name" +
|
120
|
// " WHERE (1=1)";
|
121
|
|
122
|
ResultSet rs = source.getResultSet(strQuery) ;
|
123
|
|
124
|
int i = 0;
|
125
|
while (rs.next()) {
|
126
|
|
127
|
if ((i++ % modCount) == 0 && i!= 1 ) {
|
128
|
if(logger.isInfoEnabled()) {
|
129
|
logger.info("References handled: " + (i-1));
|
130
|
}
|
131
|
}
|
132
|
|
133
|
int refId = rs.getInt("ref_id");
|
134
|
String refAuthor = rs.getString("ref_author");
|
135
|
String year = rs.getString("ref_year");
|
136
|
String title = rs.getString("ref_title");
|
137
|
String refSource = rs.getString("ref_source");
|
138
|
|
139
|
StrictReferenceBase<?> reference = null;
|
140
|
TeamOrPersonBase<Team> author = null;
|
141
|
|
142
|
try {
|
143
|
reference = Generic.NewInstance();
|
144
|
reference.setTitleCache(title);
|
145
|
reference.setDatePublished(ImportHelper.getDatePublished(year));
|
146
|
author = Team.NewInstance();
|
147
|
author.setTitleCache(refAuthor);
|
148
|
|
149
|
// FIXME: author.aut_name and Reference.ref_author don't match
|
150
|
// if (authorStore != null) {
|
151
|
// TeamOrPersonBase<?> author = authorStore.get(authorId);
|
152
|
// if (author != null) {
|
153
|
// reference.setAuthorTeam(author);
|
154
|
// }
|
155
|
// }
|
156
|
|
157
|
ImportHelper.setOriginalSource(reference, fauEuConfig.getSourceReference(), refId, namespace);
|
158
|
ImportHelper.setOriginalSource(author, fauEuConfig.getSourceReference(), refId, namespace);
|
159
|
|
160
|
// Create reference
|
161
|
|
162
|
if (!refStore.containsId(refId)) {
|
163
|
if (reference == null) {
|
164
|
logger.warn("Reference is null");
|
165
|
}
|
166
|
refStore.put(refId, reference);
|
167
|
if (logger.isDebugEnabled()) {
|
168
|
logger.debug("Stored reference (" + refId + ") " + refAuthor);
|
169
|
}
|
170
|
} else {
|
171
|
logger.warn("Not imported reference with duplicated ref_id (" + refId +
|
172
|
") " + refAuthor);
|
173
|
}
|
174
|
|
175
|
// Create authors
|
176
|
|
177
|
if (!authorStore.containsId(refId)) {
|
178
|
if (refAuthor == null) {
|
179
|
logger.warn("Reference author is null");
|
180
|
}
|
181
|
authorStore.put(refId, author);
|
182
|
if (logger.isDebugEnabled()) {
|
183
|
logger.debug("Stored author (" + refId + ") " + refAuthor);
|
184
|
}
|
185
|
} else {
|
186
|
logger.warn("Not imported author with duplicated aut_id (" + refId +
|
187
|
") " + refAuthor);
|
188
|
}
|
189
|
|
190
|
|
191
|
} catch (Exception e) {
|
192
|
logger.warn("An exception occurred when creating reference with id " + refId +
|
193
|
". Reference could not be saved.");
|
194
|
}
|
195
|
}
|
196
|
|
197
|
if(logger.isInfoEnabled()) { logger.info("Saving references ..."); }
|
198
|
|
199
|
// save authors and references
|
200
|
getReferenceService().saveReferenceAll(refStore.objects());
|
201
|
getAgentService().saveAgentAll(authorStore.objects());
|
202
|
|
203
|
if(logger.isInfoEnabled()) { logger.info("End making references ..."); }
|
204
|
|
205
|
} catch (SQLException e) {
|
206
|
logger.error("SQLException:" + e);
|
207
|
success = false;
|
208
|
}
|
209
|
return success;
|
210
|
}
|
211
|
|
212
|
|
213
|
/* (non-Javadoc)
|
214
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
215
|
*/
|
216
|
protected boolean isIgnore(FaunaEuropaeaImportState state){
|
217
|
return (state.getConfig().getDoReferences() == IImportConfigurator.DO_REFERENCES.NONE);
|
218
|
}
|
219
|
|
220
|
}
|