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.portlet.messageboards.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.DateUtil;
022    import com.liferay.portal.model.User;
023    import com.liferay.portlet.messageboards.model.MBMessage;
024    import com.liferay.portlet.messageboards.model.MBMessageFlag;
025    import com.liferay.portlet.messageboards.model.MBMessageFlagConstants;
026    import com.liferay.portlet.messageboards.model.MBThread;
027    import com.liferay.portlet.messageboards.service.base.MBMessageFlagLocalServiceBaseImpl;
028    
029    import java.util.Date;
030    import java.util.List;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class MBMessageFlagLocalServiceImpl
036            extends MBMessageFlagLocalServiceBaseImpl {
037    
038            public void addReadFlags(long userId, MBThread thread)
039                    throws PortalException, SystemException {
040    
041                    User user = userPersistence.findByPrimaryKey(userId);
042    
043                    if (user.isDefaultUser()) {
044                            return;
045                    }
046    
047                    long messageId = thread.getRootMessageId();
048                    int flag = MBMessageFlagConstants.READ_FLAG;
049    
050                    MBMessageFlag messageFlag = mbMessageFlagPersistence.fetchByU_M_F(
051                            userId, messageId, flag);
052    
053                    if (messageFlag == null) {
054                            long messageFlagId = counterLocalService.increment();
055    
056                            messageFlag = mbMessageFlagPersistence.create(messageFlagId);
057    
058                            messageFlag.setUserId(userId);
059                            messageFlag.setModifiedDate(thread.getLastPostDate());
060                            messageFlag.setThreadId(thread.getThreadId());
061                            messageFlag.setMessageId(messageId);
062                            messageFlag.setFlag(flag);
063    
064                            mbMessageFlagPersistence.update(messageFlag, false);
065    
066                            try {
067                                    mbMessageFlagPersistence.update(messageFlag, false);
068                            }
069                            catch (SystemException se) {
070                                    if (_log.isWarnEnabled()) {
071                                            _log.warn(
072                                                    "Add failed, fetch {userId=" + userId +
073                                                            ", messageId=" + messageId + ",flag=" + flag +
074                                                                    "}");
075                                    }
076    
077                                    messageFlag = mbMessageFlagPersistence.fetchByU_M_F(
078                                            userId, messageId, flag, false);
079    
080                                    if (messageFlag == null) {
081                                            throw se;
082                                    }
083                            }
084                    }
085    
086                    if (!DateUtil.equals(
087                                    messageFlag.getModifiedDate(), thread.getLastPostDate(),
088                                    true)) {
089    
090                            messageFlag.setModifiedDate(thread.getLastPostDate());
091    
092                            mbMessageFlagPersistence.update(messageFlag, false);
093                    }
094            }
095    
096            public void addQuestionFlag(long messageId)
097                    throws PortalException, SystemException {
098    
099                    MBMessage message = mbMessagePersistence.findByPrimaryKey(messageId);
100    
101                    if (!message.isRoot()) {
102                            return;
103                    }
104    
105                    MBMessageFlag questionMessageFlag =
106                            mbMessageFlagPersistence.fetchByU_M_F(
107                                    message.getUserId(), message.getMessageId(),
108                                    MBMessageFlagConstants.QUESTION_FLAG);
109    
110                    MBMessageFlag answerMessageFlag =
111                            mbMessageFlagPersistence.fetchByU_M_F(
112                                    message.getUserId(), message.getMessageId(),
113                                    MBMessageFlagConstants.ANSWER_FLAG);
114    
115                    if ((questionMessageFlag == null) && (answerMessageFlag == null)) {
116                            long messageFlagId = counterLocalService.increment();
117    
118                            questionMessageFlag = mbMessageFlagPersistence.create(
119                                    messageFlagId);
120    
121                            questionMessageFlag.setUserId(message.getUserId());
122                            questionMessageFlag.setModifiedDate(new Date());
123                            questionMessageFlag.setThreadId(message.getThreadId());
124                            questionMessageFlag.setMessageId(message.getMessageId());
125                            questionMessageFlag.setFlag(MBMessageFlagConstants.QUESTION_FLAG);
126    
127                            mbMessageFlagPersistence.update(questionMessageFlag, false);
128                    }
129            }
130    
131            public void deleteAnswerFlags(long threadId, long messageId)
132                    throws SystemException {
133    
134                    mbMessageFlagPersistence.removeByM_F(
135                            messageId, MBMessageFlagConstants.ANSWER_FLAG);
136    
137                    List<MBMessage> messages = mbMessagePersistence.findByT_P(
138                            threadId, messageId);
139    
140                    for (MBMessage message : messages) {
141                            deleteAnswerFlags(threadId, message.getMessageId());
142                    }
143            }
144    
145            public void deleteFlags(long userId) throws SystemException {
146                    mbMessageFlagPersistence.removeByUserId(userId);
147            }
148    
149            public void deleteFlags(long messageId, int flag) throws SystemException {
150                    mbMessageFlagPersistence.removeByM_F(messageId, flag);
151            }
152    
153            public void deleteQuestionAndAnswerFlags(long threadId)
154                    throws SystemException {
155    
156                    List<MBMessage> messages = mbMessagePersistence.findByThreadId(
157                            threadId);
158    
159                    for (MBMessage message : messages) {
160                            if (message.isRoot()) {
161                                    mbMessageFlagPersistence.removeByM_F(
162                                            message.getMessageId(),
163                                            MBMessageFlagConstants.QUESTION_FLAG);
164                            }
165    
166                            mbMessageFlagPersistence.removeByM_F(
167                                    message.getMessageId(), MBMessageFlagConstants.ANSWER_FLAG);
168                    }
169            }
170    
171            public void deleteThreadFlags(long threadId) throws SystemException {
172                    mbMessageFlagPersistence.removeByThreadId(threadId);
173            }
174    
175            public MBMessageFlag getReadFlag(long userId, MBThread thread)
176                    throws PortalException, SystemException {
177    
178                    User user = userPersistence.findByPrimaryKey(userId);
179    
180                    if (user.isDefaultUser()) {
181                            return null;
182                    }
183    
184                    return mbMessageFlagPersistence.fetchByU_M_F(
185                            userId, thread.getRootMessageId(),
186                            MBMessageFlagConstants.READ_FLAG);
187            }
188    
189            public boolean hasAnswerFlag(long messageId) throws SystemException {
190                    int count = mbMessageFlagPersistence.countByM_F(
191                            messageId, MBMessageFlagConstants.ANSWER_FLAG);
192    
193                    if (count > 0) {
194                            return true;
195                    }
196                    else {
197                            return false;
198                    }
199            }
200    
201            public boolean hasQuestionFlag(long messageId) throws SystemException {
202                    int count = mbMessageFlagPersistence.countByM_F(
203                            messageId, MBMessageFlagConstants.QUESTION_FLAG);
204    
205                    if (count > 0) {
206                            return true;
207                    }
208                    else {
209                            return false;
210                    }
211            }
212    
213            public boolean hasReadFlag(long userId, MBThread thread)
214                    throws PortalException, SystemException {
215    
216                    User user = userPersistence.findByPrimaryKey(userId);
217    
218                    if (user.isDefaultUser()) {
219                            return true;
220                    }
221    
222                    MBMessageFlag messageFlag = mbMessageFlagPersistence.fetchByU_M_F(
223                            userId, thread.getRootMessageId(),
224                            MBMessageFlagConstants.READ_FLAG);
225    
226                    if ((messageFlag != null) &&
227                            (DateUtil.equals(
228                                    messageFlag.getModifiedDate(), thread.getLastPostDate(),
229                                    true))) {
230    
231                            return true;
232                    }
233                    else {
234                            return false;
235                    }
236            }
237    
238            private static Log _log = LogFactoryUtil.getLog(
239                    MBMessageFlagLocalServiceImpl.class);
240    
241    }