001    /**
002     * Copyright (c) 2000-2012 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.PortalException;
022    import com.liferay.portal.kernel.exception.SystemException;
023    import com.liferay.portal.kernel.log.Log;
024    import com.liferay.portal.kernel.log.LogFactoryUtil;
025    import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
026    import com.liferay.portal.model.Group;
027    import com.liferay.portlet.messageboards.model.MBStatsUser;
028    import com.liferay.portlet.messageboards.model.impl.MBStatsUserImpl;
029    import com.liferay.portlet.messageboards.service.base.MBStatsUserLocalServiceBaseImpl;
030    
031    import java.util.Date;
032    import java.util.List;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     */
037    public class MBStatsUserLocalServiceImpl
038            extends MBStatsUserLocalServiceBaseImpl {
039    
040            public MBStatsUser addStatsUser(long groupId, long userId)
041                    throws SystemException {
042    
043                    long statsUserId = counterLocalService.increment();
044    
045                    MBStatsUser statsUser = mbStatsUserPersistence.create(statsUserId);
046    
047                    statsUser.setGroupId(groupId);
048                    statsUser.setUserId(userId);
049    
050                    try {
051                            mbStatsUserPersistence.update(statsUser, false);
052                    }
053                    catch (SystemException se) {
054                            if (_log.isWarnEnabled()) {
055                                    _log.warn(
056                                            "Add failed, fetch {groupId=" + groupId + ", userId=" +
057                                                    userId + "}");
058                            }
059    
060                            statsUser = mbStatsUserPersistence.fetchByG_U(
061                                    groupId, userId, false);
062    
063                            if (statsUser == null) {
064                                    throw se;
065                            }
066                    }
067    
068                    return statsUser;
069            }
070    
071            public void deleteStatsUser(long statsUserId)
072                    throws PortalException, SystemException {
073    
074                    MBStatsUser statsUser = mbStatsUserPersistence.findByPrimaryKey(
075                            statsUserId);
076    
077                    deleteStatsUser(statsUser);
078            }
079    
080            public void deleteStatsUser(MBStatsUser statsUser) throws SystemException {
081                    mbStatsUserPersistence.remove(statsUser);
082            }
083    
084            public void deleteStatsUsersByGroupId(long groupId) throws SystemException {
085                    List<MBStatsUser> statsUsers = mbStatsUserPersistence.findByGroupId(
086                            groupId);
087    
088                    for (MBStatsUser statsUser : statsUsers) {
089                            deleteStatsUser(statsUser);
090                    }
091            }
092    
093            public void deleteStatsUsersByUserId(long userId) throws SystemException {
094                    List<MBStatsUser> statsUsers = mbStatsUserPersistence.findByUserId(
095                            userId);
096    
097                    for (MBStatsUser statsUser : statsUsers) {
098                            deleteStatsUser(statsUser);
099                    }
100            }
101    
102            public long getMessageCountByUserId(long userId) throws SystemException {
103                    DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
104                            MBStatsUser.class, MBStatsUserImpl.TABLE_NAME,
105                            PortalClassLoaderUtil.getClassLoader());
106    
107                    dynamicQuery.setProjection(ProjectionFactoryUtil.sum("messageCount"));
108    
109                    dynamicQuery.add(PropertyFactoryUtil.forName("userId").eq(userId));
110    
111                    List<Long> results = mbStatsUserLocalService.dynamicQuery(dynamicQuery);
112    
113                    if (results.isEmpty()) {
114                            return 0;
115                    }
116    
117                    return results.get(0);
118            }
119    
120            public MBStatsUser getStatsUser(long groupId, long userId)
121                    throws SystemException {
122    
123                    MBStatsUser statsUser = mbStatsUserPersistence.fetchByG_U(
124                            groupId, userId);
125    
126                    if (statsUser == null) {
127                            statsUser = mbStatsUserLocalService.addStatsUser(groupId, userId);
128                    }
129    
130                    return statsUser;
131            }
132    
133            public List<MBStatsUser> getStatsUsersByGroupId(
134                            long groupId, int start, int end)
135                    throws PortalException, SystemException {
136    
137                    Group group = groupPersistence.findByPrimaryKey(groupId);
138    
139                    long defaultUserId = userLocalService.getDefaultUserId(
140                            group.getCompanyId());
141    
142                    return mbStatsUserPersistence.findByG_NotU_NotM(
143                            groupId, defaultUserId, 0, start, end);
144            }
145    
146            public int getStatsUsersByGroupIdCount(long groupId)
147                    throws PortalException, SystemException {
148    
149                    Group group = groupPersistence.findByPrimaryKey(groupId);
150    
151                    long defaultUserId = userLocalService.getDefaultUserId(
152                            group.getCompanyId());
153    
154                    return mbStatsUserPersistence.countByG_NotU_NotM(
155                            groupId, defaultUserId, 0);
156            }
157    
158            public List<MBStatsUser> getStatsUsersByUserId(long userId)
159                    throws SystemException {
160    
161                    return mbStatsUserPersistence.findByUserId(userId);
162            }
163    
164            public MBStatsUser updateStatsUser(long groupId, long userId)
165                    throws SystemException {
166    
167                    return updateStatsUser(groupId, userId, null);
168            }
169    
170            public MBStatsUser updateStatsUser(
171                            long groupId, long userId, Date lastPostDate)
172                    throws SystemException {
173    
174                    int messageCount = mbMessagePersistence.countByG_U(groupId, userId);
175    
176                    MBStatsUser statsUser = getStatsUser(groupId, userId);
177    
178                    statsUser.setMessageCount(messageCount);
179    
180                    if (lastPostDate != null) {
181                            statsUser.setLastPostDate(lastPostDate);
182                    }
183    
184                    mbStatsUserPersistence.update(statsUser, false);
185    
186                    return statsUser;
187            }
188    
189            private static Log _log = LogFactoryUtil.getLog(
190                    MBStatsUserLocalServiceImpl.class);
191    
192    }