Project

General

Profile

Download (4.58 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.model.agent.Person;
28
import eu.etaxonomy.cdm.model.common.CdmBase;
29
import eu.etaxonomy.cdm.model.common.User;
30

    
31

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

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

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

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

    
48
	public BerlinModelUserImport(){
49
		super(dbTableName, pluralString);
50
	}
51

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

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

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

    
67
		logger.info("start make "+pluralString+" ...");
68

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

    
76

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

    
85
					//
86
					String username = rs.getString("Username");
87
					String pwd = rs.getString("Password");
88

    
89
					if (username != null){
90
						username = username.trim();
91
					}
92
					User user = User.NewInstance(username, pwd);
93

    
94
					dbAttrName = "RealName";
95
					String realName = rs.getString(dbAttrName);
96
					if (isNotBlank(realName)){
97
					    cdmAttrName = "TitleCache";
98
					    Person person = Person.NewInstance();
99
					    user.setPerson(person);
100
					    success &= ImportHelper.addStringValue(rs, person, dbAttrName, cdmAttrName, false);
101
					}
102

    
103
					/*
104
					 * this is a crucial call, otherwise the password will not be set correctly
105
					 * and the whole authentication will not work
106
					 */
107
					authenticate(Configuration.adminLogin, Configuration.adminPassword);
108
					getUserService().createUser(user);
109

    
110
					users.add(user);
111
					state.putUser(username, user);
112
				}catch(Exception ex){
113
					logger.error(ex.getMessage());
114
					ex.printStackTrace();
115
					state.setUnsuccessfull();
116
					success = false;
117
				}
118
			} //while rs.hasNext()
119
		} catch (SQLException e) {
120
			logger.error("SQLException:" +  e);
121
			state.setUnsuccessfull();
122
			return;
123
		}
124

    
125
		logger.info("save " + i + " "+pluralString + " ...");
126
		getUserService().saveOrUpdate(users);
127

    
128
		this.commitTransaction(tx);
129

    
130
		logger.info("end make "+pluralString+" ..." + getSuccessString(success));;
131
		if (!success){
132
			state.setUnsuccessfull();
133
		}
134
		return;
135
	}
136

    
137

    
138
	@Override
139
	protected boolean isIgnore(BerlinModelImportState state){
140
		return ! state.getConfig().isDoUser();
141
	}
142

    
143
	@Override
144
	protected String getRecordQuery(BerlinModelImportConfigurator config) {
145
		return null; // not needed at the moment
146
	}
147

    
148
	@Override
149
	public boolean doPartition(ResultSetPartitioner partitioner, BerlinModelImportState state) {
150
		return true;  // not needed at the moment
151
	}
152

    
153
	@Override
154
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, BerlinModelImportState state) {
155
		return null; //not needed at the moment
156
	}
157

    
158
}
(19-19/21)