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_0;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
019    
020    import java.sql.Connection;
021    import java.sql.PreparedStatement;
022    import java.sql.ResultSet;
023    
024    /**
025     * @author Brian Wing Shun Chan
026     */
027    public class UpgradeExpando extends UpgradeProcess {
028    
029            @Override
030            protected void doUpgrade() throws Exception {
031                    updateTables();
032            }
033    
034            protected void updateColumns(
035                            long scTableId, long snTableId, long wolTableId)
036                    throws Exception {
037    
038                    Connection con = null;
039                    PreparedStatement ps = null;
040                    ResultSet rs = null;
041    
042                    try {
043                            con = DataAccess.getUpgradeOptimizedConnection();
044    
045                            ps = con.prepareStatement(
046                                    "select * from ExpandoColumn where tableId = ?");
047    
048                            ps.setLong(1, wolTableId);
049    
050                            rs = ps.executeQuery();
051    
052                            long scColumnId = 0;
053                            long snColumnId = 0;
054    
055                            while (rs.next()) {
056                                    long wolColumnId = rs.getLong("columnId");
057                                    String name = rs.getString("name");
058    
059                                    long newTableId = 0;
060    
061                                    if (name.equals("aboutMe")) {
062                                            newTableId = snTableId;
063                                            snColumnId = wolColumnId;
064                                    }
065                                    else if (name.equals("jiraUserId")) {
066                                            newTableId = scTableId;
067                                            scColumnId = wolColumnId;
068                                    }
069    
070                                    runSQL(
071                                            "update ExpandoColumn set tableId = " + newTableId +
072                                                    " where tableId = " + wolTableId + " and name = '" +
073                                                            name + "'");
074                            }
075    
076                            updateRows(
077                                    scColumnId, scTableId, snColumnId, snTableId, wolTableId);
078                    }
079                    finally {
080                            DataAccess.cleanUp(con, ps, rs);
081                    }
082            }
083    
084            protected void updateRows(
085                            long scColumnId, long scTableId, long snColumnId, long snTableId,
086                            long wolTableId)
087                    throws Exception {
088    
089                    Connection con = null;
090                    PreparedStatement ps = null;
091                    ResultSet rs = null;
092    
093                    try {
094                            con = DataAccess.getUpgradeOptimizedConnection();
095    
096                            ps = con.prepareStatement(
097                                    "select * from ExpandoRow where tableId = ?");
098    
099                            ps.setLong(1, wolTableId);
100    
101                            rs = ps.executeQuery();
102    
103                            while (rs.next()) {
104                                    long wolRowId = rs.getLong("rowId_");
105                                    long companyId = rs.getLong("companyId");
106                                    long classPK = rs.getLong("classPK");
107    
108                                    long scRowId = increment();
109    
110                                    runSQL(
111                                            "insert into ExpandoRow (rowId_, companyId, tableId, " +
112                                                    "classPK) values (" + scRowId + ", " + companyId +
113                                                            ", " + scTableId + ", " + classPK + ")");
114    
115                                    long snRowId = increment();
116    
117                                    runSQL(
118                                            "insert into ExpandoRow (rowId_, companyId, tableId, " +
119                                                    "classPK) values (" + snRowId + ", " + companyId +
120                                                            ", " + snTableId + ", " + classPK + ")");
121    
122                                    runSQL("delete from ExpandoRow where tableId = " + wolTableId);
123    
124                                    updateValues(
125                                            scColumnId, scRowId, scTableId, snColumnId, snRowId,
126                                            snTableId, wolRowId, wolTableId);
127                            }
128                    }
129                    finally {
130                            DataAccess.cleanUp(con, ps, rs);
131                    }
132            }
133    
134            protected void updateTables() throws Exception {
135                    Connection con = null;
136                    PreparedStatement ps = null;
137                    ResultSet rs = null;
138    
139                    try {
140                            con = DataAccess.getUpgradeOptimizedConnection();
141    
142                            ps = con.prepareStatement(
143                                    "select * from ExpandoTable where name = ?");
144    
145                            ps.setString(1, "WOL");
146    
147                            rs = ps.executeQuery();
148    
149                            while (rs.next()) {
150                                    long wolTableId = rs.getLong("tableId");
151                                    long companyId = rs.getLong("companyId");
152                                    long classNameId = rs.getLong("classNameId");
153    
154                                    long scTableId = increment();
155    
156                                    runSQL(
157                                            "insert into ExpandoTable (tableId, companyId, " +
158                                                    "classNameId, name) values (" + scTableId + ", " +
159                                                            companyId + ", " + classNameId + ", 'SC')");
160    
161                                    long snTableId = increment();
162    
163                                    runSQL(
164                                            "insert into ExpandoTable (tableId, companyId, " +
165                                                    "classNameId, name) values (" + snTableId + ", " +
166                                                            companyId + ", " + classNameId + ", 'SN')");
167    
168                                    runSQL(
169                                            "delete from ExpandoTable where tableId = " + wolTableId);
170    
171                                    updateColumns(scTableId, snTableId, wolTableId);
172                            }
173                    }
174                    finally {
175                            DataAccess.cleanUp(con, ps, rs);
176                    }
177            }
178    
179            protected void updateValues(
180                            long scColumnId, long scRowId, long scTableId, long snColumnId,
181                            long snRowId, long snTableId, long wolRowId, long wolTableId)
182                    throws Exception {
183    
184                    Connection con = null;
185                    PreparedStatement ps = null;
186                    ResultSet rs = null;
187    
188                    try {
189                            con = DataAccess.getUpgradeOptimizedConnection();
190    
191                            ps = con.prepareStatement(
192                                    "select * from ExpandoValue where tableId = ? and rowId_ = ?");
193    
194                            ps.setLong(1, wolTableId);
195                            ps.setLong(2, wolRowId);
196    
197                            rs = ps.executeQuery();
198    
199                            while (rs.next()) {
200                                    long valueId = rs.getLong("valueId");
201                                    long columnId = rs.getLong("columnId");
202    
203                                    long newTableId = 0;
204                                    long newRowId = 0;
205    
206                                    if (columnId == scColumnId) {
207                                            newRowId = scRowId;
208                                            newTableId = scTableId;
209                                    }
210                                    else if (columnId == snColumnId) {
211                                            newRowId = snRowId;
212                                            newTableId = snTableId;
213                                    }
214    
215                                    runSQL(
216                                            "update ExpandoValue set tableId = " + newTableId +
217                                                    ", rowId_ = " + newRowId + " where " + "valueId = " +
218                                                            valueId);
219                            }
220                    }
221                    finally {
222                            DataAccess.cleanUp(con, ps, rs);
223                    }
224            }
225    
226    }