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.util.ClassLoaderUtil;
031 import com.liferay.portal.kernel.workflow.WorkflowConstants;
032 import com.liferay.portal.model.Group;
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 @Override
048 public MBStatsUser addStatsUser(long groupId, long userId) {
049 long statsUserId = counterLocalService.increment();
050
051 MBStatsUser statsUser = mbStatsUserPersistence.create(statsUserId);
052
053 statsUser.setGroupId(groupId);
054 statsUser.setUserId(userId);
055
056 try {
057 mbStatsUserPersistence.update(statsUser);
058 }
059 catch (SystemException se) {
060 if (_log.isWarnEnabled()) {
061 _log.warn(
062 "Add failed, fetch {groupId=" + groupId + ", userId=" +
063 userId + "}");
064 }
065
066 statsUser = mbStatsUserPersistence.fetchByG_U(
067 groupId, userId, false);
068
069 if (statsUser == null) {
070 throw se;
071 }
072 }
073
074 return statsUser;
075 }
076
077 @Override
078 public void deleteStatsUser(long statsUserId) throws PortalException {
079 MBStatsUser statsUser = mbStatsUserPersistence.findByPrimaryKey(
080 statsUserId);
081
082 deleteStatsUser(statsUser);
083 }
084
085 @Override
086 public void deleteStatsUser(MBStatsUser statsUser) {
087 mbStatsUserPersistence.remove(statsUser);
088 }
089
090 @Override
091 public void deleteStatsUsersByGroupId(long groupId) {
092 List<MBStatsUser> statsUsers = mbStatsUserPersistence.findByGroupId(
093 groupId);
094
095 for (MBStatsUser statsUser : statsUsers) {
096 deleteStatsUser(statsUser);
097 }
098 }
099
100 @Override
101 public void deleteStatsUsersByUserId(long userId) {
102 List<MBStatsUser> statsUsers = mbStatsUserPersistence.findByUserId(
103 userId);
104
105 for (MBStatsUser statsUser : statsUsers) {
106 deleteStatsUser(statsUser);
107 }
108 }
109
110 @Override
111 public Date getLastPostDateByUserId(long groupId, long userId) {
112 DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
113 MBThread.class, MBStatsUserImpl.TABLE_NAME,
114 ClassLoaderUtil.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 QueryDefinition<MBThread> queryDefinition = new QueryDefinition<>(
125 WorkflowConstants.STATUS_IN_TRASH);
126
127 List<MBThread> threads = mbThreadLocalService.getGroupThreads(
128 groupId, queryDefinition);
129
130 for (MBThread thread : threads) {
131 disjunction.add(property.ne(thread.getThreadId()));
132 }
133
134 dynamicQuery.add(disjunction);
135
136 List<Date> results = mbStatsUserLocalService.dynamicQuery(dynamicQuery);
137
138 return results.get(0);
139 }
140
141 @Override
142 public long getMessageCountByGroupId(long groupId) {
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 @Override
165 public long getMessageCountByUserId(long userId) {
166 DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
167 MBStatsUser.class, MBStatsUserImpl.TABLE_NAME,
168 ClassLoaderUtil.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.get(0) == null) {
181 return 0;
182 }
183
184 return results.get(0);
185 }
186
187 @Override
188 public MBStatsUser getStatsUser(long groupId, long userId) {
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 @Override
200 public List<MBStatsUser> getStatsUsersByGroupId(
201 long groupId, int start, int end)
202 throws PortalException {
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 @Override
214 public int getStatsUsersByGroupIdCount(long groupId)
215 throws PortalException {
216
217 Group group = groupPersistence.findByPrimaryKey(groupId);
218
219 long defaultUserId = userLocalService.getDefaultUserId(
220 group.getCompanyId());
221
222 return mbStatsUserPersistence.countByG_NotU_NotM(
223 groupId, defaultUserId, 0);
224 }
225
226 @Override
227 public List<MBStatsUser> getStatsUsersByUserId(long userId) {
228 return mbStatsUserPersistence.findByUserId(userId);
229 }
230
231 @Override
232 public MBStatsUser updateStatsUser(long groupId, long userId) {
233 return updateStatsUser(
234 groupId, userId, getLastPostDateByUserId(groupId, userId));
235 }
236
237 @Override
238 public MBStatsUser updateStatsUser(
239 long groupId, long userId, Date lastPostDate) {
240
241 int messageCount = mbMessagePersistence.countByG_U_S(
242 groupId, userId, WorkflowConstants.STATUS_APPROVED);
243
244 return updateStatsUser(groupId, userId, messageCount, lastPostDate);
245 }
246
247 @Override
248 public MBStatsUser updateStatsUser(
249 long groupId, long userId, int messageCount, Date lastPostDate) {
250
251 MBStatsUser statsUser = getStatsUser(groupId, userId);
252
253 statsUser.setMessageCount(messageCount);
254
255 if (lastPostDate != null) {
256 statsUser.setLastPostDate(lastPostDate);
257 }
258
259 mbStatsUserPersistence.update(statsUser);
260
261 return statsUser;
262 }
263
264 private static final Log _log = LogFactoryUtil.getLog(
265 MBStatsUserLocalServiceImpl.class);
266
267 }