1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.upgrade.v4_4_0;
16  
17  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
18  import com.liferay.portal.kernel.upgrade.UpgradeProcess;
19  import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
20  
21  import java.sql.Connection;
22  import java.sql.PreparedStatement;
23  import java.sql.ResultSet;
24  
25  /**
26   * <a href="UpgradeUUID.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Brian Wing Shun Chan
29   */
30  public class UpgradeUUID extends UpgradeProcess {
31  
32      protected void doUpgrade() throws Exception {
33          updateTable("BlogsEntry", "entryId");
34          updateTable("BookmarksEntry", "entryId");
35          updateTable("BookmarksFolder", "folderId");
36          updateTable("DLFileEntry", "fileEntryId");
37          updateTable("DLFileShortcut", "fileShortcutId");
38          updateTable("DLFolder", "folderId");
39          updateTable("CalEvent", "eventId");
40          updateTable("IGFolder", "folderId");
41          updateTable("IGImage", "imageId");
42          updateTable("JournalArticle", "id_");
43          updateTable("JournalStructure", "id_");
44          updateTable("JournalTemplate", "id_");
45          updateTable("MBCategory", "categoryId");
46          updateTable("MBMessage", "messageId");
47          updateTable("PollsChoice", "choiceId");
48          updateTable("PollsQuestion", "questionId");
49          updateTable("User_", "userId");
50          updateTable("WikiNode", "nodeId");
51          updateTable("WikiPage", "pageId");
52      }
53  
54      protected void updateTable(String tableName, String pkColName)
55          throws Exception {
56  
57          Connection con = null;
58          PreparedStatement ps = null;
59          ResultSet rs = null;
60  
61          try {
62              con = DataAccess.getConnection();
63  
64              ps = con.prepareStatement(
65                  "select " + pkColName + " from " + tableName +
66                      " where uuid_ IS NULL or uuid_ = ''");
67  
68              rs = ps.executeQuery();
69  
70              while (rs.next()) {
71                  long pkColValue = rs.getLong(pkColName);
72  
73                  String uuid = PortalUUIDUtil.generate();
74  
75                  runSQL(
76                      "update " + tableName + " set uuid_ = '" + uuid +
77                          "' where " + pkColName + " = " + pkColValue);
78              }
79          }
80          finally {
81              DataAccess.cleanUp(con, ps, rs);
82          }
83      }
84  
85  }