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.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            @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                    Object[] group = null;
086    
087                    Connection con = null;
088                    PreparedStatement ps = null;
089                    ResultSet rs = null;
090    
091                    try {
092                            con = DataAccess.getUpgradeOptimizedConnection();
093    
094                            ps = con.prepareStatement(_GET_GROUP);
095    
096                            ps.setLong(1, groupId);
097    
098                            rs = ps.executeQuery();
099    
100                            while (rs.next()) {
101                                    long classNameId = rs.getLong("classNameId");
102                                    long classPK = rs.getLong("classPK");
103    
104                                    group = new Object[] {classNameId, classPK};
105                            }
106                    }
107                    finally {
108                            DataAccess.cleanUp(con, ps, rs);
109                    }
110    
111                    return group;
112            }
113    
114            protected Object[] getLayout(long plid) throws Exception {
115                    Object[] layout = null;
116    
117                    Connection con = null;
118                    PreparedStatement ps = null;
119                    ResultSet rs = null;
120    
121                    try {
122                            con = DataAccess.getUpgradeOptimizedConnection();
123    
124                            ps = con.prepareStatement(_GET_LAYOUT);
125    
126                            ps.setLong(1, plid);
127    
128                            rs = ps.executeQuery();
129    
130                            while (rs.next()) {
131                                    long groupId = rs.getLong("groupId");
132    
133                                    layout = new Object[] {groupId};
134                            }
135                    }
136                    finally {
137                            DataAccess.cleanUp(con, ps, rs);
138                    }
139    
140                    return layout;
141            }
142    
143            protected void updateGroupId() throws Exception {
144                    Connection con = null;
145                    PreparedStatement ps = null;
146                    ResultSet rs = null;
147    
148                    try {
149                            con = DataAccess.getUpgradeOptimizedConnection();
150    
151                            ps = con.prepareStatement(
152                                    "select distinct(groupId) from SocialActivity where groupId " +
153                                            "> 0");
154    
155                            rs = ps.executeQuery();
156    
157                            while (rs.next()) {
158                                    long groupId = rs.getLong("groupId");
159    
160                                    try {
161                                            updateGroupId(groupId);
162                                    }
163                                    catch (Exception e) {
164                                            if (_log.isWarnEnabled()) {
165                                                    _log.warn(e);
166                                            }
167                                    }
168                            }
169                    }
170                    finally {
171                            DataAccess.cleanUp(con, ps, rs);
172                    }
173            }
174    
175            protected void updateGroupId(long groupId) throws Exception {
176                    Object[] group = getGroup(groupId);
177    
178                    if (group == null) {
179                            return;
180                    }
181    
182                    long classNameId = (Long)group[0];
183    
184                    if (classNameId != PortalUtil.getClassNameId(Layout.class.getName())) {
185                            return;
186                    }
187    
188                    long classPK = (Long)group[1];
189    
190                    Object[] layout = getLayout(classPK);
191    
192                    if (layout == null) {
193                            return;
194                    }
195    
196                    long layoutGroupId = (Long)layout[0];
197    
198                    runSQL(
199                            "update SocialActivity set groupId = " + layoutGroupId +
200                                    " where groupId = " + groupId);
201            }
202    
203            private static final String _GET_GROUP =
204                    "select * from Group_ where groupId = ?";
205    
206            private static final String _GET_LAYOUT =
207                    "select * from Layout where plid = ?";
208    
209            private static Log _log = LogFactoryUtil.getLog(UpgradeSocial.class);
210    
211    }