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