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.Date;
38  import java.util.List;
39  
40  /**
41   * <a href="MBMessageFlagLocalServiceImpl.java.html"><b><i>View Source</i></b>
42   * </a>
43   *
44   * @author Brian Wing Shun Chan
45   */
46  public class MBMessageFlagLocalServiceImpl
47      extends MBMessageFlagLocalServiceBaseImpl {
48  
49      public void addQuestionFlag(long messageId)
50          throws PortalException, SystemException {
51  
52          MBMessage message = mbMessagePersistence.findByPrimaryKey(messageId);
53  
54          if (!message.isRoot()) {
55              return;
56          }
57  
58          MBMessageFlag questionMessageFlag =
59              mbMessageFlagPersistence.fetchByU_M_F(
60                  message.getUserId(), message.getMessageId(),
61                  MBMessageFlagImpl.QUESTION_FLAG);
62  
63          MBMessageFlag answerMessageFlag =
64              mbMessageFlagPersistence.fetchByU_M_F(
65                  message.getUserId(), message.getMessageId(),
66                  MBMessageFlagImpl.ANSWER_FLAG);
67  
68          if ((questionMessageFlag == null) && (answerMessageFlag == null)) {
69              long messageFlagId = counterLocalService.increment();
70  
71              questionMessageFlag = mbMessageFlagPersistence.create(
72                  messageFlagId);
73  
74              questionMessageFlag.setUserId(message.getUserId());
75              questionMessageFlag.setModifiedDate(new Date());
76              questionMessageFlag.setThreadId(message.getThreadId());
77              questionMessageFlag.setMessageId(message.getMessageId());
78              questionMessageFlag.setFlag(MBMessageFlagImpl.QUESTION_FLAG);
79  
80              mbMessageFlagPersistence.update(questionMessageFlag, false);
81          }
82      }
83  
84      public void addReadFlags(long userId, MBThread thread)
85          throws PortalException, SystemException {
86  
87          User user = userPersistence.findByPrimaryKey(userId);
88  
89          if (user.isDefaultUser()) {
90              return;
91          }
92  
93          long messageId = thread.getRootMessageId();
94          int flag = MBMessageFlagImpl.READ_FLAG;
95  
96          MBMessageFlag messageFlag = mbMessageFlagPersistence.fetchByU_M_F(
97              userId, messageId, flag);
98  
99          if (messageFlag == null) {
100             long messageFlagId = counterLocalService.increment();
101 
102             messageFlag = mbMessageFlagPersistence.create(messageFlagId);
103 
104             messageFlag.setUserId(userId);
105             messageFlag.setModifiedDate(thread.getLastPostDate());
106             messageFlag.setThreadId(thread.getThreadId());
107             messageFlag.setMessageId(messageId);
108             messageFlag.setFlag(flag);
109 
110             mbMessageFlagPersistence.update(messageFlag, false);
111 
112             try {
113                 mbMessageFlagPersistence.update(messageFlag, false);
114             }
115             catch (SystemException se) {
116                 if (_log.isWarnEnabled()) {
117                     _log.warn(
118                         "Add failed, fetch {userId=" + userId +
119                             ", messageId=" + messageId + ",flag=" + flag +
120                                 "}");
121                 }
122 
123                 messageFlag = mbMessageFlagPersistence.fetchByU_M_F(
124                     userId, messageId, flag, false);
125 
126                 if (messageFlag == null) {
127                     throw se;
128                 }
129             }
130         }
131 
132         if (!DateUtil.equals(
133                 messageFlag.getModifiedDate(), thread.getLastPostDate(),
134                 true)) {
135 
136             messageFlag.setModifiedDate(thread.getLastPostDate());
137 
138             mbMessageFlagPersistence.update(messageFlag, false);
139         }
140     }
141 
142     public void deleteFlags(long userId) throws SystemException {
143         mbMessageFlagPersistence.removeByUserId(userId);
144     }
145 
146     public void deleteFlags(long messageId, int flag) throws SystemException {
147         mbMessageFlagPersistence.removeByM_F(messageId, flag);
148     }
149 
150     public void deleteQuestionAndAnswerFlags(long threadId)
151         throws SystemException {
152 
153         List<MBMessage> messages = mbMessagePersistence.findByThreadId(
154             threadId);
155 
156         for (MBMessage message : messages) {
157             if (message.isRoot()) {
158                 mbMessageFlagPersistence.removeByM_F(
159                     message.getMessageId(), MBMessageFlagImpl.QUESTION_FLAG);
160             }
161 
162             mbMessageFlagPersistence.removeByM_F(
163                 message.getMessageId(), MBMessageFlagImpl.ANSWER_FLAG);
164         }
165     }
166 
167     public void deleteThreadFlags(long threadId) throws SystemException {
168         mbMessageFlagPersistence.removeByThreadId(threadId);
169     }
170 
171     public MBMessageFlag getReadFlag(long userId, MBThread thread)
172         throws PortalException, SystemException {
173 
174         User user = userPersistence.findByPrimaryKey(userId);
175 
176         if (user.isDefaultUser()) {
177             return null;
178         }
179 
180         return mbMessageFlagPersistence.fetchByU_M_F(
181             userId, thread.getRootMessageId(), MBMessageFlagImpl.READ_FLAG);
182     }
183 
184     public boolean hasAnswerFlag(long messageId) throws SystemException {
185         int count = mbMessageFlagPersistence.countByM_F(
186             messageId, MBMessageFlagImpl.ANSWER_FLAG);
187 
188         if (count > 0) {
189             return true;
190         }
191         else {
192             return false;
193         }
194     }
195 
196     public boolean hasQuestionFlag(long messageId) throws SystemException {
197         int count = mbMessageFlagPersistence.countByM_F(
198             messageId, MBMessageFlagImpl.QUESTION_FLAG);
199 
200         if (count > 0) {
201             return true;
202         }
203         else {
204             return false;
205         }
206     }
207 
208     public boolean hasReadFlag(long userId, MBThread thread)
209         throws PortalException, SystemException {
210 
211         User user = userPersistence.findByPrimaryKey(userId);
212 
213         if (user.isDefaultUser()) {
214             return true;
215         }
216 
217         MBMessageFlag messageFlag = mbMessageFlagPersistence.fetchByU_M_F(
218             userId, thread.getRootMessageId(), MBMessageFlagImpl.READ_FLAG);
219 
220         if ((messageFlag != null) &&
221             (DateUtil.equals(
222                 messageFlag.getModifiedDate(), thread.getLastPostDate(),
223                 true))) {
224 
225             return true;
226         }
227         else {
228             return false;
229         }
230     }
231 
232     private static Log _log =
233         LogFactoryUtil.getLog(MBMessageFlagLocalServiceImpl.class);
234 
235 }