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    
030    import java.sql.Connection;
031    import java.sql.PreparedStatement;
032    import java.sql.ResultSet;
033    import java.sql.Timestamp;
034    
035    import java.util.HashSet;
036    import java.util.Set;
037    
038    /**
039     * @author Sergio Sanchez
040     * @author Zsolt Berentey
041     */
042    public class UpgradeSocial extends UpgradeProcess {
043    
044            protected void addActivity(
045                            long activityId, long groupId, long companyId, long userId,
046                            Timestamp createDate, long mirrorActivityId, long classNameId,
047                            long classPK, int type, String extraData, long receiverUserId)
048                    throws Exception {
049    
050                    Connection con = null;
051                    PreparedStatement ps = null;
052                    ResultSet rs = null;
053    
054                    try {
055                            con = DataAccess.getUpgradeOptimizedConnection();
056    
057                            StringBundler sb = new StringBundler(5);
058    
059                            sb.append("insert into SocialActivity (activityId, groupId, ");
060                            sb.append("companyId, userId, createDate, mirrorActivityId, ");
061                            sb.append("classNameId, classPK, type_, extraData, ");
062                            sb.append("receiverUserId) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ");
063                            sb.append("?)");
064    
065                            ps = con.prepareStatement(sb.toString());
066    
067                            ps.setLong(1, activityId);
068                            ps.setLong(2, groupId);
069                            ps.setLong(3, companyId);
070                            ps.setLong(4, userId);
071                            ps.setLong(5, createDate.getTime());
072                            ps.setLong(6, mirrorActivityId);
073                            ps.setLong(7, classNameId);
074                            ps.setLong(8, classPK);
075                            ps.setInt(9, type);
076                            ps.setString(10, extraData);
077                            ps.setLong(11, receiverUserId);
078    
079                            ps.executeUpdate();
080                    }
081                    catch (Exception e) {
082                            if (_log.isWarnEnabled()) {
083                                    _log.warn("Unable to add activity " + activityId, e);
084                            }
085                    }
086                    finally {
087                            DataAccess.cleanUp(con, ps, rs);
088                    }
089            }
090    
091            @Override
092            protected void doUpgrade() throws Exception {
093                    updateDLFileVersionActivities();
094                    updateJournalActivities();
095                    updateSOSocialActivities();
096                    updateWikiPageActivities();
097            }
098    
099            protected Timestamp getUniqueModifiedDate(
100                    Set<String> keys, long groupId, long userId, Timestamp modifiedDate,
101                    long classNameId, long resourcePrimKey, double type) {
102    
103                    while (true) {
104                            StringBundler sb = new StringBundler(11);
105    
106                            sb.append(groupId);
107                            sb.append(StringPool.DASH);
108                            sb.append(userId);
109                            sb.append(StringPool.DASH);
110                            sb.append(modifiedDate);
111                            sb.append(StringPool.DASH);
112                            sb.append(classNameId);
113                            sb.append(StringPool.DASH);
114                            sb.append(resourcePrimKey);
115                            sb.append(StringPool.DASH);
116                            sb.append(type);
117    
118                            String key = sb.toString();
119    
120                            modifiedDate = new Timestamp(modifiedDate.getTime() + 1);
121    
122                            if (!keys.contains(key)) {
123                                    keys.add(key);
124    
125                                    return modifiedDate;
126                            }
127                    }
128            }
129    
130            protected void updateDLFileVersionActivities() throws Exception {
131                    long classNameId = PortalUtil.getClassNameId(DLFileEntry.class);
132    
133                    runSQL("delete from SocialActivity where classNameId = " + classNameId);
134    
135                    Connection con = null;
136                    PreparedStatement ps = null;
137                    ResultSet rs = null;
138    
139                    try {
140                            Set<String> keys = new HashSet<>();
141    
142                            con = DataAccess.getUpgradeOptimizedConnection();
143    
144                            ps = con.prepareStatement(
145                                    "select groupId, companyId, userId, modifiedDate, " +
146                                            "fileEntryId, title, version from DLFileVersion " +
147                                                    "where status = ?");
148    
149                            ps.setInt(1, WorkflowConstants.STATUS_APPROVED);
150    
151                            rs = ps.executeQuery();
152    
153                            while (rs.next()) {
154                                    long groupId = rs.getLong("groupId");
155                                    long companyId = rs.getLong("companyId");
156                                    long userId = rs.getLong("userId");
157                                    Timestamp modifiedDate = rs.getTimestamp("modifiedDate");
158                                    long fileEntryId = rs.getLong("fileEntryId");
159                                    String title = rs.getString("title");
160                                    double version = rs.getDouble("version");
161    
162                                    int type = DLActivityKeys.ADD_FILE_ENTRY;
163    
164                                    if (version > 1.0) {
165                                            type = DLActivityKeys.UPDATE_FILE_ENTRY;
166                                    }
167    
168                                    modifiedDate = getUniqueModifiedDate(
169                                            keys, groupId, userId, modifiedDate, classNameId,
170                                            fileEntryId, type);
171    
172                                    JSONObject extraDataJSONObject =
173                                            JSONFactoryUtil.createJSONObject();
174    
175                                    extraDataJSONObject.put("title", title);
176    
177                                    addActivity(
178                                            increment(), groupId, companyId, userId, modifiedDate, 0,
179                                            classNameId, fileEntryId, type,
180                                            extraDataJSONObject.toString(), 0);
181                            }
182                    }
183                    finally {
184                            DataAccess.cleanUp(con, ps, rs);
185                    }
186            }
187    
188            protected void updateJournalActivities() throws Exception {
189                    long classNameId = PortalUtil.getClassNameId(
190                            "com.liferay.portlet.journal.model.JournalArticle");
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    }