001    /**
002     * Copyright (c) 2000-2013 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                                    if (message.getStatus() == WorkflowConstants.STATUS_DRAFT) {
060                                            boolean visible = false;
061    
062                                            if (message.isApproved() &&
063                                                    ((message.getClassNameId() == 0) ||
064                                                     (message.getParentMessageId() != 0))) {
065    
066                                                    visible = true;
067                                            }
068    
069                                            AssetEntryLocalServiceUtil.updateEntry(
070                                                    message.getWorkflowClassName(), message.getMessageId(),
071                                                    null, visible);
072                                    }
073                            }
074                            catch (Exception e) {
075                                    if (_log.isWarnEnabled()) {
076                                            _log.warn(
077                                                    "Unable to update asset for message " +
078                                                            message.getMessageId() + ": " + e.getMessage());
079                                    }
080                            }
081                    }
082    
083                    if (_log.isDebugEnabled()) {
084                            _log.debug("Assets verified for messages");
085                    }
086            }
087    
088            protected void verifyAssetsForThreads() throws Exception {
089                    List<MBThread> threads = MBThreadLocalServiceUtil.getNoAssetThreads();
090    
091                    if (_log.isDebugEnabled()) {
092                            _log.debug(
093                                    "Processing " + threads.size() + " threads with no asset");
094                    }
095    
096                    for (MBThread thread : threads) {
097                            try {
098                                    AssetEntryLocalServiceUtil.updateEntry(
099                                            thread.getRootMessageUserId(), thread.getGroupId(),
100                                            thread.getStatusDate(), thread.getLastPostDate(),
101                                            MBThread.class.getName(), thread.getThreadId(), null, 0,
102                                            new long[0], new String[0], false, null, null, null, null,
103                                            String.valueOf(thread.getRootMessageId()), null, null, null,
104                                            null, 0, 0, null, false);
105                            }
106                            catch (Exception e) {
107                                    if (_log.isWarnEnabled()) {
108                                            _log.warn(
109                                                    "Unable to update asset for thread " +
110                                                            thread.getThreadId() + ": " + e.getMessage());
111                                    }
112                            }
113                    }
114    
115                    if (_log.isDebugEnabled()) {
116                            _log.debug("Assets verified for threads");
117                    }
118            }
119    
120            protected void verifyStatisticsForCategories() throws Exception {
121                    List<MBCategory> categories =
122                            MBCategoryLocalServiceUtil.getMBCategories(
123                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS);
124    
125                    if (_log.isDebugEnabled()) {
126                            _log.debug(
127                                    "Processing " + categories.size() +
128                                            " categories for statistics accuracy");
129                    }
130    
131                    for (MBCategory category : categories) {
132                            int threadCount = MBThreadLocalServiceUtil.getCategoryThreadsCount(
133                                    category.getGroupId(), category.getCategoryId(),
134                                    WorkflowConstants.STATUS_APPROVED);
135                            int messageCount =
136                                    MBMessageLocalServiceUtil.getCategoryMessagesCount(
137                                            category.getGroupId(), category.getCategoryId(),
138                                            WorkflowConstants.STATUS_APPROVED);
139    
140                            if ((category.getThreadCount() != threadCount) ||
141                                    (category.getMessageCount() != messageCount)) {
142    
143                                    category.setThreadCount(threadCount);
144                                    category.setMessageCount(messageCount);
145    
146                                    MBCategoryLocalServiceUtil.updateMBCategory(category);
147                            }
148                    }
149    
150                    if (_log.isDebugEnabled()) {
151                            _log.debug("Statistics verified for categories");
152                    }
153            }
154    
155            protected void verifyStatisticsForThreads() throws Exception {
156                    List<MBThread> threads = MBThreadLocalServiceUtil.getMBThreads(
157                            QueryUtil.ALL_POS, QueryUtil.ALL_POS);
158    
159                    if (_log.isDebugEnabled()) {
160                            _log.debug(
161                                    "Processing " + threads.size() +
162                                            " threads for statistics accuracy");
163                    }
164    
165                    for (MBThread thread : threads) {
166                            int messageCount = MBMessageLocalServiceUtil.getThreadMessagesCount(
167                                    thread.getThreadId(), WorkflowConstants.STATUS_APPROVED);
168    
169                            if (thread.getMessageCount() != messageCount) {
170                                    thread.setMessageCount(messageCount);
171    
172                                    MBThreadLocalServiceUtil.updateMBThread(thread);
173                            }
174                    }
175    
176                    if (_log.isDebugEnabled()) {
177                            _log.debug("Statistics verified for threads");
178                    }
179            }
180    
181            private static Log _log = LogFactoryUtil.getLog(VerifyMessageBoards.class);
182    
183    }