001    /**
002     * Copyright (c) 2000-2013 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.portlet.social.service.impl;
016    
017    import com.liferay.portal.kernel.dao.db.DB;
018    import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
019    import com.liferay.portal.kernel.dao.orm.QueryUtil;
020    import com.liferay.portal.kernel.exception.PortalException;
021    import com.liferay.portal.kernel.exception.SystemException;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.kernel.transaction.Propagation;
025    import com.liferay.portal.kernel.transaction.Transactional;
026    import com.liferay.portal.kernel.util.PropsKeys;
027    import com.liferay.portal.kernel.util.StringBundler;
028    import com.liferay.portal.kernel.util.StringPool;
029    import com.liferay.portal.kernel.util.StringUtil;
030    import com.liferay.portal.kernel.util.Tuple;
031    import com.liferay.portal.model.Group;
032    import com.liferay.portal.model.Lock;
033    import com.liferay.portal.model.User;
034    import com.liferay.portal.util.PortalUtil;
035    import com.liferay.portal.util.PropsValues;
036    import com.liferay.portlet.asset.model.AssetEntry;
037    import com.liferay.portlet.social.model.SocialAchievement;
038    import com.liferay.portlet.social.model.SocialActivity;
039    import com.liferay.portlet.social.model.SocialActivityCounter;
040    import com.liferay.portlet.social.model.SocialActivityCounterConstants;
041    import com.liferay.portlet.social.model.SocialActivityCounterDefinition;
042    import com.liferay.portlet.social.model.SocialActivityDefinition;
043    import com.liferay.portlet.social.model.SocialActivityLimit;
044    import com.liferay.portlet.social.model.SocialActivityProcessor;
045    import com.liferay.portlet.social.service.base.SocialActivityCounterLocalServiceBaseImpl;
046    import com.liferay.portlet.social.service.persistence.SocialActivityCounterFinderUtil;
047    import com.liferay.portlet.social.util.SocialCounterPeriodUtil;
048    
049    import java.util.Arrays;
050    import java.util.Collections;
051    import java.util.Date;
052    import java.util.HashMap;
053    import java.util.List;
054    import java.util.Map;
055    
056    /**
057     * @author Zsolt Berentey
058     * @author Shuyang Zhou
059     */
060    public class SocialActivityCounterLocalServiceImpl
061            extends SocialActivityCounterLocalServiceBaseImpl {
062    
063            @Override
064            public SocialActivityCounter addActivityCounter(
065                            long groupId, long classNameId, long classPK, String name,
066                            int ownerType, int currentValue, int totalValue, int startPeriod,
067                            int endPeriod)
068                    throws PortalException, SystemException {
069    
070                    return addActivityCounter(
071                            groupId, classNameId, classPK, name, ownerType, currentValue,
072                            totalValue, startPeriod, endPeriod, 0, 0);
073            }
074    
075            @Override
076            public SocialActivityCounter addActivityCounter(
077                            long groupId, long classNameId, long classPK, String name,
078                            int ownerType, int currentValue, int totalValue, int startPeriod,
079                            int endPeriod, long previousActivityCounterId, int periodLength)
080                    throws PortalException, SystemException {
081    
082                    SocialActivityCounter activityCounter = null;
083    
084                    String lockKey = getLockKey(
085                            groupId, classNameId, classPK, name, ownerType);
086    
087                    Lock lock = null;
088    
089                    while (true) {
090                            try {
091                                    lock = lockLocalService.lock(
092                                            SocialActivityCounter.class.getName(), lockKey, lockKey,
093                                            false);
094                            }
095                            catch (Exception e) {
096                                    if (_log.isWarnEnabled()) {
097                                            _log.warn(
098                                                    "Unable to acquire activity counter lock. Retrying.");
099                                    }
100    
101                                    continue;
102                            }
103    
104                            if (lock.isNew()) {
105                                    try {
106                                            DB db = DBFactoryUtil.getDB();
107    
108                                            String dbType = db.getType();
109    
110                                            if (dbType.equals(DB.TYPE_HYPERSONIC)) {
111    
112                                                    // LPS-25408
113    
114                                                    activityCounter = createActivityCounter(
115                                                            groupId, classNameId, classPK, name, ownerType,
116                                                            currentValue, totalValue, startPeriod, endPeriod,
117                                                            previousActivityCounterId, periodLength);
118                                            }
119                                            else {
120                                                    activityCounter =
121                                                            socialActivityCounterLocalService.
122                                                                    createActivityCounter(
123                                                                            groupId, classNameId, classPK, name,
124                                                                            ownerType, currentValue, totalValue,
125                                                                            startPeriod, endPeriod,
126                                                                            previousActivityCounterId, periodLength);
127    
128                                            }
129                                    }
130                                    finally {
131                                            lockLocalService.unlock(
132                                                    SocialActivityCounter.class.getName(), lockKey, lockKey,
133                                                    false);
134                                    }
135    
136                                    break;
137                            }
138    
139                            Date createDate = lock.getCreateDate();
140    
141                            if ((System.currentTimeMillis() - createDate.getTime()) >=
142                                            PropsValues.SOCIAL_ACTIVITY_COUNTER_LOCK_TIMEOUT) {
143    
144                                    lockLocalService.unlock(
145                                            SocialActivityCounter.class.getName(), lockKey,
146                                            lock.getOwner(), false);
147    
148                                    if (_log.isWarnEnabled()) {
149                                            _log.warn(
150                                                    "Forcibly removed lock " + lock + ". See " +
151                                                            PropsKeys.SOCIAL_ACTIVITY_COUNTER_LOCK_TIMEOUT);
152                                    }
153                            }
154                            else {
155                                    try {
156                                            Thread.sleep(
157                                                    PropsValues.SOCIAL_ACTIVITY_COUNTER_LOCK_RETRY_DELAY);
158                                    }
159                                    catch (InterruptedException ie) {
160                                            if (_log.isWarnEnabled()) {
161                                                    _log.warn(
162                                                            "Interrupted while waiting to reacquire lock", ie);
163                                            }
164                                    }
165                            }
166                    }
167    
168                    return activityCounter;
169            }
170    
171            @Override
172            public void addActivityCounters(SocialActivity activity)
173                    throws PortalException, SystemException {
174    
175                    if (!socialActivitySettingLocalService.isEnabled(
176                                    activity.getGroupId(), activity.getClassNameId())) {
177    
178                            return;
179                    }
180    
181                    User user = userPersistence.findByPrimaryKey(activity.getUserId());
182    
183                    SocialActivityDefinition activityDefinition =
184                            socialActivitySettingLocalService.getActivityDefinition(
185                                    activity.getGroupId(), activity.getClassName(),
186                                    activity.getType());
187    
188                    if ((activityDefinition == null) || !activityDefinition.isEnabled()) {
189                            return;
190                    }
191    
192                    SocialActivityProcessor activityProcessor =
193                            activityDefinition.getActivityProcessor();
194    
195                    if (activityProcessor != null) {
196                            activityProcessor.processActivity(activity);
197                    }
198    
199                    AssetEntry assetEntry = activity.getAssetEntry();
200    
201                    User assetEntryUser = userPersistence.findByPrimaryKey(
202                            assetEntry.getUserId());
203    
204                    for (SocialActivityCounterDefinition activityCounterDefinition :
205                                    activityDefinition.getActivityCounterDefinitions()) {
206    
207                            if (addActivityCounter(
208                                            user, assetEntryUser, activityCounterDefinition) &&
209                                    checkActivityLimit(user, activity, activityCounterDefinition)) {
210    
211                                    incrementActivityCounter(
212                                            activity.getGroupId(), user, activity.getAssetEntry(),
213                                            activityCounterDefinition);
214                            }
215                    }
216    
217                    for (SocialAchievement achievement :
218                                    activityDefinition.getAchievements()) {
219    
220                            achievement.processActivity(activity);
221                    }
222    
223                    if (!user.isDefaultUser() && user.isActive()) {
224                            incrementActivityCounter(
225                                    activity.getGroupId(),
226                                    PortalUtil.getClassNameId(User.class.getName()),
227                                    activity.getUserId(),
228                                    SocialActivityCounterConstants.NAME_USER_ACTIVITIES,
229                                    SocialActivityCounterConstants.TYPE_ACTOR, 1,
230                                    SocialActivityCounterConstants.PERIOD_LENGTH_SYSTEM);
231                    }
232    
233                    if (!assetEntryUser.isDefaultUser() && assetEntryUser.isActive()) {
234                            incrementActivityCounter(
235                                    activity.getGroupId(), activity.getClassNameId(),
236                                    activity.getClassPK(),
237                                    SocialActivityCounterConstants.NAME_ASSET_ACTIVITIES,
238                                    SocialActivityCounterConstants.TYPE_ASSET, 1,
239                                    SocialActivityCounterConstants.PERIOD_LENGTH_SYSTEM);
240                    }
241            }
242    
243            /**
244             * @deprecated {@link #createActivityCounter(long, long, long, String, int,
245             *             int, int, int, int, long, int)}
246             */
247            @Override
248            @Transactional(propagation = Propagation.REQUIRES_NEW)
249            public SocialActivityCounter createActivityCounter(
250                            long groupId, long classNameId, long classPK, String name,
251                            int ownerType, int currentValue, int totalValue, int startPeriod,
252                            int endPeriod)
253                    throws PortalException, SystemException {
254    
255                    return createActivityCounter(
256                            groupId, classNameId, classPK, name, ownerType, currentValue,
257                            totalValue, startPeriod, endPeriod, 0, 0);
258            }
259    
260            @Override
261            @Transactional(propagation = Propagation.REQUIRES_NEW)
262            public SocialActivityCounter createActivityCounter(
263                            long groupId, long classNameId, long classPK, String name,
264                            int ownerType, int currentValue, int totalValue, int startPeriod,
265                            int endPeriod, long previousActivityCounterId, int periodLength)
266                    throws PortalException, SystemException {
267    
268                    SocialActivityCounter activityCounter = null;
269    
270                    if (previousActivityCounterId != 0) {
271                            activityCounter = socialActivityCounterPersistence.findByPrimaryKey(
272                                    previousActivityCounterId);
273    
274                            if (periodLength ==
275                                            SocialActivityCounterConstants.PERIOD_LENGTH_SYSTEM) {
276    
277                                    activityCounter.setEndPeriod(
278                                            SocialCounterPeriodUtil.getStartPeriod() - 1);
279                            }
280                            else {
281                                    activityCounter.setEndPeriod(
282                                            activityCounter.getStartPeriod() + periodLength - 1);
283                            }
284    
285                            socialActivityCounterPersistence.update(activityCounter, false);
286                    }
287    
288                    activityCounter = socialActivityCounterPersistence.fetchByG_C_C_N_O_E(
289                            groupId, classNameId, classPK, name, ownerType, endPeriod, false);
290    
291                    if (activityCounter != null) {
292                            return activityCounter;
293                    }
294    
295                    Group group = groupPersistence.findByPrimaryKey(groupId);
296    
297                    long activityCounterId = counterLocalService.increment();
298    
299                    activityCounter = socialActivityCounterPersistence.create(
300                            activityCounterId);
301    
302                    activityCounter.setGroupId(groupId);
303                    activityCounter.setCompanyId(group.getCompanyId());
304                    activityCounter.setClassNameId(classNameId);
305                    activityCounter.setClassPK(classPK);
306                    activityCounter.setName(name);
307                    activityCounter.setOwnerType(ownerType);
308                    activityCounter.setCurrentValue(currentValue);
309                    activityCounter.setTotalValue(totalValue);
310                    activityCounter.setStartPeriod(startPeriod);
311                    activityCounter.setEndPeriod(endPeriod);
312    
313                    socialActivityCounterPersistence.update(activityCounter, false);
314    
315                    return activityCounter;
316            }
317    
318            @Override
319            public void deleteActivityCounters(AssetEntry assetEntry)
320                    throws PortalException, SystemException {
321    
322                    if (assetEntry == null) {
323                            return;
324                    }
325    
326                    SocialActivityCounter latestContributionActivityCounter =
327                            fetchLatestActivityCounter(
328                                    assetEntry.getGroupId(),
329                                    PortalUtil.getClassNameId(User.class.getName()),
330                                    assetEntry.getUserId(),
331                                    SocialActivityCounterConstants.NAME_CONTRIBUTION,
332                                    SocialActivityCounterConstants.TYPE_CREATOR);
333    
334                    SocialActivityCounter latestPopularityActivityCounter =
335                            fetchLatestActivityCounter(
336                                    assetEntry.getGroupId(), assetEntry.getClassNameId(),
337                                    assetEntry.getClassPK(),
338                                    SocialActivityCounterConstants.NAME_POPULARITY,
339                                    SocialActivityCounterConstants.TYPE_ASSET);
340    
341                    if ((latestContributionActivityCounter != null) &&
342                            (latestPopularityActivityCounter != null)) {
343    
344                            int startPeriod = SocialCounterPeriodUtil.getStartPeriod();
345    
346                            if (latestContributionActivityCounter.getStartPeriod() !=
347                                            startPeriod) {
348    
349                                    latestContributionActivityCounter = addActivityCounter(
350                                            latestContributionActivityCounter.getGroupId(),
351                                            latestContributionActivityCounter.getClassNameId(),
352                                            latestContributionActivityCounter.getClassPK(),
353                                            latestContributionActivityCounter.getName(),
354                                            latestContributionActivityCounter.getOwnerType(), 0,
355                                            latestContributionActivityCounter.getTotalValue(),
356                                            SocialCounterPeriodUtil.getStartPeriod(),
357                                            SocialActivityCounterConstants.END_PERIOD_UNDEFINED,
358                                            latestContributionActivityCounter.getActivityCounterId(),
359                                            SocialActivityCounterConstants.PERIOD_LENGTH_SYSTEM);
360                            }
361    
362                            if (latestPopularityActivityCounter.getStartPeriod() ==
363                                            startPeriod) {
364    
365                                    latestContributionActivityCounter.setCurrentValue(
366                                            latestContributionActivityCounter.getCurrentValue() -
367                                                    latestPopularityActivityCounter.getCurrentValue());
368                            }
369    
370                            latestContributionActivityCounter.setTotalValue(
371                                    latestContributionActivityCounter.getTotalValue() -
372                                            latestPopularityActivityCounter.getTotalValue());
373    
374                            socialActivityCounterPersistence.update(
375                                    latestContributionActivityCounter, false);
376                    }
377    
378                    deleteActivityCounters(
379                            assetEntry.getClassNameId(), assetEntry.getClassPK());
380    
381                    socialActivityLimitPersistence.removeByC_C(
382                            assetEntry.getClassNameId(), assetEntry.getClassPK());
383            }
384    
385            @Override
386            public void deleteActivityCounters(long classNameId, long classPK)
387                    throws SystemException {
388    
389                    socialActivityCounterPersistence.removeByC_C(classNameId, classPK);
390            }
391    
392            @Override
393            public SocialActivityCounter fetchActivityCounterByEndPeriod(
394                            long groupId, long classNameId, long classPK, String name,
395                            int ownerType, int endPeriod)
396                    throws SystemException {
397    
398                    return socialActivityCounterPersistence.fetchByG_C_C_N_O_E(
399                            groupId, classNameId, classPK, name, ownerType, endPeriod);
400            }
401    
402            @Override
403            public SocialActivityCounter fetchActivityCounterByStartPeriod(
404                            long groupId, long classNameId, long classPK, String name,
405                            int ownerType, int startPeriod)
406                    throws SystemException {
407    
408                    return socialActivityCounterPersistence.fetchByG_C_C_N_O_S(
409                            groupId, classNameId, classPK, name, ownerType, startPeriod);
410            }
411    
412            @Override
413            public SocialActivityCounter fetchLatestActivityCounter(
414                            long groupId, long classNameId, long classPK, String name,
415                            int ownerType)
416                    throws SystemException {
417    
418                    return socialActivityCounterPersistence.fetchByG_C_C_N_O_E(
419                            groupId, classNameId, classPK, name, ownerType,
420                            SocialActivityCounterConstants.END_PERIOD_UNDEFINED);
421            }
422    
423            @Override
424            public List<SocialActivityCounter> getOffsetActivityCounters(
425                            long groupId, String name, int startOffset, int endOffset)
426                    throws SystemException {
427    
428                    int startPeriod = SocialCounterPeriodUtil.getStartPeriod(startOffset);
429                    int endPeriod = SocialCounterPeriodUtil.getEndPeriod(endOffset);
430    
431                    return getPeriodActivityCounters(groupId, name, startPeriod, endPeriod);
432            }
433    
434            @Override
435            public List<SocialActivityCounter> getOffsetDistributionActivityCounters(
436                            long groupId, String name, int startOffset, int endOffset)
437                    throws SystemException {
438    
439                    int startPeriod = SocialCounterPeriodUtil.getStartPeriod(startOffset);
440                    int endPeriod = SocialCounterPeriodUtil.getEndPeriod(endOffset);
441    
442                    return getPeriodDistributionActivityCounters(
443                            groupId, name, startPeriod, endPeriod);
444            }
445    
446            @Override
447            public List<SocialActivityCounter> getPeriodActivityCounters(
448                            long groupId, String name, int startPeriod, int endPeriod)
449                    throws SystemException {
450    
451                    int offset = SocialCounterPeriodUtil.getOffset(endPeriod);
452    
453                    int periodLength = SocialCounterPeriodUtil.getPeriodLength(offset);
454    
455                    return socialActivityCounterFinder.findAC_ByG_N_S_E_1(
456                            groupId, name, startPeriod, endPeriod, periodLength);
457            }
458    
459            @Override
460            public List<SocialActivityCounter> getPeriodDistributionActivityCounters(
461                            long groupId, String name, int startPeriod, int endPeriod)
462                    throws SystemException {
463    
464                    int offset = SocialCounterPeriodUtil.getOffset(endPeriod);
465    
466                    int periodLength = SocialCounterPeriodUtil.getPeriodLength(offset);
467    
468                    return socialActivityCounterFinder.findAC_ByG_N_S_E_2(
469                            groupId, name, startPeriod, endPeriod, periodLength);
470            }
471    
472            @Override
473            public List<Tuple> getUserActivityCounters(
474                            long groupId, String[] rankingNames, String[] selectedNames,
475                            int start, int end)
476                    throws SystemException {
477    
478                    List<Long> userIds = socialActivityCounterFinder.findU_ByG_N(
479                            groupId, rankingNames, start, end);
480    
481                    if (userIds.isEmpty()) {
482                            return Collections.emptyList();
483                    }
484    
485                    Tuple[] userActivityCounters = new Tuple[userIds.size()];
486    
487                    List<SocialActivityCounter> activityCounters =
488                            SocialActivityCounterFinderUtil.findAC_By_G_C_C_N_S_E(
489                                    groupId, userIds, selectedNames, QueryUtil.ALL_POS,
490                                    QueryUtil.ALL_POS);
491    
492                    long userId = 0;
493                    Map<String, SocialActivityCounter> activityCountersMap = null;
494    
495                    for (SocialActivityCounter activityCounter : activityCounters) {
496                            if (userId != activityCounter.getClassPK()) {
497                                    userId = activityCounter.getClassPK();
498                                    activityCountersMap =
499                                            new HashMap<String, SocialActivityCounter>();
500    
501                                    Tuple userActivityCounter = new Tuple(
502                                            userId, activityCountersMap);
503    
504                                    for (int i = 0; i < userIds.size(); i++) {
505                                            long curUserId = userIds.get(i);
506    
507                                            if (userId == curUserId) {
508                                                    userActivityCounters[i] = userActivityCounter;
509    
510                                                    break;
511                                            }
512                                    }
513                            }
514    
515                            activityCountersMap.put(activityCounter.getName(), activityCounter);
516                    }
517    
518                    return Arrays.asList(userActivityCounters);
519            }
520    
521            @Override
522            public int getUserActivityCountersCount(long groupId, String[] rankingNames)
523                    throws SystemException {
524    
525                    return SocialActivityCounterFinderUtil.countU_ByG_N(
526                            groupId, rankingNames);
527            }
528    
529            @Override
530            public void incrementUserAchievementCounter(long userId, long groupId)
531                    throws PortalException, SystemException {
532    
533                    incrementActivityCounter(
534                            groupId, PortalUtil.getClassNameId(User.class.getName()), userId,
535                            SocialActivityCounterConstants.NAME_USER_ACHIEVEMENTS,
536                            SocialActivityCounterConstants.TYPE_ACTOR, 1,
537                            SocialActivityCounterConstants.PERIOD_LENGTH_SYSTEM);
538            }
539    
540            protected boolean addActivityCounter(
541                    User user, User assetEntryUser,
542                    SocialActivityCounterDefinition activityCounterDefinition) {
543    
544                    if ((user.isDefaultUser() || !user.isActive()) &&
545                            (activityCounterDefinition.getOwnerType() !=
546                                    SocialActivityCounterConstants.TYPE_ASSET)) {
547    
548                            return false;
549                    }
550    
551                    if ((assetEntryUser.isDefaultUser() || !assetEntryUser.isActive()) &&
552                            (activityCounterDefinition.getOwnerType() !=
553                                    SocialActivityCounterConstants.TYPE_ACTOR)) {
554    
555                            return false;
556                    }
557    
558                    if (!activityCounterDefinition.isEnabled() ||
559                            (activityCounterDefinition.getIncrement() == 0)) {
560    
561                            return false;
562                    }
563    
564                    String name = activityCounterDefinition.getName();
565    
566                    if ((user.getUserId() == assetEntryUser.getUserId()) &&
567                            (name.equals(SocialActivityCounterConstants.NAME_CONTRIBUTION) ||
568                             name.equals(SocialActivityCounterConstants.NAME_POPULARITY))) {
569    
570                            return false;
571                    }
572    
573                    return true;
574            }
575    
576            protected boolean checkActivityLimit(
577                            User user, SocialActivity activity,
578                            SocialActivityCounterDefinition activityCounterDefinition)
579                    throws PortalException, SystemException {
580    
581                    if (activityCounterDefinition.getLimitValue() == 0) {
582                            return true;
583                    }
584    
585                    long classPK = activity.getClassPK();
586    
587                    String name = activityCounterDefinition.getName();
588    
589                    if (name.equals(SocialActivityCounterConstants.NAME_PARTICIPATION)) {
590                            classPK = 0;
591                    }
592    
593                    SocialActivityLimit activityLimit =
594                            socialActivityLimitPersistence.fetchByG_U_C_C_A_A(
595                                    activity.getGroupId(), user.getUserId(),
596                                    activity.getClassNameId(), classPK, activity.getType(),
597                                    activityCounterDefinition.getName());
598    
599                    if (activityLimit == null) {
600                            try {
601                                    activityLimit =
602                                            socialActivityLimitLocalService.addActivityLimit(
603                                                    user.getUserId(), activity.getGroupId(),
604                                                    activity.getClassNameId(), classPK, activity.getType(),
605                                                    activityCounterDefinition.getName(),
606                                                    activityCounterDefinition.getLimitPeriod());
607                            }
608                            catch (SystemException se) {
609                                    activityLimit =
610                                            socialActivityLimitPersistence.fetchByG_U_C_C_A_A(
611                                                    activity.getGroupId(), user.getUserId(),
612                                                    activity.getClassNameId(), classPK, activity.getType(),
613                                                    activityCounterDefinition.getName());
614    
615                                    if (activityLimit == null) {
616                                            throw se;
617                                    }
618                            }
619                    }
620    
621                    int count = activityLimit.getCount(
622                            activityCounterDefinition.getLimitPeriod());
623    
624                    if (count < activityCounterDefinition.getLimitValue()) {
625                            activityLimit.setCount(
626                                    activityCounterDefinition.getLimitPeriod(), count + 1);
627    
628                            socialActivityLimitPersistence.update(activityLimit, false);
629    
630                            return true;
631                    }
632    
633                    return false;
634            }
635    
636            protected String getLockKey(
637                    long groupId, long classNameId, long classPK, String name,
638                    int ownerType) {
639    
640                    StringBundler sb = new StringBundler(7);
641    
642                    sb.append(StringUtil.toHexString(groupId));
643                    sb.append(StringPool.POUND);
644                    sb.append(StringUtil.toHexString(classNameId));
645                    sb.append(StringPool.POUND);
646                    sb.append(StringUtil.toHexString(classPK));
647                    sb.append(StringPool.POUND);
648                    sb.append(name);
649    
650                    return sb.toString();
651            }
652    
653            protected void incrementActivityCounter(
654                            long groupId, long classNameId, long classPK, String name,
655                            int ownerType, int increment, int periodLength)
656                    throws PortalException, SystemException {
657    
658                    SocialActivityCounter activityCounter = fetchLatestActivityCounter(
659                            groupId, classNameId, classPK, name, ownerType);
660    
661                    if (activityCounter == null) {
662                            activityCounter = addActivityCounter(
663                                    groupId, classNameId, classPK, name, ownerType, 0, 0,
664                                    SocialCounterPeriodUtil.getStartPeriod(),
665                                    SocialActivityCounterConstants.END_PERIOD_UNDEFINED);
666    
667                            if (periodLength > 0) {
668                                    activityCounter.setStartPeriod(
669                                            SocialCounterPeriodUtil.getActivityDay());
670                            }
671                    }
672    
673                    if (!activityCounter.isActivePeriod(periodLength)) {
674                            activityCounter = addActivityCounter(
675                                    activityCounter.getGroupId(), activityCounter.getClassNameId(),
676                                    activityCounter.getClassPK(), activityCounter.getName(),
677                                    activityCounter.getOwnerType(), 0,
678                                    activityCounter.getTotalValue(),
679                                    SocialCounterPeriodUtil.getStartPeriod(),
680                                    SocialActivityCounterConstants.END_PERIOD_UNDEFINED,
681                                    activityCounter.getActivityCounterId(), periodLength);
682                    }
683    
684                    activityCounter.setCurrentValue(
685                            activityCounter.getCurrentValue() + increment);
686                    activityCounter.setTotalValue(
687                            activityCounter.getTotalValue() + increment);
688    
689                    socialActivityCounterPersistence.update(activityCounter, false);
690            }
691    
692            protected void incrementActivityCounter(
693                            long groupId, User user, AssetEntry assetEntry,
694                            SocialActivityCounterDefinition activityCounterDefinition)
695                    throws PortalException, SystemException {
696    
697                    int ownerType = activityCounterDefinition.getOwnerType();
698                    long userClassNameId = PortalUtil.getClassNameId(User.class.getName());
699    
700                    if (ownerType == SocialActivityCounterConstants.TYPE_ACTOR) {
701                            incrementActivityCounter(
702                                    groupId, userClassNameId, user.getUserId(),
703                                    activityCounterDefinition.getName(), ownerType,
704                                    activityCounterDefinition.getIncrement(),
705                                    activityCounterDefinition.getPeriodLength());
706                    }
707                    else if (ownerType == SocialActivityCounterConstants.TYPE_ASSET) {
708                            incrementActivityCounter(
709                                    groupId, assetEntry.getClassNameId(), assetEntry.getClassPK(),
710                                    activityCounterDefinition.getName(), ownerType,
711                                    activityCounterDefinition.getIncrement(),
712                                    activityCounterDefinition.getPeriodLength());
713                    }
714                    else {
715                            incrementActivityCounter(
716                                    groupId, userClassNameId, assetEntry.getUserId(),
717                                    activityCounterDefinition.getName(), ownerType,
718                                    activityCounterDefinition.getIncrement(),
719                                    activityCounterDefinition.getPeriodLength());
720                    }
721            }
722    
723            private static Log _log = LogFactoryUtil.getLog(
724                    SocialActivityCounterLocalServiceImpl.class);
725    
726    }