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.security.permission.ActionKeys;
28  import com.liferay.portlet.messageboards.NoSuchMessageFlagException;
29  import com.liferay.portlet.messageboards.model.MBMessage;
30  import com.liferay.portlet.messageboards.model.MBMessageFlag;
31  import com.liferay.portlet.messageboards.model.MBThread;
32  import com.liferay.portlet.messageboards.model.impl.MBMessageFlagImpl;
33  import com.liferay.portlet.messageboards.service.base.MBMessageFlagServiceBaseImpl;
34  import com.liferay.portlet.messageboards.service.permission.MBMessagePermission;
35  
36  /**
37   * <a href="MBMessageFlagServiceImpl.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class MBMessageFlagServiceImpl extends MBMessageFlagServiceBaseImpl {
43  
44      public void addAnswerFlag(long messageId)
45          throws PortalException, SystemException {
46  
47          MBMessage message = mbMessagePersistence.findByPrimaryKey(messageId);
48  
49          if (message.isRoot()) {
50              return;
51          }
52  
53          MBThread thread = mbThreadPersistence.findByPrimaryKey(
54              message.getThreadId());
55  
56          MBMessage rootMessage = mbMessagePersistence.findByPrimaryKey(
57              thread.getRootMessageId());
58  
59          MBMessagePermission.check(
60              getPermissionChecker(), rootMessage.getMessageId(),
61              ActionKeys.UPDATE);
62  
63          MBMessageFlag questionMessageFlag =
64              mbMessageFlagPersistence.fetchByU_M_F(
65                  rootMessage.getUserId(), rootMessage.getMessageId(),
66                  MBMessageFlagImpl.QUESTION_FLAG);
67  
68          MBMessageFlag answerMessageFlag =
69              mbMessageFlagPersistence.fetchByU_M_F(
70                  rootMessage.getUserId(), rootMessage.getMessageId(),
71                  MBMessageFlagImpl.ANSWER_FLAG);
72  
73          if ((questionMessageFlag != null) && (answerMessageFlag == null)) {
74              questionMessageFlag.setFlag(MBMessageFlagImpl.ANSWER_FLAG);
75  
76              mbMessageFlagPersistence.update(questionMessageFlag, false);
77          }
78  
79          MBMessageFlag messageFlag = mbMessageFlagPersistence.fetchByU_M_F(
80              message.getUserId(), message.getMessageId(),
81              MBMessageFlagImpl.ANSWER_FLAG);
82  
83          if (messageFlag == null) {
84              long messageFlagId = counterLocalService.increment();
85  
86              messageFlag = mbMessageFlagPersistence.create(messageFlagId);
87  
88              messageFlag.setUserId(message.getUserId());
89              messageFlag.setMessageId(message.getMessageId());
90              messageFlag.setFlag(MBMessageFlagImpl.ANSWER_FLAG);
91  
92              mbMessageFlagPersistence.update(messageFlag, false);
93          }
94      }
95  
96      public void deleteAnswerFlag(long messageId)
97          throws PortalException, SystemException {
98  
99          MBMessage message = mbMessagePersistence.findByPrimaryKey(messageId);
100 
101         if (message.isRoot()) {
102             return;
103         }
104 
105         MBThread thread = mbThreadPersistence.findByPrimaryKey(
106             message.getThreadId());
107 
108         MBMessage rootMessage = mbMessagePersistence.findByPrimaryKey(
109             thread.getRootMessageId());
110 
111         MBMessagePermission.check(
112             getPermissionChecker(), rootMessage.getMessageId(),
113             ActionKeys.UPDATE);
114 
115         try {
116             mbMessageFlagPersistence.removeByU_M_F(
117                 message.getUserId(), message.getMessageId(),
118                 MBMessageFlagImpl.ANSWER_FLAG);
119         }
120         catch (NoSuchMessageFlagException nsmfe) {
121         }
122 
123         MBMessageFlag answerMessageFlag =
124             mbMessageFlagPersistence.fetchByU_M_F(
125                 rootMessage.getUserId(), rootMessage.getMessageId(),
126                 MBMessageFlagImpl.ANSWER_FLAG);
127 
128         if (answerMessageFlag == null) {
129             return;
130         }
131 
132         int answerFlagsCount = mbMessageFlagFinder.countByT_F(
133             message.getThreadId(), MBMessageFlagImpl.ANSWER_FLAG);
134 
135         if (answerFlagsCount == 0) {
136             answerMessageFlag.setFlag(MBMessageFlagImpl.QUESTION_FLAG);
137 
138             mbMessageFlagPersistence.update(answerMessageFlag, false);
139         }
140     }
141 
142 }