1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.List;
38  
39  /**
40   * <a href="MBMessageFlagLocalServiceImpl.java.html"><b><i>View Source</i></b>
41   * </a>
42   *
43   * @author Brian Wing Shun Chan
44   */
45  public class MBMessageFlagLocalServiceImpl
46      extends MBMessageFlagLocalServiceBaseImpl {
47  
48      /**
49       * @deprecated
50       */
51      public void addReadFlags(long userId, List<MBMessage> messages)
52          throws PortalException, SystemException {
53  
54          MBMessage message = messages.get(1);
55  
56          MBThread thread = message.getThread();
57  
58          addReadFlags(userId, thread);
59      }
60  
61      public void addReadFlags(long userId, MBThread thread)
62          throws PortalException, SystemException {
63  
64          User user = userPersistence.findByPrimaryKey(userId);
65  
66          if (user.isDefaultUser()) {
67              return;
68          }
69  
70          long messageId = thread.getRootMessageId();
71          int flag = MBMessageFlagImpl.READ_FLAG;
72  
73          MBMessageFlag messageFlag = mbMessageFlagPersistence.fetchByU_M_F(
74              userId, messageId, flag);
75  
76          if (messageFlag == null) {
77              long messageFlagId = counterLocalService.increment();
78  
79              messageFlag = mbMessageFlagPersistence.create(messageFlagId);
80  
81              messageFlag.setUserId(userId);
82              messageFlag.setModifiedDate(thread.getLastPostDate());
83              messageFlag.setThreadId(thread.getThreadId());
84              messageFlag.setMessageId(messageId);
85              messageFlag.setFlag(flag);
86  
87              mbMessageFlagPersistence.update(messageFlag, false);
88  
89              try {
90                  mbMessageFlagPersistence.update(messageFlag, false);
91              }
92              catch (SystemException se) {
93                  if (_log.isWarnEnabled()) {
94                      _log.warn(
95                          "Add failed, fetch {userId=" + userId +
96                              ", messageId=" + messageId + ",flag=" + flag +
97                                  "}");
98                  }
99  
100                 messageFlag = mbMessageFlagPersistence.fetchByU_M_F(
101                     userId, messageId, flag, false);
102 
103                 if (messageFlag == null) {
104                     throw se;
105                 }
106             }
107         }
108 
109         if (!DateUtil.equals(
110                 messageFlag.getModifiedDate(), thread.getLastPostDate(),
111                 true)) {
112 
113             messageFlag.setModifiedDate(thread.getLastPostDate());
114 
115             mbMessageFlagPersistence.update(messageFlag, false);
116         }
117     }
118 
119     public void deleteFlags(long userId) throws SystemException {
120         mbMessageFlagPersistence.removeByUserId(userId);
121     }
122 
123     public void deleteThreadFlags(long threadId) throws SystemException {
124         mbMessageFlagPersistence.removeByThreadId(threadId);
125     }
126 
127     public MBMessageFlag getReadFlag(long userId, MBThread thread)
128         throws PortalException, SystemException {
129 
130         User user = userPersistence.findByPrimaryKey(userId);
131 
132         if (user.isDefaultUser()) {
133             return null;
134         }
135 
136         return mbMessageFlagPersistence.fetchByU_M_F(
137             userId, thread.getRootMessageId(), MBMessageFlagImpl.READ_FLAG);
138     }
139 
140     /**
141      * @deprecated
142      */
143     public boolean hasReadFlag(long userId, long messageId)
144         throws PortalException, SystemException {
145 
146         MBMessage message = mbMessageLocalService.getMessage(messageId);
147 
148         return hasReadFlag(userId, message.getThread());
149     }
150 
151     public boolean hasReadFlag(long userId, MBThread thread)
152         throws PortalException, SystemException {
153 
154         User user = userPersistence.findByPrimaryKey(userId);
155 
156         if (user.isDefaultUser()) {
157             return true;
158         }
159 
160         MBMessageFlag messageFlag = mbMessageFlagPersistence.fetchByU_M_F(
161             userId, thread.getRootMessageId(), MBMessageFlagImpl.READ_FLAG);
162 
163         if ((messageFlag != null) &&
164             (DateUtil.equals(
165                 messageFlag.getModifiedDate(), thread.getLastPostDate(),
166                 true))) {
167 
168             return true;
169         }
170         else {
171             return false;
172         }
173     }
174 
175     private static Log _log =
176         LogFactoryUtil.getLog(MBMessageFlagLocalServiceImpl.class);
177 
178 }