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.upgrade.UpgradeProcess;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.util.PortalUtil;
023    import com.liferay.portlet.journal.model.JournalArticle;
024    import com.liferay.portlet.wiki.model.WikiPage;
025    import com.liferay.portlet.wiki.social.WikiActivityKeys;
026    
027    import java.sql.Connection;
028    import java.sql.PreparedStatement;
029    import java.sql.ResultSet;
030    import java.sql.Timestamp;
031    
032    /**
033     * @author Sergio Sanchez
034     * @author Zsolt Berentey
035     */
036    public class UpgradeSocial extends UpgradeProcess {
037    
038            protected void addActivity(
039                            long activityId, long groupId, long companyId, long userId,
040                            Timestamp createDate, long mirrorActivityId, long classNameId,
041                            long classPK, int type, String extraData, long receiverUserId)
042                    throws Exception {
043    
044                    Connection con = null;
045                    PreparedStatement ps = null;
046                    ResultSet rs = null;
047    
048                    try {
049                            con = DataAccess.getUpgradeOptimizedConnection();
050    
051                            StringBundler sb = new StringBundler(5);
052    
053                            sb.append("insert into SocialActivity (activityId, groupId, ");
054                            sb.append("companyId, userId, createDate, mirrorActivityId, ");
055                            sb.append("classNameId, classPK, type_, extraData, ");
056                            sb.append("receiverUserId) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ");
057                            sb.append("?)");
058    
059                            ps = con.prepareStatement(sb.toString());
060    
061                            ps.setLong(1, activityId);
062                            ps.setLong(2, groupId);
063                            ps.setLong(3, companyId);
064                            ps.setLong(4, userId);
065                            ps.setLong(5, createDate.getTime());
066                            ps.setLong(6, mirrorActivityId);
067                            ps.setLong(7, classNameId);
068                            ps.setLong(8, classPK);
069                            ps.setInt(9, type);
070                            ps.setString(10, extraData);
071                            ps.setLong(11, receiverUserId);
072    
073                            ps.executeUpdate();
074                    }
075                    finally {
076                            DataAccess.cleanUp(con, ps, rs);
077                    }
078            }
079    
080            @Override
081            protected void doUpgrade() throws Exception {
082                    updateJournalActivities();
083                    updateWikiPageActivities();
084            }
085    
086            protected void updateJournalActivities() throws Exception {
087                    long classNameId = PortalUtil.getClassNameId(JournalArticle.class);
088    
089                    String[] tableNames = {"SocialActivity", "SocialActivityCounter"};
090    
091                    for (String tableName : tableNames) {
092                            StringBundler sb = new StringBundler(7);
093    
094                            sb.append("update ");
095                            sb.append(tableName);
096                            sb.append(" set classPK = (select resourcePrimKey ");
097                            sb.append("from JournalArticle where id_ = ");
098                            sb.append(tableName);
099                            sb.append(".classPK) where classNameId = ");
100                            sb.append(classNameId);
101    
102                            runSQL(sb.toString());
103                    }
104            }
105    
106            protected void updateWikiPageActivities() throws Exception {
107                    long classNameId = PortalUtil.getClassNameId(WikiPage.class);
108    
109                    runSQL("delete from SocialActivity where classNameId = " + classNameId);
110    
111                    Connection con = null;
112                    PreparedStatement ps = null;
113                    ResultSet rs = null;
114    
115                    try {
116                            con = DataAccess.getUpgradeOptimizedConnection();
117    
118                            ps = con.prepareStatement(
119                                    "select groupId, companyId, userId, modifiedDate, " +
120                                            "resourcePrimKey, version from WikiPage");
121    
122                            rs = ps.executeQuery();
123    
124                            while (rs.next()) {
125                                    long groupId = rs.getLong("groupId");
126                                    long companyId = rs.getLong("companyId");
127                                    long userId = rs.getLong("userId");
128                                    Timestamp modifiedDate = rs.getTimestamp("modifiedDate");
129                                    long resourcePrimKey = rs.getLong("resourcePrimKey");
130                                    double version = rs.getDouble("version");
131    
132                                    int type = WikiActivityKeys.ADD_PAGE;
133    
134                                    if (version > 1.0) {
135                                            type = WikiActivityKeys.UPDATE_PAGE;
136                                    }
137    
138                                    JSONObject extraDataJSONObject =
139                                            JSONFactoryUtil.createJSONObject();
140    
141                                    extraDataJSONObject.put("version", version);
142    
143                                    addActivity(
144                                            increment(), groupId, companyId, userId, modifiedDate, 0,
145                                            classNameId, resourcePrimKey, type,
146                                            extraDataJSONObject.toString(), 0);
147                            }
148                    }
149                    finally {
150                            DataAccess.cleanUp(con, ps, rs);
151                    }
152            }
153    
154    }