001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.messageboards.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.security.permission.ActionKeys;
020    import com.liferay.portlet.messageboards.NoSuchMessageFlagException;
021    import com.liferay.portlet.messageboards.model.MBMessage;
022    import com.liferay.portlet.messageboards.model.MBMessageFlag;
023    import com.liferay.portlet.messageboards.model.MBMessageFlagConstants;
024    import com.liferay.portlet.messageboards.service.base.MBMessageFlagServiceBaseImpl;
025    import com.liferay.portlet.messageboards.service.permission.MBMessagePermission;
026    
027    import java.util.Date;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     */
032    public class MBMessageFlagServiceImpl extends MBMessageFlagServiceBaseImpl {
033    
034            public void addAnswerFlag(long messageId)
035                    throws PortalException, SystemException {
036    
037                    MBMessage message = mbMessagePersistence.findByPrimaryKey(messageId);
038    
039                    if (message.isRoot()) {
040                            return;
041                    }
042    
043                    MBMessage rootMessage = mbMessagePersistence.findByPrimaryKey(
044                            message.getRootMessageId());
045    
046                    MBMessagePermission.check(
047                            getPermissionChecker(), rootMessage.getMessageId(),
048                            ActionKeys.UPDATE);
049    
050                    MBMessageFlag questionMessageFlag =
051                            mbMessageFlagPersistence.fetchByU_M_F(
052                                    rootMessage.getUserId(), rootMessage.getMessageId(),
053                                    MBMessageFlagConstants.QUESTION_FLAG);
054    
055                    MBMessageFlag answerMessageFlag =
056                            mbMessageFlagPersistence.fetchByU_M_F(
057                                    rootMessage.getUserId(), rootMessage.getMessageId(),
058                                    MBMessageFlagConstants.ANSWER_FLAG);
059    
060                    if ((questionMessageFlag != null) && (answerMessageFlag == null)) {
061                            questionMessageFlag.setFlag(MBMessageFlagConstants.ANSWER_FLAG);
062    
063                            mbMessageFlagPersistence.update(questionMessageFlag, false);
064                    }
065    
066                    MBMessageFlag messageFlag = mbMessageFlagPersistence.fetchByU_M_F(
067                            message.getUserId(), message.getMessageId(),
068                            MBMessageFlagConstants.ANSWER_FLAG);
069    
070                    if (messageFlag == null) {
071                            long messageFlagId = counterLocalService.increment();
072    
073                            messageFlag = mbMessageFlagPersistence.create(messageFlagId);
074    
075                            messageFlag.setUserId(message.getUserId());
076                            messageFlag.setModifiedDate(new Date());
077                            messageFlag.setThreadId(message.getThreadId());
078                            messageFlag.setMessageId(message.getMessageId());
079                            messageFlag.setFlag(MBMessageFlagConstants.ANSWER_FLAG);
080    
081                            mbMessageFlagPersistence.update(messageFlag, false);
082                    }
083            }
084    
085            public void deleteAnswerFlag(long messageId)
086                    throws PortalException, SystemException {
087    
088                    MBMessage message = mbMessagePersistence.findByPrimaryKey(messageId);
089    
090                    if (message.isRoot()) {
091                            return;
092                    }
093    
094                    MBMessage rootMessage = mbMessagePersistence.findByPrimaryKey(
095                            message.getRootMessageId());
096    
097                    MBMessagePermission.check(
098                            getPermissionChecker(), rootMessage.getMessageId(),
099                            ActionKeys.UPDATE);
100    
101                    try {
102                            mbMessageFlagPersistence.removeByU_M_F(
103                                    message.getUserId(), message.getMessageId(),
104                                    MBMessageFlagConstants.ANSWER_FLAG);
105                    }
106                    catch (NoSuchMessageFlagException nsmfe) {
107                    }
108    
109                    MBMessageFlag answerMessageFlag =
110                            mbMessageFlagPersistence.fetchByU_M_F(
111                                    rootMessage.getUserId(), rootMessage.getMessageId(),
112                                    MBMessageFlagConstants.ANSWER_FLAG);
113    
114                    if (answerMessageFlag == null) {
115                            return;
116                    }
117    
118                    int answerFlagsCount = mbMessageFlagPersistence.countByT_F(
119                            message.getThreadId(), MBMessageFlagConstants.ANSWER_FLAG);
120    
121                    if (answerFlagsCount == 1) {
122                            answerMessageFlag.setFlag(MBMessageFlagConstants.QUESTION_FLAG);
123    
124                            mbMessageFlagPersistence.update(answerMessageFlag, false);
125                    }
126            }
127    
128    }