/**
* 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.portal.search.web.internal.folder.facet.portlet;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;
import com.liferay.portal.kernel.search.SearchContext;
import com.liferay.portal.kernel.search.facet.Facet;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.ListUtil;
import com.liferay.portal.kernel.util.Portal;
import com.liferay.portal.search.web.internal.facet.display.builder.FolderSearchFacetDisplayBuilder;
import com.liferay.portal.search.web.internal.facet.display.context.FolderSearchFacetDisplayContext;
import com.liferay.portal.search.web.internal.facet.display.context.FolderTitleLookup;
import com.liferay.portal.search.web.internal.facet.display.context.FolderTitleLookupImpl;
import com.liferay.portal.search.web.internal.folder.facet.constants.FolderFacetPortletKeys;
import com.liferay.portal.search.web.internal.folder.facet.constants.FolderFacetWebKeys;
import com.liferay.portal.search.web.portlet.shared.search.PortletSharedSearchContributor;
import com.liferay.portal.search.web.portlet.shared.search.PortletSharedSearchRequest;
import com.liferay.portal.search.web.portlet.shared.search.PortletSharedSearchResponse;
import com.liferay.portal.search.web.portlet.shared.search.PortletSharedSearchSettings;
import java.io.IOException;
import java.util.Arrays;
import java.util.Optional;
import javax.portlet.Portlet;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
/**
* @author Lino Alves
*/
@Component(
immediate = true,
property = {
"com.liferay.portlet.add-default-resource=true",
"com.liferay.portlet.css-class-wrapper=portlet-folder-facet",
"com.liferay.portlet.display-category=category.search",
"com.liferay.portlet.icon=/icons/search.png",
"com.liferay.portlet.instanceable=true",
"com.liferay.portlet.layout-cacheable=true",
"com.liferay.portlet.preferences-owned-by-group=true",
"com.liferay.portlet.private-request-attributes=false",
"com.liferay.portlet.private-session-attributes=false",
"com.liferay.portlet.restore-current-view=false",
"com.liferay.portlet.use-default-template=true",
"javax.portlet.display-name=Folder Facet",
"javax.portlet.expiration-cache=0",
"javax.portlet.init-param.template-path=/",
"javax.portlet.init-param.view-template=/folder/facet/view.jsp",
"javax.portlet.name=" + FolderFacetPortletKeys.FOLDER_FACET,
"javax.portlet.resource-bundle=content.Language",
"javax.portlet.security-role-ref=guest,power-user,user",
"javax.portlet.supports.mime-type=text/html"
},
service = {Portlet.class, PortletSharedSearchContributor.class}
)
public class FolderFacetPortlet
extends MVCPortlet implements PortletSharedSearchContributor {
@Override
public void contribute(
PortletSharedSearchSettings portletSharedSearchSettings) {
FolderFacetPortletPreferences folderFacetPortletPreferences =
new FolderFacetPortletPreferencesImpl(
portletSharedSearchSettings.getPortletPreferences());
Facet facet = buildFacet(
folderFacetPortletPreferences, portletSharedSearchSettings);
portletSharedSearchSettings.addFacet(facet);
}
@Override
public void render(
RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
PortletSharedSearchResponse portletSharedSearchResponse =
portletSharedSearchRequest.search(renderRequest);
FolderSearchFacetDisplayContext folderSearchFacetDisplayContext =
buildDisplayContext(portletSharedSearchResponse, renderRequest);
renderRequest.setAttribute(
FolderFacetWebKeys.DISPLAY_CONTEXT,
folderSearchFacetDisplayContext);
super.render(renderRequest, renderResponse);
}
protected FolderSearchFacetDisplayContext buildDisplayContext(
PortletSharedSearchResponse portletSharedSearchResponse,
RenderRequest renderRequest) {
Facet facet = portletSharedSearchResponse.getFacet(getFieldName());
FolderTitleLookup folderTitleLookup = new FolderTitleLookupImpl(
portal.getHttpServletRequest(renderRequest));
FolderFacetConfiguration folderFacetConfiguration =
new FolderFacetConfigurationImpl(facet.getFacetConfiguration());
FolderFacetPortletPreferences folderFacetPortletPreferences =
new FolderFacetPortletPreferencesImpl(
portletSharedSearchResponse.getPortletPreferences(
renderRequest));
FolderSearchFacetDisplayBuilder folderSearchFacetDisplayBuilder =
new FolderSearchFacetDisplayBuilder();
folderSearchFacetDisplayBuilder.setFacet(facet);
folderSearchFacetDisplayBuilder.setFolderTitleLookup(folderTitleLookup);
folderSearchFacetDisplayBuilder.setFrequenciesVisible(
folderFacetPortletPreferences.isFrequenciesVisible());
folderSearchFacetDisplayBuilder.setFrequencyThreshold(
folderFacetConfiguration.getFrequencyThreshold());
folderSearchFacetDisplayBuilder.setMaxTerms(
folderFacetConfiguration.getMaxTerms());
String parameterName = folderFacetPortletPreferences.getParameterName();
folderSearchFacetDisplayBuilder.setParameterName(parameterName);
Optional<String[]> parameterValuesOptional =
portletSharedSearchResponse.getParameterValues(
parameterName, renderRequest);
parameterValuesOptional.ifPresent(
folderSearchFacetDisplayBuilder::setParameterValues);
return folderSearchFacetDisplayBuilder.build();
}
protected Facet buildFacet(
FolderFacetPortletPreferences folderFacetPortletPreferences,
PortletSharedSearchSettings portletSharedSearchSettings) {
FolderFacetBuilder folderFacetBuilder = new FolderFacetBuilder(
folderFacetFactory);
folderFacetBuilder.setFrequencyThreshold(
folderFacetPortletPreferences.getFrequencyThreshold());
folderFacetBuilder.setMaxTerms(
folderFacetPortletPreferences.getMaxTerms());
folderFacetBuilder.setSearchContext(
portletSharedSearchSettings.getSearchContext());
Optional<String[]> parameterValuesOptional =
portletSharedSearchSettings.getParameterValues(
folderFacetPortletPreferences.getParameterName());
Optional<long[]> foldersOptional = parameterValuesOptional.map(
parameterValues -> ListUtil.toLongArray(
Arrays.asList(parameterValues), GetterUtil::getLong));
foldersOptional.ifPresent(folderFacetBuilder::setSelectedFolders);
return folderFacetBuilder.build();
}
protected String getFieldName() {
Facet facet = folderFacetFactory.newInstance(new SearchContext());
return facet.getFieldName();
}
protected FolderFacetFactory folderFacetFactory = new FolderFacetFactory();
@Reference
protected Portal portal;
@Reference
protected PortletSharedSearchRequest portletSharedSearchRequest;
}