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