KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > upgrade > ApplicationShortcutsUpgrade


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.upgrade;
21
22 import java.io.File JavaDoc;
23 import java.sql.Connection JavaDoc;
24 import java.sql.PreparedStatement JavaDoc;
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.Statement JavaDoc;
27
28 public class ApplicationShortcutsUpgrade extends AbstractDatabaseUpgrade {
29
30     ApplicationShortcutsUpgrade(File JavaDoc oldDbDir, File JavaDoc 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 JavaDoc oldConx, Connection JavaDoc newConx) throws Exception JavaDoc {
39         // // Auth Schemes
40
upgrader.info("Migrating all application shortcuts");
41         Statement JavaDoc stmt = oldConx.createStatement();
42         try {
43             ResultSet JavaDoc rs = stmt.executeQuery("SELECT * FROM APPLICATION_SHORTCUTS");
44             try {
45                 while (rs.next()) {
46                     String JavaDoc shortName = "Application Shortcut " + rs.getString("SHORTCUT_ID");
47                     String JavaDoc username = rs.getString("USERNAME");
48                     shortName = shortName + " (" + (username.equals("") ? "System" : username) + ")";
49                     PreparedStatement JavaDoc ps = newConx.prepareStatement("SELECT * FROM APPLICATION_SHORTCUTS WHERE NAME = ?");
50                     boolean found = false;
51                     try {
52                         ps.setString(1, shortName);
53                         ResultSet JavaDoc 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 JavaDoc newConx, Connection JavaDoc oldConx, ResultSet JavaDoc rs, String JavaDoc shortName) throws Exception JavaDoc {
80
81         PreparedStatement JavaDoc 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 JavaDoc ps2 = newConx.prepareStatement("SELECT RESOURCE_ID FROM APPLICATION_SHORTCUTS WHERE NAME = ?");
92             try {
93                 ps2.setString(1, shortName);
94                 ResultSet JavaDoc 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 JavaDoc("Failed to get new resource Id");
102                     }
103                 } finally {
104                     rs2.close();
105                 }
106             } finally {
107                 ps2.close();
108             }
109         } catch (Exception JavaDoc 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 JavaDoc newConx, Connection JavaDoc oldConx, int oldResourceId, int newResourceId) throws Exception JavaDoc {
117         PreparedStatement JavaDoc ps3 = oldConx.prepareStatement("SELECT * FROM APPLICATION_SHORTCUTS_PARAMETERS WHERE SHORTCUT_ID = ?");
118         try {
119             ps3.setInt(1, oldResourceId);
120             ResultSet JavaDoc rs3 = ps3.executeQuery();
121             try {
122                 while (rs3.next()) {
123                     PreparedStatement JavaDoc 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 JavaDoc 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