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.getConnection();
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.getConnection();
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.getConnection();
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.getConnection();
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()
328 throws Exception {
329
330 Connection con = null;
331 PreparedStatement ps = null;
332 ResultSet rs = null;
333
334 try {
335 con = DataAccess.getConnection();
336
337 ps = con.prepareStatement(
338 "select groupId, companyId, classNameId, enabled from " +
339 "SocialEquityGroupSetting where type_ = 1");
340
341 rs = ps.executeQuery();
342
343 while (rs.next()) {
344 long groupId = rs.getLong("groupId");
345 long companyId = rs.getLong("companyId");
346 long classNameId = rs.getLong("classNameId");
347 boolean enabled = rs.getBoolean("enabled");
348
349 addActivitySetting(
350 increment(), groupId, companyId, classNameId, 0, "enabled",
351 String.valueOf(enabled));
352 }
353
354 DataAccess.cleanUp(null, ps, rs);
355
356 StringBundler sb = new StringBundler(12);
357
358 sb.append("select groupId from SocialActivitySetting where ");
359 sb.append("activityType = 0 and name = 'enabled' and ");
360 sb.append("value = 'true' and classNameId in (");
361
362 long mbMessageClassNameId = PortalUtil.getClassNameId(
363 MBMessage.class);
364
365 sb.append(mbMessageClassNameId);
366 sb.append(", ");
367
368 long mbThreadClassNameId = PortalUtil.getClassNameId(
369 MBThread.class);
370
371 sb.append(mbThreadClassNameId);
372 sb.append(")");
373
374 ps = con.prepareStatement(sb.toString());
375
376 rs = ps.executeQuery();
377
378 while (rs.next()) {
379 long groupId = rs.getLong("groupId");
380
381 sb = new StringBundler(6);
382
383 sb.append("update SocialActivitySetting set value = 'true' ");
384 sb.append("where groupId = ");
385 sb.append(groupId);
386 sb.append(" and activityType = 0 and value = 'enabled' and ");
387 sb.append("classNameId = ");
388 sb.append(mbThreadClassNameId);
389
390 runSQL(sb.toString());
391 }
392
393 runSQL(
394 "delete from SocialActivitySetting where classNameId = " +
395 mbThreadClassNameId);
396 }
397 finally {
398 DataAccess.cleanUp(con, ps, rs);
399 }
400 }
401
402 protected void migrateEquityLog(ResultSet rs) throws Exception {
403 long assetEntryId = rs.getLong("assetEntryId");
404
405 AssetEntry assetEntry = AssetEntryLocalServiceUtil.fetchAssetEntry(
406 assetEntryId);
407
408 if (assetEntry == null) {
409 return;
410 }
411
412 String actionId = rs.getString("actionId");
413
414 if (actionId.equals(ActionKeys.SUBSCRIBE)) {
415 String className = assetEntry.getClassName();
416
417 if (className.equals(MBThread.class.getName())) {
418 MBThread mbThread = MBThreadLocalServiceUtil.fetchThread(
419 assetEntry.getClassPK());
420
421 if (mbThread == null) {
422 return;
423 }
424
425 assetEntry = AssetEntryLocalServiceUtil.fetchEntry(
426 MBMessage.class.getName(), mbThread.getRootMessageId());
427
428 if (assetEntry == null) {
429 return;
430 }
431 }
432 }
433
434 long classNameId = PortalUtil.getClassNameId(User.class);
435 long classPK = rs.getLong("userId");
436 String name = SocialActivityCounterConstants.NAME_PARTICIPATION;
437 int ownerType = SocialActivityCounterConstants.TYPE_ACTOR;
438
439 int actionDate = rs.getInt("actionDate");
440
441 Date actionDateDate = SocialCounterPeriodUtil.getDate(actionDate - 365);
442
443 int startPeriod = SocialCounterPeriodUtil.getStartPeriod(
444 actionDateDate.getTime());
445 int endPeriod = SocialCounterPeriodUtil.getEndPeriod(
446 actionDateDate.getTime());
447
448 if (endPeriod == SocialCounterPeriodUtil.getEndPeriod()) {
449 endPeriod = SocialActivityCounterConstants.END_PERIOD_UNDEFINED;
450 }
451
452 int type = rs.getInt("type_");
453 int value = rs.getInt("value");
454
455 if (type == 1) {
456 name = SocialActivityCounterConstants.NAME_CONTRIBUTION;
457 ownerType = SocialActivityCounterConstants.TYPE_CREATOR;
458
459 updateActivityCounter(
460 increment(), assetEntry.getGroupId(), assetEntry.getCompanyId(),
461 classNameId, assetEntry.getUserId(), name, ownerType,
462 startPeriod, endPeriod, value);
463
464 classNameId = assetEntry.getClassNameId();
465 classPK = assetEntry.getClassPK();
466 name = SocialActivityCounterConstants.NAME_POPULARITY;
467 ownerType = SocialActivityCounterConstants.TYPE_ASSET;
468 }
469
470 long equityLogId = rs.getLong("equityLogId");
471
472 updateActivityCounter(
473 equityLogId, assetEntry.getGroupId(), assetEntry.getCompanyId(),
474 classNameId, classPK, name, ownerType, startPeriod, endPeriod,
475 value);
476 }
477
478 protected void migrateEquityLogs() throws Exception {
479 Connection con = null;
480 PreparedStatement ps = null;
481 ResultSet rs = null;
482
483 try {
484 con = DataAccess.getConnection();
485
486 StringBundler sb = new StringBundler(4);
487
488 sb.append("select AssetEntry.classNameId, AssetEntry.classPK, ");
489 sb.append("SocialEquityLog.* from SocialEquityLog, AssetEntry ");
490 sb.append("where SocialEquityLog.assetEntryId = ");
491 sb.append("AssetEntry.entryId order by actionDate");
492
493 ps = con.prepareStatement(sb.toString());
494
495 rs = ps.executeQuery();
496
497 while (rs.next()) {
498 migrateEquityLog(rs);
499 }
500
501 DataAccess.cleanUp(null, ps, rs);
502
503 sb = new StringBundler(4);
504
505 sb.append("select groupId, classNameId, classPK, name, ");
506 sb.append("max(startPeriod) as startPeriod ");
507 sb.append("from SocialActivityCounter group by groupId, ");
508 sb.append("classNameId, classPK, name");
509
510 ps = con.prepareStatement(sb.toString());
511
512 rs = ps.executeQuery();
513
514 while (rs.next()) {
515 long groupId = rs.getLong("groupId");
516 long classNameId = rs.getLong("classNameId");
517 long classPK = rs.getLong("classPK");
518 String name = rs.getString("name");
519 int startPeriod = rs.getInt("startPeriod");
520
521 sb = new StringBundler(12);
522
523 sb.append("update SocialActivityCounter set endPeriod = ");
524 sb.append(SocialActivityCounterConstants.END_PERIOD_UNDEFINED);
525 sb.append(" where groupId = ");
526 sb.append(groupId);
527 sb.append(" and classNameId = ");
528 sb.append(classNameId);
529 sb.append(" and classPK = ");
530 sb.append(classPK);
531 sb.append(" and name = '");
532 sb.append(name);
533 sb.append("' and startPeriod = ");
534 sb.append(startPeriod);
535
536 runSQL(sb.toString());
537 }
538 }
539 finally {
540 DataAccess.cleanUp(con, ps, rs);
541 }
542 }
543
544 protected void migrateEquitySettings()
545 throws Exception {
546
547 Connection con = null;
548 PreparedStatement ps = null;
549 ResultSet rs = null;
550
551 try {
552 con = DataAccess.getConnection();
553
554 ps = con.prepareStatement(
555 "select groupId, companyId, classNameId, actionId, " +
556 "dailyLimit, type_, value from SocialEquitySetting");
557
558 rs = ps.executeQuery();
559
560 while (rs.next()) {
561 long groupId = rs.getLong("groupId");
562 long companyId = rs.getLong("companyId");
563 long classNameId = rs.getLong("classNameId");
564 String actionId = rs.getString("actionId");
565 int dailyLimit = rs.getInt("dailyLimit");
566 int type = rs.getInt("type_");
567 int value = rs.getInt("value");
568
569 Tuple tuple = _equityToActivityMap.get(
570 encodeEquityToActivityKey(classNameId, actionId));
571
572 if (tuple == null) {
573 StringBundler sb = new StringBundler();
574
575 sb.append("Unknown Social Equity setting with action ");
576 sb.append(actionId);
577 sb.append("for ");
578
579 String className = PortalUtil.getClassName(classNameId);
580
581 sb.append(className);
582
583 sb.append(". Please configure this action using the ");
584 sb.append("Social Activity portlet in the Control Panel.");
585
586 if (_log.isWarnEnabled()) {
587 _log.warn(sb.toString());
588 }
589
590 continue;
591 }
592
593 long activityClassNameId = GetterUtil.getLong(
594 tuple.getObject(0));
595 int activityType = GetterUtil.getInteger(tuple.getObject(1));
596
597 if (type == 1) {
598 addActivitySetting(
599 groupId, companyId, activityClassNameId, activityType,
600 SocialActivityCounterConstants.NAME_CONTRIBUTION,
601 SocialActivityCounterConstants.TYPE_CREATOR, dailyLimit,
602 value);
603
604 addActivitySetting(
605 groupId, companyId, activityClassNameId, activityType,
606 SocialActivityCounterConstants.NAME_POPULARITY,
607 SocialActivityCounterConstants.TYPE_ASSET, dailyLimit,
608 value);
609 }
610 else if (type == 2) {
611 addActivitySetting(
612 groupId, companyId, activityClassNameId, activityType,
613 SocialActivityCounterConstants.NAME_PARTICIPATION,
614 SocialActivityCounterConstants.TYPE_ACTOR, dailyLimit,
615 value);
616 }
617 }
618 }
619 finally {
620 DataAccess.cleanUp(con, ps, rs);
621 }
622 }
623
624 protected void putEquityToActivityMap(
625 Class<?> equityClass, String equityActionId, Class<?> activityClass,
626 int activityType) {
627
628 long equityClassNameId = PortalUtil.getClassNameId(equityClass);
629 long activityClassNameId = PortalUtil.getClassNameId(activityClass);
630
631 _equityToActivityMap.put(
632 encodeEquityToActivityKey(equityClassNameId, equityActionId),
633 new Tuple(activityClassNameId, activityType));
634 }
635
636 protected void putEquityToActivityMap(
637 Class<?> equityClass, String equityActionId, int activityType) {
638
639 putEquityToActivityMap(
640 equityClass, equityActionId, equityClass, activityType);
641 }
642
643 protected void updateActivityCounter(
644 long activityCounterId, long groupId, long companyId,
645 long classNameId, long classPK, String name, int ownerType,
646 int startPeriod, int endPeriod, int increment)
647 throws Exception {
648
649 Object[] activityCounter = getActivityCounter(
650 groupId, classNameId, classPK, name, ownerType, startPeriod,
651 endPeriod);
652
653 if (activityCounter == null) {
654 int totalValue = getTotalValue(
655 groupId, classNameId, classPK, name, ownerType, startPeriod);
656
657 addActivityCounter(
658 activityCounterId, groupId, companyId, classNameId, classPK,
659 name, ownerType, increment, totalValue + increment, 0,
660 startPeriod, endPeriod);
661
662 return;
663 }
664
665 StringBundler sb = new StringBundler(7);
666
667 sb.append("update SocialActivityCounter set currentValue = ");
668 sb.append("currentValue + ");
669 sb.append(increment);
670 sb.append(", totalValue = totalValue + ");
671 sb.append(increment);
672 sb.append(" where activityCounterId = ");
673
674 activityCounterId = GetterUtil.getLong(activityCounter[0]);
675
676 sb.append(activityCounterId);
677
678 runSQL(sb.toString());
679 }
680
681 private static Log _log = LogFactoryUtil.getLog(UpgradeSocial.class);
682
683 private Map<String, Tuple> _equityToActivityMap =
684 new HashMap<String, Tuple>();
685
686 }