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.JSONObject;
018    import com.liferay.portal.kernel.search.BooleanClause;
019    import com.liferay.portal.kernel.search.BooleanClauseFactoryUtil;
020    import com.liferay.portal.kernel.search.BooleanClauseOccur;
021    import com.liferay.portal.kernel.search.SearchContext;
022    import com.liferay.portal.kernel.search.facet.config.FacetConfiguration;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.Validator;
026    
027    /**
028     * @author Raymond Aug??
029     */
030    public class SimpleFacet extends BaseFacet {
031    
032            public SimpleFacet(SearchContext searchContext) {
033                    super(searchContext);
034            }
035    
036            @Override
037            protected BooleanClause doGetFacetClause() {
038                    SearchContext searchContext = getSearchContext();
039    
040                    FacetConfiguration facetConfiguration = getFacetConfiguration();
041    
042                    JSONObject dataJSONObject = facetConfiguration.getData();
043    
044                    String value = StringPool.BLANK;
045    
046                    if (isStatic() && dataJSONObject.has("value")) {
047                            value = dataJSONObject.getString("value");
048                    }
049    
050                    String valueParam = GetterUtil.getString(
051                            searchContext.getAttribute(getFieldId()));
052    
053                    if (!isStatic() && Validator.isNotNull(valueParam)) {
054                            value = valueParam;
055                    }
056    
057                    if (Validator.isNull(value)) {
058                            return null;
059                    }
060    
061                    return BooleanClauseFactoryUtil.create(
062                            searchContext, getFieldName(), value,
063                            BooleanClauseOccur.MUST.getName());
064            }
065    
066    }