KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ashkelon > AncestorPopulator


1 package org.ashkelon;
2
3 import java.sql.Connection JavaDoc;
4 import java.sql.PreparedStatement JavaDoc;
5 import java.sql.SQLException JavaDoc;
6
7 import org.ashkelon.db.DBMgr;
8 import org.ashkelon.util.Logger;
9
10
11 /**
12  * @author Eitan Suez
13  */

14 public class AncestorPopulator
15 {
16
17     public AncestorPopulator() throws SQLException JavaDoc
18     {
19        DBMgr mgr = DBMgr.getInstance();
20        Connection JavaDoc conn = mgr.getConnection();
21
22        Logger logger = Logger.getInstance();
23        logger.setTraceLevel(Logger.DEBUG);
24
25        String JavaDoc sql = "DELETE FROM CLASS_ANCESTORS";
26        PreparedStatement JavaDoc pstmt = conn.prepareStatement(sql);
27        int numAffected = pstmt.executeUpdate();
28        logger.debug("cleared ancestor table, "+numAffected+" records deleted");
29        
30        sql = "INSERT INTO CLASS_ANCESTORS SELECT CLASSID, SUPERCLASSID, 1 FROM SUPERCLASS WHERE SUPERCLASSID IS NOT NULL";
31        pstmt = conn.prepareStatement(sql);
32        numAffected = pstmt.executeUpdate();
33        
34        int nextLevel = 2;
35        int previousLevel = 1;
36        
37        while (true)
38        {
39           sql = "DELETE FROM TEMP_DELTA";
40           pstmt = conn.prepareStatement(sql);
41           pstmt.executeUpdate();
42           
43           sql = "INSERT INTO TEMP_DELTA SELECT A.CLASSID, C.SUPERCLASSID, "+nextLevel+
44            " FROM CLASS_ANCESTORS A, SUPERCLASS C WHERE A.SUPERCLASSID = C.CLASSID AND A.HIERARCHY = " + previousLevel
45              + " AND C.SUPERCLASSID IS NOT NULL";
46           logger.debug("sql: "+sql);
47           pstmt = conn.prepareStatement(sql);
48           numAffected = pstmt.executeUpdate();
49           
50           logger.debug("pass # "+previousLevel+": inserted "+numAffected+" records");
51
52           if (numAffected == 0)
53              break;
54
55           sql = "INSERT INTO CLASS_ANCESTORS SELECT CLASSID, SUPERCLASSID, HIERARCHY FROM TEMP_DELTA";
56           pstmt = conn.prepareStatement(sql);
57           pstmt.executeUpdate();
58
59           previousLevel = nextLevel;
60           nextLevel++;
61        }
62        
63        pstmt.close();
64        
65        mgr.releaseConnection(conn);
66        mgr = null;
67     }
68     
69     public static void main(String JavaDoc args[]) throws Exception JavaDoc
70     {
71        new AncestorPopulator();
72     }
73
74 }
75
Popular Tags