001    /**
002     * Copyright (c) 2000-2012 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.asset.service.AssetEntryLocalServiceUtil;
022    import com.liferay.portlet.messageboards.model.MBCategory;
023    import com.liferay.portlet.messageboards.model.MBMessage;
024    import com.liferay.portlet.messageboards.model.MBThread;
025    import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
026    import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
027    import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
028    
029    import java.util.List;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     * @author Zsolt Berentey
034     */
035    public class VerifyMessageBoards extends VerifyProcess {
036    
037            @Override
038            protected void doVerify() throws Exception {
039                    verifyStatisticsForCategories();
040                    verifyStatisticsForThreads();
041                    verifyAssetsForMessages();
042                    verifyAssetsForThreads();
043            }
044    
045            protected void verifyAssetsForMessages() throws Exception {
046                    List<MBMessage> messages =
047                            MBMessageLocalServiceUtil.getNoAssetMessages();
048    
049                    if (_log.isDebugEnabled()) {
050                            _log.debug(
051                                    "Processing " + messages.size() + " messages with no asset");
052                    }
053    
054                    for (MBMessage message : messages) {
055                            try {
056                                    MBMessageLocalServiceUtil.updateAsset(
057                                            message.getUserId(), message, null, null, null);
058                            }
059                            catch (Exception e) {
060                                    if (_log.isWarnEnabled()) {
061                                            _log.warn(
062                                                    "Unable to update asset for message " +
063                                                            message.getMessageId() + ": " + e.getMessage());
064                                    }
065                            }
066                    }
067    
068                    if (_log.isDebugEnabled()) {
069                            _log.debug("Assets verified for messages");
070                    }
071            }
072    
073            protected void verifyAssetsForThreads() throws Exception {
074                    List<MBThread> threads = MBThreadLocalServiceUtil.getNoAssetThreads();
075    
076                    if (_log.isDebugEnabled()) {
077                            _log.debug(
078                                    "Processing " + threads.size() + " threads with no asset");
079                    }
080    
081                    for (MBThread thread : threads) {
082                            try {
083                                    AssetEntryLocalServiceUtil.updateEntry(
084                                            thread.getRootMessageUserId(), thread.getGroupId(),
085                                            MBThread.class.getName(), thread.getThreadId(), null, 0,
086                                            new long[0], new String[0], false, null, null, null, null,
087                                            null, String.valueOf(thread.getRootMessageId()), null, null,
088                                            null, null, 0, 0, null, false);
089                            }
090                            catch (Exception e) {
091                                    if (_log.isWarnEnabled()) {
092                                            _log.warn(
093                                                    "Unable to update asset for thread " +
094                                                            thread.getThreadId() + ": " + e.getMessage());
095                                    }
096                            }
097                    }
098    
099                    if (_log.isDebugEnabled()) {
100                            _log.debug("Assets verified for threads");
101                    }
102            }
103    
104            protected void verifyStatisticsForCategories() throws Exception {
105                    List<MBCategory> categories =
106                            MBCategoryLocalServiceUtil.getMBCategories(
107                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS);
108    
109                    if (_log.isDebugEnabled()) {
110                            _log.debug(
111                                    "Processing " + categories.size() +
112                                            " categories for statistics accuracy");
113                    }
114    
115                    for (MBCategory category : categories) {
116                            int threadCount = MBThreadLocalServiceUtil.getCategoryThreadsCount(
117                                    category.getGroupId(), category.getCategoryId(),
118                                    WorkflowConstants.STATUS_APPROVED);
119                            int messageCount =
120                                    MBMessageLocalServiceUtil.getCategoryMessagesCount(
121                                            category.getGroupId(), category.getCategoryId(),
122                                            WorkflowConstants.STATUS_APPROVED);
123    
124                            if ((category.getThreadCount() != threadCount) ||
125                                    (category.getMessageCount() != messageCount)) {
126    
127                                    category.setThreadCount(threadCount);
128                                    category.setMessageCount(messageCount);
129    
130                                    MBCategoryLocalServiceUtil.updateMBCategory(category);
131                            }
132                    }
133    
134                    if (_log.isDebugEnabled()) {
135                            _log.debug("Statistics verified for categories");
136                    }
137            }
138    
139            protected void verifyStatisticsForThreads() throws Exception {
140                    List<MBThread> threads = MBThreadLocalServiceUtil.getMBThreads(
141                            QueryUtil.ALL_POS, QueryUtil.ALL_POS);
142    
143                    if (_log.isDebugEnabled()) {
144                            _log.debug(
145                                    "Processing " + threads.size() +
146                                            " threads for statistics accuracy");
147                    }
148    
149                    for (MBThread thread : threads) {
150                            int messageCount = MBMessageLocalServiceUtil.getThreadMessagesCount(
151                                    thread.getThreadId(), WorkflowConstants.STATUS_APPROVED);
152    
153                            if (thread.getMessageCount() != messageCount) {
154                                    thread.setMessageCount(messageCount);
155    
156                                    MBThreadLocalServiceUtil.updateMBThread(thread);
157                            }
158                    }
159    
160                    if (_log.isDebugEnabled()) {
161                            _log.debug("Statistics verified for threads");
162                    }
163            }
164    
165            private static Log _log = LogFactoryUtil.getLog(VerifyMessageBoards.class);
166    
167    }