Skip to content

Commit

Permalink
LPS-114845 Add type to parameterized class usages
Browse files Browse the repository at this point in the history
  • Loading branch information
adolfopa committed Jun 3, 2020
1 parent f380f73 commit 6cff9e9
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 35 deletions.
Expand Up @@ -17,6 +17,7 @@
import com.liferay.knowledge.base.constants.KBCommentConstants;
import com.liferay.knowledge.base.constants.KBFolderConstants;
import com.liferay.knowledge.base.constants.KBPortletKeys;
import com.liferay.knowledge.base.model.KBArticle;
import com.liferay.knowledge.base.model.KBComment;
import com.liferay.knowledge.base.model.KBFolder;
import com.liferay.knowledge.base.model.KBTemplate;
Expand Down Expand Up @@ -94,7 +95,7 @@ public static List<KBFolder> getAlternateRootKBFolders(
return ListUtil.sort(kbFolders, new KBFolderNameComparator(false));
}

public static OrderByComparator getKBArticleOrderByComparator(
public static OrderByComparator<KBArticle> getKBArticleOrderByComparator(
String orderByCol, String orderByType) {

if (Validator.isNull(orderByCol) || Validator.isNull(orderByType)) {
Expand Down
Expand Up @@ -27,11 +27,13 @@
import com.liferay.knowledge.base.service.KBArticleServiceUtil;
import com.liferay.knowledge.base.service.KBFolderServiceUtil;
import com.liferay.knowledge.base.service.KBTemplateServiceUtil;
import com.liferay.knowledge.base.web.internal.KBUtil;
import com.liferay.knowledge.base.web.internal.search.EntriesChecker;
import com.liferay.knowledge.base.web.internal.search.KBObjectsSearch;
import com.liferay.knowledge.base.web.internal.security.permission.resource.AdminPermission;
import com.liferay.knowledge.base.web.internal.security.permission.resource.KBArticlePermission;
import com.liferay.knowledge.base.web.internal.security.permission.resource.KBFolderPermission;
import com.liferay.knowledge.base.web.internal.util.comparator.KBOrderByComparatorAdapter;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.search.SearchContainer;
import com.liferay.portal.kernel.exception.PortalException;
Expand Down Expand Up @@ -316,7 +318,7 @@ public String getOrderByType() {
return _searchContainer.getOrderByType();
}

public SearchContainer getSearchContainer() {
public SearchContainer<Object> getSearchContainer() {
return _searchContainer;
}

Expand Down Expand Up @@ -353,7 +355,7 @@ public boolean isShowInfoButton() {
return Validator.isNull(keywords);
}

private SearchContainer _createSearchContainer()
private SearchContainer<Object> _createSearchContainer()
throws PortalException, PortletException {

long kbFolderClassNameId = PortalUtil.getClassNameId(
Expand Down Expand Up @@ -383,14 +385,23 @@ private SearchContainer _createSearchContainer()
String keywords = _getKeywords();

if (Validator.isNotNull(keywords)) {
OrderByComparator<KBArticle> kbArticleOrderByComparator =
KBUtil.getKBArticleOrderByComparator(
_searchContainer.getOrderByCol(),
_searchContainer.getOrderByType());

_searchContainer.setOrderByComparator(
new KBOrderByComparatorAdapter<>(kbArticleOrderByComparator));

KBArticleSearchDisplay kbArticleSearchDisplay =
KBArticleServiceUtil.getKBArticleSearchDisplay(
_themeDisplay.getScopeGroupId(), keywords, keywords,
WorkflowConstants.STATUS_ANY, null, null, false, new int[0],
_searchContainer.getCur(), _searchContainer.getDelta(),
_searchContainer.getOrderByComparator());
kbArticleOrderByComparator);

_searchContainer.setResults(kbArticleSearchDisplay.getResults());
_searchContainer.setResults(
new ArrayList<>(kbArticleSearchDisplay.getResults()));
_searchContainer.setTotal(kbArticleSearchDisplay.getTotal());
}
else if (kbFolderView) {
Expand All @@ -406,16 +417,25 @@ else if (kbFolderView) {
_searchContainer.getOrderByComparator()));
}
else {
OrderByComparator<KBArticle> kbArticleOrderByComparator =
KBUtil.getKBArticleOrderByComparator(
_searchContainer.getOrderByCol(),
_searchContainer.getOrderByType());

_searchContainer.setOrderByComparator(
new KBOrderByComparatorAdapter<>(kbArticleOrderByComparator));

_searchContainer.setTotal(
KBArticleServiceUtil.getKBArticlesCount(
_themeDisplay.getScopeGroupId(), parentResourcePrimKey,
WorkflowConstants.STATUS_ANY));
_searchContainer.setResults(
KBArticleServiceUtil.getKBArticles(
_themeDisplay.getScopeGroupId(), parentResourcePrimKey,
WorkflowConstants.STATUS_ANY, _searchContainer.getStart(),
_searchContainer.getEnd(),
_searchContainer.getOrderByComparator()));
new ArrayList<>(
KBArticleServiceUtil.getKBArticles(
_themeDisplay.getScopeGroupId(), parentResourcePrimKey,
WorkflowConstants.STATUS_ANY,
_searchContainer.getStart(), _searchContainer.getEnd(),
kbArticleOrderByComparator)));
}

_searchContainer.setRowChecker(
Expand Down Expand Up @@ -500,7 +520,7 @@ private String _getTemplatePath() {
private final PortletConfig _portletConfig;
private final RenderRequest _renderRequest;
private final RenderResponse _renderResponse;
private SearchContainer _searchContainer;
private SearchContainer<Object> _searchContainer;
private final ThemeDisplay _themeDisplay;

}
Expand Up @@ -14,17 +14,13 @@

package com.liferay.knowledge.base.web.internal.search;

import com.liferay.knowledge.base.constants.KBFolderConstants;
import com.liferay.knowledge.base.constants.KBPortletKeys;
import com.liferay.knowledge.base.web.internal.KBUtil;
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.portlet.PortalPreferences;
import com.liferay.portal.kernel.portlet.PortletPreferencesFactoryUtil;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.PortalUtil;

import java.util.Objects;

Expand Down Expand Up @@ -83,27 +79,8 @@ public KBObjectsSearch(
"kb-articles-order-by-type", orderByType);
}

long kbFolderClassNameId = PortalUtil.getClassNameId(
KBFolderConstants.getClassName());

long parentResourceClassNameId = ParamUtil.getLong(
portletRequest, "parentResourceClassNameId",
kbFolderClassNameId);

OrderByComparator<Object> orderByComparator = null;

if (parentResourceClassNameId == kbFolderClassNameId) {
orderByComparator = KBUtil.getKBObjectsOrderByComparator(
orderByCol, orderByType);
}
else {
orderByComparator = KBUtil.getKBArticleOrderByComparator(
orderByCol, orderByType);
}

setOrderByCol(orderByCol);
setOrderByType(orderByType);
setOrderByComparator(orderByComparator);
}
catch (Exception exception) {
_log.error(
Expand Down
@@ -0,0 +1,35 @@
/**
* 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.knowledge.base.web.internal.util.comparator;

import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.OrderByComparatorAdapter;

/**
* @author Adolfo Pérez
*/
public class KBOrderByComparatorAdapter<S, T>
extends OrderByComparatorAdapter<S, T> {

public KBOrderByComparatorAdapter(OrderByComparator<T> orderByComparator) {
super(orderByComparator);
}

@Override
public T adapt(S s) {
return (T)s;
}

}
Expand Up @@ -29,8 +29,14 @@ String kbArticleDisplayStyle = kbSectionPortletInstanceConfiguration.kbArticleDi
<portlet:param name="mvcPath" value="/section/view.jsp" />
</liferay-portlet:renderURL>

<%
KBObjectsSearch kbObjectsSearch = new KBObjectsSearch(renderRequest, iteratorURL);
kbObjectsSearch.setOrderByComparator(new KBOrderByComparatorAdapter<>(KBUtil.getKBArticleOrderByComparator(kbObjectsSearch.getOrderByCol(), kbObjectsSearch.getOrderByType())));
%>

<liferay-ui:search-container
searchContainer="<%= new KBObjectsSearch(renderRequest, iteratorURL) %>"
searchContainer="<%= kbObjectsSearch %>"
total="<%= KBArticleServiceUtil.getSectionsKBArticlesCount(scopeGroupId, kbArticlesSections, WorkflowConstants.STATUS_APPROVED) %>"
>
<liferay-ui:search-container-results
Expand Down

0 comments on commit 6cff9e9

Please sign in to comment.