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