001
014
015 package com.liferay.portlet.messageboards.service.impl;
016
017 import com.liferay.portal.kernel.dao.orm.QueryDefinition;
018 import com.liferay.portal.kernel.exception.PortalException;
019 import com.liferay.portal.kernel.search.Hits;
020 import com.liferay.portal.kernel.workflow.WorkflowConstants;
021 import com.liferay.portal.model.Lock;
022 import com.liferay.portal.security.permission.ActionKeys;
023 import com.liferay.portal.security.permission.InlineSQLHelperUtil;
024 import com.liferay.portal.service.ServiceContext;
025 import com.liferay.portlet.messageboards.LockedThreadException;
026 import com.liferay.portlet.messageboards.model.MBCategoryConstants;
027 import com.liferay.portlet.messageboards.model.MBMessage;
028 import com.liferay.portlet.messageboards.model.MBThread;
029 import com.liferay.portlet.messageboards.model.impl.MBThreadModelImpl;
030 import com.liferay.portlet.messageboards.service.base.MBThreadServiceBaseImpl;
031 import com.liferay.portlet.messageboards.service.permission.MBCategoryPermission;
032 import com.liferay.portlet.messageboards.service.permission.MBMessagePermission;
033
034 import java.util.ArrayList;
035 import java.util.Collections;
036 import java.util.Date;
037 import java.util.List;
038
039
045 public class MBThreadServiceImpl extends MBThreadServiceBaseImpl {
046
047 @Override
048 public void deleteThread(long threadId) throws PortalException {
049 if (lockLocalService.isLocked(MBThread.class.getName(), threadId)) {
050 throw new LockedThreadException();
051 }
052
053 List<MBMessage> messages = mbMessagePersistence.findByThreadId(
054 threadId);
055
056 for (MBMessage message : messages) {
057 MBMessagePermission.check(
058 getPermissionChecker(), message.getMessageId(),
059 ActionKeys.DELETE);
060 }
061
062 mbThreadLocalService.deleteThread(threadId);
063 }
064
065 @Override
066 public List<MBThread> getGroupThreads(
067 long groupId, long userId, Date modifiedDate, int status, int start,
068 int end)
069 throws PortalException {
070
071 if (!InlineSQLHelperUtil.isEnabled(groupId)) {
072 QueryDefinition<MBThread> queryDefinition =
073 new QueryDefinition<MBThread>(status, start, end, null);
074
075 return mbThreadFinder.findByG_U_LPD(
076 groupId, userId, modifiedDate, queryDefinition);
077 }
078
079 long[] categoryIds = mbCategoryService.getCategoryIds(
080 groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
081
082 if (categoryIds.length == 0) {
083 return Collections.emptyList();
084 }
085
086 List<Long> threadIds = mbMessageFinder.filterFindByG_U_MD_C_S(
087 groupId, userId, modifiedDate, categoryIds, status, start, end);
088
089 List<MBThread> threads = new ArrayList<MBThread>(threadIds.size());
090
091 for (long threadId : threadIds) {
092 MBThread thread = mbThreadPersistence.findByPrimaryKey(threadId);
093
094 threads.add(thread);
095 }
096
097 return threads;
098 }
099
100 @Override
101 public List<MBThread> getGroupThreads(
102 long groupId, long userId, int status, boolean subscribed,
103 boolean includeAnonymous, int start, int end)
104 throws PortalException {
105
106 if (!InlineSQLHelperUtil.isEnabled(groupId)) {
107 return doGetGroupThreads(
108 groupId, userId, status, subscribed, includeAnonymous, start,
109 end);
110 }
111
112 long[] categoryIds = mbCategoryService.getCategoryIds(
113 groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
114
115 if (categoryIds.length == 0) {
116 return Collections.emptyList();
117 }
118
119 List<Long> threadIds = null;
120
121 if (userId <= 0) {
122 threadIds = mbMessageFinder.filterFindByG_U_C_S(
123 groupId, 0, categoryIds, status, start, end);
124 }
125 else {
126 if (subscribed) {
127 QueryDefinition<MBThread> queryDefinition =
128 new QueryDefinition<MBThread>(status, start, end, null);
129
130 return mbThreadFinder.filterFindByS_G_U_C(
131 groupId, userId, categoryIds, queryDefinition);
132 }
133 else {
134 if (includeAnonymous) {
135 threadIds = mbMessageFinder.filterFindByG_U_C_S(
136 groupId, userId, categoryIds, status, start, end);
137 }
138 else {
139 threadIds = mbMessageFinder.filterFindByG_U_C_A_S(
140 groupId, userId, categoryIds, false, status, start,
141 end);
142 }
143 }
144 }
145
146 List<MBThread> threads = new ArrayList<MBThread>(threadIds.size());
147
148 for (long threadId : threadIds) {
149 MBThread thread = mbThreadPersistence.findByPrimaryKey(threadId);
150
151 threads.add(thread);
152 }
153
154 return threads;
155 }
156
157 @Override
158 public List<MBThread> getGroupThreads(
159 long groupId, long userId, int status, boolean subscribed,
160 int start, int end)
161 throws PortalException {
162
163 return getGroupThreads(
164 groupId, userId, status, subscribed, true, start, end);
165 }
166
167 @Override
168 public List<MBThread> getGroupThreads(
169 long groupId, long userId, int status, int start, int end)
170 throws PortalException {
171
172 return getGroupThreads(groupId, userId, status, false, start, end);
173 }
174
175 @Override
176 public int getGroupThreadsCount(
177 long groupId, long userId, Date modifiedDate, int status) {
178
179 if (!InlineSQLHelperUtil.isEnabled(groupId)) {
180 QueryDefinition<MBThread> queryDefinition =
181 new QueryDefinition<MBThread>(status);
182
183 return mbThreadFinder.countByG_U_LPD(
184 groupId, userId, modifiedDate, queryDefinition);
185 }
186
187 long[] categoryIds = mbCategoryService.getCategoryIds(
188 groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
189
190 if (categoryIds.length == 0) {
191 return 0;
192 }
193
194 return mbMessageFinder.filterCountByG_U_MD_C_S(
195 groupId, userId, modifiedDate, categoryIds, status);
196 }
197
198 @Override
199 public int getGroupThreadsCount(long groupId, long userId, int status) {
200 return getGroupThreadsCount(groupId, userId, status, false);
201 }
202
203 @Override
204 public int getGroupThreadsCount(
205 long groupId, long userId, int status, boolean subscribed) {
206
207 return getGroupThreadsCount(groupId, userId, status, subscribed, true);
208 }
209
210 @Override
211 public int getGroupThreadsCount(
212 long groupId, long userId, int status, boolean subscribed,
213 boolean includeAnonymous) {
214
215 if (!InlineSQLHelperUtil.isEnabled(groupId)) {
216 return doGetGroupThreadsCount(
217 groupId, userId, status, subscribed, includeAnonymous);
218 }
219
220 long[] categoryIds = mbCategoryService.getCategoryIds(
221 groupId, MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
222
223 if (categoryIds.length == 0) {
224 return 0;
225 }
226
227 if (userId <= 0) {
228 return mbMessageFinder.filterCountByG_U_C_S(
229 groupId, 0, categoryIds, status);
230 }
231 else {
232 if (subscribed) {
233 QueryDefinition<MBThread> queryDefinition =
234 new QueryDefinition<MBThread>(status);
235
236 return mbThreadFinder.filterCountByS_G_U_C(
237 groupId, userId, categoryIds, queryDefinition);
238 }
239 else {
240 if (includeAnonymous) {
241 return mbMessageFinder.filterCountByG_U_C_S(
242 groupId, userId, categoryIds, status);
243 }
244 else {
245 return mbMessageFinder.filterCountByG_U_C_A_S(
246 groupId, userId, categoryIds, false, status);
247 }
248 }
249 }
250 }
251
252 @Override
253 public List<MBThread> getThreads(
254 long groupId, long categoryId, int status, int start, int end) {
255
256 QueryDefinition<MBThread> queryDefinition =
257 new QueryDefinition<MBThread>(status, start, end, null);
258
259 return mbThreadFinder.filterFindByG_C(
260 groupId, categoryId, queryDefinition);
261 }
262
263 @Override
264 public int getThreadsCount(long groupId, long categoryId, int status) {
265 if (status == WorkflowConstants.STATUS_ANY) {
266 return mbThreadFinder.filterCountByG_C(groupId, categoryId);
267 }
268 else {
269 QueryDefinition<MBThread> queryDefinition =
270 new QueryDefinition<MBThread>(status);
271
272 return mbThreadFinder.filterCountByG_C(
273 groupId, categoryId, queryDefinition);
274 }
275 }
276
277 @Override
278 public Lock lockThread(long threadId) throws PortalException {
279 MBThread thread = mbThreadPersistence.findByPrimaryKey(threadId);
280
281 MBCategoryPermission.check(
282 getPermissionChecker(), thread.getGroupId(), thread.getCategoryId(),
283 ActionKeys.LOCK_THREAD);
284
285 return lockLocalService.lock(
286 getUserId(), MBThread.class.getName(), threadId,
287 String.valueOf(threadId), false,
288 MBThreadModelImpl.LOCK_EXPIRATION_TIME);
289 }
290
291 @Override
292 public MBThread moveThread(long categoryId, long threadId)
293 throws PortalException {
294
295 if (lockLocalService.isLocked(MBThread.class.getName(), threadId)) {
296 throw new LockedThreadException();
297 }
298
299 MBThread thread = mbThreadLocalService.getThread(threadId);
300
301 MBCategoryPermission.check(
302 getPermissionChecker(), thread.getGroupId(), thread.getCategoryId(),
303 ActionKeys.MOVE_THREAD);
304
305 MBCategoryPermission.check(
306 getPermissionChecker(), thread.getGroupId(), categoryId,
307 ActionKeys.MOVE_THREAD);
308
309 return mbThreadLocalService.moveThread(
310 thread.getGroupId(), categoryId, threadId);
311 }
312
313 @Override
314 public MBThread moveThreadFromTrash(long categoryId, long threadId)
315 throws PortalException {
316
317 MBThread thread = mbThreadLocalService.getThread(threadId);
318
319 MBCategoryPermission.check(
320 getPermissionChecker(), thread.getGroupId(), thread.getCategoryId(),
321 ActionKeys.UPDATE);
322
323 return mbThreadLocalService.moveThreadFromTrash(
324 getUserId(), categoryId, threadId);
325 }
326
327 @Override
328 public MBThread moveThreadToTrash(long threadId) throws PortalException {
329 if (lockLocalService.isLocked(MBThread.class.getName(), threadId)) {
330 throw new LockedThreadException();
331 }
332
333 List<MBMessage> messages = mbMessagePersistence.findByThreadId(
334 threadId);
335
336 for (MBMessage message : messages) {
337 MBMessagePermission.check(
338 getPermissionChecker(), message.getMessageId(),
339 ActionKeys.DELETE);
340 }
341
342 return mbThreadLocalService.moveThreadToTrash(getUserId(), threadId);
343 }
344
345 @Override
346 public void restoreThreadFromTrash(long threadId) throws PortalException {
347 List<MBMessage> messages = mbMessagePersistence.findByThreadId(
348 threadId);
349
350 for (MBMessage message : messages) {
351 MBMessagePermission.check(
352 getPermissionChecker(), message.getMessageId(),
353 ActionKeys.DELETE);
354 }
355
356 mbThreadLocalService.restoreThreadFromTrash(getUserId(), threadId);
357 }
358
359 @Override
360 public Hits search(
361 long groupId, long creatorUserId, int status, int start, int end)
362 throws PortalException {
363
364 return mbThreadLocalService.search(
365 groupId, getUserId(), creatorUserId, status, start, end);
366 }
367
368 @Override
369 public Hits search(
370 long groupId, long creatorUserId, long startDate, long endDate,
371 int status, int start, int end)
372 throws PortalException {
373
374 return mbThreadLocalService.search(
375 groupId, getUserId(), creatorUserId, startDate, endDate, status,
376 start, end);
377 }
378
379 @Override
380 public MBThread splitThread(
381 long messageId, String subject, ServiceContext serviceContext)
382 throws PortalException {
383
384 MBMessage message = mbMessageLocalService.getMessage(messageId);
385
386 MBCategoryPermission.check(
387 getPermissionChecker(), message.getGroupId(),
388 message.getCategoryId(), ActionKeys.MOVE_THREAD);
389
390 return mbThreadLocalService.splitThread(
391 messageId, subject, serviceContext);
392 }
393
394 @Override
395 public void unlockThread(long threadId) throws PortalException {
396 MBThread thread = mbThreadLocalService.getThread(threadId);
397
398 MBCategoryPermission.check(
399 getPermissionChecker(), thread.getGroupId(), thread.getCategoryId(),
400 ActionKeys.LOCK_THREAD);
401
402 lockLocalService.unlock(MBThread.class.getName(), threadId);
403 }
404
405 protected List<MBThread> doGetGroupThreads(
406 long groupId, long userId, int status, boolean subscribed,
407 boolean includeAnonymous, int start, int end) {
408
409 if (userId <= 0) {
410 if (status == WorkflowConstants.STATUS_ANY) {
411 return mbThreadPersistence.findByGroupId(groupId, start, end);
412 }
413 else {
414 return mbThreadPersistence.findByG_S(
415 groupId, status, start, end);
416 }
417 }
418
419 QueryDefinition<MBThread> queryDefinition =
420 new QueryDefinition<MBThread>(status, start, end, null);
421
422 if (subscribed) {
423 return mbThreadFinder.findByS_G_U(groupId, userId, queryDefinition);
424 }
425 else if (includeAnonymous) {
426 return mbThreadFinder.findByG_U(groupId, userId, queryDefinition);
427 }
428 else {
429 return mbThreadFinder.findByG_U_A(
430 groupId, userId, false, queryDefinition);
431 }
432 }
433
434 protected int doGetGroupThreadsCount(
435 long groupId, long userId, int status, boolean subscribed,
436 boolean includeAnonymous) {
437
438 if (userId <= 0) {
439 if (status == WorkflowConstants.STATUS_ANY) {
440 return mbThreadPersistence.countByGroupId(groupId);
441 }
442 else {
443 return mbThreadPersistence.countByG_S(groupId, status);
444 }
445 }
446
447 QueryDefinition<MBThread> queryDefinition =
448 new QueryDefinition<MBThread>(status);
449
450 if (subscribed) {
451 return mbThreadFinder.countByS_G_U(
452 groupId, userId, queryDefinition);
453 }
454 else if (includeAnonymous) {
455 return mbThreadFinder.countByG_U(groupId, userId, queryDefinition);
456 }
457 else {
458 return mbThreadFinder.countByG_U_A(
459 groupId, userId, false, queryDefinition);
460 }
461 }
462
463 }