001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            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    
057                    upgradeTable.updateTable();
058    
059                    // SocialRelation
060    
061                    upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
062                            SocialRelationTable.TABLE_NAME, SocialRelationTable.TABLE_COLUMNS,
063                            createDateColumn);
064    
065                    upgradeTable.setCreateSQL(SocialRelationTable.TABLE_SQL_CREATE);
066    
067                    upgradeTable.updateTable();
068    
069                    // SocialRequest
070    
071                    upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
072                            SocialRequestTable.TABLE_NAME, SocialRequestTable.TABLE_COLUMNS,
073                            createDateColumn, modifiedDateColumn);
074    
075                    upgradeTable.setCreateSQL(SocialRequestTable.TABLE_SQL_CREATE);
076    
077                    upgradeTable.updateTable();
078            }
079    
080            protected Object[] getGroup(long groupId) throws Exception {
081                    Object[] group = null;
082    
083                    Connection con = null;
084                    PreparedStatement ps = null;
085                    ResultSet rs = null;
086    
087                    try {
088                            con = DataAccess.getConnection();
089    
090                            ps = con.prepareStatement(_GET_GROUP);
091    
092                            ps.setLong(1, groupId);
093    
094                            rs = ps.executeQuery();
095    
096                            while (rs.next()) {
097                                    long classNameId = rs.getLong("classNameId");
098                                    long classPK = rs.getLong("classPK");
099    
100                                    group = new Object[] {classNameId, classPK};
101                            }
102                    }
103                    finally {
104                            DataAccess.cleanUp(con, ps, rs);
105                    }
106    
107                    return group;
108            }
109    
110            protected Object[] getLayout(long plid) throws Exception {
111                    Object[] layout = null;
112    
113                    Connection con = null;
114                    PreparedStatement ps = null;
115                    ResultSet rs = null;
116    
117                    try {
118                            con = DataAccess.getConnection();
119    
120                            ps = con.prepareStatement(_GET_LAYOUT);
121    
122                            ps.setLong(1, plid);
123    
124                            rs = ps.executeQuery();
125    
126                            while (rs.next()) {
127                                    long groupId = rs.getLong("groupId");
128    
129                                    layout = new Object[] {groupId};
130                            }
131                    }
132                    finally {
133                            DataAccess.cleanUp(con, ps, rs);
134                    }
135    
136                    return layout;
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.getConnection();
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    }