1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.messageboards.service.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.kernel.util.DateUtil;
30  import com.liferay.portal.model.User;
31  import com.liferay.portlet.messageboards.model.MBMessage;
32  import com.liferay.portlet.messageboards.model.MBMessageFlag;
33  import com.liferay.portlet.messageboards.model.MBThread;
34  import com.liferay.portlet.messageboards.model.impl.MBMessageFlagImpl;
35  import com.liferay.portlet.messageboards.service.base.MBMessageFlagLocalServiceBaseImpl;
36  
37  import java.util.Date;
38  import java.util.List;
39  
40  /**
41   * <a href="MBMessageFlagLocalServiceImpl.java.html"><b><i>View Source</i></b>
42   * </a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class MBMessageFlagLocalServiceImpl
48      extends MBMessageFlagLocalServiceBaseImpl {
49  
50      public void addReadFlags(long userId, MBThread thread)
51          throws PortalException, SystemException {
52  
53          User user = userPersistence.findByPrimaryKey(userId);
54  
55          if (user.isDefaultUser()) {
56              return;
57          }
58  
59          long messageId = thread.getRootMessageId();
60          int flag = MBMessageFlagImpl.READ_FLAG;
61  
62          MBMessageFlag messageFlag = mbMessageFlagPersistence.fetchByU_M_F(
63              userId, messageId, flag);
64  
65          if (messageFlag == null) {
66              long messageFlagId = counterLocalService.increment();
67  
68              messageFlag = mbMessageFlagPersistence.create(messageFlagId);
69  
70              messageFlag.setUserId(userId);
71              messageFlag.setModifiedDate(thread.getLastPostDate());
72              messageFlag.setThreadId(thread.getThreadId());
73              messageFlag.setMessageId(messageId);
74              messageFlag.setFlag(flag);
75  
76              mbMessageFlagPersistence.update(messageFlag, false);
77  
78              try {
79                  mbMessageFlagPersistence.update(messageFlag, false);
80              }
81              catch (SystemException se) {
82                  if (_log.isWarnEnabled()) {
83                      _log.warn(
84                          "Add failed, fetch {userId=" + userId +
85                              ", messageId=" + messageId + ",flag=" + flag +
86                                  "}");
87                  }
88  
89                  messageFlag = mbMessageFlagPersistence.fetchByU_M_F(
90                      userId, messageId, flag, false);
91  
92                  if (messageFlag == null) {
93                      throw se;
94                  }
95              }
96          }
97  
98          if (!DateUtil.equals(
99                  messageFlag.getModifiedDate(), thread.getLastPostDate(),
100                 true)) {
101 
102             messageFlag.setModifiedDate(thread.getLastPostDate());
103 
104             mbMessageFlagPersistence.update(messageFlag, false);
105         }
106     }
107 
108     public void addQuestionFlag(long messageId)
109         throws PortalException, SystemException {
110 
111         MBMessage message = mbMessagePersistence.findByPrimaryKey(messageId);
112 
113         if (!message.isRoot()) {
114             return;
115         }
116 
117         MBMessageFlag questionMessageFlag =
118             mbMessageFlagPersistence.fetchByU_M_F(
119                 message.getUserId(), message.getMessageId(),
120                 MBMessageFlagImpl.QUESTION_FLAG);
121 
122         MBMessageFlag answerMessageFlag =
123             mbMessageFlagPersistence.fetchByU_M_F(
124                 message.getUserId(), message.getMessageId(),
125                 MBMessageFlagImpl.ANSWER_FLAG);
126 
127         if ((questionMessageFlag == null) && (answerMessageFlag == null)) {
128             long messageFlagId = counterLocalService.increment();
129 
130             questionMessageFlag = mbMessageFlagPersistence.create(
131                 messageFlagId);
132 
133             questionMessageFlag.setUserId(message.getUserId());
134             questionMessageFlag.setModifiedDate(new Date());
135             questionMessageFlag.setThreadId(message.getThreadId());
136             questionMessageFlag.setMessageId(message.getMessageId());
137             questionMessageFlag.setFlag(MBMessageFlagImpl.QUESTION_FLAG);
138 
139             mbMessageFlagPersistence.update(questionMessageFlag, false);
140         }
141     }
142 
143     public void deleteFlags(long userId) throws SystemException {
144         mbMessageFlagPersistence.removeByUserId(userId);
145     }
146 
147     public void deleteFlags(long messageId, int flag) throws SystemException {
148         mbMessageFlagPersistence.removeByM_F(messageId, flag);
149     }
150 
151     public void deleteQuestionAndAnswerFlags(long threadId)
152         throws SystemException {
153 
154         List<MBMessage> messages = mbMessagePersistence.findByThreadId(
155             threadId);
156 
157         for (MBMessage message : messages) {
158             if (message.isRoot()) {
159                 mbMessageFlagPersistence.removeByM_F(
160                     message.getMessageId(), MBMessageFlagImpl.QUESTION_FLAG);
161             }
162 
163             mbMessageFlagPersistence.removeByM_F(
164                 message.getMessageId(), MBMessageFlagImpl.ANSWER_FLAG);
165         }
166     }
167 
168     public void deleteThreadFlags(long threadId) throws SystemException {
169         mbMessageFlagPersistence.removeByThreadId(threadId);
170     }
171 
172     public boolean hasAnswerFlag(long messageId) throws SystemException {
173         int count = mbMessageFlagPersistence.countByM_F(
174             messageId, MBMessageFlagImpl.ANSWER_FLAG);
175 
176         if (count > 0) {
177             return true;
178         }
179         else {
180             return false;
181         }
182     }
183 
184     public boolean hasQuestionFlag(long messageId) throws SystemException {
185         int count = mbMessageFlagPersistence.countByM_F(
186             messageId, MBMessageFlagImpl.QUESTION_FLAG);
187 
188         if (count > 0) {
189             return true;
190         }
191         else {
192             return false;
193         }
194     }
195 
196     public boolean hasReadFlag(long userId, MBThread thread)
197         throws PortalException, SystemException {
198 
199         User user = userPersistence.findByPrimaryKey(userId);
200 
201         if (user.isDefaultUser()) {
202             return true;
203         }
204 
205         MBMessageFlag messageFlag = mbMessageFlagPersistence.fetchByU_M_F(
206             userId, thread.getRootMessageId(), MBMessageFlagImpl.READ_FLAG);
207 
208         if ((messageFlag != null) &&
209             (DateUtil.equals(
210                 messageFlag.getModifiedDate(), thread.getLastPostDate(),
211                 true))) {
212 
213             return true;
214         }
215         else {
216             return false;
217         }
218     }
219 
220     private static Log _log =
221         LogFactoryUtil.getLog(MBMessageFlagLocalServiceImpl.class);
222 
223 }