001
014
015 package com.liferay.portal.kernel.search.facet;
016
017 import com.liferay.portal.kernel.json.JSONArray;
018 import com.liferay.portal.kernel.json.JSONObject;
019 import com.liferay.portal.kernel.search.BooleanClause;
020 import com.liferay.portal.kernel.search.BooleanClauseFactoryUtil;
021 import com.liferay.portal.kernel.search.BooleanClauseOccur;
022 import com.liferay.portal.kernel.search.SearchContext;
023 import com.liferay.portal.kernel.search.TermRangeQuery;
024 import com.liferay.portal.kernel.search.TermRangeQueryFactoryUtil;
025 import com.liferay.portal.kernel.search.facet.config.FacetConfiguration;
026 import com.liferay.portal.kernel.search.facet.util.RangeParserUtil;
027 import com.liferay.portal.kernel.util.GetterUtil;
028 import com.liferay.portal.kernel.util.StringPool;
029 import com.liferay.portal.kernel.util.Validator;
030
031
034 public class RangeFacet extends BaseFacet {
035
036 public RangeFacet(SearchContext searchContext) {
037 super(searchContext);
038 }
039
040 @Override
041 protected BooleanClause doGetFacetClause() {
042 SearchContext searchContext = getSearchContext();
043
044 FacetConfiguration facetConfiguration = getFacetConfiguration();
045
046 JSONObject dataJSONObject = facetConfiguration.getData();
047
048 String start = StringPool.BLANK;
049 String end = StringPool.BLANK;
050
051 if (isStatic() && dataJSONObject.has("ranges")) {
052 JSONArray rangesJSONArray = dataJSONObject.getJSONArray("ranges");
053
054 JSONObject rangeJSONObject = rangesJSONArray.getJSONObject(0);
055
056 String rangeString = rangeJSONObject.getString("range");
057
058 String[] range = RangeParserUtil.parserRange(rangeString);
059
060 start = range[0];
061 end = range[1];
062 }
063
064 String fieldName = getFieldName();
065
066 String rangeParam = GetterUtil.getString(
067 searchContext.getAttribute(fieldName));
068
069 if (!isStatic() && Validator.isNotNull(rangeParam)) {
070 String[] range = RangeParserUtil.parserRange(rangeParam);
071
072 start = range[0];
073 end = range[1];
074 }
075
076 if (Validator.isNull(start) && Validator.isNull(end)) {
077 return null;
078 }
079
080 if (Validator.isNotNull(start) && Validator.isNotNull(end) &&
081 (start.compareTo(end) > 0)) {
082
083 throw new IllegalArgumentException(
084 "End value must be greater than start value");
085 }
086
087 String startString = StringPool.STAR;
088
089 if (Validator.isNotNull(start)) {
090 startString = start;
091 }
092
093 String endString = StringPool.STAR;
094
095 if (Validator.isNotNull(end)) {
096 endString = end;
097 }
098
099 TermRangeQuery facetTermRangeQuery = TermRangeQueryFactoryUtil.create(
100 searchContext, fieldName, startString, endString, true, true);
101
102 return BooleanClauseFactoryUtil.create(
103 searchContext, facetTermRangeQuery,
104 BooleanClauseOccur.MUST.getName());
105 }
106
107 }