001    /**
002     * Copyright (c) 2000-2010 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.messageboards.service.impl;
016    
017    import com.liferay.portal.kernel.dao.orm.DynamicQuery;
018    import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
019    import com.liferay.portal.kernel.dao.orm.ProjectionFactoryUtil;
020    import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
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.util.PortalClassLoaderUtil;
025    import com.liferay.portlet.messageboards.model.MBStatsUser;
026    import com.liferay.portlet.messageboards.model.impl.MBStatsUserImpl;
027    import com.liferay.portlet.messageboards.service.base.MBStatsUserLocalServiceBaseImpl;
028    
029    import java.util.Date;
030    import java.util.List;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class MBStatsUserLocalServiceImpl
036            extends MBStatsUserLocalServiceBaseImpl {
037    
038            public MBStatsUser addStatsUser(long groupId, long userId)
039                    throws SystemException {
040    
041                    long statsUserId = counterLocalService.increment();
042    
043                    MBStatsUser statsUser = mbStatsUserPersistence.create(statsUserId);
044    
045                    statsUser.setGroupId(groupId);
046                    statsUser.setUserId(userId);
047    
048                    try {
049                            mbStatsUserPersistence.update(statsUser, false);
050                    }
051                    catch (SystemException se) {
052                            if (_log.isWarnEnabled()) {
053                                    _log.warn(
054                                            "Add failed, fetch {groupId=" + groupId + ", userId=" +
055                                                    userId + "}");
056                            }
057    
058                            statsUser = mbStatsUserPersistence.fetchByG_U(
059                                    groupId, userId, false);
060    
061                            if (statsUser == null) {
062                                    throw se;
063                            }
064                    }
065    
066                    return statsUser;
067            }
068    
069            public void deleteStatsUsersByGroupId(long groupId)
070                    throws SystemException {
071    
072                    mbStatsUserPersistence.removeByGroupId(groupId);
073            }
074    
075            public void deleteStatsUsersByUserId(long userId) throws SystemException {
076                    mbStatsUserPersistence.removeByUserId(userId);
077            }
078    
079            public long getMessageCountByUserId(long userId) throws SystemException {
080                    DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
081                            MBStatsUser.class, MBStatsUserImpl.TABLE_NAME,
082                            PortalClassLoaderUtil.getClassLoader());
083    
084                    dynamicQuery.setProjection(ProjectionFactoryUtil.sum("messageCount"));
085    
086                    dynamicQuery.add(PropertyFactoryUtil.forName("userId").eq(userId));
087    
088                    return dynamicQueryCount(dynamicQuery);
089            }
090    
091            public MBStatsUser getStatsUser(long groupId, long userId)
092                    throws SystemException {
093    
094                    MBStatsUser statsUser = mbStatsUserPersistence.fetchByG_U(
095                            groupId, userId);
096    
097                    if (statsUser == null) {
098                            statsUser = mbStatsUserLocalService.addStatsUser(groupId, userId);
099                    }
100    
101                    return statsUser;
102            }
103    
104            public List<MBStatsUser> getStatsUsersByGroupId(
105                            long groupId, int start, int end)
106                    throws SystemException {
107    
108                    return mbStatsUserPersistence.findByG_NotM(groupId, 0, start, end);
109            }
110    
111            public List<MBStatsUser> getStatsUsersByUserId(long userId)
112                    throws SystemException {
113    
114                    return mbStatsUserPersistence.findByUserId(userId);
115            }
116    
117            public int getStatsUsersByGroupIdCount(long groupId)
118                    throws SystemException {
119    
120                    return mbStatsUserPersistence.countByG_NotM(groupId, 0);
121            }
122    
123            public MBStatsUser updateStatsUser(long groupId, long userId)
124                    throws SystemException {
125    
126                    return updateStatsUser(groupId, userId, null);
127            }
128    
129            public MBStatsUser updateStatsUser(
130                            long groupId, long userId, Date lastPostDate)
131                    throws SystemException {
132    
133                    int messageCount = mbMessagePersistence.countByG_U(groupId, userId);
134    
135                    MBStatsUser statsUser = getStatsUser(groupId, userId);
136    
137                    statsUser.setMessageCount(messageCount);
138    
139                    if (lastPostDate != null) {
140                            statsUser.setLastPostDate(lastPostDate);
141                    }
142    
143                    mbStatsUserPersistence.update(statsUser, false);
144    
145                    return statsUser;
146            }
147    
148            private static Log _log = LogFactoryUtil.getLog(
149                    MBStatsUserLocalServiceImpl.class);
150    
151    }