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