1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.upgrade.v6_0_0;
16  
17  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
18  import com.liferay.portal.kernel.upgrade.UpgradeProcess;
19  
20  import java.sql.Connection;
21  import java.sql.PreparedStatement;
22  import java.sql.ResultSet;
23  
24  /**
25   * <a href="UpgradeExpando.java.html"><b><i>View Source</i></b></a>
26   *
27   * @author Brian Wing Shun Chan
28   */
29  public class UpgradeExpando extends UpgradeProcess {
30  
31      protected void doUpgrade() throws Exception {
32          updateTables();
33      }
34  
35      protected void updateColumns(
36              long scTableId, long snTableId, long wolTableId)
37          throws Exception {
38  
39          Connection con = null;
40          PreparedStatement ps = null;
41          ResultSet rs = null;
42  
43          try {
44              con = DataAccess.getConnection();
45  
46              ps = con.prepareStatement(
47                  "select * from ExpandoColumn where tableId = ?");
48  
49              ps.setLong(1, wolTableId);
50  
51              rs = ps.executeQuery();
52  
53              long scColumnId = 0;
54              long snColumnId = 0;
55  
56              while (rs.next()) {
57                  long wolColumnId = rs.getLong("columnId");
58                  String name = rs.getString("name");
59  
60                  long newTableId = 0;
61  
62                  if (name.equals("aboutMe")) {
63                      newTableId = snTableId;
64                      snColumnId = wolColumnId;
65                  }
66                  else if (name.equals("jiraUserId")) {
67                      newTableId = scTableId;
68                      scColumnId = wolColumnId;
69                  }
70  
71                  runSQL(
72                      "update ExpandoColumn set tableId = " + newTableId +
73                          " where tableId = " + wolTableId + " and name = '" +
74                              name + "'");
75              }
76  
77              updateRows(
78                  scColumnId, scTableId, snColumnId, snTableId, wolTableId);
79          }
80          finally {
81              DataAccess.cleanUp(con, ps, rs);
82          }
83      }
84  
85      protected void updateRows(
86              long scColumnId, long scTableId, long snColumnId, long snTableId,
87              long wolTableId)
88          throws Exception {
89  
90          Connection con = null;
91          PreparedStatement ps = null;
92          ResultSet rs = null;
93  
94          try {
95              con = DataAccess.getConnection();
96  
97              ps = con.prepareStatement(
98                  "select * from ExpandoRow where tableId = ?");
99  
100             ps.setLong(1, wolTableId);
101 
102             rs = ps.executeQuery();
103 
104             while (rs.next()) {
105                 long wolRowId = rs.getLong("rowId_");
106                 long companyId = rs.getLong("companyId");
107                 long classPK = rs.getLong("classPK");
108 
109                 long scRowId = increment();
110 
111                 runSQL(
112                     "insert into ExpandoRow (rowId_, companyId, tableId, " +
113                         "classPK) values (" + scRowId + ", " + companyId +
114                             ", " + scTableId + ", " + classPK + ")");
115 
116                 long snRowId = increment();
117 
118                 runSQL(
119                     "insert into ExpandoRow (rowId_, companyId, tableId, " +
120                         "classPK) values (" + snRowId + ", " + companyId +
121                             ", " + snTableId + ", " + classPK + ")");
122 
123                 runSQL("delete from ExpandoRow where tableId = " + wolTableId);
124 
125                 updateValues(
126                     scColumnId, scRowId, scTableId, snColumnId, snRowId,
127                     snTableId, wolRowId, wolTableId);
128             }
129         }
130         finally {
131             DataAccess.cleanUp(con, ps, rs);
132         }
133     }
134 
135     protected void updateTables() throws Exception {
136         Connection con = null;
137         PreparedStatement ps = null;
138         ResultSet rs = null;
139 
140         try {
141             con = DataAccess.getConnection();
142 
143             ps = con.prepareStatement(
144                 "select * from ExpandoTable where name = ?");
145 
146             ps.setString(1, "WOL");
147 
148             rs = ps.executeQuery();
149 
150             while (rs.next()) {
151                 long wolTableId = rs.getLong("tableId");
152                 long companyId = rs.getLong("companyId");
153                 long classNameId = rs.getLong("classNameId");
154 
155                 long scTableId = increment();
156 
157                 runSQL(
158                     "insert into ExpandoTable (tableId, companyId, " +
159                         "classNameId, name) values (" + scTableId + ", " +
160                             companyId + ", " + classNameId + ", 'SC')");
161 
162                 long snTableId = increment();
163 
164                 runSQL(
165                     "insert into ExpandoTable (tableId, companyId, " +
166                         "classNameId, name) values (" + snTableId + ", " +
167                             companyId + ", " + classNameId + ", 'SN')");
168 
169                 runSQL(
170                     "delete from ExpandoTable where tableId = " + wolTableId);
171 
172                 updateColumns(scTableId, snTableId, wolTableId);
173             }
174         }
175         finally {
176             DataAccess.cleanUp(con, ps, rs);
177         }
178     }
179 
180     protected void updateValues(
181             long scColumnId, long scRowId, long scTableId, long snColumnId,
182             long snRowId, long snTableId, long wolRowId, long wolTableId)
183         throws Exception {
184 
185         Connection con = null;
186         PreparedStatement ps = null;
187         ResultSet rs = null;
188 
189         try {
190             con = DataAccess.getConnection();
191 
192             ps = con.prepareStatement(
193                 "select * from ExpandoValue where tableId = ? and rowId_ = ?");
194 
195             ps.setLong(1, wolTableId);
196             ps.setLong(2, wolRowId);
197 
198             rs = ps.executeQuery();
199 
200             while (rs.next()) {
201                 long valueId = rs.getLong("valueId");
202                 long columnId = rs.getLong("columnId");
203 
204                 long newTableId = 0;
205                 long newRowId = 0;
206 
207                 if (columnId == scColumnId) {
208                     newRowId = scRowId;
209                     newTableId = scTableId;
210                 }
211                 else if (columnId == snColumnId) {
212                     newRowId = snRowId;
213                     newTableId = snTableId;
214                 }
215 
216                 runSQL(
217                     "update ExpandoValue set tableId = " + newTableId +
218                         ", rowId_ = " + newRowId + " where " + "valueId = " +
219                             valueId);
220             }
221         }
222         finally {
223             DataAccess.cleanUp(con, ps, rs);
224         }
225     }
226 
227 }