1 5 package com.tdsecurities.itracker.converter; 6 7 import java.sql.PreparedStatement ; 8 import java.sql.ResultSet ; 9 import java.sql.Timestamp ; 10 import java.sql.Types ; 11 import java.util.Date ; 12 13 import org.apache.log4j.Logger; 14 15 import com.tdsecurities.itracker.common.DataSourceManager; 16 import com.tdsecurities.itracker.security.LdapAuthenticate; 17 import com.tdsecurities.itracker.security.LdapDistinguishedName; 18 19 23 public class UserConverter extends BasicConverter 24 { 25 private static final String ADMIN = "admin"; 26 private static final String SOURCE_SQL = "select * from user"; 27 private static final String TARGET_SQL = "insert into userbean (id,login,password,first_name,last_name,email,status,registration_type,super_user,create_date,last_modified,preferences_id) values (?,?,?,?,?,?,?,?,?,?,?,?)"; 28 private static final String LAST_ID_SQL = "select max(id) from userbean"; 29 30 private Logger log = Logger.getLogger(UserConverter.class); 31 32 protected String getSourceQuery() 33 { 34 return SOURCE_SQL; 35 } 36 37 protected String getTargetQuery() 38 { 39 return TARGET_SQL; 40 } 41 42 protected String getIdStoreName() 43 { 44 return "user"; 45 } 46 47 protected String getLastIdQuery() 48 { 49 return LAST_ID_SQL; 50 } 51 52 protected int prepareTargetStatement(PreparedStatement targetStmt, ResultSet rs) throws Exception 53 { 54 String username = rs.getString("user_name"); 55 LdapAuthenticate ldap = new LdapAuthenticate(); 56 LdapDistinguishedName ldapName = ldap.getAttributesFromLoginId(username); 57 58 if( ADMIN.equals(username)) 59 { 60 log.info( "-> Skipped user " + username); 61 return BasicConverter.SKIP_ROW; 62 } 63 64 String firstName, lastName, email; 65 if( ldapName != null) 66 { 67 firstName = ldapName.getFirstName(); 68 lastName = ldapName.getLastName(); 69 email = ldapName.getEmail(); 70 } 71 else 72 { 73 log.info( "-> No LDAP entry for " + username); 74 firstName = ""; 75 lastName = rs.getString("realname"); 76 email = rs.getString("email"); 77 } 78 79 long now = (new Date ()).getTime(); 80 int col = 1; 81 targetStmt.setObject(col++, rs.getObject("user_id")); 82 targetStmt.setObject(col++, username); 83 targetStmt.setNull(col++, Types.VARCHAR); 84 targetStmt.setObject(col++, firstName); 85 targetStmt.setObject(col++, lastName); 86 targetStmt.setObject(col++, email.toLowerCase()); 87 targetStmt.setInt(col++, 1); 88 targetStmt.setInt(col++, 1); 89 targetStmt.setInt(col++, 0); 90 targetStmt.setTimestamp(col++, getDate(rs, "add_date")); 91 targetStmt.setTimestamp(col++, new Timestamp (now)); 92 targetStmt.setNull(col++, Types.VARCHAR); 93 return BasicConverter.OK; 94 } 95 96 protected void preConversionProcessing() 97 { 98 log.info( "Converting users..."); 99 executeUpdate(DataSourceManager.ITRACKER, "update userbean set id = 1 where login = 'admin'"); 100 executeUpdate(DataSourceManager.ITRACKER, "delete from userbean where login <> 'admin'"); 101 } 102 } 103 | Popular Tags |