001    /**
002     * Copyright (c) 2000-2013 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.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    /**
032     * @author Raymond Aug??
033     */
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 rangeParam = GetterUtil.getString(
065                            searchContext.getAttribute(getFieldId()));
066    
067                    if (!isStatic() && Validator.isNotNull(rangeParam)) {
068                            String[] range = RangeParserUtil.parserRange(rangeParam);
069    
070                            start = range[0];
071                            end = range[1];
072                    }
073    
074                    if (Validator.isNull(start) && Validator.isNull(end)) {
075                            return null;
076                    }
077    
078                    if (Validator.isNotNull(start) && Validator.isNotNull(end) &&
079                            (start.compareTo(end) > 0)) {
080    
081                            throw new IllegalArgumentException(
082                                    "End value must be greater than start value");
083                    }
084    
085                    String startString = StringPool.STAR;
086    
087                    if (Validator.isNotNull(start)) {
088                            startString = start;
089                    }
090    
091                    String endString = StringPool.STAR;
092    
093                    if (Validator.isNotNull(end)) {
094                            endString = end;
095                    }
096    
097                    TermRangeQuery facetTermRangeQuery = TermRangeQueryFactoryUtil.create(
098                            searchContext, getFieldName(), startString, endString, true, true);
099    
100                    return BooleanClauseFactoryUtil.create(
101                            searchContext, facetTermRangeQuery,
102                            BooleanClauseOccur.MUST.getName());
103            }
104    
105    }