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