001
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
026 import java.sql.Connection;
027 import java.sql.PreparedStatement;
028 import java.sql.ResultSet;
029
030
033 public class UpgradeExpando extends UpgradeProcess {
034
035 protected void addRow(
036 long rowId, long companyId, long tableId, long classPK)
037 throws Exception {
038
039 Connection con = null;
040 PreparedStatement ps = null;
041
042 try {
043 con = DataAccess.getUpgradeOptimizedConnection();
044
045 ps = con.prepareStatement(
046 "insert into ExpandoRow (rowId_, companyId, tableId, " +
047 "classPK) values (?, ?, ?, ?)");
048
049 ps.setLong(1, rowId);
050 ps.setLong(2, companyId);
051 ps.setLong(3, tableId);
052 ps.setLong(4, classPK);
053
054 ps.executeUpdate();
055 }
056 finally {
057 DataAccess.cleanUp(con, ps);
058 }
059 }
060
061 protected void addValue(
062 long valueId, long companyId, long tableId, long columnId,
063 long rowId, long classNameId, long classPK, String data)
064 throws Exception {
065
066 Connection con = null;
067 PreparedStatement ps = null;
068
069 try {
070 con = DataAccess.getUpgradeOptimizedConnection();
071
072 ps = con.prepareStatement(
073 "insert into ExpandoValue (valueId, companyId, tableId, " +
074 "columnId, rowId_, classNameId, classPK, data_) values " +
075 "(?, ?, ?, ?, ?, ?, ?, ?)");
076
077 ps.setLong(1, valueId);
078 ps.setLong(2, companyId);
079 ps.setLong(3, tableId);
080 ps.setLong(4, columnId);
081 ps.setLong(5, rowId);
082 ps.setLong(6, classNameId);
083 ps.setLong(7, classPK);
084 ps.setString(8, data);
085
086 ps.executeUpdate();
087 }
088 finally {
089 DataAccess.cleanUp(con, ps);
090 }
091 }
092
093 @Override
094 protected void doUpgrade() throws Exception {
095 updateTables(
096 JournalArticle.class.getName(), JournalArticleImpl.TABLE_NAME,
097 "id_");
098
099 updateTables("com.liferay.wiki.model.WikiPage", "WikiPage", "pageId");
100 }
101
102 protected boolean hasRow(long companyId, long tableId, long classPK)
103 throws Exception {
104
105 Connection con = null;
106 PreparedStatement ps = null;
107 ResultSet rs = null;
108
109 try {
110 con = DataAccess.getUpgradeOptimizedConnection();
111
112 ps = con.prepareStatement(
113 "select count(*) from ExpandoRow where companyId = ? and " +
114 "tableId = ? and classPK = ?");
115
116 ps.setLong(1, companyId);
117 ps.setLong(2, tableId);
118 ps.setLong(3, classPK);
119
120 rs = ps.executeQuery();
121
122 while (rs.next()) {
123 int count = rs.getInt(1);
124
125 if (count > 0) {
126 return true;
127 }
128 }
129
130 return false;
131 }
132 finally {
133 DataAccess.cleanUp(con, ps, rs);
134 }
135 }
136
137 protected boolean hasValue(
138 long companyId, long tableId, long columnId, long rowId)
139 throws Exception {
140
141 Connection con = null;
142 PreparedStatement ps = null;
143 ResultSet rs = null;
144
145 try {
146 con = DataAccess.getUpgradeOptimizedConnection();
147
148 ps = con.prepareStatement(
149 "select count(*) from ExpandoValue where companyId = ? and " +
150 "tableId = ? and columnId = ? and rowId_ = ?");
151
152 ps.setLong(1, companyId);
153 ps.setLong(2, tableId);
154 ps.setLong(3, columnId);
155 ps.setLong(4, rowId);
156
157 rs = ps.executeQuery();
158
159 while (rs.next()) {
160 int count = rs.getInt(1);
161
162 if (count > 0) {
163 return true;
164 }
165 }
166
167 return false;
168 }
169 finally {
170 DataAccess.cleanUp(con, ps, rs);
171 }
172 }
173
174 protected void updateRow(
175 long companyId, long classPK, String tableName, long tableId,
176 String columnName, long rowId)
177 throws Exception {
178
179 Connection con = null;
180 PreparedStatement ps = null;
181 ResultSet rs = null;
182
183 try {
184 con = DataAccess.getUpgradeOptimizedConnection();
185
186 ps = con.prepareStatement(
187 "select " + columnName + " from " + tableName + " where " +
188 "resourcePrimKey = ?");
189
190 ps.setLong(1, classPK);
191
192 rs = ps.executeQuery();
193
194 boolean delete = false;
195
196 while (rs.next()) {
197 long newClassPK = rs.getLong(columnName);
198
199 delete = true;
200
201 if (!hasRow(companyId, tableId, newClassPK)) {
202 long newRowId = increment();
203
204 addRow(newRowId, companyId, tableId, newClassPK);
205
206 updateValues(classPK, newClassPK, tableId, rowId, newRowId);
207 }
208 }
209
210 if (delete) {
211 runSQL("delete from ExpandoRow where rowId_ = " + rowId);
212 runSQL("delete from ExpandoValue where rowId_ = " + rowId);
213 }
214 }
215 finally {
216 DataAccess.cleanUp(con, ps, rs);
217 }
218 }
219
220 protected void updateRows(String tableName, long tableId, String columnName)
221 throws Exception {
222
223 Connection con = null;
224 PreparedStatement ps = null;
225 ResultSet rs = null;
226
227 try {
228 con = DataAccess.getUpgradeOptimizedConnection();
229
230 ps = con.prepareStatement(
231 "select * from ExpandoRow where tableId = ?");
232
233 ps.setLong(1, tableId);
234
235 rs = ps.executeQuery();
236
237 while (rs.next()) {
238 long rowId = rs.getLong("rowId_");
239 long companyId = rs.getLong("companyId");
240 long classPK = rs.getLong("classPK");
241
242 updateRow(
243 companyId, classPK, tableName, tableId, columnName, rowId);
244 }
245 }
246 finally {
247 DataAccess.cleanUp(con, ps, rs);
248 }
249 }
250
251 protected void updateTables(
252 String className, String tableName, String columnName)
253 throws Exception {
254
255 if (_log.isDebugEnabled()) {
256 _log.debug("Upgrading " + tableName);
257 }
258
259 long classNameId = PortalUtil.getClassNameId(className);
260
261 Connection con = null;
262 PreparedStatement ps = null;
263 ResultSet rs = null;
264
265 try {
266 con = DataAccess.getUpgradeOptimizedConnection();
267
268 ps = con.prepareStatement(
269 "select * from ExpandoTable where classNameId = ? and " +
270 "name = ?");
271
272 ps.setLong(1, classNameId);
273 ps.setString(2, ExpandoTableConstants.DEFAULT_TABLE_NAME);
274
275 rs = ps.executeQuery();
276
277 while (rs.next()) {
278 long tableId = rs.getLong("tableId");
279
280 updateRows(tableName, tableId, columnName);
281 }
282 }
283 finally {
284 DataAccess.cleanUp(con, ps, rs);
285 }
286 }
287
288 protected void updateValues(
289 long classPK, long newClassPK, long tableId, long rowId,
290 long newRowId)
291 throws Exception {
292
293 Connection con = null;
294 PreparedStatement ps = null;
295 ResultSet rs = null;
296
297 try {
298 con = DataAccess.getUpgradeOptimizedConnection();
299
300 ps = con.prepareStatement(
301 "select * from ExpandoValue where tableId = ? and rowId_ = ? " +
302 "and classPK = ?");
303
304 ps.setLong(1, tableId);
305 ps.setLong(2, rowId);
306 ps.setLong(3, classPK);
307
308 rs = ps.executeQuery();
309
310 while (rs.next()) {
311 long companyId = rs.getLong("companyId");
312 long columnId = rs.getLong("columnId");
313 long classNameId = rs.getLong("classNameId");
314 String data = rs.getString("data_");
315
316 if (!hasValue(companyId, tableId, columnId, newRowId)) {
317 long newValueId = increment();
318
319 addValue(
320 newValueId, companyId, tableId, columnId, newRowId,
321 classNameId, newClassPK, data);
322 }
323 }
324 }
325 finally {
326 DataAccess.cleanUp(con, ps, rs);
327 }
328 }
329
330 private static final Log _log = LogFactoryUtil.getLog(UpgradeExpando.class);
331
332 }