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