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.JSONArray;
018    import com.liferay.portal.kernel.json.JSONFactoryUtil;
019    import com.liferay.portal.kernel.json.JSONObject;
020    import com.liferay.portal.kernel.search.BooleanClause;
021    import com.liferay.portal.kernel.search.BooleanClauseFactoryUtil;
022    import com.liferay.portal.kernel.search.BooleanClauseOccur;
023    import com.liferay.portal.kernel.search.SearchContext;
024    import com.liferay.portal.kernel.search.facet.config.FacetConfiguration;
025    import com.liferay.portal.kernel.search.facet.util.FacetValueValidator;
026    import com.liferay.portal.kernel.search.filter.Filter;
027    import com.liferay.portal.kernel.search.filter.TermsFilter;
028    import com.liferay.portal.kernel.util.ArrayUtil;
029    import com.liferay.portal.kernel.util.GetterUtil;
030    import com.liferay.portal.kernel.util.StringUtil;
031    
032    /**
033     * @author Raymond Aug??
034     */
035    public class MultiValueFacet extends BaseFacet {
036    
037            public MultiValueFacet(SearchContext searchContext) {
038                    super(searchContext);
039            }
040    
041            public void setValues(boolean[] values) {
042                    if (ArrayUtil.isEmpty(values)) {
043                            return;
044                    }
045    
046                    JSONArray valuesJSONArray = JSONFactoryUtil.createJSONArray();
047    
048                    for (boolean value : values) {
049                            valuesJSONArray.put(value);
050                    }
051    
052                    doSetValues(valuesJSONArray);
053            }
054    
055            public void setValues(double[] values) {
056                    if (ArrayUtil.isEmpty(values)) {
057                            return;
058                    }
059    
060                    JSONArray valuesJSONArray = JSONFactoryUtil.createJSONArray();
061    
062                    for (double value : values) {
063                            valuesJSONArray.put(value);
064                    }
065    
066                    doSetValues(valuesJSONArray);
067            }
068    
069            public void setValues(int[] values) {
070                    if (ArrayUtil.isEmpty(values)) {
071                            return;
072                    }
073    
074                    JSONArray valuesJSONArray = JSONFactoryUtil.createJSONArray();
075    
076                    for (int value : values) {
077                            valuesJSONArray.put(value);
078                    }
079    
080                    doSetValues(valuesJSONArray);
081            }
082    
083            public void setValues(JSONArray values) {
084                    doSetValues(values);
085            }
086    
087            public void setValues(JSONObject[] values) {
088                    if (ArrayUtil.isEmpty(values)) {
089                            return;
090                    }
091    
092                    JSONArray valuesJSONArray = JSONFactoryUtil.createJSONArray();
093    
094                    for (JSONObject value : values) {
095                            valuesJSONArray.put(value);
096                    }
097    
098                    doSetValues(valuesJSONArray);
099            }
100    
101            public void setValues(long[] values) {
102                    if (ArrayUtil.isEmpty(values)) {
103                            return;
104                    }
105    
106                    JSONArray valuesJSONArray = JSONFactoryUtil.createJSONArray();
107    
108                    for (long value : values) {
109                            valuesJSONArray.put(value);
110                    }
111    
112                    doSetValues(valuesJSONArray);
113            }
114    
115            public void setValues(String[] values) {
116                    if (ArrayUtil.isEmpty(values)) {
117                            return;
118                    }
119    
120                    JSONArray valuesJSONArray = JSONFactoryUtil.createJSONArray();
121    
122                    for (String value : values) {
123                            valuesJSONArray.put(value);
124                    }
125    
126                    doSetValues(valuesJSONArray);
127            }
128    
129            @Override
130            protected BooleanClause<Filter> doGetFacetFilterBooleanClause() {
131                    SearchContext searchContext = getSearchContext();
132    
133                    FacetConfiguration facetConfiguration = getFacetConfiguration();
134    
135                    JSONObject dataJSONObject = facetConfiguration.getData();
136    
137                    String[] values = null;
138    
139                    if (isStatic() && dataJSONObject.has("values")) {
140                            JSONArray valuesJSONArray = dataJSONObject.getJSONArray("values");
141    
142                            values = new String[valuesJSONArray.length()];
143    
144                            for (int i = 0; i < valuesJSONArray.length(); i++) {
145                                    values[i] = valuesJSONArray.getString(i);
146                            }
147                    }
148    
149                    String[] valuesParam = StringUtil.split(
150                            GetterUtil.getString(searchContext.getAttribute(getFieldId())));
151    
152                    if (!isStatic() && (valuesParam != null) && (valuesParam.length > 0)) {
153                            values = valuesParam;
154                    }
155    
156                    if (ArrayUtil.isEmpty(values)) {
157                            return null;
158                    }
159    
160                    TermsFilter facetTermsFilter = new TermsFilter(getFieldName());
161    
162                    for (String value : values) {
163                            FacetValueValidator facetValueValidator = getFacetValueValidator();
164    
165                            if ((searchContext.getUserId() > 0) &&
166                                    !facetValueValidator.check(searchContext, value)) {
167    
168                                    continue;
169                            }
170    
171                            facetTermsFilter.addValue(value);
172                    }
173    
174                    if (facetTermsFilter.isEmpty()) {
175                            return null;
176                    }
177    
178                    return BooleanClauseFactoryUtil.createFilter(
179                            searchContext, facetTermsFilter, BooleanClauseOccur.MUST);
180            }
181    
182            protected void doSetValues(JSONArray valuesJSONArray) {
183                    FacetConfiguration facetConfiguration = getFacetConfiguration();
184    
185                    JSONObject dataJSONObject = facetConfiguration.getData();
186    
187                    dataJSONObject.put("values", valuesJSONArray);
188            }
189    
190    }