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.faunaEuropaea;
|
11
|
|
12
|
import java.sql.ResultSet;
|
13
|
import java.sql.SQLException;
|
14
|
import java.util.Collection;
|
15
|
import java.util.HashMap;
|
16
|
import java.util.Iterator;
|
17
|
import java.util.Map;
|
18
|
import java.util.Map.Entry;
|
19
|
import java.util.UUID;
|
20
|
|
21
|
import org.apache.log4j.Logger;
|
22
|
import org.springframework.stereotype.Component;
|
23
|
import org.springframework.transaction.TransactionStatus;
|
24
|
|
25
|
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
|
26
|
import eu.etaxonomy.cdm.io.common.ImportHelper;
|
27
|
import eu.etaxonomy.cdm.io.common.Source;
|
28
|
import eu.etaxonomy.cdm.model.agent.Team;
|
29
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
30
|
import eu.etaxonomy.cdm.model.common.OriginalSourceBase;
|
31
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
32
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
33
|
|
34
|
|
35
|
/**
|
36
|
* @author a.babadshanjan
|
37
|
* @created 23.08.2010
|
38
|
* @version 1.0
|
39
|
*/
|
40
|
@Component
|
41
|
public class FaunaEuropaeaUsersImport extends FaunaEuropaeaImportBase {
|
42
|
private static final Logger logger = Logger.getLogger(FaunaEuropaeaUsersImport.class);
|
43
|
|
44
|
/* Interval for progress info message when retrieving taxa */
|
45
|
private int modCount = 10000;
|
46
|
|
47
|
/* (non-Javadoc)
|
48
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
49
|
*/
|
50
|
@Override
|
51
|
protected boolean doCheck(FaunaEuropaeaImportState state) {
|
52
|
boolean result = true;
|
53
|
FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
|
54
|
logger.warn("Checking for References not yet fully implemented");
|
55
|
result &= checkReferenceStatus(fauEuConfig);
|
56
|
|
57
|
return result;
|
58
|
}
|
59
|
|
60
|
private boolean checkReferenceStatus(FaunaEuropaeaImportConfigurator fauEuConfig) {
|
61
|
boolean result = true;
|
62
|
// try {
|
63
|
Source source = fauEuConfig.getSource();
|
64
|
String sqlStr = "";
|
65
|
// ResultSet rs = source.getResultSet(sqlStr);
|
66
|
return result;
|
67
|
// } catch (SQLException e) {
|
68
|
// e.printStackTrace();
|
69
|
// return false;
|
70
|
// }
|
71
|
}
|
72
|
|
73
|
/* (non-Javadoc)
|
74
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IImportConfigurator, eu.etaxonomy.cdm.api.application.CdmApplicationController, java.util.Map)
|
75
|
*/
|
76
|
@Override
|
77
|
protected void doInvoke(FaunaEuropaeaImportState state) {
|
78
|
|
79
|
TransactionStatus txStatus = null;
|
80
|
Map<Integer, Reference> references = null;
|
81
|
Map<String,TeamOrPersonBase> authors = null;
|
82
|
Map<Integer, UUID> referenceUuids = new HashMap<Integer, UUID>();
|
83
|
int limit = state.getConfig().getLimitSave();
|
84
|
|
85
|
FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
|
86
|
Source source = fauEuConfig.getSource();
|
87
|
|
88
|
String namespace = "Reference";
|
89
|
int i = 0;
|
90
|
|
91
|
String selectCountUsers =
|
92
|
" SELECT count(*) FROM Users";
|
93
|
|
94
|
String selectColumnsUsers =
|
95
|
" SELECT usr_id, usr_title, usr_firstname, usr_lastname, usr_createdat FROM Users";
|
96
|
|
97
|
int count;
|
98
|
if(logger.isInfoEnabled()) { logger.info("Start making References (Users)..."); }
|
99
|
|
100
|
try {
|
101
|
ResultSet rsRefs = source.getResultSet(selectCountUsers);
|
102
|
rsRefs.next();
|
103
|
count = rsRefs.getInt(1);
|
104
|
|
105
|
rsRefs = source.getResultSet(selectColumnsUsers);
|
106
|
|
107
|
if (logger.isInfoEnabled()) {
|
108
|
logger.info("Get all References...");
|
109
|
logger.info("Number of rows: " + count);
|
110
|
logger.info("Count Query: " + selectCountUsers);
|
111
|
logger.info("Select Query: " + selectColumnsUsers);
|
112
|
}
|
113
|
|
114
|
while (rsRefs.next()){
|
115
|
int refId = rsRefs.getInt("usr_id");
|
116
|
String refTitle = rsRefs.getString("usr_title");
|
117
|
String refFirstname = rsRefs.getString("usr_firstname");
|
118
|
String refLastname = rsRefs.getString("usr_lastname");
|
119
|
String createdDate = rsRefs.getString("usr_createdat");
|
120
|
|
121
|
// build author
|
122
|
String refAuthor = "";
|
123
|
if (refTitle != null) {
|
124
|
refAuthor = refTitle;
|
125
|
if (! refTitle.endsWith(".")) {
|
126
|
refAuthor += ".";
|
127
|
}
|
128
|
}
|
129
|
refAuthor += refTitle == null ? NullToEmpty(refFirstname) : " " + NullToEmpty(refFirstname);
|
130
|
if ((refTitle != null || refFirstname != null) && refLastname != null) {
|
131
|
refAuthor += " " + refLastname;
|
132
|
}
|
133
|
|
134
|
// build year
|
135
|
String year = null;
|
136
|
if (createdDate != null) {
|
137
|
year = createdDate.substring(0, createdDate.indexOf("-"));
|
138
|
}
|
139
|
|
140
|
if ((i++ % limit) == 0) {
|
141
|
|
142
|
txStatus = startTransaction();
|
143
|
references = new HashMap<Integer,Reference>(limit);
|
144
|
authors = new HashMap<String,TeamOrPersonBase>(limit);
|
145
|
|
146
|
if(logger.isInfoEnabled()) {
|
147
|
logger.info("i = " + i + " - Reference import transaction started");
|
148
|
}
|
149
|
}
|
150
|
|
151
|
Reference<?> reference = null;
|
152
|
TeamOrPersonBase<Team> author = null;
|
153
|
reference = ReferenceFactory.newGeneric();
|
154
|
|
155
|
reference.setTitle("" + refId); // This unique key is needed to get a hand on this Reference in PesiTaxonExport
|
156
|
reference.setDatePublished(ImportHelper.getDatePublished(year));
|
157
|
|
158
|
if (!authors.containsKey(refAuthor)) {
|
159
|
if (refAuthor == null) {
|
160
|
logger.warn("Reference author is null");
|
161
|
}
|
162
|
author = Team.NewInstance();
|
163
|
author.setTitleCache(refAuthor, true);
|
164
|
authors.put(refAuthor,author);
|
165
|
if (logger.isTraceEnabled()) {
|
166
|
logger.trace("Stored author (" + refAuthor + ")");
|
167
|
}
|
168
|
//}
|
169
|
|
170
|
} else {
|
171
|
author = authors.get(refAuthor);
|
172
|
if (logger.isDebugEnabled()) {
|
173
|
logger.debug("Not imported author with duplicated aut_id (" + refId +
|
174
|
") " + refAuthor);
|
175
|
}
|
176
|
}
|
177
|
|
178
|
// set protected titleCache
|
179
|
StringBuilder referenceTitleCache = new StringBuilder(author.getTitleCache() + ".");
|
180
|
if (year != null) {
|
181
|
referenceTitleCache.append(" " + year);
|
182
|
}
|
183
|
reference.setTitleCache(referenceTitleCache.toString(), true);
|
184
|
|
185
|
reference.setAuthorTeam(author);
|
186
|
|
187
|
ImportHelper.setOriginalSource(reference, fauEuConfig.getSourceReference(), refId, namespace);
|
188
|
ImportHelper.setOriginalSource(author, fauEuConfig.getSourceReference(), refId, namespace);
|
189
|
|
190
|
|
191
|
// Store reference
|
192
|
if (!references.containsKey(refId)) {
|
193
|
|
194
|
if (reference == null) {
|
195
|
logger.warn("Reference is null");
|
196
|
}
|
197
|
references.put(refId, reference);
|
198
|
if (logger.isTraceEnabled()) {
|
199
|
logger.trace("Stored reference (" + refAuthor + ")");
|
200
|
}
|
201
|
} else {
|
202
|
if (logger.isDebugEnabled()) {
|
203
|
logger.debug("Duplicated reference (" + refId + ", " + refAuthor + ")");
|
204
|
}
|
205
|
//continue;
|
206
|
}
|
207
|
|
208
|
if (((i % limit) == 0 && i > 1 ) || i == count) {
|
209
|
|
210
|
Map <UUID, Reference> referenceMap =getReferenceService().save(references.values());
|
211
|
logger.info("i = " + i + " - references saved");
|
212
|
|
213
|
Iterator<Entry<UUID, Reference>> it = referenceMap.entrySet().iterator();
|
214
|
while (it.hasNext()){
|
215
|
Reference ref = it.next().getValue();
|
216
|
int refID = Integer.valueOf(((OriginalSourceBase)ref.getSources().iterator().next()).getIdInSource());
|
217
|
UUID uuid = ref.getUuid();
|
218
|
referenceUuids.put(refID, uuid);
|
219
|
}
|
220
|
references= null;
|
221
|
getAgentService().save((Collection)authors.values());
|
222
|
|
223
|
authors = null;
|
224
|
commitTransaction(txStatus);
|
225
|
}
|
226
|
|
227
|
}
|
228
|
}catch(SQLException e) {
|
229
|
logger.error("SQLException:" + e);
|
230
|
state.setUnsuccessfull();
|
231
|
}
|
232
|
|
233
|
if(logger.isInfoEnabled()) { logger.info("End making References (Users) ..."); }
|
234
|
|
235
|
return;
|
236
|
}
|
237
|
|
238
|
/**
|
239
|
* Returns an empty string in case of a null string.
|
240
|
* This avoids having the string "null" when using StringBuilder.append(null);
|
241
|
* @param string
|
242
|
* @return
|
243
|
*/
|
244
|
private String NullToEmpty(String string) {
|
245
|
if (string == null) {
|
246
|
return "";
|
247
|
} else {
|
248
|
return string;
|
249
|
}
|
250
|
}
|
251
|
|
252
|
/* (non-Javadoc)
|
253
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
254
|
*/
|
255
|
protected boolean isIgnore(FaunaEuropaeaImportState state){
|
256
|
return (state.getConfig().getDoReferences() == IImportConfigurator.DO_REFERENCES.NONE);
|
257
|
}
|
258
|
|
259
|
}
|