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