001    /**
002     * Copyright (c) 2000-present 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.asset.kernel.service.AssetEntryLocalServiceUtil;
018    import com.liferay.message.boards.kernel.model.MBCategory;
019    import com.liferay.message.boards.kernel.model.MBMessage;
020    import com.liferay.message.boards.kernel.model.MBThread;
021    import com.liferay.message.boards.kernel.service.MBCategoryLocalServiceUtil;
022    import com.liferay.message.boards.kernel.service.MBMessageLocalServiceUtil;
023    import com.liferay.message.boards.kernel.service.MBThreadLocalServiceUtil;
024    import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
025    import com.liferay.portal.kernel.log.Log;
026    import com.liferay.portal.kernel.log.LogFactoryUtil;
027    import com.liferay.portal.kernel.util.LoggingTimer;
028    import com.liferay.portal.kernel.workflow.WorkflowConstants;
029    
030    import java.util.List;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     * @author Zsolt Berentey
035     */
036    public class VerifyMessageBoards extends VerifyProcess {
037    
038            @Override
039            protected void doVerify() throws Exception {
040                    verifyStatisticsForCategories();
041                    verifyStatisticsForThreads();
042                    verifyAssetsForMessages();
043                    verifyAssetsForThreads();
044            }
045    
046            protected void verifyAssetsForMessages() throws Exception {
047                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
048                            List<MBMessage> messages =
049                                    MBMessageLocalServiceUtil.getNoAssetMessages();
050    
051                            if (_log.isDebugEnabled()) {
052                                    _log.debug(
053                                            "Processing " + messages.size() +
054                                                    " messages with no asset");
055                            }
056    
057                            for (MBMessage message : messages) {
058                                    try {
059                                            MBMessageLocalServiceUtil.updateAsset(
060                                                    message.getUserId(), message, null, null, null);
061    
062                                            if (message.getStatus() == WorkflowConstants.STATUS_DRAFT) {
063                                                    boolean visible = false;
064    
065                                                    if (message.isApproved() &&
066                                                            ((message.getClassNameId() == 0) ||
067                                                             (message.getParentMessageId() != 0))) {
068    
069                                                            visible = true;
070                                                    }
071    
072                                                    AssetEntryLocalServiceUtil.updateEntry(
073                                                            message.getWorkflowClassName(),
074                                                            message.getMessageId(), null, null, true, visible);
075                                            }
076                                    }
077                                    catch (Exception e) {
078                                            if (_log.isWarnEnabled()) {
079                                                    _log.warn(
080                                                            "Unable to update asset for message " +
081                                                                    message.getMessageId() + ": " + e.getMessage());
082                                            }
083                                    }
084                            }
085    
086                            if (_log.isDebugEnabled()) {
087                                    _log.debug("Assets verified for messages");
088                            }
089                    }
090            }
091    
092            protected void verifyAssetsForThreads() throws Exception {
093                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
094                            List<MBThread> threads =
095                                    MBThreadLocalServiceUtil.getNoAssetThreads();
096    
097                            if (_log.isDebugEnabled()) {
098                                    _log.debug(
099                                            "Processing " + threads.size() + " threads with no asset");
100                            }
101    
102                            for (MBThread thread : threads) {
103                                    try {
104                                            AssetEntryLocalServiceUtil.updateEntry(
105                                                    thread.getRootMessageUserId(), thread.getGroupId(),
106                                                    thread.getStatusDate(), thread.getLastPostDate(),
107                                                    MBThread.class.getName(), thread.getThreadId(), null, 0,
108                                                    new long[0], new String[0], true, false, null, null,
109                                                    null, null, null,
110                                                    String.valueOf(thread.getRootMessageId()), null, null,
111                                                    null, null, 0, 0, null);
112                                    }
113                                    catch (Exception e) {
114                                            if (_log.isWarnEnabled()) {
115                                                    _log.warn(
116                                                            "Unable to update asset for thread " +
117                                                                    thread.getThreadId() + ": " + e.getMessage());
118                                            }
119                                    }
120                            }
121    
122                            if (_log.isDebugEnabled()) {
123                                    _log.debug("Assets verified for threads");
124                            }
125                    }
126            }
127    
128            protected void verifyStatisticsForCategories() throws Exception {
129                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
130                            if (_log.isDebugEnabled()) {
131                                    _log.debug("Processing categories for statistics accuracy");
132                            }
133    
134                            ActionableDynamicQuery actionableDynamicQuery =
135                                    MBCategoryLocalServiceUtil.getActionableDynamicQuery();
136    
137                            actionableDynamicQuery.setParallel(true);
138                            actionableDynamicQuery.setPerformActionMethod(
139                                    new ActionableDynamicQuery.PerformActionMethod<MBCategory>() {
140    
141                                            @Override
142                                            public void performAction(MBCategory category) {
143                                                    int threadCount =
144                                                            MBThreadLocalServiceUtil.getCategoryThreadsCount(
145                                                                    category.getGroupId(), category.getCategoryId(),
146                                                                    WorkflowConstants.STATUS_APPROVED);
147                                                    int messageCount =
148                                                            MBMessageLocalServiceUtil.getCategoryMessagesCount(
149                                                                    category.getGroupId(), category.getCategoryId(),
150                                                                    WorkflowConstants.STATUS_APPROVED);
151    
152                                                    if ((category.getThreadCount() != threadCount) ||
153                                                            (category.getMessageCount() != messageCount)) {
154    
155                                                            category.setThreadCount(threadCount);
156                                                            category.setMessageCount(messageCount);
157    
158                                                            MBCategoryLocalServiceUtil.updateMBCategory(
159                                                                    category);
160                                                    }
161                                            }
162    
163                                    });
164    
165                            actionableDynamicQuery.performActions();
166    
167                            if (_log.isDebugEnabled()) {
168                                    _log.debug("Statistics verified for categories");
169                            }
170                    }
171            }
172    
173            protected void verifyStatisticsForThreads() throws Exception {
174                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
175                            if (_log.isDebugEnabled()) {
176                                    _log.debug("Processing threads for statistics accuracy");
177                            }
178    
179                            ActionableDynamicQuery actionableDynamicQuery =
180                                    MBThreadLocalServiceUtil.getActionableDynamicQuery();
181    
182                            actionableDynamicQuery.setParallel(true);
183                            actionableDynamicQuery.setPerformActionMethod(
184                                    new ActionableDynamicQuery.PerformActionMethod<MBThread>() {
185    
186                                            @Override
187                                            public void performAction(MBThread thread) {
188                                                    int messageCount =
189                                                            MBMessageLocalServiceUtil.getThreadMessagesCount(
190                                                                    thread.getThreadId(),
191                                                                    WorkflowConstants.STATUS_APPROVED);
192    
193                                                    if (thread.getMessageCount() != messageCount) {
194                                                            thread.setMessageCount(messageCount);
195    
196                                                            MBThreadLocalServiceUtil.updateMBThread(thread);
197                                                    }
198                                            }
199    
200                                    });
201    
202                            actionableDynamicQuery.performActions();
203    
204                            if (_log.isDebugEnabled()) {
205                                    _log.debug("Statistics verified for threads");
206                            }
207                    }
208            }
209    
210            private static final Log _log = LogFactoryUtil.getLog(
211                    VerifyMessageBoards.class);
212    
213    }