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