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.portlet.social.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.transaction.Propagation;
019    import com.liferay.portal.kernel.transaction.Transactional;
020    import com.liferay.portal.model.User;
021    import com.liferay.portlet.social.model.SocialActivityLimit;
022    import com.liferay.portlet.social.service.base.SocialActivityLimitLocalServiceBaseImpl;
023    
024    /**
025     * @author Zsolt Berentey
026     */
027    public class SocialActivityLimitLocalServiceImpl
028            extends SocialActivityLimitLocalServiceBaseImpl {
029    
030            @Override
031            @Transactional(propagation = Propagation.REQUIRES_NEW)
032            public SocialActivityLimit addActivityLimit(
033                            long userId, long groupId, long classNameId, long classPK,
034                            int activityType, String activityCounterName, int limitPeriod)
035                    throws PortalException {
036    
037                    SocialActivityLimit activityLimit =
038                            socialActivityLimitPersistence.fetchByG_U_C_C_A_A(
039                                    groupId, userId, classNameId, classPK, activityType,
040                                    activityCounterName, false);
041    
042                    if (activityLimit != null) {
043                            return activityLimit;
044                    }
045    
046                    User user = userPersistence.findByPrimaryKey(userId);
047    
048                    long activityLimitId = counterLocalService.increment();
049    
050                    activityLimit = socialActivityLimitPersistence.create(activityLimitId);
051    
052                    activityLimit.setGroupId(groupId);
053                    activityLimit.setCompanyId(user.getCompanyId());
054                    activityLimit.setUserId(userId);
055                    activityLimit.setClassNameId(classNameId);
056                    activityLimit.setClassPK(classPK);
057                    activityLimit.setActivityType(activityType);
058                    activityLimit.setActivityCounterName(activityCounterName);
059                    activityLimit.setCount(limitPeriod, 0);
060    
061                    socialActivityLimitPersistence.update(activityLimit);
062    
063                    return activityLimit;
064            }
065    
066            @Override
067            public SocialActivityLimit fetchActivityLimit(
068                    long groupId, long userId, long classNameId, long classPK,
069                    int activityType, String activityCounterName) {
070    
071                    return socialActivityLimitPersistence.fetchByG_U_C_C_A_A(
072                            groupId, userId, classNameId, classPK, activityType,
073                            activityCounterName);
074            }
075    
076    }