001
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.PortalUtil;
024 import com.liferay.portal.kernel.util.StringBundler;
025 import com.liferay.portal.kernel.util.StringPool;
026 import com.liferay.portal.kernel.workflow.WorkflowConstants;
027 import com.liferay.portlet.documentlibrary.social.DLActivityKeys;
028
029 import java.sql.Connection;
030 import java.sql.PreparedStatement;
031 import java.sql.ResultSet;
032 import java.sql.Timestamp;
033
034 import java.util.HashSet;
035 import java.util.Set;
036
037
041 public class UpgradeSocial extends UpgradeProcess {
042
043 protected void addActivity(
044 long activityId, long groupId, long companyId, long userId,
045 Timestamp createDate, long mirrorActivityId, long classNameId,
046 long classPK, int type, String extraData, long receiverUserId)
047 throws Exception {
048
049 Connection con = null;
050 PreparedStatement ps = null;
051 ResultSet rs = null;
052
053 try {
054 con = DataAccess.getUpgradeOptimizedConnection();
055
056 StringBundler sb = new StringBundler(5);
057
058 sb.append("insert into SocialActivity (activityId, groupId, ");
059 sb.append("companyId, userId, createDate, mirrorActivityId, ");
060 sb.append("classNameId, classPK, type_, extraData, ");
061 sb.append("receiverUserId) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ");
062 sb.append("?)");
063
064 ps = con.prepareStatement(sb.toString());
065
066 ps.setLong(1, activityId);
067 ps.setLong(2, groupId);
068 ps.setLong(3, companyId);
069 ps.setLong(4, userId);
070 ps.setLong(5, createDate.getTime());
071 ps.setLong(6, mirrorActivityId);
072 ps.setLong(7, classNameId);
073 ps.setLong(8, classPK);
074 ps.setInt(9, type);
075 ps.setString(10, extraData);
076 ps.setLong(11, receiverUserId);
077
078 ps.executeUpdate();
079 }
080 catch (Exception e) {
081 if (_log.isWarnEnabled()) {
082 _log.warn("Unable to add activity " + activityId, e);
083 }
084 }
085 finally {
086 DataAccess.cleanUp(con, ps, rs);
087 }
088 }
089
090 @Override
091 protected void doUpgrade() throws Exception {
092 updateDLFileVersionActivities();
093 updateJournalActivities();
094 updateSOSocialActivities();
095 updateWikiPageActivities();
096 }
097
098 protected Timestamp getUniqueModifiedDate(
099 Set<String> keys, long groupId, long userId, Timestamp modifiedDate,
100 long classNameId, long resourcePrimKey, double type) {
101
102 while (true) {
103 StringBundler sb = new StringBundler(11);
104
105 sb.append(groupId);
106 sb.append(StringPool.DASH);
107 sb.append(userId);
108 sb.append(StringPool.DASH);
109 sb.append(modifiedDate);
110 sb.append(StringPool.DASH);
111 sb.append(classNameId);
112 sb.append(StringPool.DASH);
113 sb.append(resourcePrimKey);
114 sb.append(StringPool.DASH);
115 sb.append(type);
116
117 String key = sb.toString();
118
119 modifiedDate = new Timestamp(modifiedDate.getTime() + 1);
120
121 if (!keys.contains(key)) {
122 keys.add(key);
123
124 return modifiedDate;
125 }
126 }
127 }
128
129 protected void updateDLFileVersionActivities() throws Exception {
130 long classNameId = PortalUtil.getClassNameId(
131 "com.liferay.portlet.documentlibrary.model.DLFolder");
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 }