Project

General

Profile

Download (4.31 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 Logger logger = Logger.getLogger(BerlinModelUserImport.class);
38

    
39
	public static final String NAMESPACE = "User";
40
	
41
	private static int modCount = 100;
42
	private static final String dbTableName = "webAuthorisation";
43
	private static final String pluralString = "Users";
44
	
45
	public BerlinModelUserImport(){
46
		super(dbTableName, pluralString);
47
	}
48
	
49
	@Override
50
	protected boolean doCheck(BerlinModelImportState state){
51
		IOValidator<BerlinModelImportState> validator = new BerlinModelUserImportValidator();
52
		return validator.validate(state);
53
	}
54
	
55
	@Override
56
	protected void doInvoke(BerlinModelImportState state){
57
		boolean success = true;
58
		
59
		BerlinModelImportConfigurator config = state.getConfig();
60
		Source source = config.getSource();
61
		String dbAttrName;
62
		String cdmAttrName;
63

    
64
		logger.info("start make "+pluralString+" ...");
65
		
66
		//get data from database
67
		String strQuery = 
68
				" SELECT *  " +
69
                " FROM "+dbTableName+" " ;
70
		ResultSet rs = source.getResultSet(strQuery) ;
71
		Collection<User> users = new ArrayList<User>();
72
		
73
		int i = 0;
74
		//for each reference
75
		try{
76
			while (rs.next()){
77
				try{
78
					if ((i++ % modCount ) == 0 && i!= 1 ){ logger.info(""+pluralString+" handled: " + (i-1));}
79
					
80
					//
81
					String username = rs.getString("Username");
82
					String pwd = rs.getString("Password");
83
					
84
					if (username != null){
85
						username = username.trim();
86
					}
87
					User user = User.NewInstance(username, pwd);
88
					
89
					Person person = Person.NewInstance();
90
					user.setPerson(person);
91
					
92
					/* 
93
					 * this is a crucial call, otherwise the password will not be set correctly
94
					 * and the whole authentication will not work 
95
					 */
96
					authenticate(Configuration.adminLogin, Configuration.adminPassword);
97
					getUserService().createUser(user);
98
					
99
					
100
					dbAttrName = "RealName";
101
					cdmAttrName = "TitleCache";
102
					success &= ImportHelper.addStringValue(rs, person, dbAttrName, cdmAttrName, false);
103
	
104
					users.add(user);
105
					state.putUser(username, user);
106
				}catch(Exception ex){
107
					logger.error(ex.getMessage());
108
					ex.printStackTrace();
109
					state.setUnsuccessfull();
110
					success = false;
111
				}
112
			} //while rs.hasNext()
113
		} catch (SQLException e) {
114
			logger.error("SQLException:" +  e);
115
			state.setUnsuccessfull();
116
			return;
117
		}
118
			
119
		logger.info("save " + i + " "+pluralString + " ...");
120
		getUserService().save(users);
121

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

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

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

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

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

    
150
}
(19-19/21)