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