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