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