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