1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class VerifyMessageBoards extends VerifyProcess {
43  
44      protected void doVerify() throws Exception {
45          List<MBCategory> categories =
46              MBCategoryLocalServiceUtil.getMBCategories(
47                  QueryUtil.ALL_POS, QueryUtil.ALL_POS);
48  
49          if (_log.isDebugEnabled()) {
50              _log.debug(
51                  "Processing " + categories.size() +
52                      " categories for statistics accuracy");
53          }
54  
55          for (MBCategory category : categories) {
56              int threadCount = MBThreadLocalServiceUtil.getCategoryThreadsCount(
57                  category.getCategoryId());
58              int messageCount =
59                  MBMessageLocalServiceUtil.getCategoryMessagesCount(
60                      category.getCategoryId());
61  
62              if ((category.getThreadCount() != threadCount) ||
63                  (category.getMessageCount() != messageCount)) {
64  
65                  category.setThreadCount(threadCount);
66                  category.setMessageCount(messageCount);
67  
68                  MBCategoryLocalServiceUtil.updateMBCategory(category);
69              }
70          }
71  
72          if (_log.isDebugEnabled()) {
73              _log.debug("Statistics verified for categories");
74          }
75  
76          List<MBThread> threads = MBThreadLocalServiceUtil.getMBThreads(
77              QueryUtil.ALL_POS, QueryUtil.ALL_POS);
78  
79          if (_log.isDebugEnabled()) {
80              _log.debug(
81                  "Processing " + threads.size() +
82                      " threads for statistics accuracy");
83          }
84  
85          for (MBThread thread : threads) {
86              int messageCount = MBMessageLocalServiceUtil.getThreadMessagesCount(
87                  thread.getThreadId());
88  
89              if (thread.getMessageCount() != messageCount) {
90                  thread.setMessageCount(messageCount);
91  
92                  MBThreadLocalServiceUtil.updateMBThread(thread);
93              }
94          }
95  
96          if (_log.isDebugEnabled()) {
97              _log.debug("Statistics verified for threads");
98          }
99  
100         List<MBMessage> messages =
101             MBMessageLocalServiceUtil.getNoAssetMessages();
102 
103         if (_log.isDebugEnabled()) {
104             _log.debug(
105                 "Processing " + messages.size() +
106                     " messages with no tags assets");
107         }
108 
109         for (MBMessage message : messages) {
110             try {
111                 MBMessageLocalServiceUtil.updateTagsAsset(
112                     message.getUserId(), message, new String[0]);
113             }
114             catch (Exception e) {
115                 if (_log.isWarnEnabled()) {
116                     _log.warn(
117                         "Unable to update tags asset for message " +
118                             message.getMessageId() + ": " + e.getMessage());
119                 }
120             }
121         }
122 
123         if (_log.isDebugEnabled()) {
124             _log.debug("Tags assets verified for messages");
125         }
126     }
127 
128     private static Log _log = LogFactoryUtil.getLog(VerifyMessageBoards.class);
129 
130 }