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.List;
30  
31  /**
32   * <a href="MBMessageFlagLocalServiceImpl.java.html"><b><i>View Source</i></b>
33   * </a>
34   *
35   * @author Brian Wing Shun Chan
36   */
37  public class MBMessageFlagLocalServiceImpl
38      extends MBMessageFlagLocalServiceBaseImpl {
39  
40      /**
41       * @deprecated
42       */
43      public void addReadFlags(long userId, List<MBMessage> messages)
44          throws PortalException, SystemException {
45  
46          MBMessage message = messages.get(1);
47  
48          MBThread thread = message.getThread();
49  
50          addReadFlags(userId, thread);
51      }
52  
53      public void addReadFlags(long userId, MBThread thread)
54          throws PortalException, SystemException {
55  
56          User user = userPersistence.findByPrimaryKey(userId);
57  
58          if (user.isDefaultUser()) {
59              return;
60          }
61  
62          long messageId = thread.getRootMessageId();
63          int flag = MBMessageFlagImpl.READ_FLAG;
64  
65          MBMessageFlag messageFlag = mbMessageFlagPersistence.fetchByU_M_F(
66              userId, messageId, flag);
67  
68          if (messageFlag == null) {
69              long messageFlagId = counterLocalService.increment();
70  
71              messageFlag = mbMessageFlagPersistence.create(messageFlagId);
72  
73              messageFlag.setUserId(userId);
74              messageFlag.setModifiedDate(thread.getLastPostDate());
75              messageFlag.setThreadId(thread.getThreadId());
76              messageFlag.setMessageId(messageId);
77              messageFlag.setFlag(flag);
78  
79              mbMessageFlagPersistence.update(messageFlag, false);
80  
81              try {
82                  mbMessageFlagPersistence.update(messageFlag, false);
83              }
84              catch (SystemException se) {
85                  if (_log.isWarnEnabled()) {
86                      _log.warn(
87                          "Add failed, fetch {userId=" + userId +
88                              ", messageId=" + messageId + ",flag=" + flag +
89                                  "}");
90                  }
91  
92                  messageFlag = mbMessageFlagPersistence.fetchByU_M_F(
93                      userId, messageId, flag, false);
94  
95                  if (messageFlag == null) {
96                      throw se;
97                  }
98              }
99          }
100 
101         if (!DateUtil.equals(
102                 messageFlag.getModifiedDate(), thread.getLastPostDate(),
103                 true)) {
104 
105             messageFlag.setModifiedDate(thread.getLastPostDate());
106 
107             mbMessageFlagPersistence.update(messageFlag, false);
108         }
109     }
110 
111     public void deleteFlags(long userId) throws SystemException {
112         mbMessageFlagPersistence.removeByUserId(userId);
113     }
114 
115     public void deleteThreadFlags(long threadId) throws SystemException {
116         mbMessageFlagPersistence.removeByThreadId(threadId);
117     }
118 
119     public MBMessageFlag getReadFlag(long userId, MBThread thread)
120         throws PortalException, SystemException {
121 
122         User user = userPersistence.findByPrimaryKey(userId);
123 
124         if (user.isDefaultUser()) {
125             return null;
126         }
127 
128         return mbMessageFlagPersistence.fetchByU_M_F(
129             userId, thread.getRootMessageId(), MBMessageFlagImpl.READ_FLAG);
130     }
131 
132     /**
133      * @deprecated
134      */
135     public boolean hasReadFlag(long userId, long messageId)
136         throws PortalException, SystemException {
137 
138         MBMessage message = mbMessageLocalService.getMessage(messageId);
139 
140         return hasReadFlag(userId, message.getThread());
141     }
142 
143     public boolean hasReadFlag(long userId, MBThread thread)
144         throws PortalException, SystemException {
145 
146         User user = userPersistence.findByPrimaryKey(userId);
147 
148         if (user.isDefaultUser()) {
149             return true;
150         }
151 
152         MBMessageFlag messageFlag = mbMessageFlagPersistence.fetchByU_M_F(
153             userId, thread.getRootMessageId(), MBMessageFlagImpl.READ_FLAG);
154 
155         if ((messageFlag != null) &&
156             (DateUtil.equals(
157                 messageFlag.getModifiedDate(), thread.getLastPostDate(),
158                 true))) {
159 
160             return true;
161         }
162         else {
163             return false;
164         }
165     }
166 
167     private static Log _log = LogFactoryUtil.getLog(
168         MBMessageFlagLocalServiceImpl.class);
169 
170 }