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.List;
024    import java.util.Map;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     * @author Hugo Huijser
029     */
030    public abstract class BaseBooleanQueryImpl
031            extends BaseQueryImpl implements BooleanQuery {
032    
033            @Override
034            public void addTerms(String[] fields, String values) throws ParseException {
035                    if (Validator.isNull(values)) {
036                            return;
037                    }
038    
039                    if (fields == null) {
040                            fields = new String[0];
041                    }
042    
043                    for (String field : fields) {
044                            addTerm(field, values);
045                    }
046            }
047    
048            @Override
049            public void addTerms(String[] fields, String value, boolean like)
050                    throws ParseException {
051    
052                    if (Validator.isNull(value)) {
053                            return;
054                    }
055    
056                    for (String field : fields) {
057                            addTerm(field, value, like);
058                    }
059            }
060    
061            protected void addTerms(
062                            String[] fields, Map<String, List<String>> termFieldsValuesMap)
063                    throws ParseException {
064    
065                    for (String field : fields) {
066                            List<String> valuesList = termFieldsValuesMap.get(field);
067    
068                            for (String value : valuesList) {
069                                    addTerm(field, value);
070                            }
071                    }
072            }
073    
074            protected String[] parseKeywords(String values) {
075                    if (!values.contains(StringPool.QUOTE)) {
076                            return StringUtil.split(values, CharPool.SPACE);
077                    }
078    
079                    List<String> keywords = new ArrayList<String>();
080    
081                    while (values.length() > 0) {
082                            if (values.startsWith(StringPool.QUOTE)) {
083                                    values = values.substring(1);
084    
085                                    if (values.contains(StringPool.QUOTE)) {
086                                            int pos = values.indexOf(StringPool.QUOTE);
087    
088                                            keywords.add(values.substring(0, pos));
089    
090                                            values = values.substring(pos + 1);
091                                            values = values.trim();
092                                    }
093                            }
094                            else {
095                                    if (values.contains(StringPool.SPACE)) {
096                                            int pos = values.indexOf(StringPool.SPACE);
097    
098                                            keywords.add(values.substring(0, pos));
099    
100                                            values = values.substring(pos + 1);
101                                            values = values.trim();
102                                    }
103                                    else {
104                                            keywords.add(values);
105    
106                                            break;
107                                    }
108                            }
109                    }
110    
111                    return keywords.toArray(new String[keywords.size()]);
112            }
113    
114    }