001    /**
002     * Copyright (c) 2000-present 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.expando.kernel.model.ExpandoTableConstants;
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.kernel.util.LoggingTimer;
022    import com.liferay.portal.kernel.util.PortalUtil;
023    
024    import java.sql.PreparedStatement;
025    import java.sql.ResultSet;
026    
027    /**
028     * @author Jorge Ferrer
029     */
030    public class UpgradeExpando extends UpgradeProcess {
031    
032            protected void addRow(
033                            long rowId, long companyId, long tableId, long classPK)
034                    throws Exception {
035    
036                    try (PreparedStatement ps = connection.prepareStatement(
037                                    "insert into ExpandoRow (rowId_, companyId, tableId, " +
038                                            "classPK) values (?, ?, ?, ?)")) {
039    
040                            ps.setLong(1, rowId);
041                            ps.setLong(2, companyId);
042                            ps.setLong(3, tableId);
043                            ps.setLong(4, classPK);
044    
045                            ps.executeUpdate();
046                    }
047            }
048    
049            protected void addValue(
050                            long valueId, long companyId, long tableId, long columnId,
051                            long rowId, long classNameId, long classPK, String data)
052                    throws Exception {
053    
054                    try (PreparedStatement ps = connection.prepareStatement(
055                                    "insert into ExpandoValue (valueId, companyId, tableId, " +
056                                            "columnId, rowId_, classNameId, classPK, data_) values " +
057                                                    "(?, ?, ?, ?, ?, ?, ?, ?)")) {
058    
059                            ps.setLong(1, valueId);
060                            ps.setLong(2, companyId);
061                            ps.setLong(3, tableId);
062                            ps.setLong(4, columnId);
063                            ps.setLong(5, rowId);
064                            ps.setLong(6, classNameId);
065                            ps.setLong(7, classPK);
066                            ps.setString(8, data);
067    
068                            ps.executeUpdate();
069                    }
070            }
071    
072            @Override
073            protected void doUpgrade() throws Exception {
074                    updateTables(
075                            "com.liferay.portlet.journal.model.JournalArticle",
076                            "JournalArticle", "id_");
077    
078                    updateTables("com.liferay.wiki.model.WikiPage", "WikiPage", "pageId");
079            }
080    
081            protected boolean hasRow(long companyId, long tableId, long classPK)
082                    throws Exception {
083    
084                    try (PreparedStatement ps = connection.prepareStatement(
085                                    "select count(*) from ExpandoRow where companyId = ? and " +
086                                            "tableId = ? and classPK = ?")) {
087    
088                            ps.setLong(1, companyId);
089                            ps.setLong(2, tableId);
090                            ps.setLong(3, classPK);
091    
092                            try (ResultSet rs = ps.executeQuery()) {
093                                    while (rs.next()) {
094                                            int count = rs.getInt(1);
095    
096                                            if (count > 0) {
097                                                    return true;
098                                            }
099                                    }
100    
101                                    return false;
102                            }
103                    }
104            }
105    
106            protected boolean hasValue(
107                            long companyId, long tableId, long columnId, long rowId)
108                    throws Exception {
109    
110                    try (PreparedStatement ps = connection.prepareStatement(
111                                    "select count(*) from ExpandoValue where companyId = ? and " +
112                                            "tableId = ? and columnId = ? and rowId_ = ?")) {
113    
114                            ps.setLong(1, companyId);
115                            ps.setLong(2, tableId);
116                            ps.setLong(3, columnId);
117                            ps.setLong(4, rowId);
118    
119                            try (ResultSet rs = ps.executeQuery()) {
120                                    while (rs.next()) {
121                                            int count = rs.getInt(1);
122    
123                                            if (count > 0) {
124                                                    return true;
125                                            }
126                                    }
127    
128                                    return false;
129                            }
130                    }
131            }
132    
133            protected void updateRow(
134                            long companyId, long classPK, String tableName, long tableId,
135                            String columnName, long rowId)
136                    throws Exception {
137    
138                    try (PreparedStatement ps = connection.prepareStatement(
139                                    "select " + columnName + " from " + tableName + " where " +
140                                            "resourcePrimKey = ?")) {
141    
142                            ps.setLong(1, classPK);
143    
144                            try (ResultSet rs = ps.executeQuery()) {
145                                    boolean delete = false;
146    
147                                    while (rs.next()) {
148                                            long newClassPK = rs.getLong(columnName);
149    
150                                            delete = true;
151    
152                                            if (!hasRow(companyId, tableId, newClassPK)) {
153                                                    long newRowId = increment();
154    
155                                                    addRow(newRowId, companyId, tableId, newClassPK);
156    
157                                                    updateValues(
158                                                            classPK, newClassPK, tableId, rowId, newRowId);
159                                            }
160                                    }
161    
162                                    if (delete) {
163                                            runSQL("delete from ExpandoRow where rowId_ = " + rowId);
164                                            runSQL("delete from ExpandoValue where rowId_ = " + rowId);
165                                    }
166                            }
167                    }
168            }
169    
170            protected void updateRows(String tableName, long tableId, String columnName)
171                    throws Exception {
172    
173                    try (PreparedStatement ps = connection.prepareStatement(
174                                    "select * from ExpandoRow where tableId = ?")) {
175    
176                            ps.setLong(1, tableId);
177    
178                            try (ResultSet rs = ps.executeQuery()) {
179                                    while (rs.next()) {
180                                            long rowId = rs.getLong("rowId_");
181                                            long companyId = rs.getLong("companyId");
182                                            long classPK = rs.getLong("classPK");
183    
184                                            updateRow(
185                                                    companyId, classPK, tableName, tableId, columnName,
186                                                    rowId);
187                                    }
188                            }
189                    }
190            }
191    
192            protected void updateTables(
193                            String className, String tableName, String columnName)
194                    throws Exception {
195    
196                    try (LoggingTimer loggingTimer = new LoggingTimer(className)) {
197                            if (_log.isDebugEnabled()) {
198                                    _log.debug("Upgrading " + tableName);
199                            }
200    
201                            long classNameId = PortalUtil.getClassNameId(className);
202    
203                            try (PreparedStatement ps = connection.prepareStatement(
204                                            "select * from ExpandoTable where classNameId = ? and " +
205                                                    "name = ?")) {
206    
207                                    ps.setLong(1, classNameId);
208                                    ps.setString(2, ExpandoTableConstants.DEFAULT_TABLE_NAME);
209    
210                                    try (ResultSet rs = ps.executeQuery()) {
211                                            while (rs.next()) {
212                                                    long tableId = rs.getLong("tableId");
213    
214                                                    updateRows(tableName, tableId, columnName);
215                                            }
216                                    }
217                            }
218                    }
219            }
220    
221            protected void updateValues(
222                            long classPK, long newClassPK, long tableId, long rowId,
223                            long newRowId)
224                    throws Exception {
225    
226                    try (PreparedStatement ps = connection.prepareStatement(
227                                    "select * from ExpandoValue where tableId = ? and rowId_ = ? " +
228                                            "and classPK = ?")) {
229    
230                            ps.setLong(1, tableId);
231                            ps.setLong(2, rowId);
232                            ps.setLong(3, classPK);
233    
234                            try (ResultSet rs = ps.executeQuery()) {
235                                    while (rs.next()) {
236                                            long companyId = rs.getLong("companyId");
237                                            long columnId = rs.getLong("columnId");
238                                            long classNameId = rs.getLong("classNameId");
239                                            String data = rs.getString("data_");
240    
241                                            if (!hasValue(companyId, tableId, columnId, newRowId)) {
242                                                    long newValueId = increment();
243    
244                                                    addValue(
245                                                            newValueId, companyId, tableId, columnId, newRowId,
246                                                            classNameId, newClassPK, data);
247                                            }
248                                    }
249                            }
250                    }
251            }
252    
253            private static final Log _log = LogFactoryUtil.getLog(UpgradeExpando.class);
254    
255    }