001
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.MBCategoryConstants;
034 import com.liferay.portlet.messageboards.model.MBStatsUser;
035 import com.liferay.portlet.messageboards.model.MBThread;
036 import com.liferay.portlet.messageboards.model.impl.MBStatsUserImpl;
037 import com.liferay.portlet.messageboards.service.base.MBStatsUserLocalServiceBaseImpl;
038
039 import java.util.Date;
040 import java.util.List;
041
042
045 public class MBStatsUserLocalServiceImpl
046 extends MBStatsUserLocalServiceBaseImpl {
047
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 public void deleteStatsUser(long statsUserId)
080 throws PortalException, SystemException {
081
082 MBStatsUser statsUser = mbStatsUserPersistence.findByPrimaryKey(
083 statsUserId);
084
085 deleteStatsUser(statsUser);
086 }
087
088 public void deleteStatsUser(MBStatsUser statsUser) throws SystemException {
089 mbStatsUserPersistence.remove(statsUser);
090 }
091
092 public void deleteStatsUsersByGroupId(long groupId) throws SystemException {
093 List<MBStatsUser> statsUsers = mbStatsUserPersistence.findByGroupId(
094 groupId);
095
096 for (MBStatsUser statsUser : statsUsers) {
097 deleteStatsUser(statsUser);
098 }
099 }
100
101 public void deleteStatsUsersByUserId(long userId) throws SystemException {
102 List<MBStatsUser> statsUsers = mbStatsUserPersistence.findByUserId(
103 userId);
104
105 for (MBStatsUser statsUser : statsUsers) {
106 deleteStatsUser(statsUser);
107 }
108 }
109
110 public Date getLastPostDateByUserId(long groupId, long userId)
111 throws SystemException {
112
113 DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
114 MBThread.class, MBStatsUserImpl.TABLE_NAME,
115 ClassLoaderUtil.getPortalClassLoader());
116
117 Projection projection = ProjectionFactoryUtil.max("lastPostDate");
118
119 dynamicQuery.setProjection(projection);
120
121 Property property = PropertyFactoryUtil.forName("threadId");
122
123 Disjunction disjunction = RestrictionsFactoryUtil.disjunction();
124
125 QueryDefinition queryDefinition = new QueryDefinition(
126 WorkflowConstants.STATUS_IN_TRASH);
127
128 List<MBThread> threads = mbThreadLocalService.getGroupThreads(
129 groupId, queryDefinition);
130
131 for (MBThread thread : threads) {
132 disjunction.add(property.ne(thread.getThreadId()));
133 }
134
135 dynamicQuery.add(disjunction);
136
137 List<Date> results = mbStatsUserLocalService.dynamicQuery(dynamicQuery);
138
139 return results.get(0);
140 }
141
142 public long getMessageCountByGroupId(long groupId) throws SystemException {
143 DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
144 MBStatsUser.class, MBStatsUserImpl.TABLE_NAME,
145 ClassLoaderUtil.getPortalClassLoader());
146
147 Projection projection = ProjectionFactoryUtil.sum("messageCount");
148
149 dynamicQuery.setProjection(projection);
150
151 Property property = PropertyFactoryUtil.forName("groupId");
152
153 dynamicQuery.add(property.eq(groupId));
154
155 List<Long> results = mbStatsUserLocalService.dynamicQuery(dynamicQuery);
156
157 if (results.get(0) == null) {
158 return 0;
159 }
160
161 return results.get(0);
162 }
163
164 public long getMessageCountByUserId(long userId) throws SystemException {
165 DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
166 MBStatsUser.class, MBStatsUserImpl.TABLE_NAME,
167 ClassLoaderUtil.getPortalClassLoader());
168
169 Projection projection = ProjectionFactoryUtil.sum("messageCount");
170
171 dynamicQuery.setProjection(projection);
172
173 Property property = PropertyFactoryUtil.forName("userId");
174
175 dynamicQuery.add(property.eq(userId));
176
177 List<Long> results = mbStatsUserLocalService.dynamicQuery(dynamicQuery);
178
179 if (results.get(0) == null) {
180 return 0;
181 }
182
183 return results.get(0);
184 }
185
186 public MBStatsUser getStatsUser(long groupId, long userId)
187 throws SystemException {
188
189 MBStatsUser statsUser = mbStatsUserPersistence.fetchByG_U(
190 groupId, userId);
191
192 if (statsUser == null) {
193 statsUser = mbStatsUserLocalService.addStatsUser(groupId, userId);
194 }
195
196 return statsUser;
197 }
198
199 public List<MBStatsUser> getStatsUsersByGroupId(
200 long groupId, int start, int end)
201 throws PortalException, SystemException {
202
203 Group group = groupPersistence.findByPrimaryKey(groupId);
204
205 long defaultUserId = userLocalService.getDefaultUserId(
206 group.getCompanyId());
207
208 return mbStatsUserPersistence.findByG_NotU_NotM(
209 groupId, defaultUserId, 0, start, end);
210 }
211
212 public int getStatsUsersByGroupIdCount(long groupId)
213 throws PortalException, SystemException {
214
215 Group group = groupPersistence.findByPrimaryKey(groupId);
216
217 long defaultUserId = userLocalService.getDefaultUserId(
218 group.getCompanyId());
219
220 return mbStatsUserPersistence.countByG_NotU_NotM(
221 groupId, defaultUserId, 0);
222 }
223
224 public List<MBStatsUser> getStatsUsersByUserId(long userId)
225 throws SystemException {
226
227 return mbStatsUserPersistence.findByUserId(userId);
228 }
229
230 public MBStatsUser updateStatsUser(long groupId, long userId)
231 throws SystemException {
232
233 return updateStatsUser(
234 groupId, userId, getLastPostDateByUserId(groupId, userId));
235 }
236
237 public MBStatsUser updateStatsUser(
238 long groupId, long userId, Date lastPostDate)
239 throws SystemException {
240
241 long[] categoryIds = mbCategoryService.getCategoryIds(
242 groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
243
244 int messageCount = mbMessagePersistence.countByG_U_C_S(
245 groupId, userId, categoryIds, WorkflowConstants.STATUS_APPROVED);
246
247 QueryDefinition queryDefinition = new QueryDefinition(
248 WorkflowConstants.STATUS_IN_TRASH);
249
250 List<MBThread> threads = mbThreadLocalService.getGroupThreads(
251 groupId, queryDefinition);
252
253 for (MBThread thread : threads) {
254 messageCount = messageCount - thread.getMessageCount();
255 }
256
257 return updateStatsUser(groupId, userId, messageCount, lastPostDate);
258 }
259
260 public MBStatsUser updateStatsUser(
261 long groupId, long userId, int messageCount, Date lastPostDate)
262 throws SystemException {
263
264 MBStatsUser statsUser = getStatsUser(groupId, userId);
265
266 statsUser.setMessageCount(messageCount);
267
268 if (lastPostDate != null) {
269 statsUser.setLastPostDate(lastPostDate);
270 }
271
272 mbStatsUserPersistence.update(statsUser);
273
274 return statsUser;
275 }
276
277 private static Log _log = LogFactoryUtil.getLog(
278 MBStatsUserLocalServiceImpl.class);
279
280 }