1 19 20 package com.sslexplorer.upgrade; 21 22 import java.io.File ; 23 import java.sql.Connection ; 24 import java.sql.PreparedStatement ; 25 import java.sql.ResultSet ; 26 import java.sql.Statement ; 27 28 public class UserUpgrade extends AbstractDatabaseUpgrade { 29 30 UserUpgrade(File oldDbDir, File newDbDir) { 31 super("Users and Roles", "Copies users and roles created " + 32 "when the Built-in User Database is in use.", true, 33 "explorer_accounts", 34 "explorer_configuration", oldDbDir, newDbDir); 35 } 36 37 public void doUpgrade(Upgrader upgrader, Connection oldConx, Connection newConx) throws Exception { 38 upgrader.info("Migrating all roles"); 40 Statement stmt = oldConx.createStatement(); 41 try { 42 ResultSet rs = stmt.executeQuery("SELECT * FROM ROLES"); 43 try { 44 while (rs.next()) { 45 PreparedStatement ps = newConx.prepareStatement("INSERT INTO ROLES (ROLENAME) VALUES (?)"); 46 try { 47 String rolename = rs.getString("ROLENAME"); 48 upgrader.info(" " + rolename); 49 ps.setString(1, rolename); 50 try { 51 ps.execute(); 52 } catch (Exception e) { 53 upgrader.warn("Failed to insert role " + rolename + ". Probably already exists."); 54 } 55 } finally { 56 ps.close(); 57 } 58 } 59 } finally { 60 rs.close(); 61 } 62 } finally { 63 stmt.close(); 64 } 65 upgrader.info("Migrating all users"); 67 stmt = oldConx.createStatement(); 68 try { 69 ResultSet rs = stmt.executeQuery("SELECT * FROM USERS"); 70 try { 71 while (rs.next()) { 72 String username = rs.getString("USERNAME"); 73 upgrader.info(" " + username); 74 PreparedStatement ps = newConx.prepareStatement("INSERT INTO USERS (USERNAME, EMAIL, FULLNAME" 75 + ", LAST_PASSWORD_CHANGE, PASSWORD) VALUES (?,?,?,?,?)"); 76 try { 77 ps.setString(1, username); 78 ps.setString(2, rs.getString("EMAIL")); 79 ps.setString(3, rs.getString("FULLNAME")); 80 ps.setString(4, rs.getString("LAST_PASSWORD_CHANGE")); 81 ps.setString(5, rs.getString("PASSWORD")); 82 try { 83 ps.execute(); 84 } catch (Exception e) { 85 upgrader.warn("Failed to insert user " + username + ". Probably already exists."); 86 } 87 } finally { 88 ps.close(); 89 } 90 } 91 } finally { 92 rs.close(); 93 } 94 } finally { 95 stmt.close(); 96 } 97 upgrader.info("Migrating all user roles"); 99 stmt = oldConx.createStatement(); 100 try { 101 ResultSet rs = stmt.executeQuery("SELECT * FROM USER_ROLES"); 102 try { 103 while (rs.next()) { 104 String username = rs.getString("USERNAME"); 105 String rolename = rs.getString("ROLENAME"); 106 upgrader.info(" " + username + "/" + rolename); 107 PreparedStatement ps = newConx 108 .prepareStatement("INSERT INTO USER_ROLES (USERNAME, ROLENAME) VALUES (?," + "?)"); 109 try { 110 ps.setString(1, username); 111 ps.setString(2, rolename); 112 try { 113 ps.execute(); 114 } catch (Exception e) { 115 upgrader.warn("Failed to insert user role " + username + "/" + rolename 116 + ".Probably already exists."); 117 } 118 } finally { 119 ps.close(); 120 } 121 } 122 } finally { 123 rs.close(); 124 } 125 } finally { 126 stmt.close(); 127 } 128 129 } 130 131 } 132 | Popular Tags |