KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > migration > b15b16 > DB_1_MoveIssueCreateInfo


1 /* ================================================================
2  * Copyright (c) 2000-2002 CollabNet. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * 3. The end-user documentation included with the redistribution, if
16  * any, must include the following acknowlegement: "This product includes
17  * software developed by Collab.Net <http://www.Collab.Net/>."
18  * Alternately, this acknowlegement may appear in the software itself, if
19  * and wherever such third-party acknowlegements normally appear.
20  *
21  * 4. The hosted project names must not be used to endorse or promote
22  * products derived from this software without prior written
23  * permission. For written permission, please contact info@collab.net.
24  *
25  * 5. Products derived from this software may not use the "Tigris" or
26  * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
27  * prior written permission of Collab.Net.
28  *
29  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
30  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
31  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32  * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
33  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
35  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
37  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
38  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
39  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * ====================================================================
42  *
43  * This software consists of voluntary contributions made by many
44  * individuals on behalf of Collab.Net.
45  */

46
47 package org.tigris.scarab.migration.b15b16;
48
49 import java.sql.*;
50 import java.util.*;
51
52 import org.apache.tools.ant.BuildException;
53 import org.tigris.scarab.migration.JDBCTask;
54
55 /**
56  * The creation transaction was previously used to retrieve the
57  * created date and creator for an issue. This makes some queries
58  * complex and error prone as well as adding inefficiency to the query.
59  * Denormalizing a bit for easier and faster queries.
60  * If the columns have not previously been added to SCARAB_ISSUE, this
61  * script adds them and populates the columns with data from the
62  * SCARAB_TRANSACTION table.
63  *
64  * @author <a HREF="mailto:jon@collab.net">John McNally</a>
65  * @version $Id: DB_1_MoveIssueCreateInfo.java 9049 2004-04-03 22:30:24Z pledbrook $
66  */

67 public class DB_1_MoveIssueCreateInfo extends JDBCTask
68 {
69     private static final String JavaDoc CREATE_ISSUE__PK = "1";
70     private static final String JavaDoc MOVE_ISSUE__PK = "3";
71
72     public DB_1_MoveIssueCreateInfo()
73     {
74     }
75
76     public void execute()
77         throws BuildException
78     {
79         boolean proceed = false;
80         try
81         {
82             // check whether SCARAB_ISSUE already has changes
83
Connection conn = null;
84             Statement stmt = null;
85             try
86             {
87                 setAutocommit(true);
88                 conn = getConnection();
89                 String JavaDoc sql = "SELECT CREATED_TRANS_ID FROM SCARAB_ISSUE";
90                 stmt = conn.createStatement();
91                 try
92                 {
93                     stmt.executeQuery(sql);
94                 }
95                 catch (SQLException e)
96                 {
97                     proceed = true;
98                 }
99             }
100             finally
101             {
102                 close(stmt, conn);
103             }
104
105             if (proceed)
106             {
107                 upgradeScarabIssue();
108             }
109         }
110         catch (Exception JavaDoc e)
111         {
112             throw new BuildException(e);
113         }
114
115         addNewIndices();
116     }
117
118     private void addNewIndices()
119     {
120         Connection conn = null;
121         Statement stmt = null;
122         try
123         {
124             setAutocommit(true);
125             conn = getConnection();
126             String JavaDoc sql = "CREATE INDEX IX_ATTACHMENT on SCARAB_ACTIVITY (ATTACHMENT_ID)";
127             try
128             {
129                 stmt = conn.createStatement();
130                 stmt.execute(sql);
131             }
132             catch (SQLException e)
133             {
134                 System.out.println("index SCARAB_ACTIVITY.IX_ATTACHMENT was not created. verify that it already exists.");
135             }
136             close(stmt, null);
137             
138             sql = "CREATE INDEX IX_ISSUE_ATTACHTYPE on SCARAB_ATTACHMENT (ISSUE_ID, ATTACHMENT_TYPE_ID)";
139             try
140             {
141                 stmt = conn.createStatement();
142                 stmt.execute(sql);
143             }
144             catch (SQLException e)
145             {
146                 System.out.println("index SCARAB_ATTACHMENT.IX_ISSUE_ATTACHTYPE was not created. verify that it already exists.");
147             }
148             close(stmt, null);
149             
150             sql = "CREATE INDEX IX_DEPEND on SCARAB_ACTIVITY (DEPEND_ID)";
151             try
152             {
153                 stmt = conn.createStatement();
154                 stmt.execute(sql);
155             }
156             catch (SQLException e)
157             {
158                 System.out.println("index SCARAB_ACTIVITY.IX_DEPEND was not created. verify that it already exists.");
159             }
160         }
161         finally
162         {
163             close(stmt, conn);
164         }
165     }
166
167
168     private static final String JavaDoc MYSQL = "mysql";
169     private static final String JavaDoc POSTGRESQL = "postgres";
170     private static final String JavaDoc ORACLE = "oracle";
171     private static final String JavaDoc[] supportedDBs =
172         {MYSQL, POSTGRESQL, ORACLE};
173     private Map intTypes = new HashMap(3);
174     private Map longTypes = new HashMap(3);
175     private Map dateTypes = new HashMap(3);
176
177     {
178         intTypes.put(MYSQL, "INTEGER");
179         intTypes.put(POSTGRESQL, "integer");
180         intTypes.put(ORACLE, "NUMBER");
181         longTypes.put(MYSQL, "BIGINT");
182         longTypes.put(POSTGRESQL, "int8");
183         longTypes.put(ORACLE, "NUMBER (20, 0)");
184         dateTypes.put(MYSQL, "DATETIME");
185         dateTypes.put(POSTGRESQL, "timestamp");
186         dateTypes.put(ORACLE, "DATE");
187     }
188
189     private String JavaDoc getCanonicalDBProductName(Connection conn)
190         throws SQLException, BuildException
191     {
192         DatabaseMetaData dmd = conn.getMetaData();
193         String JavaDoc theVendor = dmd.getDatabaseProductName().toLowerCase();
194         String JavaDoc result = null;
195         for (int i=0; i<supportedDBs.length && result == null; i++)
196         {
197             if (theVendor.indexOf(supportedDBs[i]) >= 0)
198             {
199                 result = supportedDBs[i];
200             }
201         }
202         if (result == null)
203         {
204             throw new BuildException("Unsupported database: " + theVendor);
205         }
206         
207         return result;
208     }
209
210     private void upgradeScarabIssue()
211         throws SQLException
212     {
213         Connection conn = null;
214         Statement stmt = null;
215         try
216         {
217             // could try to do this as a transaction for db's that support
218
// it, but db's should be backed up prior to migration so taking
219
// the easy way out.
220
setAutocommit(true);
221             conn = getConnection();
222             String JavaDoc dbtype = getCanonicalDBProductName(conn);
223             String JavaDoc longType = (String JavaDoc)longTypes.get(dbtype);
224             //String intType = (String)intTypes.get(dbtype);
225
//String dateType = (String)dateTypes.get(dbtype);
226

227             // add the transaction fk to SCARAB_ISSUE
228
String JavaDoc sql = "alter table SCARAB_ISSUE add " +
229                 "CREATED_TRANS_ID " + longType + " NULL";
230             System.out.println(
231                 "Adding creation info to SCARAB_ISSUE");
232             try
233             {
234                 stmt = conn.createStatement();
235                 stmt.execute(sql);
236             }
237             finally
238             {
239                 close(stmt, null);
240             }
241
242             // add fk constraint
243
sql ="alter table SCARAB_ISSUE add FOREIGN KEY (CREATED_TRANS_ID)"
244                 + " REFERENCES SCARAB_TRANSACTION(TRANSACTION_ID)";
245             try
246             {
247                 stmt = conn.createStatement();
248                 stmt.execute(sql);
249             }
250             finally
251             {
252                 close(stmt, null);
253             }
254
255             // get the highest ISSUE_ID
256
long max = -1L;
257             sql = "select max(ISSUE_ID) from SCARAB_ISSUE";
258             try
259             {
260                 stmt = conn.createStatement();
261                 ResultSet rs = stmt.executeQuery(sql);
262                 rs.next();
263                 max = rs.getLong(1);
264             }
265             finally
266             {
267                 close(stmt, null);
268             }
269             System.out.println("Updating " + max + " rows in SCARAB_ISSUE");
270             System.out.print("...");
271
272             // get create info for 1000 issue records at a time
273
// there is still 1 update statement per issue
274
for (long i=1; i<max; i+=1000L)
275             {
276                 for (Iterator idAndInfo =
277                          getCreateInfoFromInitialActivitySets(i, i+1000, conn)
278                          .entrySet().iterator(); idAndInfo.hasNext();)
279                 {
280                     Map.Entry me = (Map.Entry)idAndInfo.next();
281                     //String[] info = (String[])me.getValue();
282
//setCreatedInfo((String)me.getKey(), (String)info[0],
283
// (String)info[1], conn);
284
setCreatedInfo((String JavaDoc)me.getKey(), (String JavaDoc)me.getValue(),
285                                    conn);
286                 }
287             }
288             System.out.print("\ndone.");
289         }
290         finally
291         {
292             close(stmt, conn);
293         }
294     }
295
296     /**
297      * The initial activity set from issue creation.
298      *
299      * @return a <code>ActivitySet</code> value
300      * @exception Exception if an error occurs
301      */

302     private Map getCreateInfoFromInitialActivitySets(long startIssueId,
303                                                      long endIssueId,
304                                                      Connection conn)
305         throws SQLException
306     {
307         Map result = new HashMap(1500);
308         Statement stmt = null;
309         String JavaDoc sql = "select " +
310             //"a.ISSUE_ID, t.CREATED_DATE, t.CREATED_BY, t.TYPE_ID " +
311
"a.ISSUE_ID, t.TRANSACTION_ID, t.TYPE_ID " +
312             "FROM SCARAB_TRANSACTION t, SCARAB_ACTIVITY a " +
313             "WHERE a.ISSUE_ID >= " + startIssueId +
314             " and a.ISSUE_ID < " + endIssueId +
315             " and t.TYPE_ID IN (" + CREATE_ISSUE__PK + "," + MOVE_ISSUE__PK +
316             ") and a.TRANSACTION_ID=t.TRANSACTION_ID " +
317             "ORDER BY t.TYPE_ID ASC";
318             System.out.print(".");
319         try
320         {
321             stmt = conn.createStatement();
322             ResultSet rs = stmt.executeQuery(sql);
323             while (rs.next())
324             {
325                 String JavaDoc id = rs.getString(1);
326                 if (!result.containsKey(id))
327                 {
328                     //String[] info = {rs.getString(2), rs.getString(3)};
329
result.put(id, rs.getString(2));
330                 }
331             }
332         }
333         finally
334         {
335             close(stmt, null);
336         }
337         return result;
338     }
339
340     //private void setCreatedInfo(String issueId, String date, String userId,
341
private void setCreatedInfo(String JavaDoc issueId, String JavaDoc activitySetId,
342                                 Connection conn)
343         throws SQLException
344     {
345         Statement stmt = null;
346         //String sql = "update SCARAB_ISSUE set CREATED_DATE='" + date +
347
// "', CREATED_USER_ID=" + userId + " where ISSUE_ID=" + issueId;
348
String JavaDoc sql = "update SCARAB_ISSUE set CREATED_TRANS_ID=" +
349             activitySetId + " where ISSUE_ID=" + issueId;
350         try
351         {
352             stmt = conn.createStatement();
353             stmt.executeUpdate(sql);
354         }
355         finally
356         {
357             close(stmt, null);
358         }
359     }
360
361     private void close(Statement stmt, Connection conn)
362     {
363         if (stmt != null)
364         {
365             try
366             {
367                 stmt.close();
368             }
369             catch (SQLException ignore) {}
370         }
371         if (conn != null)
372         {
373             try
374             {
375                 conn.close();
376             }
377             catch (SQLException ignore) {}
378         }
379     }
380 }
381
Popular Tags