All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.liferay.portal.kernel.search.BaseBooleanQueryImpl Maven / Gradle / Ivy

Go to download

Contains interfaces for the portal services. Interfaces are only loaded by the global class loader and are shared by all plugins.

There is a newer version: 156.0.0
Show newest version
/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.portal.kernel.search;

import com.liferay.portal.kernel.util.CharPool;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.Validator;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Brian Wing Shun Chan
 * @author Hugo Huijser
 */
public abstract class BaseBooleanQueryImpl
	extends BaseQueryImpl implements BooleanQuery {

	@Override
	public Map addTerms(String[] fields, String values)
		throws ParseException {

		if (Validator.isNull(values)) {
			return Collections.emptyMap();
		}

		if (fields == null) {
			fields = new String[0];
		}

		Map queries = new HashMap<>((int)(fields.length / .75));

		for (String field : fields) {
			Query query = addTerm(field, values);

			queries.put(field, query);
		}

		return queries;
	}

	@Override
	public Map addTerms(
			String[] fields, String value, boolean like)
		throws ParseException {

		if (Validator.isNull(value)) {
			return Collections.emptyMap();
		}

		Map queries = new HashMap<>((int)(fields.length / .75));

		for (String field : fields) {
			Query query = addTerm(field, value, like);

			queries.put(field, query);
		}

		return queries;
	}

	protected Map> addTerms(
			String[] fields, Map> termFieldsValuesMap)
		throws ParseException {

		Map> queries = new HashMap<>(
			(int)(fields.length / .75));

		for (String field : fields) {
			List valuesList = termFieldsValuesMap.get(field);

			List queriesList = new ArrayList<>(valuesList.size());

			queries.put(field, queriesList);

			for (String value : valuesList) {
				Query query = addTerm(field, value);

				queriesList.add(query);
			}
		}

		return queries;
	}

	protected String[] parseKeywords(String values) {
		if (!values.contains(StringPool.QUOTE)) {
			return StringUtil.split(values, CharPool.SPACE);
		}

		List keywords = new ArrayList<>();

		while (values.length() > 0) {
			if (values.startsWith(StringPool.QUOTE)) {
				values = values.substring(1);

				if (values.contains(StringPool.QUOTE)) {
					int pos = values.indexOf(StringPool.QUOTE);

					keywords.add(values.substring(0, pos));

					values = values.substring(pos + 1);
					values = values.trim();
				}
			}
			else {
				if (values.contains(StringPool.SPACE)) {
					int pos = values.indexOf(StringPool.SPACE);

					keywords.add(values.substring(0, pos));

					values = values.substring(pos + 1);
					values = values.trim();
				}
				else {
					keywords.add(values);

					break;
				}
			}
		}

		return keywords.toArray(new String[keywords.size()]);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy