Project

General

Profile

Download (11.1 KB) Statistics
| Branch: | Revision:
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.permission.User;
37
import eu.etaxonomy.cdm.model.reference.OriginalSourceBase;
38

    
39

    
40
/**
41
 * @author a.babadshanjan
42
 * @since 23.08.2010
43
 */
44
@Component
45
public class FaunaEuropaeaUsersImport extends FaunaEuropaeaImportBase {
46

    
47
    private static final long serialVersionUID = 2307694402632743697L;
48
    private static final Logger logger = Logger.getLogger(FaunaEuropaeaUsersImport.class);
49

    
50
	/* Interval for progress info message when retrieving taxa */
51
	private final int modCount = 10000;
52

    
53
	 protected DefaultTransactionDefinition txDefinition = new DefaultTransactionDefinition();
54

    
55
	@Override
56
	protected boolean doCheck(FaunaEuropaeaImportState state) {
57
		boolean result = true;
58
		FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
59
		logger.warn("Checking for References not yet fully implemented");
60
		result &= checkReferenceStatus(fauEuConfig);
61

    
62
		return result;
63
	}
64

    
65
	private boolean checkReferenceStatus(FaunaEuropaeaImportConfigurator fauEuConfig) {
66
		boolean result = true;
67
//		try {
68
		Source source = fauEuConfig.getSource();
69
		String sqlStr = "";
70
//		ResultSet rs = source.getResultSet(sqlStr);
71
		return result;
72
//		} catch (SQLException e) {
73
//		e.printStackTrace();
74
//		return false;
75
//		}
76
	}
77

    
78
	@Override
79
	protected void doInvoke(FaunaEuropaeaImportState state) {
80
		/*
81
		logger.warn("Start User doInvoke");
82
		ProfilerController.memorySnapshot();
83
		*/
84

    
85
		if (!state.getConfig().isDoAuthors()){
86
			return;
87
		}
88
		TransactionStatus txStatus = null;
89
		Map<String, Person> persons = null;
90
		Map<String, User> users= null;
91

    
92
		Map<Integer, UUID> userUuids = new HashMap<Integer, UUID>();
93
		Map<Integer, Institution> institiutions= new HashMap<Integer, Institution>();
94
		Collection<Institution> institutionsToSave = new HashSet<Institution>();
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 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, "
109
			+ "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";
110

    
111
		int count;
112
		if(logger.isInfoEnabled()) { logger.info("Start making References (Users)..."); }
113

    
114
		try {
115
			ResultSet rsUser = source.getResultSet(selectCountUsers);
116
			rsUser.next();
117
			count = rsUser.getInt(1);
118

    
119
			rsUser= source.getResultSet(selectColumnsUsers);
120

    
121
	        if (logger.isInfoEnabled()) {
122
	        	logger.info("Get all References...");
123
				logger.info("Number of rows: " + count);
124
				logger.info("Count Query: " + selectCountUsers);
125
				logger.info("Select Query: " + selectColumnsUsers);
126
			}
127

    
128
	        while (rsUser.next()){//usr_country_id, usr_org_id, usr_email, usr_homepage, usr_groupname, usr_groupicon, usr_groupnote, usr_description, usr_active)
129
	        	int refId = rsUser.getInt("userId");
130
				String userTitle = rsUser.getString("title");
131
				String userFirstname = rsUser.getString("firstname");
132
				String userLastname = rsUser.getString("lastname");
133
				String createdDate = rsUser.getString("created");
134
				String userPwd = rsUser.getString("password");
135
				String userCountry = rsUser.getString("country");
136

    
137
				String userMail = rsUser.getString("mail");
138
				String userHomepage = rsUser.getString("homepage");
139
				//String userGroupName = rsUser.getString("u.usr_groupname");
140
				String userDescription = rsUser.getString("description");
141
				int userActive = rsUser.getInt("active");
142
				String orgName = rsUser.getString("organisationName");
143
				String orgHomepage = rsUser.getString("organisationHomepage");
144
				int institutionId = rsUser.getInt("institutionId");
145

    
146
				// build person
147
				String userPerson = "";
148
				if (userTitle != null) {
149
					userPerson = userTitle;
150
					if (! userTitle.endsWith(".")) {
151
						userPerson += ".";
152
					}
153
				}
154
				userPerson += userTitle == null ? NullToEmpty(userFirstname) : " " + NullToEmpty(userFirstname);
155
				if ((userTitle != null || userFirstname != null) && userLastname != null) {
156
					userPerson += " " + userLastname;
157
				}
158
				this.authenticate("admin", "00000");
159
				// build year
160
				String year = null;
161
				if (createdDate != null) {
162
					year = createdDate.substring(0, createdDate.indexOf("-"));
163
				}
164

    
165
				if ((i++ % limit) == 0) {
166

    
167
					txStatus = startTransaction();
168
					persons= new HashMap<String,Person>(limit);
169
					users = new HashMap<String,User>(limit);
170

    
171

    
172
					if(logger.isInfoEnabled()) {
173
						logger.info("i = " + i + " - User import transaction started");
174
					}
175
				}
176

    
177
				Person person = null;
178
				User user = null;
179

    
180
				person= Person.NewTitledInstance(userPerson);
181

    
182
				person.addEmailAddress(userMail);
183
				try{
184
				    if (!StringUtils.isBlank(userHomepage)){
185
				        person.addUrl(URI.create(userHomepage));
186
				    }
187
				}catch(IllegalArgumentException e){
188
				    logger.debug(e.getMessage());
189
				}
190
				if (institutionId != 1){//1 = private
191
				    Institution institution ;
192
				    if (!institiutions.containsKey(institutionId)){
193
				        institution = Institution.NewInstance();
194
				        institution.setName(orgName);
195
				        Contact contact = Contact.NewInstance();
196
	                    try{
197
	                        if (!StringUtils.isBlank(orgHomepage)){
198
	                            contact.addUrl(URI.create(orgHomepage));
199
	                        }
200
	                    }catch(IllegalArgumentException e){
201
	                        logger.debug(e.getMessage());
202
	                    }
203
	                    institution.setContact(contact);
204
	                    institutionsToSave.add(institution);
205
				    } else {
206
				        institution = institiutions.get(institutionId);
207
				    }
208

    
209

    
210
    				person.addInstitutionalMembership(institution, null, null, null);
211
				}
212
				user = User.NewInstance(userPerson, userPwd);
213
				user.setPerson(person);
214
				if (userActive == FaunaEuropaeaTransformer.U_ACTIVE){
215
				    user.setAccountNonLocked(false);
216
				} else{
217
				    user.setAccountNonLocked(true);
218
				}
219

    
220
				if (!persons.containsKey(userPerson)) {
221
					if (userPerson == null) {
222
						logger.warn("User is null");
223
					}
224

    
225
					persons.put(userPerson, person);
226
					if (logger.isTraceEnabled()) {
227
						logger.trace("Stored user (" + userPerson + ")");
228
					}
229
				//}
230

    
231
				} else {
232
					person = persons.get(userPerson);
233
					if (logger.isDebugEnabled()) {
234
						logger.debug("Not imported user with duplicated ref_id (" + refId +
235
							") " + userPerson);
236
					}
237
				}
238

    
239
				// set protected titleCache
240
				StringBuilder referenceTitleCache = new StringBuilder(person.getTitleCache() + ".");
241
				if (year != null) {
242
					referenceTitleCache.append(" " + year);
243
				}
244

    
245

    
246
				//ImportHelper.setOriginalSource(user, fauEuConfig.getSourceReference(), userId, namespace);
247
				ImportHelper.setOriginalSource(person, fauEuConfig.getSourceReference(), refId, namespace);
248

    
249

    
250
				// Store persons
251
				if (!users.containsKey(userPerson.toLowerCase())) {
252

    
253
					if (user == null) {
254
						logger.warn("User is null");
255
					}
256
					users.put(userPerson.toLowerCase(), user);
257
					if (logger.isTraceEnabled()) {
258
						logger.trace("Stored user (" + userPerson + ")");
259
					}
260
				} else {
261
					//if (logger.isDebugEnabled()) {
262
						logger.info("Duplicated user(" + userPerson +")");
263
					//}
264
					//continue;
265
				}
266

    
267
				if (((i % limit) == 0 && i > 1 ) || i == count ) {
268

    
269
					commitUsers(txStatus, persons, users,
270
							state.getAgentMap(),institutionsToSave, i);
271

    
272
					users = null;
273
					persons= null;
274
				}
275

    
276
	        }
277
	        if (users != null){
278
	        	commitUsers(txStatus, persons, users,  state.getAgentMap(), institutionsToSave,i);
279
	        	users = null;
280
				persons= null;
281
	        }
282
		}catch(SQLException e) {
283
			logger.error("SQLException:" +  e);
284
			state.setUnsuccessfull();
285
		}
286
		/*
287
		logger.warn("End User doInvoke");
288
		ProfilerController.memorySnapshot();
289
		*/
290
		if(logger.isInfoEnabled()) { logger.info("End making References (Users) ..."); }
291

    
292
		return;
293
	}
294

    
295
	private void commitUsers(TransactionStatus txStatus,
296
			Map<String, Person> persons,
297
			Map<String, User> users,
298
			Map<Integer, UUID> agentsUUID,
299
			Collection<Institution> institutionsToSave,
300
			int i) {
301

    
302
	    Map<UUID, AgentBase> instMap = getAgentService().save((Collection)institutionsToSave);
303
		Map<UUID, AgentBase> userMap = getAgentService().save((Collection)persons.values());
304
		logger.info("i = " + i + " - persons saved");
305

    
306

    
307

    
308
		Iterator<Entry<UUID, AgentBase>> it = userMap.entrySet().iterator();
309
		while (it.hasNext()){
310
			AgentBase person = it.next().getValue();
311
			int userID = Integer.valueOf(((OriginalSourceBase)person.getSources().iterator().next()).getIdInSource());
312
			UUID uuid = person.getUuid();
313
			agentsUUID.put(userID, uuid);
314
		}
315

    
316
		getUserService().save(users.values());
317
		logger.info("i = " + users.size() + " - users saved");
318
		//getReferenceService().save(references.values());
319
		//logger.info("i = " +references.size() + " - references saved");
320
		commitTransaction(txStatus);
321
	}
322

    
323
	/**
324
	 * Returns an empty string in case of a null string.
325
	 * This avoids having the string "null" when using StringBuilder.append(null);
326
	 * @param string
327
	 * @return
328
	 */
329
	private String NullToEmpty(String string) {
330
		if (string == null) {
331
			return "";
332
		} else {
333
			return string;
334
		}
335
	}
336

    
337
	@Override
338
    protected boolean isIgnore(FaunaEuropaeaImportState state){
339
		return (state.getConfig().getDoReferences() == IImportConfigurator.DO_REFERENCES.NONE);
340
	}
341

    
342
}
(17-17/20)