com.liferay.mentions.portlet.MentionsPortlet.java Source code

Java tutorial

Introduction

Here is the source code for com.liferay.mentions.portlet.MentionsPortlet.java

Source

/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.mentions.portlet;

import com.liferay.mentions.util.MentionsUserFinderUtil;
import com.liferay.mentions.util.MentionsUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.json.JSONArray;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.servlet.ServletResponseUtil;
import com.liferay.portal.kernel.util.ContentTypes;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.User;
import com.liferay.portal.theme.PortletDisplay;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import com.liferay.portlet.social.util.SocialInteractionsConfiguration;
import com.liferay.portlet.social.util.SocialInteractionsConfigurationUtil;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;

import java.util.List;

import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Ivn Zaera
 * @author Sergio Gonzlez
 */
public class MentionsPortlet extends MVCPortlet {

    @Override
    public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) {

        try {
            ThemeDisplay themeDisplay = (ThemeDisplay) resourceRequest.getAttribute(WebKeys.THEME_DISPLAY);

            if (!MentionsUtil.isMentionsEnabled(themeDisplay.getSiteGroupId())) {

                return;
            }

            HttpServletRequest request = PortalUtil.getHttpServletRequest(resourceRequest);

            JSONArray jsonArray = getJSONArray(request);

            HttpServletResponse response = PortalUtil.getHttpServletResponse(resourceResponse);

            response.setContentType(ContentTypes.APPLICATION_JSON);

            ServletResponseUtil.write(response, jsonArray.toString());
        } catch (Exception e) {
            _log.error(e, e);
        }

        return;
    }

    protected JSONArray getJSONArray(HttpServletRequest request) throws PortalException {

        JSONArray jsonArray = JSONFactoryUtil.createJSONArray();

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

        PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();

        SocialInteractionsConfiguration socialInteractionsConfiguration = SocialInteractionsConfigurationUtil
                .getSocialInteractionsConfiguration(themeDisplay.getCompanyId(), portletDisplay.getId());

        String query = ParamUtil.getString(request, "query");

        List<User> users = MentionsUserFinderUtil.getUsers(themeDisplay.getCompanyId(), themeDisplay.getUserId(),
                query, socialInteractionsConfiguration);

        for (User user : users) {
            if (user.isDefaultUser() || (themeDisplay.getUserId() == user.getUserId())) {

                continue;
            }

            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();

            jsonObject.put("fullName", user.getFullName());
            jsonObject.put("portraitURL", user.getPortraitURL(themeDisplay));
            jsonObject.put("profileURL", user.getDisplayURL(themeDisplay));
            jsonObject.put("screenName", user.getScreenName());

            jsonArray.put(jsonObject);
        }

        return jsonArray;
    }

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

}