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