001/*
002 *  Copyright 2021 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 RichTextQuery extends AbstractTextQuery
022{
023    
024    /**
025     * Build a StringQuery testing the existence of the field.
026     * @param fieldPath the field path
027     */
028    public RichTextQuery(String fieldPath)
029    {
030        this(fieldPath, Operator.EXISTS, null, null);
031    }
032    
033    /**
034     * Build a string query.
035     * @param fieldPath the field's path
036     * @param value the value.
037     */
038    public RichTextQuery(String fieldPath, String value)
039    {
040        this(fieldPath, value, null);
041    }
042    
043    /**
044     * Build a string query.
045     * @param fieldPath the field's path
046     * @param value the value.
047     * @param language the query language (can be null).
048     */
049    public RichTextQuery(String fieldPath, String value, String language)
050    {
051        this(fieldPath, Operator.EQ, value, language);
052    }
053    
054    /**
055     * Build a string query.
056     * @param fieldPath the field's path
057     * @param op the operator.
058     * @param value the value.
059     * @param language the query language (can be null).
060     */
061    public RichTextQuery(String fieldPath, Operator op, String value, String language)
062    {
063        this(fieldPath, op, value, language, false);
064    }
065    
066    /**
067     * Build a string query.
068     * @param fieldPath the field's path
069     * @param op the operator.
070     * @param value the value.
071     * @param language the query language (can be null).
072     * @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}.
073     */
074    public RichTextQuery(String fieldPath, Operator op, String value, String language, boolean alreadyEscaped)
075    {
076        super(fieldPath, op, value, language, alreadyEscaped);
077    }
078    
079    @Override
080    public String build() throws QuerySyntaxException
081    {
082        StringBuilder query = new StringBuilder();
083        
084        if (_operator == Operator.EXISTS)
085        {
086            
087            query.append(_fieldPath).append("_txt");
088            if (_language != null)
089            {
090                query.append("_").append(_language);
091            }
092            query.append(":").append(QueryHelper.EXISTS_VALUE);
093            return query.toString();
094        }
095        
096        return super.build();
097    }
098}