1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.messageboards.service.impl;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.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.MBThread;
26  import com.liferay.portlet.messageboards.model.impl.MBMessageFlagImpl;
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 addQuestionFlag(long messageId)
42          throws PortalException, SystemException {
43  
44          MBMessage message = mbMessagePersistence.findByPrimaryKey(messageId);
45  
46          if (!message.isRoot()) {
47              return;
48          }
49  
50          MBMessageFlag questionMessageFlag =
51              mbMessageFlagPersistence.fetchByU_M_F(
52                  message.getUserId(), message.getMessageId(),
53                  MBMessageFlagImpl.QUESTION_FLAG);
54  
55          MBMessageFlag answerMessageFlag =
56              mbMessageFlagPersistence.fetchByU_M_F(
57                  message.getUserId(), message.getMessageId(),
58                  MBMessageFlagImpl.ANSWER_FLAG);
59  
60          if ((questionMessageFlag == null) && (answerMessageFlag == null)) {
61              long messageFlagId = counterLocalService.increment();
62  
63              questionMessageFlag = mbMessageFlagPersistence.create(
64                  messageFlagId);
65  
66              questionMessageFlag.setUserId(message.getUserId());
67              questionMessageFlag.setModifiedDate(new Date());
68              questionMessageFlag.setThreadId(message.getThreadId());
69              questionMessageFlag.setMessageId(message.getMessageId());
70              questionMessageFlag.setFlag(MBMessageFlagImpl.QUESTION_FLAG);
71  
72              mbMessageFlagPersistence.update(questionMessageFlag, false);
73          }
74      }
75  
76      public void addReadFlags(long userId, MBThread thread)
77          throws PortalException, SystemException {
78  
79          User user = userPersistence.findByPrimaryKey(userId);
80  
81          if (user.isDefaultUser()) {
82              return;
83          }
84  
85          long messageId = thread.getRootMessageId();
86          int flag = MBMessageFlagImpl.READ_FLAG;
87  
88          MBMessageFlag messageFlag = mbMessageFlagPersistence.fetchByU_M_F(
89              userId, messageId, flag);
90  
91          if (messageFlag == null) {
92              long messageFlagId = counterLocalService.increment();
93  
94              messageFlag = mbMessageFlagPersistence.create(messageFlagId);
95  
96              messageFlag.setUserId(userId);
97              messageFlag.setModifiedDate(thread.getLastPostDate());
98              messageFlag.setThreadId(thread.getThreadId());
99              messageFlag.setMessageId(messageId);
100             messageFlag.setFlag(flag);
101 
102             try {
103                 mbMessageFlagPersistence.update(messageFlag, false);
104             }
105             catch (SystemException se) {
106                 if (_log.isWarnEnabled()) {
107                     _log.warn(
108                         "Add failed, fetch {userId=" + userId +
109                             ", messageId=" + messageId + ",flag=" + flag +
110                                 "}");
111                 }
112 
113                 messageFlag = mbMessageFlagPersistence.fetchByU_M_F(
114                     userId, messageId, flag, false);
115 
116                 if (messageFlag == null) {
117                     throw se;
118                 }
119             }
120         }
121 
122         if (!DateUtil.equals(
123                 messageFlag.getModifiedDate(), thread.getLastPostDate(),
124                 true)) {
125 
126             messageFlag.setModifiedDate(thread.getLastPostDate());
127 
128             mbMessageFlagPersistence.update(messageFlag, false);
129         }
130     }
131 
132     public void deleteAnswerFlags(long threadId, long messageId)
133         throws SystemException {
134 
135         mbMessageFlagPersistence.removeByM_F(
136             messageId, MBMessageFlagImpl.ANSWER_FLAG);
137 
138         List<MBMessage> messages = mbMessagePersistence.findByT_P(
139             threadId, messageId);
140 
141         for (MBMessage message : messages) {
142             deleteAnswerFlags(threadId, message.getMessageId());
143         }
144     }
145 
146     public void deleteFlags(long userId) throws SystemException {
147         mbMessageFlagPersistence.removeByUserId(userId);
148     }
149 
150     public void deleteFlags(long messageId, int flag) throws SystemException {
151         mbMessageFlagPersistence.removeByM_F(messageId, flag);
152     }
153 
154     public void deleteQuestionAndAnswerFlags(long threadId)
155         throws SystemException {
156 
157         List<MBMessage> messages = mbMessagePersistence.findByThreadId(
158             threadId);
159 
160         for (MBMessage message : messages) {
161             if (message.isRoot()) {
162                 mbMessageFlagPersistence.removeByM_F(
163                     message.getMessageId(), MBMessageFlagImpl.QUESTION_FLAG);
164             }
165 
166             mbMessageFlagPersistence.removeByM_F(
167                 message.getMessageId(), MBMessageFlagImpl.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(), MBMessageFlagImpl.READ_FLAG);
186     }
187 
188     public boolean hasAnswerFlag(long messageId) throws SystemException {
189         int count = mbMessageFlagPersistence.countByM_F(
190             messageId, MBMessageFlagImpl.ANSWER_FLAG);
191 
192         if (count > 0) {
193             return true;
194         }
195         else {
196             return false;
197         }
198     }
199 
200     public boolean hasQuestionFlag(long messageId) throws SystemException {
201         int count = mbMessageFlagPersistence.countByM_F(
202             messageId, MBMessageFlagImpl.QUESTION_FLAG);
203 
204         if (count > 0) {
205             return true;
206         }
207         else {
208             return false;
209         }
210     }
211 
212     public boolean hasReadFlag(long userId, MBThread thread)
213         throws PortalException, SystemException {
214 
215         User user = userPersistence.findByPrimaryKey(userId);
216 
217         if (user.isDefaultUser()) {
218             return true;
219         }
220 
221         MBMessageFlag messageFlag = mbMessageFlagPersistence.fetchByU_M_F(
222             userId, thread.getRootMessageId(), MBMessageFlagImpl.READ_FLAG);
223 
224         if ((messageFlag != null) &&
225             (DateUtil.equals(
226                 messageFlag.getModifiedDate(), thread.getLastPostDate(),
227                 true))) {
228 
229             return true;
230         }
231         else {
232             return false;
233         }
234     }
235 
236     private static Log _log = LogFactoryUtil.getLog(
237         MBMessageFlagLocalServiceImpl.class);
238 
239 }