001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.upgrade.v6_0_2;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
021    import com.liferay.portal.util.PortalUtil;
022    import com.liferay.portlet.expando.model.ExpandoTableConstants;
023    import com.liferay.portlet.journal.model.JournalArticle;
024    import com.liferay.portlet.journal.model.impl.JournalArticleImpl;
025    import com.liferay.portlet.wiki.model.WikiPage;
026    import com.liferay.portlet.wiki.model.impl.WikiPageImpl;
027    
028    import java.sql.Connection;
029    import java.sql.PreparedStatement;
030    import java.sql.ResultSet;
031    
032    /**
033     * @author Jorge Ferrer
034     */
035    public class UpgradeExpando extends UpgradeProcess {
036    
037            protected void addRow(
038                            long rowId, long companyId, long tableId, long classPK)
039                    throws Exception {
040    
041                    Connection con = null;
042                    PreparedStatement ps = null;
043    
044                    try {
045                            con = DataAccess.getUpgradeOptimizedConnection();
046    
047                            ps = con.prepareStatement(
048                                    "insert into ExpandoRow (rowId_, companyId, tableId, " +
049                                            "classPK) values (?, ?, ?, ?)");
050    
051                            ps.setLong(1, rowId);
052                            ps.setLong(2, companyId);
053                            ps.setLong(3, tableId);
054                            ps.setLong(4, classPK);
055    
056                            ps.executeUpdate();
057                    }
058                    finally {
059                            DataAccess.cleanUp(con, ps);
060                    }
061            }
062    
063            protected void addValue(
064                            long valueId, long companyId, long tableId, long columnId,
065                            long rowId, long classNameId, long classPK, String data)
066                    throws Exception {
067    
068                    Connection con = null;
069                    PreparedStatement ps = null;
070    
071                    try {
072                            con = DataAccess.getUpgradeOptimizedConnection();
073    
074                            ps = con.prepareStatement(
075                                    "insert into ExpandoValue (valueId, companyId, tableId, " +
076                                            "columnId, rowId_, classNameId, classPK, data_) values " +
077                                                    "(?, ?, ?, ?, ?, ?, ?, ?)");
078    
079                            ps.setLong(1, valueId);
080                            ps.setLong(2, companyId);
081                            ps.setLong(3, tableId);
082                            ps.setLong(4, columnId);
083                            ps.setLong(5, rowId);
084                            ps.setLong(6, classNameId);
085                            ps.setLong(7, classPK);
086                            ps.setString(8, data);
087    
088                            ps.executeUpdate();
089                    }
090                    finally {
091                            DataAccess.cleanUp(con, ps);
092                    }
093            }
094    
095            @Override
096            protected void doUpgrade() throws Exception {
097                    updateTables(
098                            JournalArticle.class.getName(), JournalArticleImpl.TABLE_NAME,
099                            "id_");
100    
101                    updateTables(
102                            WikiPage.class.getName(), WikiPageImpl.TABLE_NAME, "pageId");
103            }
104    
105            protected boolean hasRow(long companyId, long tableId, long classPK)
106                    throws Exception {
107    
108                    Connection con = null;
109                    PreparedStatement ps = null;
110                    ResultSet rs = null;
111    
112                    try {
113                            con = DataAccess.getUpgradeOptimizedConnection();
114    
115                            ps = con.prepareStatement(
116                                    "select count(*) from ExpandoRow where companyId = ? and " +
117                                            "tableId = ? and classPK = ?");
118    
119                            ps.setLong(1, companyId);
120                            ps.setLong(2, tableId);
121                            ps.setLong(3, classPK);
122    
123                            rs = ps.executeQuery();
124    
125                            while (rs.next()) {
126                                    int count = rs.getInt(1);
127    
128                                    if (count > 0) {
129                                            return true;
130                                    }
131                            }
132    
133                            return false;
134                    }
135                    finally {
136                            DataAccess.cleanUp(con, ps, rs);
137                    }
138            }
139    
140            protected boolean hasValue(
141                            long companyId, long tableId, long columnId, long rowId)
142                    throws Exception {
143    
144                    Connection con = null;
145                    PreparedStatement ps = null;
146                    ResultSet rs = null;
147    
148                    try {
149                            con = DataAccess.getUpgradeOptimizedConnection();
150    
151                            ps = con.prepareStatement(
152                                    "select count(*) from ExpandoValue where companyId = ? and " +
153                                            "tableId = ? and columnId = ? and rowId_ = ?");
154    
155                            ps.setLong(1, companyId);
156                            ps.setLong(2, tableId);
157                            ps.setLong(3, columnId);
158                            ps.setLong(4, rowId);
159    
160                            rs = ps.executeQuery();
161    
162                            while (rs.next()) {
163                                    int count = rs.getInt(1);
164    
165                                    if (count > 0) {
166                                            return true;
167                                    }
168                            }
169    
170                            return false;
171                    }
172                    finally {
173                            DataAccess.cleanUp(con, ps, rs);
174                    }
175            }
176    
177            protected void updateRow(
178                            long companyId, long classPK, String tableName, long tableId,
179                            String columnName, long rowId)
180                    throws Exception {
181    
182                    Connection con = null;
183                    PreparedStatement ps = null;
184                    ResultSet rs = null;
185    
186                    try {
187                            con = DataAccess.getUpgradeOptimizedConnection();
188    
189                            ps = con.prepareStatement(
190                                    "select " + columnName + " from " + tableName + " where " +
191                                            "resourcePrimKey = ?");
192    
193                            ps.setLong(1, classPK);
194    
195                            rs = ps.executeQuery();
196    
197                            boolean delete = false;
198    
199                            while (rs.next()) {
200                                    long newClassPK = rs.getLong(columnName);
201    
202                                    delete = true;
203    
204                                    if (!hasRow(companyId, tableId, newClassPK)) {
205                                            long newRowId = increment();
206    
207                                            addRow(newRowId, companyId, tableId, newClassPK);
208    
209                                            updateValues(classPK, newClassPK, tableId, rowId, newRowId);
210                                    }
211                            }
212    
213                            if (delete) {
214                                    runSQL("delete from ExpandoRow where rowId_ = " + rowId);
215                                    runSQL("delete from ExpandoValue where rowId_ = " + rowId);
216                            }
217                    }
218                    finally {
219                            DataAccess.cleanUp(con, ps, rs);
220                    }
221            }
222    
223            protected void updateRows(String tableName, long tableId, String columnName)
224                    throws Exception {
225    
226                    Connection con = null;
227                    PreparedStatement ps = null;
228                    ResultSet rs = null;
229    
230                    try {
231                            con = DataAccess.getUpgradeOptimizedConnection();
232    
233                            ps = con.prepareStatement(
234                                    "select * from ExpandoRow where tableId = ?");
235    
236                            ps.setLong(1, tableId);
237    
238                            rs = ps.executeQuery();
239    
240                            while (rs.next()) {
241                                    long rowId = rs.getLong("rowId_");
242                                    long companyId = rs.getLong("companyId");
243                                    long classPK = rs.getLong("classPK");
244    
245                                    updateRow(
246                                            companyId, classPK, tableName, tableId, columnName, rowId);
247                            }
248                    }
249                    finally {
250                            DataAccess.cleanUp(con, ps, rs);
251                    }
252            }
253    
254            protected void updateTables(
255                            String className, String tableName, String columnName)
256                    throws Exception {
257    
258                    if (_log.isDebugEnabled()) {
259                            _log.debug("Upgrading " + tableName);
260                    }
261    
262                    long classNameId = PortalUtil.getClassNameId(className);
263    
264                    Connection con = null;
265                    PreparedStatement ps = null;
266                    ResultSet rs = null;
267    
268                    try {
269                            con = DataAccess.getUpgradeOptimizedConnection();
270    
271                            ps = con.prepareStatement(
272                                    "select * from ExpandoTable where classNameId = ? and " +
273                                            "name = ?");
274    
275                            ps.setLong(1, classNameId);
276                            ps.setString(2, ExpandoTableConstants.DEFAULT_TABLE_NAME);
277    
278                            rs = ps.executeQuery();
279    
280                            while (rs.next()) {
281                                    long tableId = rs.getLong("tableId");
282    
283                                    updateRows(tableName, tableId, columnName);
284                            }
285                    }
286                    finally {
287                            DataAccess.cleanUp(con, ps, rs);
288                    }
289            }
290    
291            protected void updateValues(
292                            long classPK, long newClassPK, long tableId, long rowId,
293                            long newRowId)
294                    throws Exception {
295    
296                    Connection con = null;
297                    PreparedStatement ps = null;
298                    ResultSet rs = null;
299    
300                    try {
301                            con = DataAccess.getUpgradeOptimizedConnection();
302    
303                            ps = con.prepareStatement(
304                                    "select * from ExpandoValue where tableId = ? and rowId_ = ? " +
305                                            "and classPK = ?");
306    
307                            ps.setLong(1, tableId);
308                            ps.setLong(2, rowId);
309                            ps.setLong(3, classPK);
310    
311                            rs = ps.executeQuery();
312    
313                            while (rs.next()) {
314                                    long companyId = rs.getLong("companyId");
315                                    long columnId = rs.getLong("columnId");
316                                    long classNameId = rs.getLong("classNameId");
317                                    String data = rs.getString("data_");
318    
319                                    if (!hasValue(companyId, tableId, columnId, newRowId)) {
320                                            long newValueId = increment();
321    
322                                            addValue(
323                                                    newValueId, companyId, tableId, columnId, newRowId,
324                                                    classNameId, newClassPK, data);
325                                    }
326                            }
327                    }
328                    finally {
329                            DataAccess.cleanUp(con, ps, rs);
330                    }
331            }
332    
333            private static Log _log = LogFactoryUtil.getLog(UpgradeExpando.class);
334    
335    }