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            protected void addActivitySet(
088                            long activitySetId, long groupId, long companyId, long userId,
089                            long createDate, long classNameId, long classPK, int type_)
090                    throws Exception {
091    
092                    Connection con = null;
093                    PreparedStatement ps = null;
094    
095                    try {
096                            con = DataAccess.getUpgradeOptimizedConnection();
097    
098                            StringBundler sb = new StringBundler(4);
099    
100                            sb.append("insert into SocialActivitySet (activitySetId, ");
101                            sb.append("groupId, companyId, userId, createDate, modifiedDate, ");
102                            sb.append("classNameId, classPK, type_, activityCount) values ");
103                            sb.append("(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
104    
105                            ps = con.prepareStatement(sb.toString());
106    
107                            ps.setLong(1, activitySetId);
108                            ps.setLong(2, groupId);
109                            ps.setLong(3, companyId);
110                            ps.setLong(4, userId);
111                            ps.setLong(5, createDate);
112                            ps.setLong(6, createDate);
113                            ps.setLong(7, classNameId);
114                            ps.setLong(8, classPK);
115                            ps.setInt(9, type_);
116                            ps.setInt(10, 1);
117    
118                            ps.executeUpdate();
119                    }
120                    catch (Exception e) {
121                            if (_log.isWarnEnabled()) {
122                                    _log.warn("Unable to add activity set " + activitySetId, e);
123                            }
124                    }
125                    finally {
126                            DataAccess.cleanUp(con, ps);
127                    }
128            }
129    
130            @Override
131            protected void doUpgrade() throws Exception {
132                    updateJournalActivities();
133                    updateSOSocialActivities();
134                    updateWikiPageActivities();
135    
136                    migrateActivities();
137            }
138    
139            protected boolean hasActivitySets() throws Exception {
140                    Connection con = null;
141                    PreparedStatement ps = null;
142                    ResultSet rs = null;
143    
144                    try {
145                            con = DataAccess.getUpgradeOptimizedConnection();
146    
147                            ps = con.prepareStatement("select count(*) from SocialActivitySet");
148    
149                            rs = ps.executeQuery();
150    
151                            while (rs.next()) {
152                                    int count = rs.getInt(1);
153    
154                                    if (count > 0) {
155                                            return true;
156                                    }
157                            }
158    
159                            return false;
160                    }
161                    finally {
162                            DataAccess.cleanUp(con, ps, rs);
163                    }
164            }
165    
166            protected void migrateActivities() throws Exception {
167                    if (hasActivitySets()) {
168                            return;
169                    }
170    
171                    Connection con = null;
172                    PreparedStatement ps = null;
173                    ResultSet rs = null;
174    
175                    try {
176                            con = DataAccess.getUpgradeOptimizedConnection();
177    
178                            ps = con.prepareStatement(
179                                    "select groupId, companyId, userId, createDate, classNameId, " +
180                                            "classPK, type_ from SocialActivity");
181    
182                            rs = ps.executeQuery();
183    
184                            while (rs.next()) {
185                                    long groupId = rs.getLong("groupId");
186                                    long companyId = rs.getLong("companyId");
187                                    long userId = rs.getLong("userId");
188                                    long createDate = rs.getLong("createDate");
189                                    long classNameId = rs.getLong("classNameId");
190                                    long classPK = rs.getLong("classPK");
191                                    int type_ = rs.getInt("type_");
192    
193                                    addActivitySet(
194                                            increment(), groupId, companyId, userId, createDate,
195                                            classNameId, classPK, type_);
196                            }
197                    }
198                    finally {
199                            DataAccess.cleanUp(con, ps, rs);
200                    }
201            }
202    
203            protected void updateJournalActivities() throws Exception {
204                    long classNameId = PortalUtil.getClassNameId(JournalArticle.class);
205    
206                    String[] tableNames = {"SocialActivity", "SocialActivityCounter"};
207    
208                    for (String tableName : tableNames) {
209                            StringBundler sb = new StringBundler(7);
210    
211                            sb.append("update ");
212                            sb.append(tableName);
213                            sb.append(" set classPK = (select resourcePrimKey ");
214                            sb.append("from JournalArticle where id_ = ");
215                            sb.append(tableName);
216                            sb.append(".classPK) where classNameId = ");
217                            sb.append(classNameId);
218    
219                            runSQL(sb.toString());
220                    }
221            }
222    
223            protected void updateSOSocialActivities() throws Exception {
224                    if (!hasTable("SO_SocialActivity")) {
225                            return;
226                    }
227    
228                    Connection con = null;
229                    PreparedStatement ps = null;
230                    ResultSet rs = null;
231    
232                    try {
233                            con = DataAccess.getUpgradeOptimizedConnection();
234    
235                            ps = con.prepareStatement(
236                                    "select activityId, activitySetId from SO_SocialActivity");
237    
238                            rs = ps.executeQuery();
239    
240                            while (rs.next()) {
241                                    long activityId = rs.getLong("activityId");
242                                    long activitySetId = rs.getLong("activitySetId");
243    
244                                    StringBundler sb = new StringBundler(4);
245    
246                                    sb.append("update SocialActivity set activitySetId = ");
247                                    sb.append(activitySetId);
248                                    sb.append(" where activityId = ");
249                                    sb.append(activityId);
250    
251                                    runSQL(sb.toString());
252                            }
253                    }
254                    finally {
255                            DataAccess.cleanUp(con, ps, rs);
256                    }
257    
258                    runSQL("drop table SO_SocialActivity");
259            }
260    
261            protected void updateWikiPageActivities() throws Exception {
262                    long classNameId = PortalUtil.getClassNameId(WikiPage.class);
263    
264                    runSQL("delete from SocialActivity where classNameId = " + classNameId);
265    
266                    Connection con = null;
267                    PreparedStatement ps = null;
268                    ResultSet rs = null;
269    
270                    try {
271                            con = DataAccess.getUpgradeOptimizedConnection();
272    
273                            ps = con.prepareStatement(
274                                    "select groupId, companyId, userId, modifiedDate, " +
275                                            "resourcePrimKey, version from WikiPage");
276    
277                            rs = ps.executeQuery();
278    
279                            while (rs.next()) {
280                                    long groupId = rs.getLong("groupId");
281                                    long companyId = rs.getLong("companyId");
282                                    long userId = rs.getLong("userId");
283                                    Timestamp modifiedDate = rs.getTimestamp("modifiedDate");
284                                    long resourcePrimKey = rs.getLong("resourcePrimKey");
285                                    double version = rs.getDouble("version");
286    
287                                    int type = WikiActivityKeys.ADD_PAGE;
288    
289                                    if (version > 1.0) {
290                                            type = WikiActivityKeys.UPDATE_PAGE;
291                                    }
292    
293                                    JSONObject extraDataJSONObject =
294                                            JSONFactoryUtil.createJSONObject();
295    
296                                    extraDataJSONObject.put("version", version);
297    
298                                    addActivity(
299                                            increment(), groupId, companyId, userId, modifiedDate, 0,
300                                            classNameId, resourcePrimKey, type,
301                                            extraDataJSONObject.toString(), 0);
302                            }
303                    }
304                    finally {
305                            DataAccess.cleanUp(con, ps, rs);
306                    }
307            }
308    
309            private static Log _log = LogFactoryUtil.getLog(UpgradeSocial.class);
310    
311    }