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.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.blogs.model.BlogsEntry;
031    import com.liferay.portlet.messageboards.model.MBCategory;
032    import com.liferay.portlet.messageboards.model.MBMessage;
033    import com.liferay.portlet.messageboards.model.MBThread;
034    import com.liferay.portlet.social.model.SocialActivityConstants;
035    import com.liferay.portlet.social.model.SocialActivityCounterConstants;
036    import com.liferay.portlet.social.model.SocialActivityCounterDefinition;
037    import com.liferay.portlet.social.util.SocialCounterPeriodUtil;
038    
039    import java.sql.Connection;
040    import java.sql.PreparedStatement;
041    import java.sql.ResultSet;
042    
043    import java.util.Date;
044    import java.util.HashMap;
045    import java.util.Map;
046    
047    /**
048     * @author Zsolt Berentey
049     */
050    public class UpgradeSocial extends UpgradeProcess {
051    
052            public UpgradeSocial() {
053                    putEquityToActivityMap(
054                            BlogsEntry.class.getName(), ActionKeys.ADD_DISCUSSION,
055                            SocialActivityConstants.TYPE_ADD_COMMENT);
056                    putEquityToActivityMap(
057                            BlogsEntry.class.getName(), ActionKeys.ADD_ENTRY, 2);
058                    putEquityToActivityMap(
059                            BlogsEntry.class.getName(), ActionKeys.ADD_VOTE,
060                            SocialActivityConstants.TYPE_ADD_VOTE);
061                    putEquityToActivityMap(
062                            BlogsEntry.class.getName(), ActionKeys.SUBSCRIBE,
063                            SocialActivityConstants.TYPE_SUBSCRIBE);
064                    putEquityToActivityMap(
065                            BlogsEntry.class.getName(), ActionKeys.UPDATE, 3);
066                    putEquityToActivityMap(
067                            BlogsEntry.class.getName(), ActionKeys.VIEW,
068                            SocialActivityConstants.TYPE_VIEW);
069    
070                    putEquityToActivityMap(
071                            MBCategory.class.getName(), ActionKeys.SUBSCRIBE,
072                            SocialActivityConstants.TYPE_SUBSCRIBE);
073                    putEquityToActivityMap(
074                            MBMessage.class.getName(), ActionKeys.ADD_MESSAGE, 1);
075                    putEquityToActivityMap(
076                            MBMessage.class.getName(), ActionKeys.ADD_VOTE,
077                            SocialActivityConstants.TYPE_ADD_VOTE);
078                    putEquityToActivityMap(
079                            MBMessage.class.getName(), ActionKeys.REPLY_TO_MESSAGE, 2);
080                    putEquityToActivityMap(
081                            MBMessage.class.getName(), ActionKeys.VIEW,
082                            SocialActivityConstants.TYPE_VIEW);
083                    putEquityToActivityMap(
084                            MBThread.class.getName(), ActionKeys.SUBSCRIBE,
085                            MBMessage.class.getName(), 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.wiki.model.WikiNode", ActionKeys.SUBSCRIBE,
103                            SocialActivityConstants.TYPE_SUBSCRIBE);
104                    putEquityToActivityMap(
105                            "com.liferay.wiki.model.WikiPage", ActionKeys.ADD_ATTACHMENT,
106                            SocialActivityConstants.TYPE_ADD_ATTACHMENT);
107                    putEquityToActivityMap(
108                            "com.liferay.wiki.model.WikiPage", ActionKeys.ADD_DISCUSSION,
109                            SocialActivityConstants.TYPE_ADD_COMMENT);
110                    putEquityToActivityMap(
111                            "com.liferay.wiki.model.WikiPage", ActionKeys.ADD_PAGE, 1);
112                    putEquityToActivityMap(
113                            "com.liferay.wiki.model.WikiPage", ActionKeys.SUBSCRIBE,
114                            SocialActivityConstants.TYPE_SUBSCRIBE);
115                    putEquityToActivityMap(
116                            "com.liferay.wiki.model.WikiPage", ActionKeys.UPDATE, 2);
117                    putEquityToActivityMap(
118                            "com.liferay.wiki.model.WikiPage", ActionKeys.VIEW,
119                            SocialActivityConstants.TYPE_VIEW);
120            }
121    
122            protected void addActivityCounter(
123                            long activityCounterId, long groupId, long companyId,
124                            long classNameId, long classPK, String name, int ownerType,
125                            int currentValue, int totalValue, int graceValue, int startPeriod,
126                            int endPeriod)
127                    throws Exception {
128    
129                    Connection con = null;
130                    PreparedStatement ps = null;
131                    ResultSet rs = null;
132    
133                    try {
134                            con = DataAccess.getUpgradeOptimizedConnection();
135    
136                            StringBundler sb = new StringBundler(5);
137    
138                            sb.append("insert into SocialActivityCounter (activityCounterId, ");
139                            sb.append("groupId, companyId, classNameId, classPK, name, ");
140                            sb.append("ownerType, currentValue, totalValue, graceValue, ");
141                            sb.append("startPeriod, endPeriod) values (?, ?, ?, ?, ?, ?, ?, ");
142                            sb.append("?, ?, ?, ?, ?)");
143    
144                            ps = con.prepareStatement(sb.toString());
145    
146                            ps.setLong(1, activityCounterId);
147                            ps.setLong(2, groupId);
148                            ps.setLong(3, companyId);
149                            ps.setLong(4, classNameId);
150                            ps.setLong(5, classPK);
151                            ps.setString(6, name);
152                            ps.setInt(7, ownerType);
153                            ps.setInt(8, currentValue);
154                            ps.setInt(9, totalValue);
155                            ps.setInt(10, graceValue);
156                            ps.setInt(11, startPeriod);
157                            ps.setInt(12, endPeriod);
158    
159                            ps.executeUpdate();
160                    }
161                    finally {
162                            DataAccess.cleanUp(con, ps, rs);
163                    }
164            }
165    
166            protected void addActivitySetting(
167                            long groupId, long companyId, long classNameId, int activityType,
168                            String name, int ownerType, int limitValue, int value)
169                    throws Exception {
170    
171                    JSONObject valueJSONObject = JSONFactoryUtil.createJSONObject();
172    
173                    valueJSONObject.put("enabled", true);
174                    valueJSONObject.put(
175                            "limitPeriod", SocialActivityCounterDefinition.LIMIT_PERIOD_DAY);
176                    valueJSONObject.put("limitValue", limitValue);
177                    valueJSONObject.put("ownerType", ownerType);
178                    valueJSONObject.put("value", value);
179    
180                    addActivitySetting(
181                            increment(), groupId, companyId, classNameId, activityType, name,
182                            valueJSONObject.toString());
183            }
184    
185            protected void addActivitySetting(
186                            long activitySettingId, long groupId, long companyId,
187                            long classNameId, int activityType, String name, String value)
188                    throws Exception {
189    
190                    Connection con = null;
191                    PreparedStatement ps = null;
192                    ResultSet rs = null;
193    
194                    try {
195                            con = DataAccess.getUpgradeOptimizedConnection();
196    
197                            ps = con.prepareStatement(
198                                    "insert into SocialActivitySetting (activitySettingId, " +
199                                            "groupId, companyId, classNameId, activityType, name, " +
200                                                    "value) values (?, ?, ?, ?, ?, ?, ?)");
201    
202                            ps.setLong(1, activitySettingId);
203                            ps.setLong(2, groupId);
204                            ps.setLong(3, companyId);
205                            ps.setLong(4, classNameId);
206                            ps.setInt(5, activityType);
207                            ps.setString(6, name);
208                            ps.setString(7, value);
209    
210                            ps.executeUpdate();
211                    }
212                    finally {
213                            DataAccess.cleanUp(con, ps, rs);
214                    }
215            }
216    
217            @Override
218            protected void doUpgrade() throws Exception {
219                    migrateEquityGroupSettings();
220                    migrateEquitySettings();
221                    migrateEquityLogs();
222    
223                    dropEquityTables();
224            }
225    
226            protected void dropEquityTables() throws Exception {
227                    runSQL("drop table SocialEquityAssetEntry");
228                    runSQL("drop table SocialEquityGroupSetting");
229                    runSQL("drop table SocialEquityHistory");
230                    runSQL("drop table SocialEquityLog");
231                    runSQL("drop table SocialEquitySetting");
232                    runSQL("drop table SocialEquityUser");
233            }
234    
235            protected String encodeEquityToActivityKey(
236                    long classNameId, String actionId) {
237    
238                    StringBundler sb = new StringBundler(3);
239    
240                    sb.append(classNameId);
241                    sb.append(StringPool.POUND);
242                    sb.append(actionId);
243    
244                    return sb.toString();
245            }
246    
247            protected Object[] getActivityCounter(
248                            long groupId, long classNameId, long classPK, String name,
249                            int ownerType, int startPeriod, int endPeriod)
250                    throws Exception {
251    
252                    Connection con = null;
253                    PreparedStatement ps = null;
254                    ResultSet rs = null;
255    
256                    try {
257                            con = DataAccess.getUpgradeOptimizedConnection();
258    
259                            StringBundler sb = new StringBundler(4);
260    
261                            sb.append("select activityCounterId, totalValue from ");
262                            sb.append("SocialActivityCounter where groupId = ? and ");
263                            sb.append("classNameId = ? and classPK = ? and name = ? and ");
264                            sb.append("ownerType = ? and startPeriod = ? and endPeriod = ?");
265    
266                            ps = con.prepareStatement(sb.toString());
267    
268                            ps.setLong(1, groupId);
269                            ps.setLong(2, classNameId);
270                            ps.setLong(3, classPK);
271                            ps.setString(4, name);
272                            ps.setInt(5, ownerType);
273                            ps.setInt(6, startPeriod);
274                            ps.setInt(7, endPeriod);
275    
276                            rs = ps.executeQuery();
277    
278                            if (rs.next()) {
279                                    long activityCounterId = rs.getLong("activityCounterId");
280                                    int totalValue = rs.getInt("totalValue");
281    
282                                    return new Object[] {activityCounterId, totalValue};
283                            }
284    
285                            return null;
286                    }
287                    finally {
288                            DataAccess.cleanUp(con, ps, rs);
289                    }
290            }
291    
292            protected long[] getAssetEntryArray(long assetEntryId) throws Exception {
293                    Connection con = null;
294                    PreparedStatement ps = null;
295                    ResultSet rs = null;
296    
297                    try {
298                            con = DataAccess.getUpgradeOptimizedConnection();
299    
300                            ps = con.prepareStatement(
301                                    "select groupId, companyId, userId, classNameId, classPK " +
302                                            "from AssetEntry where entryId = " + assetEntryId);
303    
304                            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                                    long classNameId = rs.getLong("classNameId");
311                                    long classPK = rs.getLong("classPK");
312    
313                                    return new long[] {
314                                            groupId, companyId, userId, classNameId, classPK
315                                    };
316                            }
317    
318                            return null;
319                    }
320                    finally {
321                            DataAccess.cleanUp(con, ps, rs);
322                    }
323            }
324    
325            protected long[] getAssetEntryArray(String className, long classPK)
326                    throws Exception {
327    
328                    Connection con = null;
329                    PreparedStatement ps = null;
330                    ResultSet rs = null;
331    
332                    try {
333                            con = DataAccess.getUpgradeOptimizedConnection();
334    
335                            long classNameId = PortalUtil.getClassNameId(className);
336    
337                            StringBundler sb = new StringBundler(5);
338    
339                            sb.append("select groupId, companyId, userId from AssetEntry ");
340                            sb.append("where classNameId = ");
341                            sb.append(classNameId);
342                            sb.append(" and classPK = ");
343                            sb.append(classPK);
344    
345                            ps = con.prepareStatement(sb.toString());
346    
347                            rs = ps.executeQuery();
348    
349                            if (rs.next()) {
350                                    long groupId = rs.getLong("groupId");
351                                    long companyId = rs.getLong("companyId");
352                                    long userId = rs.getLong("userId");
353    
354                                    return new long[] {
355                                            groupId, companyId, userId, classNameId, classPK
356                                    };
357                            }
358    
359                            return null;
360                    }
361                    finally {
362                            DataAccess.cleanUp(con, ps, rs);
363                    }
364            }
365    
366            protected long getMBThreadRootMessageId(long mbThreadId) throws Exception {
367                    Connection con = null;
368                    PreparedStatement ps = null;
369                    ResultSet rs = null;
370    
371                    try {
372                            con = DataAccess.getUpgradeOptimizedConnection();
373    
374                            ps = con.prepareStatement(
375                                    "select rootMessageId from MBThread where threadId = " +
376                                            mbThreadId);
377    
378                            rs = ps.executeQuery();
379    
380                            if (rs.next()) {
381                                    return rs.getLong("rootMessageId");
382                            }
383    
384                            return -1;
385                    }
386                    finally {
387                            DataAccess.cleanUp(con, ps, rs);
388                    }
389            }
390    
391            protected int getTotalValue(
392                            long groupId, long classNameId, long classPK, String name,
393                            int ownerType, int startPeriod)
394                    throws Exception {
395    
396                    Connection con = null;
397                    PreparedStatement ps = null;
398                    ResultSet rs = null;
399    
400                    try {
401                            con = DataAccess.getUpgradeOptimizedConnection();
402    
403                            StringBundler sb = new StringBundler(4);
404    
405                            sb.append("select max(totalValue) as totalValue from ");
406                            sb.append("SocialActivityCounter where groupId = ? and ");
407                            sb.append("classNameId = ? and classPK = ? and name = ? and ");
408                            sb.append("ownerType = ? and startPeriod < ?");
409    
410                            ps = con.prepareStatement(sb.toString());
411    
412                            ps.setLong(1, groupId);
413                            ps.setLong(2, classNameId);
414                            ps.setLong(3, classPK);
415                            ps.setString(4, name);
416                            ps.setInt(5, ownerType);
417                            ps.setInt(6, startPeriod);
418    
419                            rs = ps.executeQuery();
420    
421                            if (rs.next()) {
422                                    return rs.getInt("totalValue");
423                            }
424    
425                            return 0;
426                    }
427                    finally {
428                            DataAccess.cleanUp(con, ps, rs);
429                    }
430            }
431    
432            protected void migrateEquityGroupSettings() throws Exception {
433                    Connection con = null;
434                    PreparedStatement ps = null;
435                    ResultSet rs = null;
436    
437                    try {
438                            con = DataAccess.getUpgradeOptimizedConnection();
439    
440                            ps = con.prepareStatement(
441                                    "select groupId, companyId, classNameId, enabled from " +
442                                            "SocialEquityGroupSetting where type_ = 1");
443    
444                            rs = ps.executeQuery();
445    
446                            while (rs.next()) {
447                                    long groupId = rs.getLong("groupId");
448                                    long companyId = rs.getLong("companyId");
449                                    long classNameId = rs.getLong("classNameId");
450                                    boolean enabled = rs.getBoolean("enabled");
451    
452                                    addActivitySetting(
453                                            increment(), groupId, companyId, classNameId, 0, "enabled",
454                                            String.valueOf(enabled));
455                            }
456    
457                            DataAccess.cleanUp(ps, rs);
458    
459                            StringBundler sb = new StringBundler(7);
460    
461                            sb.append("select groupId from SocialActivitySetting where ");
462                            sb.append("activityType = 0 and name = 'enabled' and ");
463                            sb.append("value = 'true' and classNameId in (");
464    
465                            long mbMessageClassNameId = PortalUtil.getClassNameId(
466                                    MBMessage.class);
467    
468                            sb.append(mbMessageClassNameId);
469                            sb.append(", ");
470    
471                            long mbThreadClassNameId = PortalUtil.getClassNameId(
472                                    MBThread.class);
473    
474                            sb.append(mbThreadClassNameId);
475                            sb.append(StringPool.CLOSE_PARENTHESIS);
476    
477                            ps = con.prepareStatement(sb.toString());
478    
479                            rs = ps.executeQuery();
480    
481                            while (rs.next()) {
482                                    long groupId = rs.getLong("groupId");
483    
484                                    sb = new StringBundler(6);
485    
486                                    sb.append("update SocialActivitySetting set value = 'true' ");
487                                    sb.append("where groupId = ");
488                                    sb.append(groupId);
489                                    sb.append(" and activityType = 0 and value = 'enabled' and ");
490                                    sb.append("classNameId = ");
491                                    sb.append(mbThreadClassNameId);
492    
493                                    runSQL(sb.toString());
494                            }
495    
496                            runSQL(
497                                    "delete from SocialActivitySetting where classNameId = " +
498                                            mbThreadClassNameId);
499                    }
500                    finally {
501                            DataAccess.cleanUp(con, ps, rs);
502                    }
503            }
504    
505            protected void migrateEquityLog(ResultSet rs) throws Exception {
506                    long assetEntryId = rs.getLong("assetEntryId");
507    
508                    long[] assetEntryArray = getAssetEntryArray(assetEntryId);
509    
510                    if (assetEntryArray == null) {
511                            return;
512                    }
513    
514                    String actionId = rs.getString("actionId");
515    
516                    if (actionId.equals(ActionKeys.SUBSCRIBE)) {
517                            long classNameId = assetEntryArray[3];
518    
519                            String className = PortalUtil.getClassName(classNameId);
520    
521                            if (className.equals(MBThread.class.getName())) {
522                                    long classPK = assetEntryArray[4];
523    
524                                    long mbThreadRootMessageId = getMBThreadRootMessageId(classPK);
525    
526                                    if (mbThreadRootMessageId == -1) {
527                                            return;
528                                    }
529    
530                                    assetEntryArray = getAssetEntryArray(
531                                            MBMessage.class.getName(), mbThreadRootMessageId);
532    
533                                    if (assetEntryArray == null) {
534                                            return;
535                                    }
536                            }
537                    }
538    
539                    long classNameId = PortalUtil.getClassNameId(User.class);
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    }