001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.search.facet;
016    
017    import com.liferay.portal.kernel.json.JSONObject;
018    import com.liferay.portal.kernel.search.BooleanClause;
019    import com.liferay.portal.kernel.search.BooleanClauseFactoryUtil;
020    import com.liferay.portal.kernel.search.BooleanClauseOccur;
021    import com.liferay.portal.kernel.search.SearchContext;
022    import com.liferay.portal.kernel.search.facet.config.FacetConfiguration;
023    import com.liferay.portal.kernel.search.filter.Filter;
024    import com.liferay.portal.kernel.util.GetterUtil;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.Validator;
027    
028    /**
029     * @author Raymond Aug??
030     */
031    public class SimpleFacet extends BaseFacet {
032    
033            public SimpleFacet(SearchContext searchContext) {
034                    super(searchContext);
035            }
036    
037            @Override
038            protected BooleanClause<Filter> doGetFacetFilterBooleanClause() {
039                    SearchContext searchContext = getSearchContext();
040    
041                    FacetConfiguration facetConfiguration = getFacetConfiguration();
042    
043                    JSONObject dataJSONObject = facetConfiguration.getData();
044    
045                    String value = StringPool.BLANK;
046    
047                    if (isStatic() && dataJSONObject.has("value")) {
048                            value = dataJSONObject.getString("value");
049                    }
050    
051                    String valueParam = GetterUtil.getString(
052                            searchContext.getAttribute(getFieldId()));
053    
054                    if (!isStatic() && Validator.isNotNull(valueParam)) {
055                            value = valueParam;
056                    }
057    
058                    if (Validator.isNull(value)) {
059                            return null;
060                    }
061    
062                    return BooleanClauseFactoryUtil.createFilter(
063                            searchContext, getFieldName(), value, BooleanClauseOccur.MUST);
064            }
065    
066    }