Project

General

Profile

Download (5.7 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.Map;
16

    
17
import org.apache.log4j.Logger;
18
import org.springframework.stereotype.Component;
19
import org.springframework.transaction.TransactionStatus;
20

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

    
32

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

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

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

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

    
49
	private ImportDeduplicationHelper<BerlinModelImportState> deduplicationHelper;
50

    
51

    
52
	public BerlinModelUserImport(){
53
		super(dbTableName, pluralString);
54
	}
55

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

    
62
	@Override
63
	protected void doInvoke(BerlinModelImportState state){
64
		boolean success = true;
65
	    this.deduplicationHelper = ImportDeduplicationHelper.NewInstance(this, state);
66

    
67
		BerlinModelImportConfigurator config = state.getConfig();
68
		Source source = config.getSource();
69
		String dbAttrName;
70
		String cdmAttrName;
71

    
72
		logger.info("start make "+pluralString+" ...");
73

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

    
81

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

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

    
95
					if (username != null){
96
						username = username.trim();
97
					}
98
					User user = User.NewInstance(username, pwd);
99

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

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

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

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

    
141
		logger.info("save " + i + " "+pluralString + " ...");
142
		getUserService().saveOrUpdate(users);
143

    
144
		this.commitTransaction(tx);
145

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

    
153
	private Person deduplicatePerson(BerlinModelImportState state, Person person) {
154
        Person result = deduplicationHelper.getExistingAuthor(state, person);
155
        return result;
156
    }
157

    
158

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

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

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

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

    
179
}
(20-20/22)