Project

General

Profile

Download (4.42 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<>();
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

    
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

    
101
					dbAttrName = "RealName";
102
					if (isNotBlank(rs.getString(dbAttrName))){
103
					    cdmAttrName = "TitleCache";
104
					    Person person = Person.NewInstance();
105
					    user.setPerson(person);
106
					    success &= ImportHelper.addStringValue(rs, person, dbAttrName, cdmAttrName, false);
107
					}
108

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

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

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

    
134

    
135
	@Override
136
	protected boolean isIgnore(BerlinModelImportState state){
137
		return ! state.getConfig().isDoUser();
138
	}
139

    
140
	@Override
141
	protected String getRecordQuery(BerlinModelImportConfigurator config) {
142
		return null; // not needed at the moment
143
	}
144

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

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

    
155
}
(19-19/21)