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,
176 name, 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 =
363 PortalUtil.getClassNameId(MBMessage.class);
364
365 sb.append(mbMessageClassNameId);
366 sb.append(", ");
367
368 long mbThreadClassNameId =
369 PortalUtil.getClassNameId(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(
442 actionDate - 365);
443
444 int startPeriod = SocialCounterPeriodUtil.getStartPeriod(
445 actionDateDate.getTime());
446 int endPeriod = SocialCounterPeriodUtil.getEndPeriod(
447 actionDateDate.getTime());
448
449 if (endPeriod == SocialCounterPeriodUtil.getEndPeriod()) {
450 endPeriod = SocialActivityCounterConstants.END_PERIOD_UNDEFINED;
451 }
452
453 int type = rs.getInt("type_");
454 int value = rs.getInt("value");
455
456 if (type == 1) {
457 name = SocialActivityCounterConstants.NAME_CONTRIBUTION;
458 ownerType = SocialActivityCounterConstants.TYPE_CREATOR;
459
460 updateActivityCounter(
461 increment(), assetEntry.getGroupId(), assetEntry.getCompanyId(),
462 classNameId, assetEntry.getUserId(), name, ownerType,
463 startPeriod, endPeriod, value);
464
465 classNameId = assetEntry.getClassNameId();
466 classPK = assetEntry.getClassPK();
467 name = SocialActivityCounterConstants.NAME_POPULARITY;
468 ownerType = SocialActivityCounterConstants.TYPE_ASSET;
469 }
470
471 long equityLogId = rs.getLong("equityLogId");
472
473 updateActivityCounter(
474 equityLogId, assetEntry.getGroupId(), assetEntry.getCompanyId(),
475 classNameId, classPK, name, ownerType, startPeriod, endPeriod,
476 value);
477 }
478
479 protected void migrateEquityLogs() throws Exception {
480 Connection con = null;
481 PreparedStatement ps = null;
482 ResultSet rs = null;
483
484 try {
485 con = DataAccess.getConnection();
486
487 StringBundler sb = new StringBundler(4);
488
489 sb.append("select AssetEntry.classNameId, AssetEntry.classPK, ");
490 sb.append("SocialEquityLog.* from SocialEquityLog, AssetEntry ");
491 sb.append("where SocialEquityLog.assetEntryId = ");
492 sb.append("AssetEntry.entryId order by actionDate");
493
494 ps = con.prepareStatement(sb.toString());
495
496 rs = ps.executeQuery();
497
498 while (rs.next()) {
499 migrateEquityLog(rs);
500 }
501
502 DataAccess.cleanUp(null, ps, rs);
503
504 sb = new StringBundler(4);
505
506 sb.append("select groupId, classNameId, classPK, name, ");
507 sb.append("max(startPeriod) as startPeriod ");
508 sb.append("from SocialActivityCounter group by groupId, ");
509 sb.append("classNameId, classPK, name");
510
511 ps = con.prepareStatement(sb.toString());
512
513 rs = ps.executeQuery();
514
515 while (rs.next()) {
516 long groupId = rs.getLong("groupId");
517 long classNameId = rs.getLong("classNameId");
518 long classPK = rs.getLong("classPK");
519 String name = rs.getString("name");
520 int startPeriod = rs.getInt("startPeriod");
521
522 sb = new StringBundler(12);
523
524 sb.append("update SocialActivityCounter set endPeriod = ");
525 sb.append(SocialActivityCounterConstants.END_PERIOD_UNDEFINED);
526 sb.append(" where groupId = ");
527 sb.append(groupId);
528 sb.append(" and classNameId = ");
529 sb.append(classNameId);
530 sb.append(" and classPK = ");
531 sb.append(classPK);
532 sb.append(" and name = '");
533 sb.append(name);
534 sb.append("' and startPeriod = ");
535 sb.append(startPeriod);
536
537 runSQL(sb.toString());
538 }
539 }
540 finally {
541 DataAccess.cleanUp(con, ps, rs);
542 }
543 }
544
545 protected void migrateEquitySettings()
546 throws Exception {
547
548 Connection con = null;
549 PreparedStatement ps = null;
550 ResultSet rs = null;
551
552 try {
553 con = DataAccess.getConnection();
554
555 ps = con.prepareStatement(
556 "select groupId, companyId, classNameId, actionId, " +
557 "dailyLimit, type_, value from SocialEquitySetting");
558
559 rs = ps.executeQuery();
560
561 while (rs.next()) {
562 long groupId = rs.getLong("groupId");
563 long companyId = rs.getLong("companyId");
564 long classNameId = rs.getLong("classNameId");
565 String actionId = rs.getString("actionId");
566 int dailyLimit = rs.getInt("dailyLimit");
567 int type = rs.getInt("type_");
568 int value = rs.getInt("value");
569
570 Tuple tuple = _equityToActivityMap.get(
571 encodeEquityToActivityKey(classNameId, actionId));
572
573 if (tuple == null) {
574 StringBundler sb = new StringBundler();
575
576 sb.append("Unknown Social Equity setting with action ");
577 sb.append(actionId);
578 sb.append("for ");
579
580 String className = PortalUtil.getClassName(classNameId);
581
582 sb.append(className);
583
584 sb.append(". Please configure this action using the ");
585 sb.append("Social Activity portlet in the Control Panel.");
586
587 if (_log.isWarnEnabled()) {
588 _log.warn(sb.toString());
589 }
590
591 continue;
592 }
593
594 long activityClassNameId = GetterUtil.getLong(
595 tuple.getObject(0));
596 int activityType = GetterUtil.getInteger(tuple.getObject(1));
597
598 if (type == 1) {
599 addActivitySetting(
600 groupId, companyId, activityClassNameId, activityType,
601 SocialActivityCounterConstants.NAME_CONTRIBUTION,
602 SocialActivityCounterConstants.TYPE_CREATOR, dailyLimit,
603 value);
604
605 addActivitySetting(
606 groupId, companyId, activityClassNameId, activityType,
607 SocialActivityCounterConstants.NAME_POPULARITY,
608 SocialActivityCounterConstants.TYPE_ASSET, dailyLimit,
609 value);
610 }
611 else if (type == 2) {
612 addActivitySetting(
613 groupId, companyId, activityClassNameId, activityType,
614 SocialActivityCounterConstants.NAME_PARTICIPATION,
615 SocialActivityCounterConstants.TYPE_ACTOR, dailyLimit,
616 value);
617 }
618 }
619 }
620 finally {
621 DataAccess.cleanUp(con, ps, rs);
622 }
623 }
624
625 protected void putEquityToActivityMap(
626 Class<?> equityClass, String equityActionId, Class<?> activityClass,
627 int activityType) {
628
629 long equityClassNameId = PortalUtil.getClassNameId(equityClass);
630 long activityClassNameId = PortalUtil.getClassNameId(activityClass);
631
632 _equityToActivityMap.put(
633 encodeEquityToActivityKey(equityClassNameId, equityActionId),
634 new Tuple(activityClassNameId, activityType));
635 }
636
637 protected void putEquityToActivityMap(
638 Class<?> equityClass, String equityActionId, int activityType) {
639
640 putEquityToActivityMap(
641 equityClass, equityActionId, equityClass, activityType);
642 }
643
644 protected void updateActivityCounter(
645 long activityCounterId, long groupId, long companyId,
646 long classNameId, long classPK, String name, int ownerType,
647 int startPeriod, int endPeriod, int increment)
648 throws Exception {
649
650 Object[] activityCounter = getActivityCounter(
651 groupId, classNameId, classPK, name, ownerType, startPeriod,
652 endPeriod);
653
654 if (activityCounter == null) {
655 int totalValue = getTotalValue(
656 groupId, classNameId, classPK, name, ownerType, startPeriod);
657
658 addActivityCounter(
659 activityCounterId, groupId, companyId, classNameId, classPK,
660 name, ownerType, increment, totalValue + increment, 0,
661 startPeriod, endPeriod);
662
663 return;
664 }
665
666 StringBundler sb = new StringBundler(7);
667
668 sb.append("update SocialActivityCounter set currentValue = ");
669 sb.append("currentValue + ");
670 sb.append(increment);
671 sb.append(", totalValue = totalValue + ");
672 sb.append(increment);
673 sb.append(" where activityCounterId = ");
674
675 activityCounterId = GetterUtil.getLong(activityCounter[0]);
676
677 sb.append(activityCounterId);
678
679 runSQL(sb.toString());
680 }
681
682 private static Log _log = LogFactoryUtil.getLog(UpgradeSocial.class);
683
684 private Map<String, Tuple> _equityToActivityMap =
685 new HashMap<String, Tuple>();
686
687 }