package org.xcolab.jspTags.discussion.actions;

import com.ext.portlet.model.Contest;
import com.ext.portlet.service.ProposalLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.search.Indexer;
import com.liferay.portal.kernel.search.IndexerRegistryUtil;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.User;
import com.liferay.portal.theme.ThemeDisplay;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.xcolab.activityEntry.discussion.DiscussionAddCommentActivityEntry;
import org.xcolab.activityEntry.discussion.DiscussionAddProposalCommentActivityEntry;
import org.xcolab.analytics.AnalyticsUtil;
import org.xcolab.client.activities.helper.ActivityEntryHelper;
import org.xcolab.client.admin.enums.ConfigurationAttributeKey;
import org.xcolab.client.comment.CommentClient;
import org.xcolab.client.comment.exceptions.ThreadNotFoundException;
import org.xcolab.client.comment.pojo.Comment;
import org.xcolab.client.comment.pojo.CommentThread;
import org.xcolab.client.filtering.FilteringClient;
import org.xcolab.client.filtering.exceptions.FilteredEntryNotFoundException;
import org.xcolab.client.filtering.pojo.FilteredEntry;
import org.xcolab.client.sharedcolab.SharedColabClient;
import org.xcolab.jspTags.discussion.DiscussionPermissions;
import org.xcolab.jspTags.discussion.exceptions.DiscussionAuthorizationException;
import org.xcolab.jspTags.discussion.wrappers.NewMessageWrapper;
import org.xcolab.liferay.SharedColabUtil;
import org.xcolab.util.html.HtmlUtil;
import org.xcolab.utils.LinkUtils;

import java.io.IOException;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.servlet.http.Cookie;

@Controller
@RequestMapping("view")
public class AddDiscussionMessageActionController extends BaseDiscussionsActionController {

    private final static Log _log = LogFactoryUtil
            .getLog(AddDiscussionMessageActionController.class);

    private final static String COMMENT_ANALYTICS_KEY = "COMMENT_CONTEST_ENTRIES";
    private final static String COMMENT_ANALYTICS_CATEGORY = "User";
    private final static String COMMENT_ANALYTICS_ACTION = "Comment on contest entry";
    private final static String COMMENT_ANALYTICS_LABEL = "";

    @RequestMapping(params = "action=addDiscussionMessage")
    public void handleAction(ActionRequest request, ActionResponse response,
            NewMessageWrapper newMessage)
            throws IOException, PortalException, SystemException, DiscussionAuthorizationException {

        ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

        try {
            long threadId = Long.parseLong(newMessage.getThreadId());

            checkPermissions(request, "User isn't allowed to add comment", 0L);
            long userId = themeDisplay.getUser().getUserId();

            final String body = HtmlUtil
                    .cleanSome(newMessage.getDescription(), LinkUtils.getBaseUri(request));

            Comment comment = new Comment();
            comment.setContent(body);
            comment.setAuthorId(userId);
            comment.setThreadId(threadId);
            comment = CommentClient.createComment(comment);
            CommentThread commentThread = CommentClient.getThread(threadId);

            updateAnalyticsAndActivities(commentThread, comment, userId, request);

            if (!commentThread.getIsQuiet()) {

                if (commentThread.getCategory() == null) {
                    final Long proposalIdForThread = CommentClient
                            .getProposalIdForThread(commentThread.getThreadId());
                    if (proposalIdForThread != null && proposalIdForThread != 0L) {
                        ActivityEntryHelper.createActivityEntry(userId, commentThread.getThreadId(),
                                comment.getCommentId() + "",
                                new DiscussionAddProposalCommentActivityEntry());
                        Contest contest = ProposalLocalServiceUtil.getLatestProposalContest(proposalIdForThread);
                        SharedColabUtil.checkTriggerForAutoUserCreationInContest(contest.getContestPK(),userId);
                    }
                } else {
                    ActivityEntryHelper.createActivityEntry(userId,
                            commentThread.getCategory().getCategoryId(),
                            comment.getCommentId() + "",
                            new DiscussionAddCommentActivityEntry());
                }
            }
            if (ConfigurationAttributeKey.FILTER_PROFANITY.get()) {
                try {
                    FilteredEntry filteredEntry = FilteringClient
                            .getFilteredEntryByUuid(newMessage.getUuid());
                    filteredEntry.setSourceId(comment.getCommentId());
                    filteredEntry.setAuthorId(userId);
                    FilteringClient.updateFilteredEntry(filteredEntry);
                } catch (FilteredEntryNotFoundException ignored) {
                }
            }

            //delete the cached comment cookie, if it exists
            Cookie[] cookies = request.getCookies();
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                    if (cookie.getName().equals("proposal-comment-body")) {
                        cookie.setValue(null);
                        cookie.setMaxAge(0);
                        cookie.setPath("/");
                        response.addProperty(cookie);
                    }
                }
            }
        } catch (NumberFormatException e) {
            _log.warn(String.format(
                    "Could not convert discussionId %s and threadId %s to longs (userId = %d)",
                    newMessage.getDiscussionId(), newMessage.getThreadId(),
                    themeDisplay.getUserId()));
        } catch (ThreadNotFoundException ignored) {
        }

        redirectToReferrer(request, response);
    }

    @SuppressWarnings("OverlyBroadThrowsClause")
    public void updateAnalyticsAndActivities(CommentThread thread, Comment comment, long userId,
            ActionRequest request)
            throws SystemException, PortalException {
        // Update activity counter for user
        Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
        indexer.reindex(userId);

        int commentCount = CommentClient.countCommentsByAuthor(userId);
        if (commentCount > 0) {
            int analyticsValue = AnalyticsUtil.getAnalyticsValueForCount(commentCount);
            AnalyticsUtil.publishEvent(request, userId, COMMENT_ANALYTICS_KEY + analyticsValue,
                    COMMENT_ANALYTICS_CATEGORY,
                    COMMENT_ANALYTICS_ACTION,
                    COMMENT_ANALYTICS_LABEL,
                    analyticsValue);
        }
    }

    @Override
    public boolean isUserAllowed(DiscussionPermissions permissions, long additionalId) {
        return permissions.getCanAddComment();
    }
}

@@ -705,17 +705,16 @@
 apping;%0A
-%0A
 import o
@@ -1446,64 +1446,8 @@
 ry;%0A
-import org.xcolab.client.sharedcolab.SharedColabClient;%0A
 impo
@@ -1695,110 +1695,8 @@
 til;
-%0Aimport org.xcolab.util.html.HtmlUtil;%0Aimport org.xcolab.utils.LinkUtils;%0A%0Aimport java.io.IOException;
 %0A%0Aim
@@ -1799,16 +1799,44 @@
 .Cookie;
+%0Aimport java.io.IOException;
 %0A%0A@Contr
@@ -2991,24 +2991,176 @@
             
+// Since linebreaks are escaped by HtmlUtil%0A             String body = newMessage.getDescription().replaceAll(%22%5C%5Cr%5C%5Cn%7C%5C%5Cr%7C%5C%5Cn%22, %22%3C/br%3E%22);%0A            //
 final String
@@ -3175,37 +3175,16 @@
 HtmlUtil
-%0A                    
 .cleanSo
@@ -3243,25 +3243,24 @@
 (request));%0A
-%0A
             

Replace HtmlUtil function for cleaning comment input since it would crop linebreaks