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