001    /**
002     * Copyright (c) 2000-present 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.kernel.util.StringPool;
025    import com.liferay.portal.kernel.workflow.WorkflowConstants;
026    import com.liferay.portal.util.PortalUtil;
027    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
028    import com.liferay.portlet.documentlibrary.social.DLActivityKeys;
029    import com.liferay.portlet.journal.model.JournalArticle;
030    
031    import java.sql.Connection;
032    import java.sql.PreparedStatement;
033    import java.sql.ResultSet;
034    import java.sql.Timestamp;
035    
036    import java.util.HashSet;
037    import java.util.Set;
038    
039    /**
040     * @author Sergio Sanchez
041     * @author Zsolt Berentey
042     */
043    public class UpgradeSocial extends UpgradeProcess {
044    
045            protected void addActivity(
046                            long activityId, long groupId, long companyId, long userId,
047                            Timestamp createDate, long mirrorActivityId, long classNameId,
048                            long classPK, int type, String extraData, long receiverUserId)
049                    throws Exception {
050    
051                    Connection con = null;
052                    PreparedStatement ps = null;
053                    ResultSet rs = null;
054    
055                    try {
056                            con = DataAccess.getUpgradeOptimizedConnection();
057    
058                            StringBundler sb = new StringBundler(5);
059    
060                            sb.append("insert into SocialActivity (activityId, groupId, ");
061                            sb.append("companyId, userId, createDate, mirrorActivityId, ");
062                            sb.append("classNameId, classPK, type_, extraData, ");
063                            sb.append("receiverUserId) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ");
064                            sb.append("?)");
065    
066                            ps = con.prepareStatement(sb.toString());
067    
068                            ps.setLong(1, activityId);
069                            ps.setLong(2, groupId);
070                            ps.setLong(3, companyId);
071                            ps.setLong(4, userId);
072                            ps.setLong(5, createDate.getTime());
073                            ps.setLong(6, mirrorActivityId);
074                            ps.setLong(7, classNameId);
075                            ps.setLong(8, classPK);
076                            ps.setInt(9, type);
077                            ps.setString(10, extraData);
078                            ps.setLong(11, receiverUserId);
079    
080                            ps.executeUpdate();
081                    }
082                    catch (Exception e) {
083                            if (_log.isWarnEnabled()) {
084                                    _log.warn("Unable to add activity " + activityId, e);
085                            }
086                    }
087                    finally {
088                            DataAccess.cleanUp(con, ps, rs);
089                    }
090            }
091    
092            @Override
093            protected void doUpgrade() throws Exception {
094                    updateDLFileVersionActivities();
095                    updateJournalActivities();
096                    updateSOSocialActivities();
097                    updateWikiPageActivities();
098            }
099    
100            protected Timestamp getUniqueModifiedDate(
101                    Set<String> keys, long groupId, long userId, Timestamp modifiedDate,
102                    long classNameId, long resourcePrimKey, double type) {
103    
104                    while (true) {
105                            StringBundler sb = new StringBundler(11);
106    
107                            sb.append(groupId);
108                            sb.append(StringPool.DASH);
109                            sb.append(userId);
110                            sb.append(StringPool.DASH);
111                            sb.append(modifiedDate);
112                            sb.append(StringPool.DASH);
113                            sb.append(classNameId);
114                            sb.append(StringPool.DASH);
115                            sb.append(resourcePrimKey);
116                            sb.append(StringPool.DASH);
117                            sb.append(type);
118    
119                            String key = sb.toString();
120    
121                            modifiedDate = new Timestamp(modifiedDate.getTime() + 1);
122    
123                            if (!keys.contains(key)) {
124                                    keys.add(key);
125    
126                                    return modifiedDate;
127                            }
128                    }
129            }
130    
131            protected void updateDLFileVersionActivities() throws Exception {
132                    long classNameId = PortalUtil.getClassNameId(DLFileEntry.class);
133    
134                    runSQL("delete from SocialActivity where classNameId = " + classNameId);
135    
136                    Connection con = null;
137                    PreparedStatement ps = null;
138                    ResultSet rs = null;
139    
140                    try {
141                            Set<String> keys = new HashSet<>();
142    
143                            con = DataAccess.getUpgradeOptimizedConnection();
144    
145                            ps = con.prepareStatement(
146                                    "select groupId, companyId, userId, modifiedDate, " +
147                                            "fileEntryId, title, version from DLFileVersion " +
148                                                    "where status = ?");
149    
150                            ps.setInt(1, WorkflowConstants.STATUS_APPROVED);
151    
152                            rs = ps.executeQuery();
153    
154                            while (rs.next()) {
155                                    long groupId = rs.getLong("groupId");
156                                    long companyId = rs.getLong("companyId");
157                                    long userId = rs.getLong("userId");
158                                    Timestamp modifiedDate = rs.getTimestamp("modifiedDate");
159                                    long fileEntryId = rs.getLong("fileEntryId");
160                                    String title = rs.getString("title");
161                                    double version = rs.getDouble("version");
162    
163                                    int type = DLActivityKeys.ADD_FILE_ENTRY;
164    
165                                    if (version > 1.0) {
166                                            type = DLActivityKeys.UPDATE_FILE_ENTRY;
167                                    }
168    
169                                    modifiedDate = getUniqueModifiedDate(
170                                            keys, groupId, userId, modifiedDate, classNameId,
171                                            fileEntryId, type);
172    
173                                    JSONObject extraDataJSONObject =
174                                            JSONFactoryUtil.createJSONObject();
175    
176                                    extraDataJSONObject.put("title", title);
177    
178                                    addActivity(
179                                            increment(), groupId, companyId, userId, modifiedDate, 0,
180                                            classNameId, fileEntryId, type,
181                                            extraDataJSONObject.toString(), 0);
182                            }
183                    }
184                    finally {
185                            DataAccess.cleanUp(con, ps, rs);
186                    }
187            }
188    
189            protected void updateJournalActivities() throws Exception {
190                    long classNameId = PortalUtil.getClassNameId(JournalArticle.class);
191    
192                    String[] tableNames = {"SocialActivity", "SocialActivityCounter"};
193    
194                    for (String tableName : tableNames) {
195                            StringBundler sb = new StringBundler(7);
196    
197                            sb.append("update ");
198                            sb.append(tableName);
199                            sb.append(" set classPK = (select resourcePrimKey ");
200                            sb.append("from JournalArticle where id_ = ");
201                            sb.append(tableName);
202                            sb.append(".classPK) where classNameId = ");
203                            sb.append(classNameId);
204    
205                            runSQL(sb.toString());
206                    }
207            }
208    
209            protected void updateSOSocialActivities() throws Exception {
210                    if (!hasTable("SO_SocialActivity")) {
211                            return;
212                    }
213    
214                    Connection con = null;
215                    PreparedStatement ps = null;
216                    ResultSet rs = null;
217    
218                    try {
219                            con = DataAccess.getUpgradeOptimizedConnection();
220    
221                            ps = con.prepareStatement(
222                                    "select activityId, activitySetId from SO_SocialActivity");
223    
224                            rs = ps.executeQuery();
225    
226                            while (rs.next()) {
227                                    long activityId = rs.getLong("activityId");
228                                    long activitySetId = rs.getLong("activitySetId");
229    
230                                    StringBundler sb = new StringBundler(4);
231    
232                                    sb.append("update SocialActivity set activitySetId = ");
233                                    sb.append(activitySetId);
234                                    sb.append(" where activityId = ");
235                                    sb.append(activityId);
236    
237                                    runSQL(sb.toString());
238                            }
239                    }
240                    finally {
241                            DataAccess.cleanUp(con, ps, rs);
242                    }
243    
244                    runSQL("drop table SO_SocialActivity");
245            }
246    
247            protected void updateWikiPageActivities() throws Exception {
248                    long classNameId = PortalUtil.getClassNameId(
249                            "com.liferay.wiki.model.WikiPage");
250    
251                    runSQL("delete from SocialActivity where classNameId = " + classNameId);
252    
253                    Connection con = null;
254                    PreparedStatement ps = null;
255                    ResultSet rs = null;
256    
257                    try {
258                            Set<String> keys = new HashSet<>();
259    
260                            con = DataAccess.getUpgradeOptimizedConnection();
261    
262                            ps = con.prepareStatement(
263                                    "select groupId, companyId, userId, modifiedDate, " +
264                                            "resourcePrimKey, version from WikiPage");
265    
266                            rs = ps.executeQuery();
267    
268                            while (rs.next()) {
269                                    long groupId = rs.getLong("groupId");
270                                    long companyId = rs.getLong("companyId");
271                                    long userId = rs.getLong("userId");
272                                    Timestamp modifiedDate = rs.getTimestamp("modifiedDate");
273                                    long resourcePrimKey = rs.getLong("resourcePrimKey");
274                                    double version = rs.getDouble("version");
275    
276                                    int type = 1;
277    
278                                    if (version > 1.0) {
279                                            type = 2;
280                                    }
281    
282                                    modifiedDate = getUniqueModifiedDate(
283                                            keys, groupId, userId, modifiedDate, classNameId,
284                                            resourcePrimKey, type);
285    
286                                    JSONObject extraDataJSONObject =
287                                            JSONFactoryUtil.createJSONObject();
288    
289                                    extraDataJSONObject.put("version", version);
290    
291                                    addActivity(
292                                            increment(), groupId, companyId, userId, modifiedDate, 0,
293                                            classNameId, resourcePrimKey, type,
294                                            extraDataJSONObject.toString(), 0);
295                            }
296                    }
297                    finally {
298                            DataAccess.cleanUp(con, ps, rs);
299                    }
300            }
301    
302            private static final Log _log = LogFactoryUtil.getLog(UpgradeSocial.class);
303    
304    }