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.net.URI;
|
13
|
import java.sql.ResultSet;
|
14
|
import java.sql.SQLException;
|
15
|
import java.util.Collection;
|
16
|
import java.util.HashMap;
|
17
|
import java.util.HashSet;
|
18
|
import java.util.Iterator;
|
19
|
import java.util.Map;
|
20
|
import java.util.Map.Entry;
|
21
|
import java.util.UUID;
|
22
|
|
23
|
import org.apache.commons.lang.StringUtils;
|
24
|
import org.apache.log4j.Logger;
|
25
|
import org.springframework.stereotype.Component;
|
26
|
import org.springframework.transaction.TransactionStatus;
|
27
|
import org.springframework.transaction.support.DefaultTransactionDefinition;
|
28
|
|
29
|
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
|
30
|
import eu.etaxonomy.cdm.io.common.ImportHelper;
|
31
|
import eu.etaxonomy.cdm.io.common.Source;
|
32
|
import eu.etaxonomy.cdm.model.agent.AgentBase;
|
33
|
import eu.etaxonomy.cdm.model.agent.Contact;
|
34
|
import eu.etaxonomy.cdm.model.agent.Institution;
|
35
|
import eu.etaxonomy.cdm.model.agent.Person;
|
36
|
import eu.etaxonomy.cdm.model.common.OriginalSourceBase;
|
37
|
import eu.etaxonomy.cdm.model.common.User;
|
38
|
|
39
|
|
40
|
/**
|
41
|
* @author a.babadshanjan
|
42
|
* @created 23.08.2010
|
43
|
*/
|
44
|
@Component
|
45
|
public class FaunaEuropaeaUsersImport extends FaunaEuropaeaImportBase {
|
46
|
private static final Logger logger = Logger.getLogger(FaunaEuropaeaUsersImport.class);
|
47
|
|
48
|
/* Interval for progress info message when retrieving taxa */
|
49
|
private final int modCount = 10000;
|
50
|
|
51
|
protected DefaultTransactionDefinition txDefinition = new DefaultTransactionDefinition();
|
52
|
|
53
|
/* (non-Javadoc)
|
54
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
55
|
*/
|
56
|
@Override
|
57
|
protected boolean doCheck(FaunaEuropaeaImportState state) {
|
58
|
boolean result = true;
|
59
|
FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
|
60
|
logger.warn("Checking for References not yet fully implemented");
|
61
|
result &= checkReferenceStatus(fauEuConfig);
|
62
|
|
63
|
return result;
|
64
|
}
|
65
|
|
66
|
private boolean checkReferenceStatus(FaunaEuropaeaImportConfigurator fauEuConfig) {
|
67
|
boolean result = true;
|
68
|
// try {
|
69
|
Source source = fauEuConfig.getSource();
|
70
|
String sqlStr = "";
|
71
|
// ResultSet rs = source.getResultSet(sqlStr);
|
72
|
return result;
|
73
|
// } catch (SQLException e) {
|
74
|
// e.printStackTrace();
|
75
|
// return false;
|
76
|
// }
|
77
|
}
|
78
|
|
79
|
/* (non-Javadoc)
|
80
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IImportConfigurator, eu.etaxonomy.cdm.api.application.CdmApplicationController, java.util.Map)
|
81
|
*/
|
82
|
@Override
|
83
|
protected void doInvoke(FaunaEuropaeaImportState state) {
|
84
|
/*
|
85
|
logger.warn("Start User doInvoke");
|
86
|
ProfilerController.memorySnapshot();
|
87
|
*/
|
88
|
|
89
|
if (!state.getConfig().isDoAuthors()){
|
90
|
return;
|
91
|
}
|
92
|
TransactionStatus txStatus = null;
|
93
|
Map<String, Person> persons = null;
|
94
|
Map<String, User> users= null;
|
95
|
|
96
|
Map<Integer, UUID> userUuids = new HashMap<Integer, UUID>();
|
97
|
Map<Integer, Institution> institiutions= new HashMap<Integer, Institution>();
|
98
|
Collection<Institution> institutionsToSave = new HashSet<Institution>();
|
99
|
int limit = state.getConfig().getLimitSave();
|
100
|
//this.authenticate("admin", "00000");
|
101
|
|
102
|
FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
|
103
|
Source source = fauEuConfig.getSource();
|
104
|
|
105
|
String namespace = "User";
|
106
|
int i = 0;
|
107
|
|
108
|
String selectCountUsers =
|
109
|
" SELECT count(*) FROM Users";
|
110
|
|
111
|
String selectColumnsUsers =
|
112
|
" SELECT u.usr_id as userId, u.usr_title as title, u.usr_firstname as firstname, u.usr_lastname as lastname, u.usr_createdat as created, u.usr_password as password, u.usr_cou_id as country, u.usr_email as mail, "
|
113
|
+ "u.usr_homepage as homepage, u.usr_description as description, u.usr_active as active, o.org_name as organisationName, o.org_homepage as organisationHomepage, o.org_id as institutionId FROM Users u LEFT JOIN Organisation o ON o.org_id= u.usr_org_id";
|
114
|
|
115
|
int count;
|
116
|
if(logger.isInfoEnabled()) { logger.info("Start making References (Users)..."); }
|
117
|
|
118
|
try {
|
119
|
ResultSet rsUser = source.getResultSet(selectCountUsers);
|
120
|
rsUser.next();
|
121
|
count = rsUser.getInt(1);
|
122
|
|
123
|
rsUser= source.getResultSet(selectColumnsUsers);
|
124
|
|
125
|
if (logger.isInfoEnabled()) {
|
126
|
logger.info("Get all References...");
|
127
|
logger.info("Number of rows: " + count);
|
128
|
logger.info("Count Query: " + selectCountUsers);
|
129
|
logger.info("Select Query: " + selectColumnsUsers);
|
130
|
}
|
131
|
|
132
|
while (rsUser.next()){//usr_country_id, usr_org_id, usr_email, usr_homepage, usr_groupname, usr_groupicon, usr_groupnote, usr_description, usr_active)
|
133
|
int refId = rsUser.getInt("userId");
|
134
|
String userTitle = rsUser.getString("title");
|
135
|
String userFirstname = rsUser.getString("firstname");
|
136
|
String userLastname = rsUser.getString("lastname");
|
137
|
String createdDate = rsUser.getString("created");
|
138
|
String userPwd = rsUser.getString("password");
|
139
|
String userCountry = rsUser.getString("country");
|
140
|
|
141
|
String userMail = rsUser.getString("mail");
|
142
|
String userHomepage = rsUser.getString("homepage");
|
143
|
//String userGroupName = rsUser.getString("u.usr_groupname");
|
144
|
String userDescription = rsUser.getString("description");
|
145
|
int userActive = rsUser.getInt("active");
|
146
|
String orgName = rsUser.getString("organisationName");
|
147
|
String orgHomepage = rsUser.getString("organisationHomepage");
|
148
|
int institutionId = rsUser.getInt("institutionId");
|
149
|
|
150
|
// build person
|
151
|
String userPerson = "";
|
152
|
if (userTitle != null) {
|
153
|
userPerson = userTitle;
|
154
|
if (! userTitle.endsWith(".")) {
|
155
|
userPerson += ".";
|
156
|
}
|
157
|
}
|
158
|
userPerson += userTitle == null ? NullToEmpty(userFirstname) : " " + NullToEmpty(userFirstname);
|
159
|
if ((userTitle != null || userFirstname != null) && userLastname != null) {
|
160
|
userPerson += " " + userLastname;
|
161
|
}
|
162
|
this.authenticate("admin", "00000");
|
163
|
// build year
|
164
|
String year = null;
|
165
|
if (createdDate != null) {
|
166
|
year = createdDate.substring(0, createdDate.indexOf("-"));
|
167
|
}
|
168
|
|
169
|
if ((i++ % limit) == 0) {
|
170
|
|
171
|
txStatus = startTransaction();
|
172
|
persons= new HashMap<String,Person>(limit);
|
173
|
users = new HashMap<String,User>(limit);
|
174
|
|
175
|
|
176
|
if(logger.isInfoEnabled()) {
|
177
|
logger.info("i = " + i + " - User import transaction started");
|
178
|
}
|
179
|
}
|
180
|
|
181
|
Person person = null;
|
182
|
User user = null;
|
183
|
|
184
|
person= Person.NewTitledInstance(userPerson);
|
185
|
|
186
|
person.addEmailAddress(userMail);
|
187
|
try{
|
188
|
if (!StringUtils.isBlank(userHomepage)){
|
189
|
person.addUrl(URI.create(userHomepage));
|
190
|
}
|
191
|
}catch(IllegalArgumentException e){
|
192
|
logger.debug(e.getMessage());
|
193
|
}
|
194
|
if (institutionId != 1){//1 = private
|
195
|
Institution institution ;
|
196
|
if (!institiutions.containsKey(institutionId)){
|
197
|
institution = Institution.NewInstance();
|
198
|
institution.setName(orgName);
|
199
|
Contact contact = Contact.NewInstance();
|
200
|
try{
|
201
|
if (!StringUtils.isBlank(orgHomepage)){
|
202
|
contact.addUrl(URI.create(orgHomepage));
|
203
|
}
|
204
|
}catch(IllegalArgumentException e){
|
205
|
logger.debug(e.getMessage());
|
206
|
}
|
207
|
institution.setContact(contact);
|
208
|
institutionsToSave.add(institution);
|
209
|
} else {
|
210
|
institution = institiutions.get(institutionId);
|
211
|
}
|
212
|
|
213
|
|
214
|
person.addInstitutionalMembership(institution, null, null, null);
|
215
|
}
|
216
|
user = User.NewInstance(userPerson, userPwd);
|
217
|
user.setPerson(person);
|
218
|
if (userActive == FaunaEuropaeaTransformer.U_ACTIVE){
|
219
|
user.setAccountNonLocked(false);
|
220
|
} else{
|
221
|
user.setAccountNonLocked(true);
|
222
|
}
|
223
|
|
224
|
if (!persons.containsKey(userPerson)) {
|
225
|
if (userPerson == null) {
|
226
|
logger.warn("User is null");
|
227
|
}
|
228
|
|
229
|
persons.put(userPerson, person);
|
230
|
if (logger.isTraceEnabled()) {
|
231
|
logger.trace("Stored user (" + userPerson + ")");
|
232
|
}
|
233
|
//}
|
234
|
|
235
|
} else {
|
236
|
person = persons.get(userPerson);
|
237
|
if (logger.isDebugEnabled()) {
|
238
|
logger.debug("Not imported user with duplicated ref_id (" + refId +
|
239
|
") " + userPerson);
|
240
|
}
|
241
|
}
|
242
|
|
243
|
// set protected titleCache
|
244
|
StringBuilder referenceTitleCache = new StringBuilder(person.getTitleCache() + ".");
|
245
|
if (year != null) {
|
246
|
referenceTitleCache.append(" " + year);
|
247
|
}
|
248
|
|
249
|
|
250
|
//ImportHelper.setOriginalSource(user, fauEuConfig.getSourceReference(), userId, namespace);
|
251
|
ImportHelper.setOriginalSource(person, fauEuConfig.getSourceReference(), refId, namespace);
|
252
|
|
253
|
|
254
|
// Store persons
|
255
|
if (!users.containsKey(userPerson.toLowerCase())) {
|
256
|
|
257
|
if (user == null) {
|
258
|
logger.warn("User is null");
|
259
|
}
|
260
|
users.put(userPerson.toLowerCase(), user);
|
261
|
if (logger.isTraceEnabled()) {
|
262
|
logger.trace("Stored user (" + userPerson + ")");
|
263
|
}
|
264
|
} else {
|
265
|
//if (logger.isDebugEnabled()) {
|
266
|
logger.info("Duplicated user(" + userPerson +")");
|
267
|
//}
|
268
|
//continue;
|
269
|
}
|
270
|
|
271
|
if (((i % limit) == 0 && i > 1 ) || i == count ) {
|
272
|
|
273
|
commitUsers(txStatus, persons, users,
|
274
|
state.getAgentMap(),institutionsToSave, i);
|
275
|
|
276
|
users = null;
|
277
|
persons= null;
|
278
|
}
|
279
|
|
280
|
}
|
281
|
if (users != null){
|
282
|
commitUsers(txStatus, persons, users, state.getAgentMap(), institutionsToSave,i);
|
283
|
users = null;
|
284
|
persons= null;
|
285
|
}
|
286
|
}catch(SQLException e) {
|
287
|
logger.error("SQLException:" + e);
|
288
|
state.setUnsuccessfull();
|
289
|
}
|
290
|
/*
|
291
|
logger.warn("End User doInvoke");
|
292
|
ProfilerController.memorySnapshot();
|
293
|
*/
|
294
|
if(logger.isInfoEnabled()) { logger.info("End making References (Users) ..."); }
|
295
|
|
296
|
return;
|
297
|
}
|
298
|
|
299
|
private void commitUsers(TransactionStatus txStatus,
|
300
|
Map<String, Person> persons,
|
301
|
Map<String, User> users,
|
302
|
Map<Integer, UUID> agentsUUID,
|
303
|
Collection<Institution> institutionsToSave,
|
304
|
int i) {
|
305
|
|
306
|
Map<UUID, AgentBase> instMap = getAgentService().save((Collection)institutionsToSave);
|
307
|
Map<UUID, AgentBase> userMap = getAgentService().save((Collection)persons.values());
|
308
|
logger.info("i = " + i + " - persons saved");
|
309
|
|
310
|
|
311
|
|
312
|
Iterator<Entry<UUID, AgentBase>> it = userMap.entrySet().iterator();
|
313
|
while (it.hasNext()){
|
314
|
AgentBase person = it.next().getValue();
|
315
|
int userID = Integer.valueOf(((OriginalSourceBase)person.getSources().iterator().next()).getIdInSource());
|
316
|
UUID uuid = person.getUuid();
|
317
|
agentsUUID.put(userID, uuid);
|
318
|
}
|
319
|
|
320
|
getUserService().save(users.values());
|
321
|
logger.info("i = " + users.size() + " - users saved");
|
322
|
//getReferenceService().save(references.values());
|
323
|
//logger.info("i = " +references.size() + " - references saved");
|
324
|
commitTransaction(txStatus);
|
325
|
}
|
326
|
|
327
|
/**
|
328
|
* Returns an empty string in case of a null string.
|
329
|
* This avoids having the string "null" when using StringBuilder.append(null);
|
330
|
* @param string
|
331
|
* @return
|
332
|
*/
|
333
|
private String NullToEmpty(String string) {
|
334
|
if (string == null) {
|
335
|
return "";
|
336
|
} else {
|
337
|
return string;
|
338
|
}
|
339
|
}
|
340
|
|
341
|
/* (non-Javadoc)
|
342
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
343
|
*/
|
344
|
@Override
|
345
|
protected boolean isIgnore(FaunaEuropaeaImportState state){
|
346
|
return (state.getConfig().getDoReferences() == IImportConfigurator.DO_REFERENCES.NONE);
|
347
|
}
|
348
|
|
349
|
}
|