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