001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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(getFieldName()));
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                            getFieldName(), value, BooleanClauseOccur.MUST.getName());
063            }
064    
065    }