1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.messageboards.service.impl;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.security.permission.ActionKeys;
20  import com.liferay.portlet.messageboards.NoSuchMessageFlagException;
21  import com.liferay.portlet.messageboards.model.MBMessage;
22  import com.liferay.portlet.messageboards.model.MBMessageFlag;
23  import com.liferay.portlet.messageboards.model.MBMessageFlagConstants;
24  import com.liferay.portlet.messageboards.model.MBThread;
25  import com.liferay.portlet.messageboards.service.base.MBMessageFlagServiceBaseImpl;
26  import com.liferay.portlet.messageboards.service.permission.MBMessagePermission;
27  
28  import java.util.Date;
29  
30  /**
31   * <a href="MBMessageFlagServiceImpl.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   */
35  public class MBMessageFlagServiceImpl extends MBMessageFlagServiceBaseImpl {
36  
37      public void addAnswerFlag(long messageId)
38          throws PortalException, SystemException {
39  
40          MBMessage message = mbMessagePersistence.findByPrimaryKey(messageId);
41  
42          if (message.isRoot()) {
43              return;
44          }
45  
46          MBThread thread = mbThreadPersistence.findByPrimaryKey(
47              message.getThreadId());
48  
49          MBMessage rootMessage = mbMessagePersistence.findByPrimaryKey(
50              thread.getRootMessageId());
51  
52          MBMessagePermission.check(
53              getPermissionChecker(), rootMessage.getMessageId(),
54              ActionKeys.UPDATE);
55  
56          MBMessageFlag questionMessageFlag =
57              mbMessageFlagPersistence.fetchByU_M_F(
58                  rootMessage.getUserId(), rootMessage.getMessageId(),
59                  MBMessageFlagConstants.QUESTION_FLAG);
60  
61          MBMessageFlag answerMessageFlag =
62              mbMessageFlagPersistence.fetchByU_M_F(
63                  rootMessage.getUserId(), rootMessage.getMessageId(),
64                  MBMessageFlagConstants.ANSWER_FLAG);
65  
66          if ((questionMessageFlag != null) && (answerMessageFlag == null)) {
67              questionMessageFlag.setFlag(MBMessageFlagConstants.ANSWER_FLAG);
68  
69              mbMessageFlagPersistence.update(questionMessageFlag, false);
70          }
71  
72          MBMessageFlag messageFlag = mbMessageFlagPersistence.fetchByU_M_F(
73              message.getUserId(), message.getMessageId(),
74              MBMessageFlagConstants.ANSWER_FLAG);
75  
76          if (messageFlag == null) {
77              long messageFlagId = counterLocalService.increment();
78  
79              messageFlag = mbMessageFlagPersistence.create(messageFlagId);
80  
81              messageFlag.setUserId(message.getUserId());
82              messageFlag.setModifiedDate(new Date());
83              messageFlag.setThreadId(message.getThreadId());
84              messageFlag.setMessageId(message.getMessageId());
85              messageFlag.setFlag(MBMessageFlagConstants.ANSWER_FLAG);
86  
87              mbMessageFlagPersistence.update(messageFlag, false);
88          }
89      }
90  
91      public void deleteAnswerFlag(long messageId)
92          throws PortalException, SystemException {
93  
94          MBMessage message = mbMessagePersistence.findByPrimaryKey(messageId);
95  
96          if (message.isRoot()) {
97              return;
98          }
99  
100         MBThread thread = mbThreadPersistence.findByPrimaryKey(
101             message.getThreadId());
102 
103         MBMessage rootMessage = mbMessagePersistence.findByPrimaryKey(
104             thread.getRootMessageId());
105 
106         MBMessagePermission.check(
107             getPermissionChecker(), rootMessage.getMessageId(),
108             ActionKeys.UPDATE);
109 
110         try {
111             mbMessageFlagPersistence.removeByU_M_F(
112                 message.getUserId(), message.getMessageId(),
113                 MBMessageFlagConstants.ANSWER_FLAG);
114         }
115         catch (NoSuchMessageFlagException nsmfe) {
116         }
117 
118         MBMessageFlag answerMessageFlag =
119             mbMessageFlagPersistence.fetchByU_M_F(
120                 rootMessage.getUserId(), rootMessage.getMessageId(),
121                 MBMessageFlagConstants.ANSWER_FLAG);
122 
123         if (answerMessageFlag == null) {
124             return;
125         }
126 
127         int answerFlagsCount = mbMessageFlagPersistence.countByT_F(
128             message.getThreadId(), MBMessageFlagConstants.ANSWER_FLAG);
129 
130         if (answerFlagsCount == 1) {
131             answerMessageFlag.setFlag(MBMessageFlagConstants.QUESTION_FLAG);
132 
133             mbMessageFlagPersistence.update(answerMessageFlag, false);
134         }
135     }
136 
137 }