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             mbMessageFlagPersistence.update(messageFlag, false);
103 
104             try {
105                 mbMessageFlagPersistence.update(messageFlag, false);
106             }
107             catch (SystemException se) {
108                 if (_log.isWarnEnabled()) {
109                     _log.warn(
110                         "Add failed, fetch {userId=" + userId +
111                             ", messageId=" + messageId + ",flag=" + flag +
112                                 "}");
113                 }
114 
115                 messageFlag = mbMessageFlagPersistence.fetchByU_M_F(
116                     userId, messageId, flag, false);
117 
118                 if (messageFlag == null) {
119                     throw se;
120                 }
121             }
122         }
123 
124         if (!DateUtil.equals(
125                 messageFlag.getModifiedDate(), thread.getLastPostDate(),
126                 true)) {
127 
128             messageFlag.setModifiedDate(thread.getLastPostDate());
129 
130             mbMessageFlagPersistence.update(messageFlag, 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(), MBMessageFlagImpl.QUESTION_FLAG);
152             }
153 
154             mbMessageFlagPersistence.removeByM_F(
155                 message.getMessageId(), MBMessageFlagImpl.ANSWER_FLAG);
156         }
157     }
158 
159     public void deleteThreadFlags(long threadId) throws SystemException {
160         mbMessageFlagPersistence.removeByThreadId(threadId);
161     }
162 
163     public MBMessageFlag getReadFlag(long userId, MBThread thread)
164         throws PortalException, SystemException {
165 
166         User user = userPersistence.findByPrimaryKey(userId);
167 
168         if (user.isDefaultUser()) {
169             return null;
170         }
171 
172         return mbMessageFlagPersistence.fetchByU_M_F(
173             userId, thread.getRootMessageId(), MBMessageFlagImpl.READ_FLAG);
174     }
175 
176     public boolean hasAnswerFlag(long messageId) throws SystemException {
177         int count = mbMessageFlagPersistence.countByM_F(
178             messageId, MBMessageFlagImpl.ANSWER_FLAG);
179 
180         if (count > 0) {
181             return true;
182         }
183         else {
184             return false;
185         }
186     }
187 
188     public boolean hasQuestionFlag(long messageId) throws SystemException {
189         int count = mbMessageFlagPersistence.countByM_F(
190             messageId, MBMessageFlagImpl.QUESTION_FLAG);
191 
192         if (count > 0) {
193             return true;
194         }
195         else {
196             return false;
197         }
198     }
199 
200     public boolean hasReadFlag(long userId, MBThread thread)
201         throws PortalException, SystemException {
202 
203         User user = userPersistence.findByPrimaryKey(userId);
204 
205         if (user.isDefaultUser()) {
206             return true;
207         }
208 
209         MBMessageFlag messageFlag = mbMessageFlagPersistence.fetchByU_M_F(
210             userId, thread.getRootMessageId(), MBMessageFlagImpl.READ_FLAG);
211 
212         if ((messageFlag != null) &&
213             (DateUtil.equals(
214                 messageFlag.getModifiedDate(), thread.getLastPostDate(),
215                 true))) {
216 
217             return true;
218         }
219         else {
220             return false;
221         }
222     }
223 
224     private static Log _log = LogFactoryUtil.getLog(
225         MBMessageFlagLocalServiceImpl.class);
226 
227 }