1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.verify;
24  
25  import com.liferay.portal.kernel.dao.orm.QueryUtil;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portlet.messageboards.model.MBCategory;
29  import com.liferay.portlet.messageboards.model.MBMessage;
30  import com.liferay.portlet.messageboards.model.MBThread;
31  import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
32  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
33  import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
34  
35  import java.util.List;
36  
37  /**
38   * <a href="VerifyMessageBoards.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class VerifyMessageBoards extends VerifyProcess {
44  
45      public void verify() throws VerifyException {
46          _log.info("Verifying");
47  
48          try {
49              verifyMessageBoards();
50          }
51          catch (Exception e) {
52              throw new VerifyException(e);
53          }
54      }
55  
56      protected void verifyMessageBoards() throws Exception {
57          List<MBCategory> categories =
58              MBCategoryLocalServiceUtil.getMBCategories(
59                  QueryUtil.ALL_POS, QueryUtil.ALL_POS);
60  
61          if (_log.isDebugEnabled()) {
62              _log.debug(
63                  "Processing " + categories.size() +
64                      " categories for statistics accuracy");
65          }
66  
67          for (MBCategory category : categories) {
68              int threadCount = MBThreadLocalServiceUtil.getCategoryThreadsCount(
69                  category.getCategoryId());
70              int messageCount =
71                  MBMessageLocalServiceUtil.getCategoryMessagesCount(
72                      category.getCategoryId());
73  
74              if ((category.getThreadCount() != threadCount) ||
75                  (category.getMessageCount() != messageCount)) {
76  
77                  category.setThreadCount(threadCount);
78                  category.setMessageCount(messageCount);
79  
80                  MBCategoryLocalServiceUtil.updateMBCategory(category);
81              }
82          }
83  
84          if (_log.isDebugEnabled()) {
85              _log.debug("Statistics verified for categories");
86          }
87  
88          List<MBThread> threads = MBThreadLocalServiceUtil.getMBThreads(
89              QueryUtil.ALL_POS, QueryUtil.ALL_POS);
90  
91          if (_log.isDebugEnabled()) {
92              _log.debug(
93                  "Processing " + threads.size() +
94                      " threads for statistics accuracy");
95          }
96  
97          for (MBThread thread : threads) {
98              int messageCount = MBMessageLocalServiceUtil.getThreadMessagesCount(
99                  thread.getThreadId());
100 
101             if (thread.getMessageCount() != messageCount) {
102                 thread.setMessageCount(messageCount);
103 
104                 MBThreadLocalServiceUtil.updateMBThread(thread);
105             }
106         }
107 
108         if (_log.isDebugEnabled()) {
109             _log.debug("Statistics verified for threads");
110         }
111 
112         List<MBMessage> messages =
113             MBMessageLocalServiceUtil.getNoAssetMessages();
114 
115         if (_log.isDebugEnabled()) {
116             _log.debug(
117                 "Processing " + messages.size() +
118                     " messages with no tags assets");
119         }
120 
121         for (MBMessage message : messages) {
122             try {
123                 MBMessageLocalServiceUtil.updateTagsAsset(
124                     message.getUserId(), message, new String[0]);
125             }
126             catch (Exception e) {
127                 if (_log.isWarnEnabled()) {
128                     _log.warn(
129                         "Unable to update tags asset for message " +
130                             message.getMessageId() + ": " + e.getMessage());
131                 }
132             }
133         }
134 
135         if (_log.isDebugEnabled()) {
136             _log.debug("Tags assets verified for messages");
137         }
138     }
139 
140     private static Log _log = LogFactoryUtil.getLog(VerifyMessageBoards.class);
141 
142 }