KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > business > utils > UpgradeDatabase


1 package org.roller.business.utils;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.roller.RollerException;
6
7 import java.sql.Connection JavaDoc;
8 import java.sql.PreparedStatement JavaDoc;
9 import java.sql.ResultSet JavaDoc;
10 import java.sql.SQLException JavaDoc;
11 import java.sql.Statement JavaDoc;
12
13 /**
14  * Upgrade Roller database from 0.9.8 to 1.0.
15  *
16  * Creates root Category for each Website and associations for each Category.
17  * Sets each Website's default Category and default Blogger.com Category
18  * Creates associations for each Folder.
19  */

20 public class UpgradeDatabase
21 {
22     private static Log mLogger =
23         LogFactory.getFactory().getInstance(UpgradeDatabase.class);
24
25     public static void upgradeDatabase(Connection JavaDoc con) throws RollerException
26     {
27         // get the db version first
28
try
29         {
30             Statement JavaDoc versionStatement = con.createStatement();
31             ResultSet JavaDoc versionResultSet =
32               versionStatement.executeQuery("select dbversion from rollerconfig");
33             versionResultSet.next();
34             String JavaDoc dbversion = versionResultSet.getString(1);
35             if (dbversion != null) return;
36             
37         } catch(Exception JavaDoc e) {
38             // assume this is a new db using the roller_properties table
39
return;
40         }
41         
42         
43         try
44         {
45             // Prepated statements for all queries in the loop
46

47             PreparedStatement JavaDoc rootCatStatement = con.prepareStatement(
48                "select a.id from weblogcategoryassoc as a, weblogcategory as c "+
49                "where c.websiteid=? and a.categoryid=c.id and a.ancestorid is null and a.relation='PARENT'");
50             
51             PreparedStatement JavaDoc rootCatCreateStatement = con.prepareStatement(
52                "insert into weblogcategory (id,name,description,websiteid,image) "+
53                "values (?,'root','root',?,NULL)");
54             
55             PreparedStatement JavaDoc updateWebsiteStatement = con.prepareStatement(
56                "update website set bloggercatid=?, defaultcatid=? where id=?");
57             
58             PreparedStatement JavaDoc catsStatement = con.prepareStatement(
59                "select id from weblogcategory where websiteid=? and id<>?");
60             
61             PreparedStatement JavaDoc assocCreateStatement = con.prepareStatement(
62                "insert into weblogcategoryassoc (id,categoryid,ancestorid,relation) "+
63                "values (?,?,?,'PARENT')");
64
65             PreparedStatement JavaDoc rootFolderStatement = con.prepareStatement(
66                 "select id from folder where websiteid=? and parentid is null");
67       
68             PreparedStatement JavaDoc foldersStatement = con.prepareStatement(
69                 "select id,parentid from folder where websiteid=?");
70       
71             PreparedStatement JavaDoc folderAssocCreateStatement = con.prepareStatement(
72                 "insert into folderassoc (id,folderid,ancestorid,relation) "+
73                 "values (?,?,?,'PARENT')");
74
75             // loop through all websites
76
Statement JavaDoc websitesStatement = con.createStatement();
77             ResultSet JavaDoc websitesResultSet =
78                 websitesStatement.executeQuery("select id from website");
79             while (websitesResultSet.next())
80             {
81                 String JavaDoc websiteId = websitesResultSet.getString(1);
82                 mLogger.info("Upgrading website id="+websiteId);
83                 
84                 rootCatStatement.clearParameters();
85                 rootCatStatement.setString(1, websiteId);
86                 ResultSet JavaDoc rootCatResultSet = rootCatStatement.executeQuery();
87                 
88                 
89                 if (!rootCatResultSet.first()) // if website has no root cat
90
{
91                     // then create one
92
rootCatCreateStatement.clearParameters();
93                     rootCatCreateStatement.setString(1, websiteId+"R");
94                     rootCatCreateStatement.setString(2, websiteId);
95                     rootCatCreateStatement.executeUpdate();
96                     
97                     // and make it the default one for the website
98
updateWebsiteStatement.clearParameters();
99                     updateWebsiteStatement.setString(1, websiteId+"R");
100                     updateWebsiteStatement.setString(2, websiteId+"R");
101                     updateWebsiteStatement.setString(3, websiteId);
102                     updateWebsiteStatement.executeUpdate();
103                     
104                     // and create an association for it
105
assocCreateStatement.clearParameters();
106                     assocCreateStatement.setString(1, websiteId+"A0");
107                     assocCreateStatement.setString(2, websiteId+"R");
108                     assocCreateStatement.setString(3, null);
109                     assocCreateStatement.executeUpdate();
110
111                     // and create associations for all of it's children
112
catsStatement.clearParameters();
113                     catsStatement.setString(1, websiteId);
114                     catsStatement.setString(2, websiteId+"R");
115                     ResultSet JavaDoc cats = catsStatement.executeQuery();
116                     int count = 1;
117                     while (cats.next())
118                     {
119                         String JavaDoc catid = cats.getString(1);
120                         assocCreateStatement.clearParameters();
121                         assocCreateStatement.setString(1, websiteId+"A"+count++);
122                         assocCreateStatement.setString(2, catid);
123                         assocCreateStatement.setString(3, websiteId+"R");
124                         assocCreateStatement.executeUpdate();
125                     }
126                     mLogger.debug(" Created root categories and associations");
127                     
128                     // determine root bookmark folder of website
129
rootFolderStatement.clearParameters();
130                     rootFolderStatement.setString(1, websiteId);
131                     ResultSet JavaDoc rootFolderResultSet = rootFolderStatement.executeQuery();
132                     rootFolderResultSet.next();
133                     String JavaDoc rootFolderId = rootFolderResultSet.getString(1);
134                     
135                     // create associations for all children fo root folder
136
foldersStatement.clearParameters();
137                     foldersStatement.setString(1, websiteId);
138                     ResultSet JavaDoc folders = foldersStatement.executeQuery();
139                     while (folders.next())
140                     {
141                         String JavaDoc id = folders.getString(1);
142                         String JavaDoc parentId = folders.getString(2);
143                         folderAssocCreateStatement.clearParameters();
144                         folderAssocCreateStatement.setString(1, id+"R");
145                         folderAssocCreateStatement.setString(2, id);
146                         if (parentId == null)
147                         {
148                             folderAssocCreateStatement.setString(3, null);
149                         }
150                         else
151                         {
152                             folderAssocCreateStatement.setString(3, rootFolderId);
153                         }
154                         folderAssocCreateStatement.executeUpdate();
155                     }
156                     mLogger.debug(" Created folder associations");
157                 }
158             }
159             
160             Statement JavaDoc versionUpdateStatement = con.createStatement();
161             versionUpdateStatement.executeUpdate(
162                 "update rollerconfig set dbversion='995'");
163             mLogger.info("Database upgrade complete.");
164         }
165         catch (SQLException JavaDoc e)
166         {
167             mLogger.error("ERROR in database upgrade",e);
168             throw new RollerException("ERROR in database upgrade",e);
169         }
170     }
171 }
172
Popular Tags