1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.messageboards.service.impl;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.security.permission.ActionKeys;
25  import com.liferay.portlet.messageboards.NoSuchMessageFlagException;
26  import com.liferay.portlet.messageboards.model.MBMessage;
27  import com.liferay.portlet.messageboards.model.MBMessageFlag;
28  import com.liferay.portlet.messageboards.model.MBThread;
29  import com.liferay.portlet.messageboards.model.impl.MBMessageFlagImpl;
30  import com.liferay.portlet.messageboards.service.base.MBMessageFlagServiceBaseImpl;
31  import com.liferay.portlet.messageboards.service.permission.MBMessagePermission;
32  
33  /**
34   * <a href="MBMessageFlagServiceImpl.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   *
38   */
39  public class MBMessageFlagServiceImpl extends MBMessageFlagServiceBaseImpl {
40  
41      public void addAnswerFlag(long messageId)
42          throws PortalException, SystemException {
43  
44          MBMessage message = mbMessagePersistence.findByPrimaryKey(messageId);
45  
46          if (message.isRoot()) {
47              return;
48          }
49  
50          MBThread thread = mbThreadPersistence.findByPrimaryKey(
51              message.getThreadId());
52  
53          MBMessage rootMessage = mbMessagePersistence.findByPrimaryKey(
54              thread.getRootMessageId());
55  
56          MBMessagePermission.check(
57              getPermissionChecker(), rootMessage.getMessageId(),
58              ActionKeys.UPDATE);
59  
60          MBMessageFlag questionMessageFlag =
61              mbMessageFlagPersistence.fetchByU_M_F(
62                  rootMessage.getUserId(), rootMessage.getMessageId(),
63                  MBMessageFlagImpl.QUESTION_FLAG);
64  
65          MBMessageFlag answerMessageFlag =
66              mbMessageFlagPersistence.fetchByU_M_F(
67                  rootMessage.getUserId(), rootMessage.getMessageId(),
68                  MBMessageFlagImpl.ANSWER_FLAG);
69  
70          if ((questionMessageFlag != null) && (answerMessageFlag == null)) {
71              questionMessageFlag.setFlag(MBMessageFlagImpl.ANSWER_FLAG);
72  
73              mbMessageFlagPersistence.update(questionMessageFlag, false);
74          }
75  
76          MBMessageFlag messageFlag = mbMessageFlagPersistence.fetchByU_M_F(
77              message.getUserId(), message.getMessageId(),
78              MBMessageFlagImpl.ANSWER_FLAG);
79  
80          if (messageFlag == null) {
81              long messageFlagId = counterLocalService.increment();
82  
83              messageFlag = mbMessageFlagPersistence.create(messageFlagId);
84  
85              messageFlag.setUserId(message.getUserId());
86              messageFlag.setMessageId(message.getMessageId());
87              messageFlag.setFlag(MBMessageFlagImpl.ANSWER_FLAG);
88  
89              mbMessageFlagPersistence.update(messageFlag, false);
90          }
91      }
92  
93      public void deleteAnswerFlag(long messageId)
94          throws PortalException, SystemException {
95  
96          MBMessage message = mbMessagePersistence.findByPrimaryKey(messageId);
97  
98          if (message.isRoot()) {
99              return;
100         }
101 
102         MBThread thread = mbThreadPersistence.findByPrimaryKey(
103             message.getThreadId());
104 
105         MBMessage rootMessage = mbMessagePersistence.findByPrimaryKey(
106             thread.getRootMessageId());
107 
108         MBMessagePermission.check(
109             getPermissionChecker(), rootMessage.getMessageId(),
110             ActionKeys.UPDATE);
111 
112         try {
113             mbMessageFlagPersistence.removeByU_M_F(
114                 message.getUserId(), message.getMessageId(),
115                 MBMessageFlagImpl.ANSWER_FLAG);
116         }
117         catch (NoSuchMessageFlagException nsmfe) {
118         }
119 
120         MBMessageFlag answerMessageFlag =
121             mbMessageFlagPersistence.fetchByU_M_F(
122                 rootMessage.getUserId(), rootMessage.getMessageId(),
123                 MBMessageFlagImpl.ANSWER_FLAG);
124 
125         if (answerMessageFlag == null) {
126             return;
127         }
128 
129         int answerFlagsCount = mbMessageFlagPersistence.countByT_F(
130             message.getThreadId(), MBMessageFlagImpl.ANSWER_FLAG);
131 
132         if (answerFlagsCount == 0) {
133             answerMessageFlag.setFlag(MBMessageFlagImpl.QUESTION_FLAG);
134 
135             mbMessageFlagPersistence.update(answerMessageFlag, false);
136         }
137     }
138 
139 }