001    /**
002     * Copyright (c) 2000-2012 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.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.search.BooleanClause;
022    import com.liferay.portal.kernel.search.BooleanClauseFactoryUtil;
023    import com.liferay.portal.kernel.search.BooleanClauseOccur;
024    import com.liferay.portal.kernel.search.BooleanQuery;
025    import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
026    import com.liferay.portal.kernel.search.ParseException;
027    import com.liferay.portal.kernel.search.SearchContext;
028    import com.liferay.portal.kernel.search.TermQuery;
029    import com.liferay.portal.kernel.search.TermQueryFactoryUtil;
030    import com.liferay.portal.kernel.search.facet.config.FacetConfiguration;
031    import com.liferay.portal.kernel.search.facet.util.FacetValueValidator;
032    import com.liferay.portal.kernel.util.GetterUtil;
033    import com.liferay.portal.kernel.util.StringUtil;
034    
035    /**
036     * @author Raymond Augé
037     */
038    public class MultiValueFacet extends BaseFacet {
039    
040            public MultiValueFacet(SearchContext searchContext) {
041                    super(searchContext);
042            }
043    
044            @Override
045            protected BooleanClause doGetFacetClause() {
046                    SearchContext searchContext = getSearchContext();
047    
048                    FacetConfiguration facetConfiguration = getFacetConfiguration();
049    
050                    JSONObject dataJSONObject = facetConfiguration.getData();
051    
052                    String[] values = null;
053    
054                    if (isStatic() && dataJSONObject.has("values")) {
055                            JSONArray valuesJSONArray = dataJSONObject.getJSONArray("values");
056    
057                            values = new String[valuesJSONArray.length()];
058    
059                            for (int i = 0; i < valuesJSONArray.length(); i++) {
060                                    values[i] = valuesJSONArray.getString(i);
061                            }
062                    }
063    
064                    String[] valuesParam = StringUtil.split(
065                            GetterUtil.getString(searchContext.getAttribute(getFieldName())));
066    
067                    if (!isStatic() && (valuesParam != null) && (valuesParam.length > 0)) {
068                            values = valuesParam;
069                    }
070    
071                    if ((values == null) || (values.length == 0)) {
072                            return null;
073                    }
074    
075                    BooleanQuery facetQuery = BooleanQueryFactoryUtil.create(searchContext);
076    
077                    for (String value : values) {
078                            FacetValueValidator facetValueValidator = getFacetValueValidator();
079    
080                            if ((searchContext.getUserId() > 0) &&
081                                    (!facetValueValidator.check(searchContext, value))) {
082    
083                                    continue;
084                            }
085    
086                            TermQuery termQuery = TermQueryFactoryUtil.create(
087                                    searchContext, getFieldName(), value);
088    
089                            try {
090                                    facetQuery.add(termQuery, BooleanClauseOccur.SHOULD);
091                            }
092                            catch (ParseException pe) {
093                                    _log.error(pe, pe);
094                            }
095                    }
096    
097                    if (!facetQuery.hasClauses()) {
098                            return null;
099                    }
100    
101                    return BooleanClauseFactoryUtil.create(
102                            facetQuery, BooleanClauseOccur.MUST.getName());
103            }
104    
105            private static Log _log = LogFactoryUtil.getLog(MultiValueFacet.class);
106    
107    }