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