com.liferay.knowledgebase.admin.search.KBArticleSearch.java Source code

Java tutorial

Introduction

Here is the source code for com.liferay.knowledgebase.admin.search.KBArticleSearch.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.knowledgebase.admin.search;

import com.liferay.knowledgebase.model.KBArticle;
import com.liferay.knowledgebase.util.KnowledgeBaseUtil;
import com.liferay.knowledgebase.util.PortletKeys;
import com.liferay.portal.kernel.dao.search.SearchContainer;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portlet.PortalPreferences;
import com.liferay.portlet.PortletPreferencesFactoryUtil;

import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequest;
import javax.portlet.PortletURL;

/**
 * @author Peter Shin
 * @author Brian Wing Shun Chan
 */
public class KBArticleSearch extends SearchContainer<KBArticle> {

    public static final String EMPTY_RESULTS_MESSAGE = "no-articles-were-found";

    public KBArticleSearch(PortletRequest portletRequest, PortletURL iteratorURL) {

        super(portletRequest, new KBArticleDisplayTerms(portletRequest), new KBArticleSearchTerms(portletRequest),
                DEFAULT_CUR_PARAM, DEFAULT_DELTA, iteratorURL, null, EMPTY_RESULTS_MESSAGE);

        KBArticleDisplayTerms displayTerms = (KBArticleDisplayTerms) getDisplayTerms();

        iteratorURL.setParameter(KBArticleDisplayTerms.ANYTIME, String.valueOf(displayTerms.isAnytime()));
        iteratorURL.setParameter(KBArticleDisplayTerms.CONTENT, displayTerms.getContent());
        iteratorURL.setParameter(KBArticleDisplayTerms.END_DATE_DAY, String.valueOf(displayTerms.getEndDateDay()));
        iteratorURL.setParameter(KBArticleDisplayTerms.END_DATE_MONTH,
                String.valueOf(displayTerms.getEndDateMonth()));
        iteratorURL.setParameter(KBArticleDisplayTerms.END_DATE_YEAR,
                String.valueOf(displayTerms.getEndDateYear()));
        iteratorURL.setParameter(KBArticleDisplayTerms.START_DATE_DAY,
                String.valueOf(displayTerms.getStartDateDay()));
        iteratorURL.setParameter(KBArticleDisplayTerms.START_DATE_MONTH,
                String.valueOf(displayTerms.getStartDateMonth()));
        iteratorURL.setParameter(KBArticleDisplayTerms.START_DATE_YEAR,
                String.valueOf(displayTerms.getStartDateYear()));
        iteratorURL.setParameter(KBArticleDisplayTerms.STATUS, String.valueOf(displayTerms.getStatus()));
        iteratorURL.setParameter(KBArticleDisplayTerms.TITLE, displayTerms.getTitle());

        try {
            PortalPreferences preferences = PortletPreferencesFactoryUtil.getPortalPreferences(portletRequest);

            PortletPreferences portletPreferences = PortletPreferencesFactoryUtil.getPortletSetup(portletRequest);

            String portletOrderByCol = portletPreferences.getValue("kbArticlesOrderByCol", StringPool.BLANK);
            String portletOrderByType = portletPreferences.getValue("kbArticlesOrderByType", StringPool.BLANK);

            String oldOrderByCol = preferences.getValue(PortletKeys.KNOWLEDGE_BASE_ADMIN,
                    "kb-articles-order-by-col", portletOrderByCol);
            String oldOrderByType = preferences.getValue(PortletKeys.KNOWLEDGE_BASE_ADMIN,
                    "kb-articles-order-by-type", portletOrderByType);

            String orderByCol = ParamUtil.getString(portletRequest, "orderByCol", oldOrderByCol);
            String orderByType = ParamUtil.getString(portletRequest, "orderByType", oldOrderByType);

            if (!Validator.equals(orderByCol, oldOrderByCol) || !Validator.equals(orderByType, oldOrderByType)) {

                preferences.setValue(PortletKeys.KNOWLEDGE_BASE_ADMIN, "kb-articles-order-by-col", orderByCol);
                preferences.setValue(PortletKeys.KNOWLEDGE_BASE_ADMIN, "kb-articles-order-by-type", orderByType);

                KBArticleSearchTerms searchTerms = (KBArticleSearchTerms) getSearchTerms();

                searchTerms.setCurStartValues(new int[0]);
            }

            OrderByComparator<KBArticle> orderByComparator = KnowledgeBaseUtil
                    .getKBArticleOrderByComparator(orderByCol, orderByType);

            setOrderByCol(orderByCol);
            setOrderByType(orderByType);
            setOrderByComparator(orderByComparator);
        } catch (Exception e) {
            _log.error(e);
        }
    }

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

}