001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.verify;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.workflow.WorkflowConstants;
021    import com.liferay.portlet.messageboards.model.MBCategory;
022    import com.liferay.portlet.messageboards.model.MBMessage;
023    import com.liferay.portlet.messageboards.model.MBThread;
024    import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
025    import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
026    import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
027    
028    import java.util.List;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class VerifyMessageBoards extends VerifyProcess {
034    
035            protected void doVerify() throws Exception {
036                    List<MBCategory> categories =
037                            MBCategoryLocalServiceUtil.getMBCategories(
038                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS);
039    
040                    if (_log.isDebugEnabled()) {
041                            _log.debug(
042                                    "Processing " + categories.size() +
043                                            " categories for statistics accuracy");
044                    }
045    
046                    for (MBCategory category : categories) {
047                            int threadCount = MBThreadLocalServiceUtil.getCategoryThreadsCount(
048                                    category.getGroupId(), category.getCategoryId(),
049                                    WorkflowConstants.STATUS_APPROVED);
050                            int messageCount =
051                                    MBMessageLocalServiceUtil.getCategoryMessagesCount(
052                                            category.getGroupId(), category.getCategoryId(),
053                                            WorkflowConstants.STATUS_APPROVED);
054    
055                            if ((category.getThreadCount() != threadCount) ||
056                                    (category.getMessageCount() != messageCount)) {
057    
058                                    category.setThreadCount(threadCount);
059                                    category.setMessageCount(messageCount);
060    
061                                    MBCategoryLocalServiceUtil.updateMBCategory(category);
062                            }
063                    }
064    
065                    if (_log.isDebugEnabled()) {
066                            _log.debug("Statistics verified for categories");
067                    }
068    
069                    List<MBThread> threads = MBThreadLocalServiceUtil.getMBThreads(
070                            QueryUtil.ALL_POS, QueryUtil.ALL_POS);
071    
072                    if (_log.isDebugEnabled()) {
073                            _log.debug(
074                                    "Processing " + threads.size() +
075                                            " threads for statistics accuracy");
076                    }
077    
078                    for (MBThread thread : threads) {
079                            int messageCount = MBMessageLocalServiceUtil.getThreadMessagesCount(
080                                    thread.getThreadId(), WorkflowConstants.STATUS_APPROVED);
081    
082                            if (thread.getMessageCount() != messageCount) {
083                                    thread.setMessageCount(messageCount);
084    
085                                    MBThreadLocalServiceUtil.updateMBThread(thread);
086                            }
087                    }
088    
089                    if (_log.isDebugEnabled()) {
090                            _log.debug("Statistics verified for threads");
091                    }
092    
093                    List<MBMessage> messages =
094                            MBMessageLocalServiceUtil.getNoAssetMessages();
095    
096                    if (_log.isDebugEnabled()) {
097                            _log.debug(
098                                    "Processing " + messages.size() + " messages with no asset");
099                    }
100    
101                    for (MBMessage message : messages) {
102                            try {
103                                    MBMessageLocalServiceUtil.updateAsset(
104                                            message.getUserId(), message, null, null);
105                            }
106                            catch (Exception e) {
107                                    if (_log.isWarnEnabled()) {
108                                            _log.warn(
109                                                    "Unable to update asset for message " +
110                                                            message.getMessageId() + ": " + e.getMessage());
111                                    }
112                            }
113                    }
114    
115                    if (_log.isDebugEnabled()) {
116                            _log.debug("Assets verified for messages");
117                    }
118            }
119    
120            private static Log _log = LogFactoryUtil.getLog(VerifyMessageBoards.class);
121    
122    }