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 ApplicationShortcutsUpgrade extends AbstractDatabaseUpgrade { 29 30 ApplicationShortcutsUpgrade(File oldDbDir, File newDbDir) { 31 super("Application Shortcuts", "Copies all web forwards to the new format. " + 32 "Any user created shortcuts " + 33 "are also copied (with the owner username appended to " + 34 "the new resource name). The resource will not be attached to " + 35 "any policies.", true, "explorer_configuration", "explorer_configuration", oldDbDir, newDbDir); 36 } 37 38 public void doUpgrade(Upgrader upgrader, Connection oldConx, Connection newConx) throws Exception { 39 upgrader.info("Migrating all application shortcuts"); 41 Statement stmt = oldConx.createStatement(); 42 try { 43 ResultSet rs = stmt.executeQuery("SELECT * FROM APPLICATION_SHORTCUTS"); 44 try { 45 while (rs.next()) { 46 String shortName = "Application Shortcut " + rs.getString("SHORTCUT_ID"); 47 String username = rs.getString("USERNAME"); 48 shortName = shortName + " (" + (username.equals("") ? "System" : username) + ")"; 49 PreparedStatement ps = newConx.prepareStatement("SELECT * FROM APPLICATION_SHORTCUTS WHERE NAME = ?"); 50 boolean found = false; 51 try { 52 ps.setString(1, shortName); 53 ResultSet rs2 = ps.executeQuery(); 54 try { 55 if (rs2.next()) { 56 found = true; 57 } 58 } finally { 59 rs2.close(); 60 } 61 } finally { 62 ps.close(); 63 } 64 if (found) { 65 upgrader.warn(" Application shortcut '" + shortName + "' already exists, skipping"); 66 } else { 67 insertApplicationShortcut(upgrader, newConx, oldConx, rs, shortName); 68 } 69 } 70 } finally { 71 rs.close(); 72 } 73 } finally { 74 stmt.close(); 75 } 76 77 } 78 79 void insertApplicationShortcut(Upgrader upgrader, Connection newConx, Connection oldConx, ResultSet rs, String shortName) throws Exception { 80 81 PreparedStatement ps = newConx.prepareStatement("INSERT INTO APPLICATION_SHORTCUTS (NAME," 82 + "DESCRIPTION, APPLICATION, PARENT_RESOURCE_PERMISSION, " 83 + "DATE_CREATED, DATE_AMENDED) VALUES " + "(?,?,?,?,NOW(),NOW())"); 84 try { 85 upgrader.info(" " + shortName); 86 ps.setString(1, shortName); 87 ps.setString(2, rs.getString("DESCRIPTION")); 88 ps.setString(3, rs.getString("APPLICATION")); 89 ps.setInt(4, 0); 90 ps.execute(); 91 PreparedStatement ps2 = newConx.prepareStatement("SELECT RESOURCE_ID FROM APPLICATION_SHORTCUTS WHERE NAME = ?"); 92 try { 93 ps2.setString(1, shortName); 94 ResultSet rs2 = ps2.executeQuery(); 95 try { 96 if (rs2.next()) { 97 int oldResourceId = rs.getInt("SHORTCUT_ID"); 98 int newResourceId = rs2.getInt("RESOURCE_ID"); 99 updateParameters(upgrader, newConx, oldConx, oldResourceId, newResourceId); 100 } else { 101 throw new Exception ("Failed to get new resource Id"); 102 } 103 } finally { 104 rs2.close(); 105 } 106 } finally { 107 ps2.close(); 108 } 109 } catch (Exception e) { 110 upgrader.warn("Failed to insert application shortcut " + shortName + ". Probably already exists.", e); 111 } finally { 112 ps.close(); 113 } 114 } 115 116 void updateParameters(Upgrader upgrader, Connection newConx, Connection oldConx, int oldResourceId, int newResourceId) throws Exception { 117 PreparedStatement ps3 = oldConx.prepareStatement("SELECT * FROM APPLICATION_SHORTCUTS_PARAMETERS WHERE SHORTCUT_ID = ?"); 118 try { 119 ps3.setInt(1, oldResourceId); 120 ResultSet rs3 = ps3.executeQuery(); 121 try { 122 while (rs3.next()) { 123 PreparedStatement ps4 = newConx.prepareStatement( 124 "INSERT INTO APPLICATION_SHORTCUTS_PARAMETERS " + 125 "(SHORTCUT_ID,PARAMETER, VALUE) VALUES(?,?,?)"); 126 try { 127 ps4.setInt(1, newResourceId); 128 ps4.setString(2, rs3.getString("PARAMETER")); 129 ps4.setString(3, rs3.getString("VALUE")); 130 try { 131 ps4.execute(); 132 } catch (Exception e) { 133 upgrader.warn("Failed to insert application shortcut parameters. Probably already exists.", e); 134 } 135 } finally { 136 ps4.close(); 137 } 138 } 139 } finally { 140 rs3.close(); 141 } 142 } finally { 143 ps3.close(); 144 } 145 } 146 } 147 | Popular Tags |