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.QueryUtil;
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
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 getLasPostDateByUserId(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 List<MBThread> threads = mbThreadLocalService.getGroupThreads(
125 groupId, WorkflowConstants.STATUS_IN_TRASH, QueryUtil.ALL_POS,
126 QueryUtil.ALL_POS);
127
128 for (MBThread thread : threads) {
129 disjunction.add(property.ne(thread.getThreadId()));
130 }
131
132 dynamicQuery.add(disjunction);
133
134 List<Date> results = mbStatsUserLocalService.dynamicQuery(dynamicQuery);
135
136 if (results.isEmpty()) {
137 return null;
138 }
139
140 return results.get(0);
141 }
142
143 public long getMessageCountByGroupId(long groupId) throws SystemException {
144 DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
145 MBStatsUser.class, MBStatsUserImpl.TABLE_NAME,
146 PACLClassLoaderUtil.getPortalClassLoader());
147
148 Projection projection = ProjectionFactoryUtil.sum("messageCount");
149
150 dynamicQuery.setProjection(projection);
151
152 Property property = PropertyFactoryUtil.forName("groupId");
153
154 dynamicQuery.add(property.eq(groupId));
155
156 List<Long> results = mbStatsUserLocalService.dynamicQuery(dynamicQuery);
157
158 if (results.isEmpty()) {
159 return 0;
160 }
161
162 return results.get(0);
163 }
164
165 public long getMessageCountByUserId(long userId) throws SystemException {
166 DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
167 MBStatsUser.class, MBStatsUserImpl.TABLE_NAME,
168 PACLClassLoaderUtil.getPortalClassLoader());
169
170 Projection projection = ProjectionFactoryUtil.sum("messageCount");
171
172 dynamicQuery.setProjection(projection);
173
174 Property property = PropertyFactoryUtil.forName("userId");
175
176 dynamicQuery.add(property.eq(userId));
177
178 List<Long> results = mbStatsUserLocalService.dynamicQuery(dynamicQuery);
179
180 if (results.isEmpty()) {
181 return 0;
182 }
183
184 return results.get(0);
185 }
186
187 public MBStatsUser getStatsUser(long groupId, long userId)
188 throws SystemException {
189
190 MBStatsUser statsUser = mbStatsUserPersistence.fetchByG_U(
191 groupId, userId);
192
193 if (statsUser == null) {
194 statsUser = mbStatsUserLocalService.addStatsUser(groupId, userId);
195 }
196
197 return statsUser;
198 }
199
200 public List<MBStatsUser> getStatsUsersByGroupId(
201 long groupId, int start, int end)
202 throws PortalException, SystemException {
203
204 Group group = groupPersistence.findByPrimaryKey(groupId);
205
206 long defaultUserId = userLocalService.getDefaultUserId(
207 group.getCompanyId());
208
209 return mbStatsUserPersistence.findByG_NotU_NotM(
210 groupId, defaultUserId, 0, start, end);
211 }
212
213 public int getStatsUsersByGroupIdCount(long groupId)
214 throws PortalException, SystemException {
215
216 Group group = groupPersistence.findByPrimaryKey(groupId);
217
218 long defaultUserId = userLocalService.getDefaultUserId(
219 group.getCompanyId());
220
221 return mbStatsUserPersistence.countByG_NotU_NotM(
222 groupId, defaultUserId, 0);
223 }
224
225 public List<MBStatsUser> getStatsUsersByUserId(long userId)
226 throws SystemException {
227
228 return mbStatsUserPersistence.findByUserId(userId);
229 }
230
231 public MBStatsUser updateStatsUser(long groupId, long userId)
232 throws SystemException {
233
234 return updateStatsUser(
235 groupId, userId, getLasPostDateByUserId(groupId, userId));
236 }
237
238 public MBStatsUser updateStatsUser(
239 long groupId, long userId, Date lastPostDate)
240 throws SystemException {
241
242 int messageCount = mbMessagePersistence.countByG_U_S(
243 groupId, userId, WorkflowConstants.STATUS_APPROVED);
244
245 List<MBThread> threads = mbThreadLocalService.getGroupThreads(
246 groupId, WorkflowConstants.STATUS_IN_TRASH, QueryUtil.ALL_POS,
247 QueryUtil.ALL_POS);
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 }