001    /**
002     * Copyright (c) 2000-present 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.CharPool;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.StringUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    
022    import java.util.ArrayList;
023    import java.util.Collections;
024    import java.util.HashMap;
025    import java.util.List;
026    import java.util.Map;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     * @author Hugo Huijser
031     */
032    public abstract class BaseBooleanQueryImpl
033            extends BaseQueryImpl implements BooleanQuery {
034    
035            @Override
036            public Map<String, Query> addTerms(String[] fields, String values)
037                    throws ParseException {
038    
039                    if (Validator.isNull(values)) {
040                            return Collections.emptyMap();
041                    }
042    
043                    if (fields == null) {
044                            fields = new String[0];
045                    }
046    
047                    Map<String, Query> queries = new HashMap<>((int)(fields.length / .75));
048    
049                    for (String field : fields) {
050                            Query query = addTerm(field, values);
051    
052                            queries.put(field, query);
053                    }
054    
055                    return queries;
056            }
057    
058            @Override
059            public Map<String, Query> addTerms(
060                            String[] fields, String value, boolean like)
061                    throws ParseException {
062    
063                    if (Validator.isNull(value)) {
064                            return Collections.emptyMap();
065                    }
066    
067                    Map<String, Query> queries = new HashMap<>((int)(fields.length / .75));
068    
069                    for (String field : fields) {
070                            Query query = addTerm(field, value, like);
071    
072                            queries.put(field, query);
073                    }
074    
075                    return queries;
076            }
077    
078            protected Map<String, List<Query>> addTerms(
079                            String[] fields, Map<String, List<String>> termFieldsValuesMap)
080                    throws ParseException {
081    
082                    Map<String, List<Query>> queries = new HashMap<>(
083                            (int)(fields.length / .75));
084    
085                    for (String field : fields) {
086                            List<String> valuesList = termFieldsValuesMap.get(field);
087    
088                            List<Query> queriesList = new ArrayList<>(valuesList.size());
089    
090                            queries.put(field, queriesList);
091    
092                            for (String value : valuesList) {
093                                    Query query = addTerm(field, value);
094    
095                                    queriesList.add(query);
096                            }
097                    }
098    
099                    return queries;
100            }
101    
102            protected String[] parseKeywords(String values) {
103                    if (!values.contains(StringPool.QUOTE)) {
104                            return StringUtil.split(values, CharPool.SPACE);
105                    }
106    
107                    List<String> keywords = new ArrayList<>();
108    
109                    while (values.length() > 0) {
110                            if (values.startsWith(StringPool.QUOTE)) {
111                                    values = values.substring(1);
112    
113                                    if (values.contains(StringPool.QUOTE)) {
114                                            int pos = values.indexOf(StringPool.QUOTE);
115    
116                                            keywords.add(values.substring(0, pos));
117    
118                                            values = values.substring(pos + 1);
119                                            values = values.trim();
120                                    }
121                            }
122                            else {
123                                    if (values.contains(StringPool.SPACE)) {
124                                            int pos = values.indexOf(StringPool.SPACE);
125    
126                                            keywords.add(values.substring(0, pos));
127    
128                                            values = values.substring(pos + 1);
129                                            values = values.trim();
130                                    }
131                                    else {
132                                            keywords.add(values);
133    
134                                            break;
135                                    }
136                            }
137                    }
138    
139                    return keywords.toArray(new String[keywords.size()]);
140            }
141    
142    }