001    /**
002     * Copyright (c) 2000-2011 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(
066                                    searchContext.getAttribute(getFieldName())));
067    
068                    if (!isStatic() && (valuesParam != null) && (valuesParam.length > 0)) {
069                            values = valuesParam;
070                    }
071    
072                    if ((values == null) || (values.length == 0)) {
073                            return null;
074                    }
075    
076                    BooleanQuery facetQuery = BooleanQueryFactoryUtil.create(searchContext);
077    
078                    for (String value : values) {
079                            FacetValueValidator facetValueValidator = getFacetValueValidator();
080    
081                            if ((searchContext.getUserId() > 0) &&
082                                    (!facetValueValidator.check(searchContext, value))) {
083    
084                                    continue;
085                            }
086    
087                            TermQuery termQuery = TermQueryFactoryUtil.create(
088                                    searchContext, getFieldName(), value);
089    
090                            try {
091                                    facetQuery.add(termQuery, BooleanClauseOccur.SHOULD);
092                            }
093                            catch (ParseException pe) {
094                                    _log.error(pe, pe);
095                            }
096                    }
097    
098                    if (facetQuery.clauses().isEmpty()) {
099                            return null;
100                    }
101    
102                    return BooleanClauseFactoryUtil.create(
103                            facetQuery, BooleanClauseOccur.MUST.getName());
104            }
105    
106            private static final Log _log = LogFactoryUtil.getLog(
107                    MultiValueFacet.class);
108    
109    }