001/*
002 *  Copyright 2022 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
018import java.util.Objects;
019
020/**
021 * Base class for all operator-based queries.
022 * @param <T> the value type.
023 */
024public abstract class AbstractOperatorQuery<T> implements Query, Cloneable
025{
026    private Operator _operator;
027    private String _fieldName;
028    private T _value;
029    
030    /**
031     * Create a new Query
032     * @param fieldName the Solr field name
033     * @param operator the {@link org.ametys.cms.search.query.Query.Operator}
034     * @param value the value
035     */
036    public AbstractOperatorQuery(String fieldName, Operator operator, T value)
037    {
038        _fieldName = fieldName;
039        _operator = operator;
040        _value = value;
041    }
042    
043    /**
044     * Return the {@link org.ametys.cms.search.query.Query.Operator}
045     * @return the operator
046     */
047    public Operator getOperator()
048    {
049        return _operator;
050    }
051    
052    /**
053     * Returns the typed value
054     * @return the value
055     */
056    public T getValue()
057    {
058        return _value;
059    }
060    
061    /**
062     * Returns the Solr field name.
063     * @return the field name
064     */
065    public String getFieldName()
066    {
067        return _fieldName;
068    }
069    
070    /**
071     * Computes the value for Solr client
072     * @param value the typed value
073     * @return the value, adapted for Solr
074     */
075    public String valueForQuery(T value)
076    {
077        return value.toString();
078    }
079
080    @Override
081    public String build() throws QuerySyntaxException
082    {
083        return QueryHelper.getStandardQuery(getFieldName(), getOperator(), valueForQuery(getValue()));
084    }
085    
086    public Query rewrite()
087    {
088        if (_operator == Operator.NE)
089        {
090            try
091            {
092                @SuppressWarnings("unchecked")
093                AbstractOperatorQuery<T> query = (AbstractOperatorQuery<T>) clone();
094                
095                query._operator = Operator.EQ;
096                return new NotQuery(query);
097            }
098            catch (CloneNotSupportedException e)
099            {
100                throw new IllegalArgumentException("This query could not be cloned", e);
101            }
102        }
103        
104        return this;
105    }
106    
107    @Override
108    public int hashCode()
109    {
110        return Objects.hash(_fieldName, _operator, _value);
111    }
112
113    @Override
114    public boolean equals(Object obj)
115    {
116        if (this == obj)
117        {
118            return true;
119        }
120        
121        if (obj == null || getClass() != obj.getClass())
122        {
123            return false;
124        }
125        
126        AbstractOperatorQuery other = (AbstractOperatorQuery) obj;
127        return Objects.equals(_fieldName, other._fieldName)
128            && Objects.equals(_value, other._value)
129            && Objects.equals(_operator, other._operator);
130    }
131}