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