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 deleteFlags(long userId) throws SystemException {
135         mbMessageFlagPersistence.removeByUserId(userId);
136     }
137 
138     public void deleteFlags(long messageId, int flag) throws SystemException {
139         mbMessageFlagPersistence.removeByM_F(messageId, flag);
140     }
141 
142     public void deleteQuestionAndAnswerFlags(long threadId)
143         throws SystemException {
144 
145         List<MBMessage> messages = mbMessagePersistence.findByThreadId(
146             threadId);
147 
148         for (MBMessage message : messages) {
149             if (message.isRoot()) {
150                 mbMessageFlagPersistence.removeByM_F(
151                     message.getMessageId(),
152                     MBMessageFlagConstants.QUESTION_FLAG);
153             }
154 
155             mbMessageFlagPersistence.removeByM_F(
156                 message.getMessageId(), MBMessageFlagConstants.ANSWER_FLAG);
157         }
158     }
159 
160     public void deleteThreadFlags(long threadId) throws SystemException {
161         mbMessageFlagPersistence.removeByThreadId(threadId);
162     }
163 
164     public MBMessageFlag getReadFlag(long userId, MBThread thread)
165         throws PortalException, SystemException {
166 
167         User user = userPersistence.findByPrimaryKey(userId);
168 
169         if (user.isDefaultUser()) {
170             return null;
171         }
172 
173         return mbMessageFlagPersistence.fetchByU_M_F(
174             userId, thread.getRootMessageId(),
175             MBMessageFlagConstants.READ_FLAG);
176     }
177 
178     public boolean hasAnswerFlag(long messageId) throws SystemException {
179         int count = mbMessageFlagPersistence.countByM_F(
180             messageId, MBMessageFlagConstants.ANSWER_FLAG);
181 
182         if (count > 0) {
183             return true;
184         }
185         else {
186             return false;
187         }
188     }
189 
190     public boolean hasQuestionFlag(long messageId) throws SystemException {
191         int count = mbMessageFlagPersistence.countByM_F(
192             messageId, MBMessageFlagConstants.QUESTION_FLAG);
193 
194         if (count > 0) {
195             return true;
196         }
197         else {
198             return false;
199         }
200     }
201 
202     public boolean hasReadFlag(long userId, MBThread thread)
203         throws PortalException, SystemException {
204 
205         User user = userPersistence.findByPrimaryKey(userId);
206 
207         if (user.isDefaultUser()) {
208             return true;
209         }
210 
211         MBMessageFlag messageFlag = mbMessageFlagPersistence.fetchByU_M_F(
212             userId, thread.getRootMessageId(),
213             MBMessageFlagConstants.READ_FLAG);
214 
215         if ((messageFlag != null) &&
216             (DateUtil.equals(
217                 messageFlag.getModifiedDate(), thread.getLastPostDate(),
218                 true))) {
219 
220             return true;
221         }
222         else {
223             return false;
224         }
225     }
226 
227     private static Log _log = LogFactoryUtil.getLog(
228         MBMessageFlagLocalServiceImpl.class);
229 
230 }