1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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   */
46  public class MBMessageFlagLocalServiceImpl
47      extends MBMessageFlagLocalServiceBaseImpl {
48  
49      /**
50       * @deprecated
51       */
52      public void addReadFlags(long userId, List<MBMessage> messages)
53          throws PortalException, SystemException {
54  
55          MBMessage message = messages.get(1);
56  
57          MBThread thread = message.getThread();
58  
59          addReadFlags(userId, thread);
60      }
61  
62      public void addReadFlags(long userId, MBThread thread)
63          throws PortalException, SystemException {
64  
65          User user = userPersistence.findByPrimaryKey(userId);
66  
67          if (user.isDefaultUser()) {
68              return;
69          }
70  
71          long messageId = thread.getRootMessageId();
72          int flag = MBMessageFlagImpl.READ_FLAG;
73  
74          MBMessageFlag messageFlag = mbMessageFlagPersistence.fetchByU_M_F(
75              userId, messageId, flag);
76  
77          if (messageFlag == null) {
78              long messageFlagId = counterLocalService.increment();
79  
80              messageFlag = mbMessageFlagPersistence.create(messageFlagId);
81  
82              messageFlag.setUserId(userId);
83              messageFlag.setModifiedDate(thread.getLastPostDate());
84              messageFlag.setThreadId(thread.getThreadId());
85              messageFlag.setMessageId(messageId);
86              messageFlag.setFlag(flag);
87  
88              mbMessageFlagPersistence.update(messageFlag, false);
89  
90              try {
91                  mbMessageFlagPersistence.update(messageFlag, false);
92              }
93              catch (SystemException se) {
94                  if (_log.isWarnEnabled()) {
95                      _log.warn(
96                          "Add failed, fetch {userId=" + userId +
97                              ", messageId=" + messageId + ",flag=" + flag +
98                                  "}");
99                  }
100 
101                 messageFlag = mbMessageFlagPersistence.fetchByU_M_F(
102                     userId, messageId, flag, false);
103 
104                 if (messageFlag == null) {
105                     throw se;
106                 }
107             }
108         }
109 
110         if (!DateUtil.equals(
111                 messageFlag.getModifiedDate(), thread.getLastPostDate(),
112                 true)) {
113 
114             messageFlag.setModifiedDate(thread.getLastPostDate());
115 
116             mbMessageFlagPersistence.update(messageFlag, false);
117         }
118     }
119 
120     public void deleteFlags(long userId) throws SystemException {
121         mbMessageFlagPersistence.removeByUserId(userId);
122     }
123 
124     public void deleteThreadFlags(long threadId) throws SystemException {
125         mbMessageFlagPersistence.removeByThreadId(threadId);
126     }
127 
128     /**
129      * @deprecated
130      */
131     public boolean hasReadFlag(long userId, long messageId)
132         throws PortalException, SystemException {
133 
134         MBMessage message = mbMessageLocalService.getMessage(messageId);
135 
136         return hasReadFlag(userId, message.getThread());
137     }
138 
139     public boolean hasReadFlag(long userId, MBThread thread)
140         throws PortalException, SystemException {
141 
142         User user = userPersistence.findByPrimaryKey(userId);
143 
144         if (user.isDefaultUser()) {
145             return true;
146         }
147 
148         MBMessageFlag messageFlag = mbMessageFlagPersistence.fetchByU_M_F(
149             userId, thread.getRootMessageId(), MBMessageFlagImpl.READ_FLAG);
150 
151         if ((messageFlag != null) &&
152             (DateUtil.equals(
153                 messageFlag.getModifiedDate(), thread.getLastPostDate(),
154                 true))) {
155 
156             return true;
157         }
158         else {
159             return false;
160         }
161     }
162 
163     private static Log _log =
164         LogFactoryUtil.getLog(MBMessageFlagLocalServiceImpl.class);
165 
166 }