001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 =
075                            MBThreadLocalServiceUtil.getNoAssetThreads();
076    
077                    if (_log.isDebugEnabled()) {
078                            _log.debug(
079                                    "Processing " + threads.size() + " threads with no asset");
080                    }
081    
082                    for (MBThread thread : threads) {
083                            try {
084                                    AssetEntryLocalServiceUtil.updateEntry(
085                                            thread.getRootMessageUserId(), thread.getGroupId(),
086                                            MBThread.class.getName(), thread.getThreadId(), null, 0,
087                                            new long[0], new String[0], false, null, null, null, null,
088                                            null, String.valueOf(thread.getRootMessageId()), null, null,
089                                            null, null, 0, 0, null, false);
090                            }
091                            catch (Exception e) {
092                                    if (_log.isWarnEnabled()) {
093                                            _log.warn(
094                                                    "Unable to update asset for thread " +
095                                                            thread.getThreadId() + ": " + e.getMessage());
096                                    }
097                            }
098                    }
099    
100                    if (_log.isDebugEnabled()) {
101                            _log.debug("Assets verified for threads");
102                    }
103            }
104    
105            protected void verifyStatisticsForCategories() throws Exception {
106                    List<MBCategory> categories =
107                            MBCategoryLocalServiceUtil.getMBCategories(
108                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS);
109    
110                    if (_log.isDebugEnabled()) {
111                            _log.debug(
112                                    "Processing " + categories.size() +
113                                            " categories for statistics accuracy");
114                    }
115    
116                    for (MBCategory category : categories) {
117                            int threadCount = MBThreadLocalServiceUtil.getCategoryThreadsCount(
118                                    category.getGroupId(), category.getCategoryId(),
119                                    WorkflowConstants.STATUS_APPROVED);
120                            int messageCount =
121                                    MBMessageLocalServiceUtil.getCategoryMessagesCount(
122                                            category.getGroupId(), category.getCategoryId(),
123                                            WorkflowConstants.STATUS_APPROVED);
124    
125                            if ((category.getThreadCount() != threadCount) ||
126                                    (category.getMessageCount() != messageCount)) {
127    
128                                    category.setThreadCount(threadCount);
129                                    category.setMessageCount(messageCount);
130    
131                                    MBCategoryLocalServiceUtil.updateMBCategory(category);
132                            }
133                    }
134    
135                    if (_log.isDebugEnabled()) {
136                            _log.debug("Statistics verified for categories");
137                    }
138            }
139    
140            protected void verifyStatisticsForThreads() throws Exception {
141                    List<MBThread> threads = MBThreadLocalServiceUtil.getMBThreads(
142                            QueryUtil.ALL_POS, QueryUtil.ALL_POS);
143    
144                    if (_log.isDebugEnabled()) {
145                            _log.debug(
146                                    "Processing " + threads.size() +
147                                            " threads for statistics accuracy");
148                    }
149    
150                    for (MBThread thread : threads) {
151                            int messageCount = MBMessageLocalServiceUtil.getThreadMessagesCount(
152                                    thread.getThreadId(), WorkflowConstants.STATUS_APPROVED);
153    
154                            if (thread.getMessageCount() != messageCount) {
155                                    thread.setMessageCount(messageCount);
156    
157                                    MBThreadLocalServiceUtil.updateMBThread(thread);
158                            }
159                    }
160    
161                    if (_log.isDebugEnabled()) {
162                            _log.debug("Statistics verified for threads");
163                    }
164            }
165    
166            private static Log _log = LogFactoryUtil.getLog(VerifyMessageBoards.class);
167    
168    }