Example usage for com.liferay.portal.kernel.search SortFactoryUtil getSort

List of usage examples for com.liferay.portal.kernel.search SortFactoryUtil getSort

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.search SortFactoryUtil getSort.

Prototype

public static Sort getSort(Class<?> clazz, int type, String orderByCol, String orderByType) 

Source Link

Usage

From source file:com.liferay.asset.tags.admin.web.internal.display.context.AssetTagsDisplayContext.java

License:Open Source License

public SearchContainer getTagsSearchContainer() throws PortalException {
    if (_tagsSearchContainer != null) {
        return _tagsSearchContainer;
    }//from  w  ww  .j a v a 2s  . c  o  m

    SearchContainer tagsSearchContainer = new SearchContainer(_renderRequest, _renderResponse.createRenderURL(),
            null, "there-are-no-tags");

    String keywords = getKeywords();

    if (Validator.isNull(keywords)) {
        if (isShowAddButton()) {
            tagsSearchContainer.setEmptyResultsMessage("there-are-no-tags.-you-can-add-a-tag-by-clicking-the-"
                    + "plus-button-on-the-bottom-right-corner");
            tagsSearchContainer
                    .setEmptyResultsMessageCssClass("taglib-empty-result-message-header-has-plus-btn");
        }
    } else {
        tagsSearchContainer.setSearch(true);
    }

    tagsSearchContainer.setRowChecker(new EmptyOnClickRowChecker(_renderResponse));

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

    if (Validator.isNotNull(keywords)) {
        Sort sort = null;

        String orderByCol = getOrderByCol();

        if (orderByCol.equals("name")) {
            sort = SortFactoryUtil.getSort(AssetTag.class, Sort.STRING_TYPE, Field.NAME, getOrderByType());
        } else if (orderByCol.equals("usages")) {
            sort = SortFactoryUtil.getSort(AssetTag.class, Sort.INT_TYPE, "assetCount_Number",
                    getOrderByType());
        }

        BaseModelSearchResult<AssetTag> baseModelSearchResult = AssetTagLocalServiceUtil.searchTags(
                new long[] { themeDisplay.getScopeGroupId() }, keywords, tagsSearchContainer.getStart(),
                tagsSearchContainer.getEnd(), sort);

        tagsSearchContainer.setResults(baseModelSearchResult.getBaseModels());
        tagsSearchContainer.setTotal(baseModelSearchResult.getLength());
    } else {
        String orderByCol = getOrderByCol();

        tagsSearchContainer.setOrderByCol(orderByCol);

        OrderByComparator<AssetTag> orderByComparator = null;

        boolean orderByAsc = false;

        String orderByType = getOrderByType();

        if (orderByType.equals("asc")) {
            orderByAsc = true;
        }

        if (orderByCol.equals("name")) {
            orderByComparator = new AssetTagNameComparator(orderByAsc);
        } else if (orderByCol.equals("usages")) {
            orderByComparator = new AssetTagAssetCountComparator(orderByAsc);
        }

        tagsSearchContainer.setOrderByComparator(orderByComparator);

        tagsSearchContainer.setOrderByType(orderByType);

        long scopeGroupId = themeDisplay.getScopeGroupId();

        int tagsCount = AssetTagServiceUtil.getTagsCount(scopeGroupId, keywords);

        tagsSearchContainer.setTotal(tagsCount);

        List<AssetTag> tags = AssetTagServiceUtil.getTags(scopeGroupId, StringPool.BLANK,
                tagsSearchContainer.getStart(), tagsSearchContainer.getEnd(),
                tagsSearchContainer.getOrderByComparator());

        tagsSearchContainer.setResults(tags);
    }

    _tagsSearchContainer = tagsSearchContainer;

    return _tagsSearchContainer;
}