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.kernel.social;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.model.ClassedModel;
019    import com.liferay.portal.model.GroupedModel;
020    import com.liferay.portlet.social.model.SocialActivity;
021    import com.liferay.portlet.social.model.SocialActivityConstants;
022    import com.liferay.portlet.social.service.SocialActivityLocalService;
023    
024    import java.util.Date;
025    
026    /**
027     * @author Adolfo P??rez
028     */
029    public abstract class BaseSocialActivityManager
030            <T extends ClassedModel & GroupedModel>
031                    implements SocialActivityManager<T> {
032    
033            @Override
034            public void addActivity(
035                            long userId, T model, int type, String extraData,
036                            long receiverUserId)
037                    throws PortalException {
038    
039                    String className = getClassName(model);
040                    long primaryKey = getPrimaryKey(model);
041    
042                    if (type == SocialActivityConstants.TYPE_SUBSCRIBE) {
043                            if (primaryKey != model.getGroupId()) {
044                                    getSocialActivityLocalService().addActivity(
045                                            userId, model.getGroupId(), className, primaryKey,
046                                            SocialActivityConstants.TYPE_SUBSCRIBE, extraData, 0);
047                            }
048                    }
049                    else {
050                            getSocialActivityLocalService().addActivity(
051                                    userId, model.getGroupId(), className, primaryKey, type,
052                                    extraData, receiverUserId);
053                    }
054            }
055    
056            @Override
057            public void addUniqueActivity(
058                            long userId, Date createDate, T model, int type, String extraData,
059                            long receiverUserId)
060                    throws PortalException {
061    
062                    String className = getClassName(model);
063                    long primaryKey = getPrimaryKey(model);
064    
065                    getSocialActivityLocalService().addUniqueActivity(
066                            userId, model.getGroupId(), createDate, className, primaryKey, type,
067                            extraData, receiverUserId);
068            }
069    
070            @Override
071            public void addUniqueActivity(
072                            long userId, T model, int type, String extraData,
073                            long receiverUserId)
074                    throws PortalException {
075    
076                    String className = getClassName(model);
077                    long primaryKey = getPrimaryKey(model);
078    
079                    getSocialActivityLocalService().addUniqueActivity(
080                            userId, model.getGroupId(), className, primaryKey, type, extraData,
081                            receiverUserId);
082            }
083    
084            @Override
085            public void deleteActivities(T model) throws PortalException {
086                    String className = getClassName(model);
087                    long primaryKey = getPrimaryKey(model);
088    
089                    getSocialActivityLocalService().deleteActivities(className, primaryKey);
090            }
091    
092            @Override
093            public void updateLastSocialActivity(
094                    long userId, T model, int type, Date createDate) {
095    
096                    String className = getClassName(model);
097                    long primaryKey = getPrimaryKey(model);
098    
099                    SocialActivity lastSocialActivity =
100                            getSocialActivityLocalService().fetchFirstActivity(
101                                    className, primaryKey, type);
102    
103                    if (lastSocialActivity != null) {
104                            lastSocialActivity.setCreateDate(createDate.getTime());
105                            lastSocialActivity.setUserId(userId);
106    
107                            getSocialActivityLocalService().updateSocialActivity(
108                                    lastSocialActivity);
109                    }
110            }
111    
112            protected String getClassName(T classedModel) {
113                    return classedModel.getModelClassName();
114            }
115    
116            protected long getPrimaryKey(T classedModel) {
117                    if (!(classedModel.getPrimaryKeyObj() instanceof Long)) {
118                            throw new IllegalArgumentException(
119                                    "Only models with a primary key of type Long can make use " +
120                                            "of SocialActivityManagers");
121                    }
122    
123                    return (Long)classedModel.getPrimaryKeyObj();
124            }
125    
126            protected abstract SocialActivityLocalService
127                    getSocialActivityLocalService();
128    
129    }