Project

General

Profile

Download (5.72 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
package eu.etaxonomy.cdm.io.berlinModel.in;
10

    
11
import java.sql.ResultSet;
12
import java.sql.SQLException;
13
import java.util.ArrayList;
14
import java.util.Collection;
15
import java.util.HashSet;
16
import java.util.Map;
17
import java.util.Set;
18

    
19
import org.apache.log4j.Logger;
20
import org.springframework.stereotype.Component;
21
import org.springframework.transaction.TransactionStatus;
22

    
23
import eu.etaxonomy.cdm.config.Configuration;
24
import eu.etaxonomy.cdm.io.berlinModel.in.validation.BerlinModelUserImportValidator;
25
import eu.etaxonomy.cdm.io.common.IOValidator;
26
import eu.etaxonomy.cdm.io.common.ImportHelper;
27
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
28
import eu.etaxonomy.cdm.io.common.Source;
29
import eu.etaxonomy.cdm.model.agent.Person;
30
import eu.etaxonomy.cdm.model.common.CdmBase;
31
import eu.etaxonomy.cdm.model.permission.User;
32

    
33

    
34
/**
35
 * @author a.mueller
36
 * @since 20.03.2008
37
 */
38
@Component
39
public class BerlinModelUserImport extends BerlinModelImportBase {
40
    private static final long serialVersionUID = 3277951604022442721L;
41

    
42
    private static final Logger logger = Logger.getLogger(BerlinModelUserImport.class);
43

    
44
	public static final String NAMESPACE = "User";
45

    
46
	private static int modCount = 100;
47
	private static final String dbTableName = "webAuthorisation";
48
	private static final String pluralString = "Users";
49

    
50
	public BerlinModelUserImport(){
51
		super(dbTableName, pluralString);
52
	}
53

    
54
	@Override
55
	protected boolean doCheck(BerlinModelImportState state){
56
		IOValidator<BerlinModelImportState> validator = new BerlinModelUserImportValidator();
57
		return validator.validate(state);
58
	}
59

    
60
	@Override
61
	protected void doInvoke(BerlinModelImportState state){
62
		boolean success = true;
63

    
64
		BerlinModelImportConfigurator config = state.getConfig();
65
		Source source = config.getSource();
66
		String dbAttrName;
67
		String cdmAttrName;
68

    
69
		logger.info("start make "+pluralString+" ...");
70

    
71
		//get data from database
72
		String strQuery =
73
				" SELECT *  " +
74
                " FROM " + dbTableName + " " ;
75
		ResultSet rs = source.getResultSet(strQuery) ;
76
		Collection<User> users = new ArrayList<>();
77
		Set<String> existingUsernames = new HashSet<>();
78

    
79

    
80
		TransactionStatus tx = this.startTransaction();
81
		int i = 0;
82
		//for each user
83
		try{
84
			while (rs.next()){
85
				try{
86
					if ((i++ % modCount ) == 0 && i!= 1 ){ logger.info(""+pluralString+" handled: " + (i-1));}
87

    
88
					//
89
					String username = rs.getString("Username");
90
					String pwd = rs.getString("Password");
91
					Integer id = nullSafeInt(rs, "AuthorisationId");
92

    
93
					if (username != null){
94
					    username = normalizeUsername(state, username);
95
					}
96
					if (existingUsernames.contains(username)){
97
					    continue;
98
					}
99
					User user = User.NewInstance(username, pwd);
100

    
101
					dbAttrName = "RealName";
102
					String realName = rs.getString(dbAttrName);
103
					if (isNotBlank(realName)){
104
					    cdmAttrName = "TitleCache";
105
					    Person person = Person.NewInstance();
106
					    success &= ImportHelper.addStringValue(rs, person, dbAttrName, cdmAttrName, false);
107
					    //only to make deduplication work, due to issue that nomenclaturalTitle does not match because set automatically during save
108
					    cdmAttrName = "nomenclaturalTitle";
109
					    success &= ImportHelper.addStringValue(rs, person, dbAttrName, cdmAttrName, false);
110

    
111
					    Person dedupPerson = deduplicatePerson(state, person);
112
			            if (dedupPerson != person){
113
			                logger.debug("User person deduplicated: " + id);
114
			            }else{
115
			                person.addImportSource(String.valueOf(id), dbTableName, state.getTransactionalSourceReference(), null);
116
			            }
117
			            user.setPerson(dedupPerson);
118
					}
119

    
120
					/*
121
					 * this is a crucial call, otherwise the password will not be set correctly
122
					 * and the whole authentication will not work
123
					 */
124
					authenticate(Configuration.adminLogin, Configuration.adminPassword);
125
					getUserService().createUser(user);
126
					existingUsernames.add(username);
127

    
128
					users.add(user);
129
					state.putUser(username, user);
130
				}catch(Exception ex){
131
					logger.error(ex.getMessage());
132
					ex.printStackTrace();
133
					state.setUnsuccessfull();
134
					success = false;
135
				}
136
			} //while rs.hasNext()
137
		} catch (SQLException e) {
138
			logger.error("SQLException:" +  e);
139
			state.setUnsuccessfull();
140
			return;
141
		}
142

    
143
		logger.info("save " + i + " "+pluralString + " ...");
144
		getUserService().saveOrUpdate(users);
145

    
146
		this.commitTransaction(tx);
147

    
148
		logger.info("end make "+pluralString+" ..." + getSuccessString(success));;
149
		if (!success){
150
			state.setUnsuccessfull();
151
		}
152
		return;
153
	}
154

    
155

    
156
    private Person deduplicatePerson(BerlinModelImportState state, Person person) {
157
        Person result = state.getDeduplicationHelper().getExistingAuthor(person);
158
        return result;
159
    }
160

    
161
	@Override
162
	protected boolean isIgnore(BerlinModelImportState state){
163
		return ! state.getConfig().isDoUser();
164
	}
165

    
166
	@Override
167
	protected String getRecordQuery(BerlinModelImportConfigurator config) {
168
		return null; // not needed at the moment
169
	}
170

    
171
	@Override
172
	public boolean doPartition(@SuppressWarnings("rawtypes") ResultSetPartitioner partitioner, BerlinModelImportState state) {
173
		return true;  // not needed at the moment
174
	}
175

    
176
	@Override
177
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, BerlinModelImportState state) {
178
		return null; //not needed at the moment
179
	}
180

    
181
}
(20-20/22)