001/* 002 * Copyright 2014 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.cms.search.query; 017 018/** 019 * Represents a {@link Query} testing a string field. 020 */ 021public class StringQuery extends AbstractTextQuery 022{ 023 /** 024 * Build a StringQuery testing the existence of the field. 025 * @param fieldPath the field path 026 */ 027 public StringQuery(String fieldPath) 028 { 029 this(fieldPath, Operator.EXISTS, null, null); 030 } 031 032 /** 033 * Build a string query. 034 * @param fieldPath the field's path 035 * @param value the value. 036 */ 037 public StringQuery(String fieldPath, String value) 038 { 039 this(fieldPath, value, null); 040 } 041 042 /** 043 * Build a string query. 044 * @param fieldPath the field's path 045 * @param value the value. 046 * @param language the query language (can be null). 047 */ 048 public StringQuery(String fieldPath, String value, String language) 049 { 050 this(fieldPath, Operator.EQ, value, language); 051 } 052 053 /** 054 * Build a string query. 055 * @param fieldPath the field's path 056 * @param op the operator. 057 * @param value the value. 058 * @param language the query language (can be null). 059 */ 060 public StringQuery(String fieldPath, Operator op, String value, String language) 061 { 062 this(fieldPath, op, value, language, false); 063 } 064 065 /** 066 * Build a string query. 067 * @param fieldPath the field's path 068 * @param op the operator. 069 * @param value the value. 070 * @param language the query language (can be null). 071 * @param alreadyEscaped true if the value is already escaped and there is no need to escape again the value during {@link #build() the build of the query}. 072 */ 073 public StringQuery(String fieldPath, Operator op, String value, String language, boolean alreadyEscaped) 074 { 075 super(fieldPath, op, value, language, alreadyEscaped); 076 } 077 078 @Override 079 public String build() throws QuerySyntaxException 080 { 081 StringBuilder query = new StringBuilder(); 082 083 if (getOperator() == Operator.EXISTS) 084 { 085 query.append(getFieldName()).append("_s:").append(QueryHelper.EXISTS_VALUE); 086 return query.toString(); 087 } 088 089 return super.build(); 090 } 091}