001    /**
002     * Copyright (c) 2000-2010 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;
016    
017    import com.liferay.portal.kernel.util.GetterUtil;
018    import com.liferay.portal.kernel.util.UnicodeProperties;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portlet.expando.model.ExpandoBridge;
021    import com.liferay.portlet.expando.util.ExpandoBridgeIndexer;
022    import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
023    
024    import java.util.Set;
025    
026    /**
027     * @author Hugo Huijser
028     */
029    public abstract class ExpandoIndexer extends BaseIndexer {
030    
031            protected void addSearchQueryParams(
032                            BooleanQuery searchQuery, SearchContext searchContext,
033                            ExpandoBridge expandoBridge, Set<String> attributeNames, String key,
034                            Object value)
035                    throws Exception {
036    
037                    if (attributeNames.contains(key)) {
038                            UnicodeProperties properties = expandoBridge.getAttributeProperties(
039                                    key);
040    
041                            if (GetterUtil.getBoolean(
042                                            properties.getProperty(ExpandoBridgeIndexer.INDEXABLE))) {
043    
044                                    String fieldName = ExpandoBridgeIndexerUtil.encodeFieldName(
045                                            key);
046    
047                                    if (Validator.isNotNull((String)value)) {
048                                            if (searchContext.isAndSearch()) {
049                                                    searchQuery.addRequiredTerm(
050                                                            fieldName, (String)value, true);
051                                            }
052                                            else {
053                                                    searchQuery.addTerm(fieldName, (String)value, true);
054                                            }
055                                    }
056                            }
057                    }
058                    else if (Validator.isNotNull(key) && Validator.isNotNull(value)) {
059                            if (searchContext.isAndSearch()) {
060                                    searchQuery.addRequiredTerm(key, String.valueOf(value));
061                            }
062                            else {
063                                    searchQuery.addTerm(key, String.valueOf(value));
064                            }
065                    }
066            }
067    
068    }