001
014
015 package com.liferay.portal.upgrade.v6_1_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.GetterUtil;
024 import com.liferay.portal.kernel.util.StringBundler;
025 import com.liferay.portal.kernel.util.StringPool;
026 import com.liferay.portal.kernel.util.Tuple;
027 import com.liferay.portal.model.User;
028 import com.liferay.portal.security.permission.ActionKeys;
029 import com.liferay.portal.util.PortalUtil;
030 import com.liferay.portlet.asset.model.AssetEntry;
031 import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
032 import com.liferay.portlet.blogs.model.BlogsEntry;
033 import com.liferay.portlet.journal.model.JournalArticle;
034 import com.liferay.portlet.messageboards.model.MBCategory;
035 import com.liferay.portlet.messageboards.model.MBMessage;
036 import com.liferay.portlet.messageboards.model.MBThread;
037 import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
038 import com.liferay.portlet.social.model.SocialActivityConstants;
039 import com.liferay.portlet.social.model.SocialActivityCounterConstants;
040 import com.liferay.portlet.social.model.SocialActivityCounterDefinition;
041 import com.liferay.portlet.social.util.SocialCounterPeriodUtil;
042 import com.liferay.portlet.wiki.model.WikiNode;
043 import com.liferay.portlet.wiki.model.WikiPage;
044
045 import java.sql.Connection;
046 import java.sql.PreparedStatement;
047 import java.sql.ResultSet;
048
049 import java.util.Date;
050 import java.util.HashMap;
051 import java.util.Map;
052
053
056 public class UpgradeSocial extends UpgradeProcess {
057
058 public UpgradeSocial() {
059 putEquityToActivityMap(
060 BlogsEntry.class, ActionKeys.ADD_DISCUSSION,
061 SocialActivityConstants.TYPE_ADD_COMMENT);
062 putEquityToActivityMap(BlogsEntry.class, ActionKeys.ADD_ENTRY, 2);
063 putEquityToActivityMap(
064 BlogsEntry.class, ActionKeys.ADD_VOTE,
065 SocialActivityConstants.TYPE_ADD_VOTE);
066 putEquityToActivityMap(
067 BlogsEntry.class, ActionKeys.SUBSCRIBE,
068 SocialActivityConstants.TYPE_SUBSCRIBE);
069 putEquityToActivityMap(BlogsEntry.class, ActionKeys.UPDATE, 3);
070 putEquityToActivityMap(
071 BlogsEntry.class, ActionKeys.VIEW,
072 SocialActivityConstants.TYPE_VIEW);
073
074 putEquityToActivityMap(JournalArticle.class, ActionKeys.ADD_ARTICLE, 1);
075 putEquityToActivityMap(
076 JournalArticle.class, ActionKeys.ADD_DISCUSSION,
077 SocialActivityConstants.TYPE_ADD_COMMENT);
078 putEquityToActivityMap(JournalArticle.class, ActionKeys.UPDATE, 2);
079 putEquityToActivityMap(
080 JournalArticle.class, ActionKeys.VIEW,
081 SocialActivityConstants.TYPE_VIEW);
082
083 putEquityToActivityMap(
084 MBCategory.class, ActionKeys.SUBSCRIBE,
085 SocialActivityConstants.TYPE_SUBSCRIBE);
086 putEquityToActivityMap(MBMessage.class, ActionKeys.ADD_MESSAGE, 1);
087 putEquityToActivityMap(
088 MBMessage.class, ActionKeys.ADD_VOTE,
089 SocialActivityConstants.TYPE_ADD_VOTE);
090 putEquityToActivityMap(MBMessage.class, ActionKeys.REPLY_TO_MESSAGE, 2);
091 putEquityToActivityMap(
092 MBMessage.class, ActionKeys.VIEW,
093 SocialActivityConstants.TYPE_VIEW);
094 putEquityToActivityMap(
095 MBThread.class, ActionKeys.SUBSCRIBE, MBMessage.class,
096 SocialActivityConstants.TYPE_SUBSCRIBE);
097
098 putEquityToActivityMap(
099 WikiNode.class, ActionKeys.SUBSCRIBE,
100 SocialActivityConstants.TYPE_SUBSCRIBE);
101 putEquityToActivityMap(
102 WikiPage.class, ActionKeys.ADD_ATTACHMENT,
103 SocialActivityConstants.TYPE_ADD_ATTACHMENT);
104 putEquityToActivityMap(
105 WikiPage.class, ActionKeys.ADD_DISCUSSION,
106 SocialActivityConstants.TYPE_ADD_COMMENT);
107 putEquityToActivityMap(WikiPage.class, ActionKeys.ADD_PAGE, 1);
108 putEquityToActivityMap(
109 WikiPage.class, ActionKeys.SUBSCRIBE,
110 SocialActivityConstants.TYPE_SUBSCRIBE);
111 putEquityToActivityMap(WikiPage.class, ActionKeys.UPDATE, 2);
112 putEquityToActivityMap(
113 WikiPage.class, ActionKeys.VIEW, SocialActivityConstants.TYPE_VIEW);
114 }
115
116 protected void addActivityCounter(
117 long activityCounterId, long groupId, long companyId,
118 long classNameId, long classPK, String name, int ownerType,
119 int currentValue, int totalValue, int graceValue, int startPeriod,
120 int endPeriod)
121 throws Exception {
122
123 Connection con = null;
124 PreparedStatement ps = null;
125 ResultSet rs = null;
126
127 try {
128 con = DataAccess.getUpgradeOptimizedConnection();
129
130 StringBundler sb = new StringBundler(5);
131
132 sb.append("insert into SocialActivityCounter (activityCounterId, ");
133 sb.append("groupId, companyId, classNameId, classPK, name, ");
134 sb.append("ownerType, currentValue, totalValue, graceValue, ");
135 sb.append("startPeriod, endPeriod) values (?, ?, ?, ?, ?, ?, ?, ");
136 sb.append("?, ?, ?, ?, ?)");
137
138 ps = con.prepareStatement(sb.toString());
139
140 ps.setLong(1, activityCounterId);
141 ps.setLong(2, groupId);
142 ps.setLong(3, companyId);
143 ps.setLong(4, classNameId);
144 ps.setLong(5, classPK);
145 ps.setString(6, name);
146 ps.setInt(7, ownerType);
147 ps.setInt(8, currentValue);
148 ps.setInt(9, totalValue);
149 ps.setInt(10, graceValue);
150 ps.setInt(11, startPeriod);
151 ps.setInt(12, endPeriod);
152
153 ps.executeUpdate();
154 }
155 finally {
156 DataAccess.cleanUp(con, ps, rs);
157 }
158 }
159
160 protected void addActivitySetting(
161 long groupId, long companyId, long classNameId, int activityType,
162 String name, int ownerType, int limitValue, int value)
163 throws Exception {
164
165 JSONObject valueJSONObject = JSONFactoryUtil.createJSONObject();
166
167 valueJSONObject.put("enabled", true);
168 valueJSONObject.put(
169 "limitPeriod", SocialActivityCounterDefinition.LIMIT_PERIOD_DAY);
170 valueJSONObject.put("limitValue", limitValue);
171 valueJSONObject.put("ownerType", ownerType);
172 valueJSONObject.put("value", value);
173
174 addActivitySetting(
175 increment(), groupId, companyId, classNameId, activityType, name,
176 valueJSONObject.toString());
177 }
178
179 protected void addActivitySetting(
180 long activitySettingId, long groupId, long companyId,
181 long classNameId, int activityType, String name, String value)
182 throws Exception {
183
184 Connection con = null;
185 PreparedStatement ps = null;
186 ResultSet rs = null;
187
188 try {
189 con = DataAccess.getUpgradeOptimizedConnection();
190
191 ps = con.prepareStatement(
192 "insert into SocialActivitySetting (activitySettingId, " +
193 "groupId, companyId, classNameId, activityType, name, " +
194 "value) values (?, ?, ?, ?, ?, ?, ?)");
195
196 ps.setLong(1, activitySettingId);
197 ps.setLong(2, groupId);
198 ps.setLong(3, companyId);
199 ps.setLong(4, classNameId);
200 ps.setInt(5, activityType);
201 ps.setString(6, name);
202 ps.setString(7, value);
203
204 ps.executeUpdate();
205 }
206 finally {
207 DataAccess.cleanUp(con, ps, rs);
208 }
209 }
210
211 @Override
212 protected void doUpgrade() throws Exception {
213 migrateEquityGroupSettings();
214 migrateEquitySettings();
215 migrateEquityLogs();
216
217 dropEquityTables();
218 }
219
220 protected void dropEquityTables() throws Exception {
221 runSQL("drop table SocialEquityAssetEntry");
222 runSQL("drop table SocialEquityGroupSetting");
223 runSQL("drop table SocialEquityHistory");
224 runSQL("drop table SocialEquityLog");
225 runSQL("drop table SocialEquitySetting");
226 runSQL("drop table SocialEquityUser");
227 }
228
229 protected String encodeEquityToActivityKey(
230 long classNameId, String actionId) {
231
232 StringBundler sb = new StringBundler(3);
233
234 sb.append(classNameId);
235 sb.append(StringPool.POUND);
236 sb.append(actionId);
237
238 return sb.toString();
239 }
240
241 protected Object[] getActivityCounter(
242 long groupId, long classNameId, long classPK, String name,
243 int ownerType, int startPeriod, int endPeriod)
244 throws Exception {
245
246 Connection con = null;
247 PreparedStatement ps = null;
248 ResultSet rs = null;
249
250 try {
251 con = DataAccess.getUpgradeOptimizedConnection();
252
253 StringBundler sb = new StringBundler(4);
254
255 sb.append("select activityCounterId, totalValue from ");
256 sb.append("SocialActivityCounter where groupId = ? and ");
257 sb.append("classNameId = ? and classPK = ? and name = ? and ");
258 sb.append("ownerType = ? and startPeriod = ? and endPeriod = ?");
259
260 ps = con.prepareStatement(sb.toString());
261
262 ps.setLong(1, groupId);
263 ps.setLong(2, classNameId);
264 ps.setLong(3, classPK);
265 ps.setString(4, name);
266 ps.setInt(5, ownerType);
267 ps.setInt(6, startPeriod);
268 ps.setInt(7, endPeriod);
269
270 rs = ps.executeQuery();
271
272 if (rs.next()) {
273 long activityCounterId = rs.getLong("activityCounterId");
274 int totalValue = rs.getInt("totalValue");
275
276 return new Object[] {activityCounterId, totalValue};
277 }
278
279 return null;
280 }
281 finally {
282 DataAccess.cleanUp(con, ps, rs);
283 }
284 }
285
286 protected int getTotalValue(
287 long groupId, long classNameId, long classPK, String name,
288 int ownerType, int startPeriod)
289 throws Exception {
290
291 Connection con = null;
292 PreparedStatement ps = null;
293 ResultSet rs = null;
294
295 try {
296 con = DataAccess.getUpgradeOptimizedConnection();
297
298 StringBundler sb = new StringBundler(4);
299
300 sb.append("select max(totalValue) as totalValue from ");
301 sb.append("SocialActivityCounter where groupId = ? and ");
302 sb.append("classNameId = ? and classPK = ? and name = ? and ");
303 sb.append("ownerType = ? and startPeriod < ?");
304
305 ps = con.prepareStatement(sb.toString());
306
307 ps.setLong(1, groupId);
308 ps.setLong(2, classNameId);
309 ps.setLong(3, classPK);
310 ps.setString(4, name);
311 ps.setInt(5, ownerType);
312 ps.setInt(6, startPeriod);
313
314 rs = ps.executeQuery();
315
316 if (rs.next()) {
317 return rs.getInt("totalValue");
318 }
319
320 return 0;
321 }
322 finally {
323 DataAccess.cleanUp(con, ps, rs);
324 }
325 }
326
327 protected void migrateEquityGroupSettings() throws Exception {
328 Connection con = null;
329 PreparedStatement ps = null;
330 ResultSet rs = null;
331
332 try {
333 con = DataAccess.getUpgradeOptimizedConnection();
334
335 ps = con.prepareStatement(
336 "select groupId, companyId, classNameId, enabled from " +
337 "SocialEquityGroupSetting where type_ = 1");
338
339 rs = ps.executeQuery();
340
341 while (rs.next()) {
342 long groupId = rs.getLong("groupId");
343 long companyId = rs.getLong("companyId");
344 long classNameId = rs.getLong("classNameId");
345 boolean enabled = rs.getBoolean("enabled");
346
347 addActivitySetting(
348 increment(), groupId, companyId, classNameId, 0, "enabled",
349 String.valueOf(enabled));
350 }
351
352 DataAccess.cleanUp(null, ps, rs);
353
354 StringBundler sb = new StringBundler(12);
355
356 sb.append("select groupId from SocialActivitySetting where ");
357 sb.append("activityType = 0 and name = 'enabled' and ");
358 sb.append("value = 'true' and classNameId in (");
359
360 long mbMessageClassNameId = PortalUtil.getClassNameId(
361 MBMessage.class);
362
363 sb.append(mbMessageClassNameId);
364 sb.append(", ");
365
366 long mbThreadClassNameId = PortalUtil.getClassNameId(
367 MBThread.class);
368
369 sb.append(mbThreadClassNameId);
370 sb.append(")");
371
372 ps = con.prepareStatement(sb.toString());
373
374 rs = ps.executeQuery();
375
376 while (rs.next()) {
377 long groupId = rs.getLong("groupId");
378
379 sb = new StringBundler(6);
380
381 sb.append("update SocialActivitySetting set value = 'true' ");
382 sb.append("where groupId = ");
383 sb.append(groupId);
384 sb.append(" and activityType = 0 and value = 'enabled' and ");
385 sb.append("classNameId = ");
386 sb.append(mbThreadClassNameId);
387
388 runSQL(sb.toString());
389 }
390
391 runSQL(
392 "delete from SocialActivitySetting where classNameId = " +
393 mbThreadClassNameId);
394 }
395 finally {
396 DataAccess.cleanUp(con, ps, rs);
397 }
398 }
399
400 protected void migrateEquityLog(ResultSet rs) throws Exception {
401 long assetEntryId = rs.getLong("assetEntryId");
402
403 AssetEntry assetEntry = AssetEntryLocalServiceUtil.fetchAssetEntry(
404 assetEntryId);
405
406 if (assetEntry == null) {
407 return;
408 }
409
410 String actionId = rs.getString("actionId");
411
412 if (actionId.equals(ActionKeys.SUBSCRIBE)) {
413 String className = assetEntry.getClassName();
414
415 if (className.equals(MBThread.class.getName())) {
416 MBThread mbThread = MBThreadLocalServiceUtil.fetchThread(
417 assetEntry.getClassPK());
418
419 if (mbThread == null) {
420 return;
421 }
422
423 assetEntry = AssetEntryLocalServiceUtil.fetchEntry(
424 MBMessage.class.getName(), mbThread.getRootMessageId());
425
426 if (assetEntry == null) {
427 return;
428 }
429 }
430 }
431
432 long classNameId = PortalUtil.getClassNameId(User.class);
433 long classPK = rs.getLong("userId");
434 String name = SocialActivityCounterConstants.NAME_PARTICIPATION;
435 int ownerType = SocialActivityCounterConstants.TYPE_ACTOR;
436
437 int actionDate = rs.getInt("actionDate");
438
439 Date actionDateDate = SocialCounterPeriodUtil.getDate(actionDate - 365);
440
441 int startPeriod = SocialCounterPeriodUtil.getStartPeriod(
442 actionDateDate.getTime());
443 int endPeriod = SocialCounterPeriodUtil.getEndPeriod(
444 actionDateDate.getTime());
445
446 if (endPeriod == SocialCounterPeriodUtil.getEndPeriod()) {
447 endPeriod = SocialActivityCounterConstants.END_PERIOD_UNDEFINED;
448 }
449
450 int type = rs.getInt("type_");
451 int value = rs.getInt("value");
452
453 if (type == 1) {
454 name = SocialActivityCounterConstants.NAME_CONTRIBUTION;
455 ownerType = SocialActivityCounterConstants.TYPE_CREATOR;
456
457 updateActivityCounter(
458 increment(), assetEntry.getGroupId(), assetEntry.getCompanyId(),
459 classNameId, assetEntry.getUserId(), name, ownerType,
460 startPeriod, endPeriod, value);
461
462 classNameId = assetEntry.getClassNameId();
463 classPK = assetEntry.getClassPK();
464 name = SocialActivityCounterConstants.NAME_POPULARITY;
465 ownerType = SocialActivityCounterConstants.TYPE_ASSET;
466 }
467
468 long equityLogId = rs.getLong("equityLogId");
469
470 updateActivityCounter(
471 equityLogId, assetEntry.getGroupId(), assetEntry.getCompanyId(),
472 classNameId, classPK, name, ownerType, startPeriod, endPeriod,
473 value);
474 }
475
476 protected void migrateEquityLogs() throws Exception {
477 Connection con = null;
478 PreparedStatement ps = null;
479 ResultSet rs = null;
480
481 try {
482 con = DataAccess.getUpgradeOptimizedConnection();
483
484 StringBundler sb = new StringBundler(4);
485
486 sb.append("select AssetEntry.classNameId, AssetEntry.classPK, ");
487 sb.append("SocialEquityLog.* from SocialEquityLog, AssetEntry ");
488 sb.append("where SocialEquityLog.assetEntryId = ");
489 sb.append("AssetEntry.entryId order by actionDate");
490
491 ps = con.prepareStatement(sb.toString());
492
493 rs = ps.executeQuery();
494
495 while (rs.next()) {
496 migrateEquityLog(rs);
497 }
498
499 DataAccess.cleanUp(null, ps, rs);
500
501 sb = new StringBundler(4);
502
503 sb.append("select groupId, classNameId, classPK, name, ");
504 sb.append("max(startPeriod) as startPeriod ");
505 sb.append("from SocialActivityCounter group by groupId, ");
506 sb.append("classNameId, classPK, name");
507
508 ps = con.prepareStatement(sb.toString());
509
510 rs = ps.executeQuery();
511
512 while (rs.next()) {
513 long groupId = rs.getLong("groupId");
514 long classNameId = rs.getLong("classNameId");
515 long classPK = rs.getLong("classPK");
516 String name = rs.getString("name");
517 int startPeriod = rs.getInt("startPeriod");
518
519 sb = new StringBundler(12);
520
521 sb.append("update SocialActivityCounter set endPeriod = ");
522 sb.append(SocialActivityCounterConstants.END_PERIOD_UNDEFINED);
523 sb.append(" where groupId = ");
524 sb.append(groupId);
525 sb.append(" and classNameId = ");
526 sb.append(classNameId);
527 sb.append(" and classPK = ");
528 sb.append(classPK);
529 sb.append(" and name = '");
530 sb.append(name);
531 sb.append("' and startPeriod = ");
532 sb.append(startPeriod);
533
534 runSQL(sb.toString());
535 }
536 }
537 finally {
538 DataAccess.cleanUp(con, ps, rs);
539 }
540 }
541
542 protected void migrateEquitySettings() throws Exception {
543 Connection con = null;
544 PreparedStatement ps = null;
545 ResultSet rs = null;
546
547 try {
548 con = DataAccess.getUpgradeOptimizedConnection();
549
550 ps = con.prepareStatement(
551 "select groupId, companyId, classNameId, actionId, " +
552 "dailyLimit, type_, value from SocialEquitySetting");
553
554 rs = ps.executeQuery();
555
556 while (rs.next()) {
557 long groupId = rs.getLong("groupId");
558 long companyId = rs.getLong("companyId");
559 long classNameId = rs.getLong("classNameId");
560 String actionId = rs.getString("actionId");
561 int dailyLimit = rs.getInt("dailyLimit");
562 int type = rs.getInt("type_");
563 int value = rs.getInt("value");
564
565 Tuple tuple = _equityToActivityMap.get(
566 encodeEquityToActivityKey(classNameId, actionId));
567
568 if (tuple == null) {
569 StringBundler sb = new StringBundler();
570
571 sb.append("Unknown Social Equity setting with action ");
572 sb.append(actionId);
573 sb.append("for ");
574
575 String className = PortalUtil.getClassName(classNameId);
576
577 sb.append(className);
578
579 sb.append(". Please configure this action using the ");
580 sb.append("Social Activity portlet in the Control Panel.");
581
582 if (_log.isWarnEnabled()) {
583 _log.warn(sb.toString());
584 }
585
586 continue;
587 }
588
589 long activityClassNameId = GetterUtil.getLong(
590 tuple.getObject(0));
591 int activityType = GetterUtil.getInteger(tuple.getObject(1));
592
593 if (type == 1) {
594 addActivitySetting(
595 groupId, companyId, activityClassNameId, activityType,
596 SocialActivityCounterConstants.NAME_CONTRIBUTION,
597 SocialActivityCounterConstants.TYPE_CREATOR, dailyLimit,
598 value);
599
600 addActivitySetting(
601 groupId, companyId, activityClassNameId, activityType,
602 SocialActivityCounterConstants.NAME_POPULARITY,
603 SocialActivityCounterConstants.TYPE_ASSET, dailyLimit,
604 value);
605 }
606 else if (type == 2) {
607 addActivitySetting(
608 groupId, companyId, activityClassNameId, activityType,
609 SocialActivityCounterConstants.NAME_PARTICIPATION,
610 SocialActivityCounterConstants.TYPE_ACTOR, dailyLimit,
611 value);
612 }
613 }
614 }
615 finally {
616 DataAccess.cleanUp(con, ps, rs);
617 }
618 }
619
620 protected void putEquityToActivityMap(
621 Class<?> equityClass, String equityActionId, Class<?> activityClass,
622 int activityType) {
623
624 long equityClassNameId = PortalUtil.getClassNameId(equityClass);
625 long activityClassNameId = PortalUtil.getClassNameId(activityClass);
626
627 _equityToActivityMap.put(
628 encodeEquityToActivityKey(equityClassNameId, equityActionId),
629 new Tuple(activityClassNameId, activityType));
630 }
631
632 protected void putEquityToActivityMap(
633 Class<?> equityClass, String equityActionId, int activityType) {
634
635 putEquityToActivityMap(
636 equityClass, equityActionId, equityClass, activityType);
637 }
638
639 protected void updateActivityCounter(
640 long activityCounterId, long groupId, long companyId,
641 long classNameId, long classPK, String name, int ownerType,
642 int startPeriod, int endPeriod, int increment)
643 throws Exception {
644
645 Object[] activityCounter = getActivityCounter(
646 groupId, classNameId, classPK, name, ownerType, startPeriod,
647 endPeriod);
648
649 if (activityCounter == null) {
650 int totalValue = getTotalValue(
651 groupId, classNameId, classPK, name, ownerType, startPeriod);
652
653 addActivityCounter(
654 activityCounterId, groupId, companyId, classNameId, classPK,
655 name, ownerType, increment, totalValue + increment, 0,
656 startPeriod, endPeriod);
657
658 return;
659 }
660
661 StringBundler sb = new StringBundler(7);
662
663 sb.append("update SocialActivityCounter set currentValue = ");
664 sb.append("currentValue + ");
665 sb.append(increment);
666 sb.append(", totalValue = totalValue + ");
667 sb.append(increment);
668 sb.append(" where activityCounterId = ");
669
670 activityCounterId = GetterUtil.getLong(activityCounter[0]);
671
672 sb.append(activityCounterId);
673
674 runSQL(sb.toString());
675 }
676
677 private static Log _log = LogFactoryUtil.getLog(UpgradeSocial.class);
678
679 private Map<String, Tuple> _equityToActivityMap =
680 new HashMap<String, Tuple>();
681
682 }