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.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
021    import com.liferay.portal.kernel.upgrade.util.DateUpgradeColumnImpl;
022    import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
023    import com.liferay.portal.kernel.upgrade.util.UpgradeTable;
024    import com.liferay.portal.kernel.upgrade.util.UpgradeTableFactoryUtil;
025    import com.liferay.portal.model.Layout;
026    import com.liferay.portal.upgrade.v6_0_0.util.SocialActivityTable;
027    import com.liferay.portal.upgrade.v6_0_0.util.SocialRelationTable;
028    import com.liferay.portal.upgrade.v6_0_0.util.SocialRequestTable;
029    import com.liferay.portal.util.PortalUtil;
030    
031    import java.sql.Connection;
032    import java.sql.PreparedStatement;
033    import java.sql.ResultSet;
034    
035    /**
036     * @author Amos Fong
037     * @author Brian Wing Shun Chan
038     */
039    public class UpgradeSocial extends UpgradeProcess {
040    
041            @Override
042            protected void doUpgrade() throws Exception {
043                    updateGroupId();
044    
045                    // SocialActivity
046    
047                    UpgradeColumn createDateColumn = new DateUpgradeColumnImpl(
048                            "createDate");
049                    UpgradeColumn modifiedDateColumn = new DateUpgradeColumnImpl(
050                            "modifiedDate");
051    
052                    UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
053                            SocialActivityTable.TABLE_NAME, SocialActivityTable.TABLE_COLUMNS,
054                            createDateColumn);
055    
056                    upgradeTable.setCreateSQL(SocialActivityTable.TABLE_SQL_CREATE);
057                    upgradeTable.setIndexesSQL(SocialActivityTable.TABLE_SQL_ADD_INDEXES);
058    
059                    upgradeTable.updateTable();
060    
061                    // SocialRelation
062    
063                    upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
064                            SocialRelationTable.TABLE_NAME, SocialRelationTable.TABLE_COLUMNS,
065                            createDateColumn);
066    
067                    upgradeTable.setCreateSQL(SocialRelationTable.TABLE_SQL_CREATE);
068                    upgradeTable.setIndexesSQL(SocialRelationTable.TABLE_SQL_ADD_INDEXES);
069    
070                    upgradeTable.updateTable();
071    
072                    // SocialRequest
073    
074                    upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
075                            SocialRequestTable.TABLE_NAME, SocialRequestTable.TABLE_COLUMNS,
076                            createDateColumn, modifiedDateColumn);
077    
078                    upgradeTable.setCreateSQL(SocialRequestTable.TABLE_SQL_CREATE);
079                    upgradeTable.setIndexesSQL(SocialRequestTable.TABLE_SQL_ADD_INDEXES);
080    
081                    upgradeTable.updateTable();
082            }
083    
084            protected Object[] getGroup(long groupId) throws Exception {
085                    Connection con = null;
086                    PreparedStatement ps = null;
087                    ResultSet rs = null;
088    
089                    try {
090                            con = DataAccess.getUpgradeOptimizedConnection();
091    
092                            ps = con.prepareStatement(_GET_GROUP);
093    
094                            ps.setLong(1, groupId);
095    
096                            rs = ps.executeQuery();
097    
098                            if (rs.next()) {
099                                    long classNameId = rs.getLong("classNameId");
100                                    long classPK = rs.getLong("classPK");
101    
102                                    return new Object[] {classNameId, classPK};
103                            }
104    
105                            return null;
106                    }
107                    finally {
108                            DataAccess.cleanUp(con, ps, rs);
109                    }
110            }
111    
112            protected Object[] getLayout(long plid) throws Exception {
113                    Connection con = null;
114                    PreparedStatement ps = null;
115                    ResultSet rs = null;
116    
117                    try {
118                            con = DataAccess.getUpgradeOptimizedConnection();
119    
120                            ps = con.prepareStatement(_GET_LAYOUT);
121    
122                            ps.setLong(1, plid);
123    
124                            rs = ps.executeQuery();
125    
126                            if (rs.next()) {
127                                    long groupId = rs.getLong("groupId");
128    
129                                    return new Object[] {groupId};
130                            }
131    
132                            return null;
133                    }
134                    finally {
135                            DataAccess.cleanUp(con, ps, rs);
136                    }
137            }
138    
139            protected void updateGroupId() throws Exception {
140                    Connection con = null;
141                    PreparedStatement ps = null;
142                    ResultSet rs = null;
143    
144                    try {
145                            con = DataAccess.getUpgradeOptimizedConnection();
146    
147                            ps = con.prepareStatement(
148                                    "select distinct(groupId) from SocialActivity where groupId " +
149                                            "> 0");
150    
151                            rs = ps.executeQuery();
152    
153                            while (rs.next()) {
154                                    long groupId = rs.getLong("groupId");
155    
156                                    try {
157                                            updateGroupId(groupId);
158                                    }
159                                    catch (Exception e) {
160                                            if (_log.isWarnEnabled()) {
161                                                    _log.warn(e);
162                                            }
163                                    }
164                            }
165                    }
166                    finally {
167                            DataAccess.cleanUp(con, ps, rs);
168                    }
169            }
170    
171            protected void updateGroupId(long groupId) throws Exception {
172                    Object[] group = getGroup(groupId);
173    
174                    if (group == null) {
175                            return;
176                    }
177    
178                    long classNameId = (Long)group[0];
179    
180                    if (classNameId != PortalUtil.getClassNameId(Layout.class.getName())) {
181                            return;
182                    }
183    
184                    long classPK = (Long)group[1];
185    
186                    Object[] layout = getLayout(classPK);
187    
188                    if (layout == null) {
189                            return;
190                    }
191    
192                    long layoutGroupId = (Long)layout[0];
193    
194                    runSQL(
195                            "update SocialActivity set groupId = " + layoutGroupId +
196                                    " where groupId = " + groupId);
197            }
198    
199            private static final String _GET_GROUP =
200                    "select * from Group_ where groupId = ?";
201    
202            private static final String _GET_LAYOUT =
203                    "select * from Layout where plid = ?";
204    
205            private static Log _log = LogFactoryUtil.getLog(UpgradeSocial.class);
206    
207    }