Project

General

Profile

Download (4.35 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

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

    
30

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

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

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

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

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

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

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

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

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

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

    
75
		int i = 0;
76
		//for each reference
77
		try{
78
			while (rs.next()){
79
				try{
80
					if ((i++ % modCount ) == 0 && i!= 1 ){ logger.info(""+pluralString+" handled: " + (i-1));}
81

    
82
					//
83
					String username = rs.getString("Username");
84
					String pwd = rs.getString("Password");
85

    
86
					if (username != null){
87
						username = username.trim();
88
					}
89
					User user = User.NewInstance(username, pwd);
90

    
91
					Person person = Person.NewInstance();
92
					user.setPerson(person);
93

    
94
					/*
95
					 * this is a crucial call, otherwise the password will not be set correctly
96
					 * and the whole authentication will not work
97
					 */
98
					authenticate(Configuration.adminLogin, Configuration.adminPassword);
99
					getUserService().createUser(user);
100

    
101

    
102
					dbAttrName = "RealName";
103
					cdmAttrName = "TitleCache";
104
					success &= ImportHelper.addStringValue(rs, person, dbAttrName, cdmAttrName, false);
105

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

    
121
		logger.info("save " + i + " "+pluralString + " ...");
122
		getUserService().saveOrUpdate(users);
123

    
124
		logger.info("end make "+pluralString+" ..." + getSuccessString(success));;
125
		if (!success){
126
			state.setUnsuccessfull();
127
		}
128
		return;
129
	}
130

    
131

    
132
	@Override
133
	protected boolean isIgnore(BerlinModelImportState state){
134
		return ! state.getConfig().isDoUser();
135
	}
136

    
137
	@Override
138
	protected String getRecordQuery(BerlinModelImportConfigurator config) {
139
		return null; // not needed at the moment
140
	}
141

    
142
	@Override
143
	public boolean doPartition(ResultSetPartitioner partitioner, BerlinModelImportState state) {
144
		return true;  // not needed at the moment
145
	}
146

    
147
	@Override
148
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, BerlinModelImportState state) {
149
		return null; //not needed at the moment
150
	}
151

    
152
}
(19-19/21)