org.gnenc.yams.portlet.search.OrganizationSearch.java Source code

Java tutorial

Introduction

Here is the source code for org.gnenc.yams.portlet.search.OrganizationSearch.java

Source

/**
 *  Copyright (c) 2012-2013 Educational Service Unit 10.
 *
 *  This file is part of the YAMS portlet.
 *
 *  YAMS portlet is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  YAMS portlet 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the YAMS portlet.  If not, see <http://www.gnu.org/licenses/>.
 **/
package org.gnenc.yams.portlet.search;

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.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portlet.PortalPreferences;
import com.liferay.portlet.PortletPreferencesFactoryUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import org.gnenc.yams.model.Account;
import org.gnenc.yams.portlet.util.PortletKeys;

/**
 * Modeled after {@link com.liferay.portlet.Organizationsadmin.search.OrganizationSearch}
 * written by Brian Wing Shun Chan
 *
 * @author Drew A. Blessing
 */
public class OrganizationSearch extends SearchContainer<Account> {

    static List<String> headerNames = new ArrayList<String>();
    static Map<String, String> orderableHeaders = new HashMap<String, String>();

    static {
        headerNames.add("name");
        headerNames.add("location");

        orderableHeaders.put("fname", "name");
        orderableHeaders.put("location", "location");
    }

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

    public OrganizationSearch(PortletRequest portletRequest, PortletURL iteratorURL) {
        this(portletRequest, DEFAULT_CUR_PARAM, iteratorURL);
    }

    public OrganizationSearch(PortletRequest portletRequest, String curParam, PortletURL iteratorURL) {

        super(portletRequest, new OrganizationDisplayTerms(portletRequest),
                new OrganizationSearchTerms(portletRequest), curParam, DEFAULT_DELTA, iteratorURL, headerNames,
                EMPTY_RESULTS_MESSAGE);

        OrganizationDisplayTerms displayTerms = (OrganizationDisplayTerms) getDisplayTerms();

        iteratorURL.setParameter(OrganizationDisplayTerms.NAME, displayTerms.getName());
        iteratorURL.setParameter(OrganizationDisplayTerms.LOCATION, displayTerms.getLocation());

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

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

            if (Validator.isNotNull(orderByCol) && Validator.isNotNull(orderByType)) {
                preferences.setValue(PortletKeys.SEARCH, "search-org-order-by-col", orderByCol);
                preferences.setValue(PortletKeys.SEARCH, "search-org-order-by-type", orderByType);
            } else {
                orderByCol = preferences.getValue(PortletKeys.SEARCH, "search-org-order-by-col", "cn");
                orderByType = preferences.getValue(PortletKeys.SEARCH, "search-org-order-by-type", "asc");
            }

            setOrderableHeaders(orderableHeaders);
            setOrderByCol(orderByCol);
            setOrderByType(orderByType);
        } catch (Exception e) {
            _log.error(e);
        }
    }

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

}