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.v6_2_0;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.json.JSONFactoryUtil;
019    import com.liferay.portal.kernel.json.JSONObject;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
023    import com.liferay.portal.kernel.util.StringBundler;
024    import com.liferay.portal.util.PortalUtil;
025    import com.liferay.portlet.journal.model.JournalArticle;
026    import com.liferay.portlet.wiki.model.WikiPage;
027    import com.liferay.portlet.wiki.social.WikiActivityKeys;
028    
029    import java.sql.Connection;
030    import java.sql.PreparedStatement;
031    import java.sql.ResultSet;
032    import java.sql.Timestamp;
033    
034    /**
035     * @author Sergio Sanchez
036     * @author Zsolt Berentey
037     */
038    public class UpgradeSocial extends UpgradeProcess {
039    
040            protected void addActivity(
041                            long activityId, long groupId, long companyId, long userId,
042                            Timestamp createDate, long mirrorActivityId, long classNameId,
043                            long classPK, int type, String extraData, long receiverUserId)
044                    throws Exception {
045    
046                    Connection con = null;
047                    PreparedStatement ps = null;
048                    ResultSet rs = null;
049    
050                    try {
051                            con = DataAccess.getUpgradeOptimizedConnection();
052    
053                            StringBundler sb = new StringBundler(5);
054    
055                            sb.append("insert into SocialActivity (activityId, groupId, ");
056                            sb.append("companyId, userId, createDate, mirrorActivityId, ");
057                            sb.append("classNameId, classPK, type_, extraData, ");
058                            sb.append("receiverUserId) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ");
059                            sb.append("?)");
060    
061                            ps = con.prepareStatement(sb.toString());
062    
063                            ps.setLong(1, activityId);
064                            ps.setLong(2, groupId);
065                            ps.setLong(3, companyId);
066                            ps.setLong(4, userId);
067                            ps.setLong(5, createDate.getTime());
068                            ps.setLong(6, mirrorActivityId);
069                            ps.setLong(7, classNameId);
070                            ps.setLong(8, classPK);
071                            ps.setInt(9, type);
072                            ps.setString(10, extraData);
073                            ps.setLong(11, receiverUserId);
074    
075                            ps.executeUpdate();
076                    }
077                    catch (Exception e) {
078                            if (_log.isWarnEnabled()) {
079                                    _log.warn("Unable to add activity " + activityId, e);
080                            }
081                    }
082                    finally {
083                            DataAccess.cleanUp(con, ps, rs);
084                    }
085            }
086    
087            @Override
088            protected void doUpgrade() throws Exception {
089                    updateJournalActivities();
090                    updateSOSocialActivities();
091                    updateWikiPageActivities();
092            }
093    
094            protected void updateJournalActivities() throws Exception {
095                    long classNameId = PortalUtil.getClassNameId(JournalArticle.class);
096    
097                    String[] tableNames = {"SocialActivity", "SocialActivityCounter"};
098    
099                    for (String tableName : tableNames) {
100                            StringBundler sb = new StringBundler(7);
101    
102                            sb.append("update ");
103                            sb.append(tableName);
104                            sb.append(" set classPK = (select resourcePrimKey ");
105                            sb.append("from JournalArticle where id_ = ");
106                            sb.append(tableName);
107                            sb.append(".classPK) where classNameId = ");
108                            sb.append(classNameId);
109    
110                            runSQL(sb.toString());
111                    }
112            }
113    
114            protected void updateSOSocialActivities() throws Exception {
115                    if (!hasTable("SO_SocialActivity")) {
116                            return;
117                    }
118    
119                    Connection con = null;
120                    PreparedStatement ps = null;
121                    ResultSet rs = null;
122    
123                    try {
124                            con = DataAccess.getUpgradeOptimizedConnection();
125    
126                            ps = con.prepareStatement(
127                                    "select activityId, activitySetId from SO_SocialActivity");
128    
129                            rs = ps.executeQuery();
130    
131                            while (rs.next()) {
132                                    long activityId = rs.getLong("activityId");
133                                    long activitySetId = rs.getLong("activitySetId");
134    
135                                    StringBundler sb = new StringBundler(4);
136    
137                                    sb.append("update SocialActivity set activitySetId = ");
138                                    sb.append(activitySetId);
139                                    sb.append(" where activityId = ");
140                                    sb.append(activityId);
141    
142                                    runSQL(sb.toString());
143                            }
144                    }
145                    finally {
146                            DataAccess.cleanUp(con, ps, rs);
147                    }
148    
149                    runSQL("drop table SO_SocialActivity");
150            }
151    
152            protected void updateWikiPageActivities() throws Exception {
153                    long classNameId = PortalUtil.getClassNameId(WikiPage.class);
154    
155                    runSQL("delete from SocialActivity where classNameId = " + classNameId);
156    
157                    Connection con = null;
158                    PreparedStatement ps = null;
159                    ResultSet rs = null;
160    
161                    try {
162                            con = DataAccess.getUpgradeOptimizedConnection();
163    
164                            ps = con.prepareStatement(
165                                    "select groupId, companyId, userId, modifiedDate, " +
166                                            "resourcePrimKey, version from WikiPage");
167    
168                            rs = ps.executeQuery();
169    
170                            while (rs.next()) {
171                                    long groupId = rs.getLong("groupId");
172                                    long companyId = rs.getLong("companyId");
173                                    long userId = rs.getLong("userId");
174                                    Timestamp modifiedDate = rs.getTimestamp("modifiedDate");
175                                    long resourcePrimKey = rs.getLong("resourcePrimKey");
176                                    double version = rs.getDouble("version");
177    
178                                    int type = WikiActivityKeys.ADD_PAGE;
179    
180                                    if (version > 1.0) {
181                                            type = WikiActivityKeys.UPDATE_PAGE;
182                                    }
183    
184                                    JSONObject extraDataJSONObject =
185                                            JSONFactoryUtil.createJSONObject();
186    
187                                    extraDataJSONObject.put("version", version);
188    
189                                    addActivity(
190                                            increment(), groupId, companyId, userId, modifiedDate, 0,
191                                            classNameId, resourcePrimKey, type,
192                                            extraDataJSONObject.toString(), 0);
193                            }
194                    }
195                    finally {
196                            DataAccess.cleanUp(con, ps, rs);
197                    }
198            }
199    
200            private static Log _log = LogFactoryUtil.getLog(UpgradeSocial.class);
201    
202    }