1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.messageboards.service.impl;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.kernel.util.DateUtil;
22  import com.liferay.portal.model.User;
23  import com.liferay.portlet.messageboards.model.MBMessage;
24  import com.liferay.portlet.messageboards.model.MBMessageFlag;
25  import com.liferay.portlet.messageboards.model.MBMessageFlagConstants;
26  import com.liferay.portlet.messageboards.model.MBThread;
27  import com.liferay.portlet.messageboards.service.base.MBMessageFlagLocalServiceBaseImpl;
28  
29  import java.util.Date;
30  import java.util.List;
31  
32  /**
33   * <a href="MBMessageFlagLocalServiceImpl.java.html"><b><i>View Source</i></b>
34   * </a>
35   *
36   * @author Brian Wing Shun Chan
37   */
38  public class MBMessageFlagLocalServiceImpl
39      extends MBMessageFlagLocalServiceBaseImpl {
40  
41      public void addReadFlags(long userId, MBThread thread)
42          throws PortalException, SystemException {
43  
44          User user = userPersistence.findByPrimaryKey(userId);
45  
46          if (user.isDefaultUser()) {
47              return;
48          }
49  
50          long messageId = thread.getRootMessageId();
51          int flag = MBMessageFlagConstants.READ_FLAG;
52  
53          MBMessageFlag messageFlag = mbMessageFlagPersistence.fetchByU_M_F(
54              userId, messageId, flag);
55  
56          if (messageFlag == null) {
57              long messageFlagId = counterLocalService.increment();
58  
59              messageFlag = mbMessageFlagPersistence.create(messageFlagId);
60  
61              messageFlag.setUserId(userId);
62              messageFlag.setModifiedDate(thread.getLastPostDate());
63              messageFlag.setThreadId(thread.getThreadId());
64              messageFlag.setMessageId(messageId);
65              messageFlag.setFlag(flag);
66  
67              mbMessageFlagPersistence.update(messageFlag, false);
68  
69              try {
70                  mbMessageFlagPersistence.update(messageFlag, false);
71              }
72              catch (SystemException se) {
73                  if (_log.isWarnEnabled()) {
74                      _log.warn(
75                          "Add failed, fetch {userId=" + userId +
76                              ", messageId=" + messageId + ",flag=" + flag +
77                                  "}");
78                  }
79  
80                  messageFlag = mbMessageFlagPersistence.fetchByU_M_F(
81                      userId, messageId, flag, false);
82  
83                  if (messageFlag == null) {
84                      throw se;
85                  }
86              }
87          }
88  
89          if (!DateUtil.equals(
90                  messageFlag.getModifiedDate(), thread.getLastPostDate(),
91                  true)) {
92  
93              messageFlag.setModifiedDate(thread.getLastPostDate());
94  
95              mbMessageFlagPersistence.update(messageFlag, false);
96          }
97      }
98  
99      public void addQuestionFlag(long messageId)
100         throws PortalException, SystemException {
101 
102         MBMessage message = mbMessagePersistence.findByPrimaryKey(messageId);
103 
104         if (!message.isRoot()) {
105             return;
106         }
107 
108         MBMessageFlag questionMessageFlag =
109             mbMessageFlagPersistence.fetchByU_M_F(
110                 message.getUserId(), message.getMessageId(),
111                 MBMessageFlagConstants.QUESTION_FLAG);
112 
113         MBMessageFlag answerMessageFlag =
114             mbMessageFlagPersistence.fetchByU_M_F(
115                 message.getUserId(), message.getMessageId(),
116                 MBMessageFlagConstants.ANSWER_FLAG);
117 
118         if ((questionMessageFlag == null) && (answerMessageFlag == null)) {
119             long messageFlagId = counterLocalService.increment();
120 
121             questionMessageFlag = mbMessageFlagPersistence.create(
122                 messageFlagId);
123 
124             questionMessageFlag.setUserId(message.getUserId());
125             questionMessageFlag.setModifiedDate(new Date());
126             questionMessageFlag.setThreadId(message.getThreadId());
127             questionMessageFlag.setMessageId(message.getMessageId());
128             questionMessageFlag.setFlag(MBMessageFlagConstants.QUESTION_FLAG);
129 
130             mbMessageFlagPersistence.update(questionMessageFlag, false);
131         }
132     }
133 
134     public void deleteAnswerFlags(long threadId, long messageId)
135         throws SystemException {
136 
137         mbMessageFlagPersistence.removeByM_F(
138             messageId, MBMessageFlagConstants.ANSWER_FLAG);
139 
140         List<MBMessage> messages = mbMessagePersistence.findByT_P(
141             threadId, messageId);
142 
143         for (MBMessage message : messages) {
144             deleteAnswerFlags(threadId, message.getMessageId());
145         }
146     }
147 
148     public void deleteFlags(long userId) throws SystemException {
149         mbMessageFlagPersistence.removeByUserId(userId);
150     }
151 
152     public void deleteFlags(long messageId, int flag) throws SystemException {
153         mbMessageFlagPersistence.removeByM_F(messageId, flag);
154     }
155 
156     public void deleteQuestionAndAnswerFlags(long threadId)
157         throws SystemException {
158 
159         List<MBMessage> messages = mbMessagePersistence.findByThreadId(
160             threadId);
161 
162         for (MBMessage message : messages) {
163             if (message.isRoot()) {
164                 mbMessageFlagPersistence.removeByM_F(
165                     message.getMessageId(),
166                     MBMessageFlagConstants.QUESTION_FLAG);
167             }
168 
169             mbMessageFlagPersistence.removeByM_F(
170                 message.getMessageId(), MBMessageFlagConstants.ANSWER_FLAG);
171         }
172     }
173 
174     public void deleteThreadFlags(long threadId) throws SystemException {
175         mbMessageFlagPersistence.removeByThreadId(threadId);
176     }
177 
178     public MBMessageFlag getReadFlag(long userId, MBThread thread)
179         throws PortalException, SystemException {
180 
181         User user = userPersistence.findByPrimaryKey(userId);
182 
183         if (user.isDefaultUser()) {
184             return null;
185         }
186 
187         return mbMessageFlagPersistence.fetchByU_M_F(
188             userId, thread.getRootMessageId(),
189             MBMessageFlagConstants.READ_FLAG);
190     }
191 
192     public boolean hasAnswerFlag(long messageId) throws SystemException {
193         int count = mbMessageFlagPersistence.countByM_F(
194             messageId, MBMessageFlagConstants.ANSWER_FLAG);
195 
196         if (count > 0) {
197             return true;
198         }
199         else {
200             return false;
201         }
202     }
203 
204     public boolean hasQuestionFlag(long messageId) throws SystemException {
205         int count = mbMessageFlagPersistence.countByM_F(
206             messageId, MBMessageFlagConstants.QUESTION_FLAG);
207 
208         if (count > 0) {
209             return true;
210         }
211         else {
212             return false;
213         }
214     }
215 
216     public boolean hasReadFlag(long userId, MBThread thread)
217         throws PortalException, SystemException {
218 
219         User user = userPersistence.findByPrimaryKey(userId);
220 
221         if (user.isDefaultUser()) {
222             return true;
223         }
224 
225         MBMessageFlag messageFlag = mbMessageFlagPersistence.fetchByU_M_F(
226             userId, thread.getRootMessageId(),
227             MBMessageFlagConstants.READ_FLAG);
228 
229         if ((messageFlag != null) &&
230             (DateUtil.equals(
231                 messageFlag.getModifiedDate(), thread.getLastPostDate(),
232                 true))) {
233 
234             return true;
235         }
236         else {
237             return false;
238         }
239     }
240 
241     private static Log _log = LogFactoryUtil.getLog(
242         MBMessageFlagLocalServiceImpl.class);
243 
244 }