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_2;
16  
17  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.upgrade.UpgradeProcess;
21  import com.liferay.portal.util.PortalUtil;
22  import com.liferay.portlet.expando.model.ExpandoTableConstants;
23  import com.liferay.portlet.journal.model.JournalArticle;
24  import com.liferay.portlet.journal.model.impl.JournalArticleImpl;
25  import com.liferay.portlet.wiki.model.WikiPage;
26  import com.liferay.portlet.wiki.model.impl.WikiPageImpl;
27  
28  import java.sql.Connection;
29  import java.sql.PreparedStatement;
30  import java.sql.ResultSet;
31  
32  /**
33   * <a href="UpgradeExpando.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Jorge Ferrer
36   */
37  public class UpgradeExpando extends UpgradeProcess {
38  
39      protected void addRow(
40              long rowId, long companyId, long tableId, long classPK)
41          throws Exception {
42  
43          Connection con = null;
44          PreparedStatement ps = null;
45  
46          try {
47              con = DataAccess.getConnection();
48  
49              ps = con.prepareStatement(
50                  "insert into ExpandoRow (rowId_, companyId, tableId, " +
51                      "classPK) values (?, ?, ?, ?)");
52  
53              ps.setLong(1, rowId);
54              ps.setLong(2, companyId);
55              ps.setLong(3, tableId);
56              ps.setLong(4, classPK);
57  
58              ps.executeUpdate();
59          }
60          finally {
61              DataAccess.cleanUp(con, ps);
62          }
63      }
64  
65      protected void addValue(
66              long valueId, long companyId, long tableId, long columnId,
67              long rowId, long classNameId, long classPK, String data)
68          throws Exception {
69  
70          Connection con = null;
71          PreparedStatement ps = null;
72  
73          try {
74              con = DataAccess.getConnection();
75  
76              ps = con.prepareStatement(
77                  "insert into ExpandoValue (valueId, companyId, tableId, " +
78                      "columnId, rowId_, classNameId, classPK, data_) values " +
79                          "(?, ?, ?, ?, ?, ?, ?, ?)");
80  
81              ps.setLong(1, valueId);
82              ps.setLong(2, companyId);
83              ps.setLong(3, tableId);
84              ps.setLong(4, columnId);
85              ps.setLong(5, rowId);
86              ps.setLong(6, classNameId);
87              ps.setLong(7, classPK);
88              ps.setString(8, data);
89  
90              ps.executeUpdate();
91          }
92          finally {
93              DataAccess.cleanUp(con, ps);
94          }
95      }
96  
97      protected void doUpgrade() throws Exception {
98          updateTables(
99              JournalArticle.class.getName(),
100             JournalArticleImpl.TABLE_NAME, "id_");
101 
102         updateTables(
103             WikiPage.class.getName(), WikiPageImpl.TABLE_NAME, "pageId");
104     }
105 
106     protected boolean hasRow(long companyId, long tableId, long classPK)
107         throws Exception{
108 
109         Connection con = null;
110         PreparedStatement ps = null;
111         ResultSet rs = null;
112 
113         try {
114             con = DataAccess.getConnection();
115 
116             ps = con.prepareStatement(
117                 "select count(*) from ExpandoRow where companyId = ? and " +
118                     "tableId = ?  and classPK = ?");
119 
120             ps.setLong(1, companyId);
121             ps.setLong(2, tableId);
122             ps.setLong(3, classPK);
123 
124             rs = ps.executeQuery();
125 
126             while (rs.next()) {
127                 long count = rs.getLong(1);
128 
129                 if (count > 0) {
130                     return true;
131                 }
132             }
133 
134             return false;
135         }
136         finally {
137             DataAccess.cleanUp(con, ps, rs);
138         }
139     }
140 
141     protected boolean hasValue(
142             long companyId, long tableId, long columnId, long rowId)
143         throws Exception{
144 
145         Connection con = null;
146         PreparedStatement ps = null;
147         ResultSet rs = null;
148 
149         try {
150             con = DataAccess.getConnection();
151 
152             ps = con.prepareStatement(
153                 "select count(*) from ExpandoValue where companyId = ? and " +
154                     "tableId = ? and columnId = ? and rowId_ = ?");
155 
156             ps.setLong(1, companyId);
157             ps.setLong(2, tableId);
158             ps.setLong(3, columnId);
159             ps.setLong(4, rowId);
160 
161             rs = ps.executeQuery();
162 
163             while (rs.next()) {
164                 long count = rs.getLong(1);
165 
166                 if (count > 0) {
167                     return true;
168                 }
169             }
170 
171             return false;
172         }
173         finally {
174             DataAccess.cleanUp(con, ps, rs);
175         }
176     }
177 
178     protected void updateRow(
179             long companyId, long classPK, String tableName, long tableId,
180             String columnName, long rowId)
181         throws Exception {
182 
183         Connection con = null;
184         PreparedStatement ps = null;
185         ResultSet rs = null;
186 
187         try {
188             con = DataAccess.getConnection();
189 
190             ps = con.prepareStatement(
191                 "select " + columnName + " from " + tableName + " where " +
192                     "resourcePrimKey = ?");
193 
194             ps.setLong(1, classPK);
195 
196             rs = ps.executeQuery();
197 
198             boolean delete = false;
199 
200             while (rs.next()) {
201                 long newClassPK = rs.getLong(columnName);
202 
203                 delete = true;
204 
205                 if (!hasRow(companyId, tableId, newClassPK)) {
206                     long newRowId = increment();
207 
208                     addRow(newRowId, companyId, tableId, newClassPK);
209 
210                     updateValues(classPK, newClassPK, tableId, rowId, newRowId);
211                 }
212             }
213 
214             if (delete) {
215                 runSQL("delete from ExpandoRow where rowId_ = " + rowId);
216                 runSQL("delete from ExpandoValue where rowId_ = " + rowId);
217             }
218         }
219         finally {
220             DataAccess.cleanUp(con, ps, rs);
221         }
222     }
223 
224     protected void updateRows(
225             String tableName, long tableId, String columnName)
226         throws Exception {
227 
228         Connection con = null;
229         PreparedStatement ps = null;
230         ResultSet rs = null;
231 
232         try {
233             con = DataAccess.getConnection();
234 
235             ps = con.prepareStatement(
236                 "select * from ExpandoRow where tableId = ?");
237 
238             ps.setLong(1, tableId);
239 
240             rs = ps.executeQuery();
241 
242             while (rs.next()) {
243                 long rowId = rs.getLong("rowId_");
244                 long companyId = rs.getLong("companyId");
245                 long classPK = rs.getLong("classPK");
246 
247                 updateRow(
248                     companyId, classPK, tableName, tableId, columnName, rowId);
249             }
250         }
251         finally {
252             DataAccess.cleanUp(con, ps, rs);
253         }
254     }
255 
256     protected void updateTables(
257             String className, String tableName, String columnName)
258         throws Exception {
259 
260         if (_log.isDebugEnabled()) {
261             _log.debug("Upgrading " + tableName);
262         }
263 
264         long classNameId = PortalUtil.getClassNameId(className);
265 
266         Connection con = null;
267         PreparedStatement ps = null;
268         ResultSet rs = null;
269 
270         try {
271             con = DataAccess.getConnection();
272 
273             ps = con.prepareStatement(
274                 "select * from ExpandoTable where classNameId = ? and " +
275                     "name = ?");
276 
277             ps.setLong(1, classNameId);
278             ps.setString(2, ExpandoTableConstants.DEFAULT_TABLE_NAME);
279 
280             rs = ps.executeQuery();
281 
282             while (rs.next()) {
283                 long tableId = rs.getLong("tableId");
284 
285                 updateRows(tableName, tableId, columnName);
286             }
287         }
288         finally {
289             DataAccess.cleanUp(con, ps, rs);
290         }
291     }
292 
293     protected void updateValues(
294             long classPK, long newClassPK, long tableId, long rowId,
295             long newRowId)
296         throws Exception {
297 
298         Connection con = null;
299         PreparedStatement ps = null;
300         ResultSet rs = null;
301 
302         try {
303             con = DataAccess.getConnection();
304 
305             ps = con.prepareStatement(
306                 "select * from ExpandoValue where tableId = ? and rowId_ = ? " +
307                     "and classPK = ?");
308 
309             ps.setLong(1, tableId);
310             ps.setLong(2, rowId);
311             ps.setLong(3, classPK);
312 
313             rs = ps.executeQuery();
314 
315             while (rs.next()) {
316                 long companyId = rs.getLong("companyId");
317                 long columnId = rs.getLong("columnId");
318                 long classNameId = rs.getLong("classNameId");
319                 String data = rs.getString("data_");
320 
321                 if (!hasValue(companyId, tableId, columnId, newRowId)) {
322                     long newValueId = increment();
323 
324                     addValue(
325                         newValueId, companyId, tableId, columnId, newRowId,
326                         classNameId, newClassPK, data);
327                 }
328             }
329         }
330         finally {
331             DataAccess.cleanUp(con, ps, rs);
332         }
333     }
334 
335     private static Log _log = LogFactoryUtil.getLog(UpgradeExpando.class);
336 
337 }