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.Disjunction;
018    import com.liferay.portal.kernel.dao.orm.DynamicQuery;
019    import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
020    import com.liferay.portal.kernel.dao.orm.Projection;
021    import com.liferay.portal.kernel.dao.orm.ProjectionFactoryUtil;
022    import com.liferay.portal.kernel.dao.orm.Property;
023    import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
024    import com.liferay.portal.kernel.dao.orm.QueryDefinition;
025    import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil;
026    import com.liferay.portal.kernel.exception.PortalException;
027    import com.liferay.portal.kernel.exception.SystemException;
028    import com.liferay.portal.kernel.log.Log;
029    import com.liferay.portal.kernel.log.LogFactoryUtil;
030    import com.liferay.portal.kernel.workflow.WorkflowConstants;
031    import com.liferay.portal.model.Group;
032    import com.liferay.portal.security.pacl.PACLClassLoaderUtil;
033    import com.liferay.portlet.messageboards.model.MBStatsUser;
034    import com.liferay.portlet.messageboards.model.MBThread;
035    import com.liferay.portlet.messageboards.model.impl.MBStatsUserImpl;
036    import com.liferay.portlet.messageboards.service.base.MBStatsUserLocalServiceBaseImpl;
037    
038    import java.util.Date;
039    import java.util.List;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     */
044    public class MBStatsUserLocalServiceImpl
045            extends MBStatsUserLocalServiceBaseImpl {
046    
047            public MBStatsUser addStatsUser(long groupId, long userId)
048                    throws SystemException {
049    
050                    long statsUserId = counterLocalService.increment();
051    
052                    MBStatsUser statsUser = mbStatsUserPersistence.create(statsUserId);
053    
054                    statsUser.setGroupId(groupId);
055                    statsUser.setUserId(userId);
056    
057                    try {
058                            mbStatsUserPersistence.update(statsUser);
059                    }
060                    catch (SystemException se) {
061                            if (_log.isWarnEnabled()) {
062                                    _log.warn(
063                                            "Add failed, fetch {groupId=" + groupId + ", userId=" +
064                                                    userId + "}");
065                            }
066    
067                            statsUser = mbStatsUserPersistence.fetchByG_U(
068                                    groupId, userId, false);
069    
070                            if (statsUser == null) {
071                                    throw se;
072                            }
073                    }
074    
075                    return statsUser;
076            }
077    
078            public void deleteStatsUser(long statsUserId)
079                    throws PortalException, SystemException {
080    
081                    MBStatsUser statsUser = mbStatsUserPersistence.findByPrimaryKey(
082                            statsUserId);
083    
084                    deleteStatsUser(statsUser);
085            }
086    
087            public void deleteStatsUser(MBStatsUser statsUser) throws SystemException {
088                    mbStatsUserPersistence.remove(statsUser);
089            }
090    
091            public void deleteStatsUsersByGroupId(long groupId) throws SystemException {
092                    List<MBStatsUser> statsUsers = mbStatsUserPersistence.findByGroupId(
093                            groupId);
094    
095                    for (MBStatsUser statsUser : statsUsers) {
096                            deleteStatsUser(statsUser);
097                    }
098            }
099    
100            public void deleteStatsUsersByUserId(long userId) throws SystemException {
101                    List<MBStatsUser> statsUsers = mbStatsUserPersistence.findByUserId(
102                            userId);
103    
104                    for (MBStatsUser statsUser : statsUsers) {
105                            deleteStatsUser(statsUser);
106                    }
107            }
108    
109            public Date getLastPostDateByUserId(long groupId, long userId)
110                    throws SystemException {
111    
112                    DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
113                            MBThread.class, MBStatsUserImpl.TABLE_NAME,
114                            PACLClassLoaderUtil.getPortalClassLoader());
115    
116                    Projection projection = ProjectionFactoryUtil.max("lastPostDate");
117    
118                    dynamicQuery.setProjection(projection);
119    
120                    Property property = PropertyFactoryUtil.forName("threadId");
121    
122                    Disjunction disjunction = RestrictionsFactoryUtil.disjunction();
123    
124                    QueryDefinition queryDefinition = new QueryDefinition(
125                            WorkflowConstants.STATUS_IN_TRASH);
126    
127                    List<MBThread> threads = mbThreadLocalService.getGroupThreads(
128                            groupId, queryDefinition);
129    
130                    for (MBThread thread : threads) {
131                            disjunction.add(property.ne(thread.getThreadId()));
132                    }
133    
134                    dynamicQuery.add(disjunction);
135    
136                    List<Date> results = mbStatsUserLocalService.dynamicQuery(dynamicQuery);
137    
138                    return results.get(0);
139            }
140    
141            public long getMessageCountByGroupId(long groupId) throws SystemException {
142                    DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
143                            MBStatsUser.class, MBStatsUserImpl.TABLE_NAME,
144                            PACLClassLoaderUtil.getPortalClassLoader());
145    
146                    Projection projection = ProjectionFactoryUtil.sum("messageCount");
147    
148                    dynamicQuery.setProjection(projection);
149    
150                    Property property = PropertyFactoryUtil.forName("groupId");
151    
152                    dynamicQuery.add(property.eq(groupId));
153    
154                    List<Long> results = mbStatsUserLocalService.dynamicQuery(dynamicQuery);
155    
156                    if (results.get(0) == null) {
157                            return 0;
158                    }
159    
160                    return results.get(0);
161            }
162    
163            public long getMessageCountByUserId(long userId) throws SystemException {
164                    DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
165                            MBStatsUser.class, MBStatsUserImpl.TABLE_NAME,
166                            PACLClassLoaderUtil.getPortalClassLoader());
167    
168                    Projection projection = ProjectionFactoryUtil.sum("messageCount");
169    
170                    dynamicQuery.setProjection(projection);
171    
172                    Property property = PropertyFactoryUtil.forName("userId");
173    
174                    dynamicQuery.add(property.eq(userId));
175    
176                    List<Long> results = mbStatsUserLocalService.dynamicQuery(dynamicQuery);
177    
178                    if (results.get(0) == null) {
179                            return 0;
180                    }
181    
182                    return results.get(0);
183            }
184    
185            public MBStatsUser getStatsUser(long groupId, long userId)
186                    throws SystemException {
187    
188                    MBStatsUser statsUser = mbStatsUserPersistence.fetchByG_U(
189                            groupId, userId);
190    
191                    if (statsUser == null) {
192                            statsUser = mbStatsUserLocalService.addStatsUser(groupId, userId);
193                    }
194    
195                    return statsUser;
196            }
197    
198            public List<MBStatsUser> getStatsUsersByGroupId(
199                            long groupId, int start, int end)
200                    throws PortalException, SystemException {
201    
202                    Group group = groupPersistence.findByPrimaryKey(groupId);
203    
204                    long defaultUserId = userLocalService.getDefaultUserId(
205                            group.getCompanyId());
206    
207                    return mbStatsUserPersistence.findByG_NotU_NotM(
208                            groupId, defaultUserId, 0, start, end);
209            }
210    
211            public int getStatsUsersByGroupIdCount(long groupId)
212                    throws PortalException, SystemException {
213    
214                    Group group = groupPersistence.findByPrimaryKey(groupId);
215    
216                    long defaultUserId = userLocalService.getDefaultUserId(
217                            group.getCompanyId());
218    
219                    return mbStatsUserPersistence.countByG_NotU_NotM(
220                            groupId, defaultUserId, 0);
221            }
222    
223            public List<MBStatsUser> getStatsUsersByUserId(long userId)
224                    throws SystemException {
225    
226                    return mbStatsUserPersistence.findByUserId(userId);
227            }
228    
229            public MBStatsUser updateStatsUser(long groupId, long userId)
230                    throws SystemException {
231    
232                    return updateStatsUser(
233                            groupId, userId, getLastPostDateByUserId(groupId, userId));
234            }
235    
236            public MBStatsUser updateStatsUser(
237                            long groupId, long userId, Date lastPostDate)
238                    throws SystemException {
239    
240                    int messageCount = mbMessagePersistence.countByG_U_S(
241                            groupId, userId, WorkflowConstants.STATUS_APPROVED);
242    
243                    QueryDefinition queryDefinition = new QueryDefinition(
244                            WorkflowConstants.STATUS_IN_TRASH);
245    
246                    List<MBThread> threads = mbThreadLocalService.getGroupThreads(
247                            groupId, queryDefinition);
248    
249                    for (MBThread thread : threads) {
250                            messageCount = messageCount - thread.getMessageCount();
251                    }
252    
253                    return updateStatsUser(groupId, userId, messageCount, lastPostDate);
254            }
255    
256            public MBStatsUser updateStatsUser(
257                            long groupId, long userId, int messageCount, Date lastPostDate)
258                    throws SystemException {
259    
260                    MBStatsUser statsUser = getStatsUser(groupId, userId);
261    
262                    statsUser.setMessageCount(messageCount);
263    
264                    if (lastPostDate != null) {
265                            statsUser.setLastPostDate(lastPostDate);
266                    }
267    
268                    mbStatsUserPersistence.update(statsUser);
269    
270                    return statsUser;
271            }
272    
273            private static Log _log = LogFactoryUtil.getLog(
274                    MBStatsUserLocalServiceImpl.class);
275    
276    }