1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.verify;
16  
17  import com.liferay.portal.kernel.dao.orm.QueryUtil;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.workflow.StatusConstants;
21  import com.liferay.portlet.messageboards.model.MBCategory;
22  import com.liferay.portlet.messageboards.model.MBMessage;
23  import com.liferay.portlet.messageboards.model.MBThread;
24  import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
25  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
26  import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
27  
28  import java.util.List;
29  
30  /**
31   * <a href="VerifyMessageBoards.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   */
35  public class VerifyMessageBoards extends VerifyProcess {
36  
37      protected void doVerify() throws Exception {
38          List<MBCategory> categories =
39              MBCategoryLocalServiceUtil.getMBCategories(
40                  QueryUtil.ALL_POS, QueryUtil.ALL_POS);
41  
42          if (_log.isDebugEnabled()) {
43              _log.debug(
44                  "Processing " + categories.size() +
45                      " categories for statistics accuracy");
46          }
47  
48          for (MBCategory category : categories) {
49              int threadCount = MBThreadLocalServiceUtil.getCategoryThreadsCount(
50                  category.getGroupId(), category.getCategoryId(),
51                  StatusConstants.APPROVED);
52              int messageCount =
53                  MBMessageLocalServiceUtil.getCategoryMessagesCount(
54                      category.getGroupId(), category.getCategoryId(),
55                      StatusConstants.APPROVED);
56  
57              if ((category.getThreadCount() != threadCount) ||
58                  (category.getMessageCount() != messageCount)) {
59  
60                  category.setThreadCount(threadCount);
61                  category.setMessageCount(messageCount);
62  
63                  MBCategoryLocalServiceUtil.updateMBCategory(category);
64              }
65          }
66  
67          if (_log.isDebugEnabled()) {
68              _log.debug("Statistics verified for categories");
69          }
70  
71          List<MBThread> threads = MBThreadLocalServiceUtil.getMBThreads(
72              QueryUtil.ALL_POS, QueryUtil.ALL_POS);
73  
74          if (_log.isDebugEnabled()) {
75              _log.debug(
76                  "Processing " + threads.size() +
77                      " threads for statistics accuracy");
78          }
79  
80          for (MBThread thread : threads) {
81              int messageCount = MBMessageLocalServiceUtil.getThreadMessagesCount(
82                  thread.getThreadId(), StatusConstants.APPROVED);
83  
84              if (thread.getMessageCount() != messageCount) {
85                  thread.setMessageCount(messageCount);
86  
87                  MBThreadLocalServiceUtil.updateMBThread(thread);
88              }
89          }
90  
91          if (_log.isDebugEnabled()) {
92              _log.debug("Statistics verified for threads");
93          }
94  
95          List<MBMessage> messages =
96              MBMessageLocalServiceUtil.getNoAssetMessages();
97  
98          if (_log.isDebugEnabled()) {
99              _log.debug(
100                 "Processing " + messages.size() + " messages with no asset");
101         }
102 
103         for (MBMessage message : messages) {
104             try {
105                 MBMessageLocalServiceUtil.updateAsset(
106                     message.getUserId(), message, null, null);
107             }
108             catch (Exception e) {
109                 if (_log.isWarnEnabled()) {
110                     _log.warn(
111                         "Unable to update asset for message " +
112                             message.getMessageId() + ": " + e.getMessage());
113                 }
114             }
115         }
116 
117         if (_log.isDebugEnabled()) {
118             _log.debug("Assets verified for messages");
119         }
120     }
121 
122     private static Log _log = LogFactoryUtil.getLog(VerifyMessageBoards.class);
123 
124 }