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