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
018import java.time.LocalDate;
019
020import org.ametys.cms.content.indexing.solr.SolrFieldNames;
021import org.ametys.core.util.date.AdaptableDate;
022
023/**
024 * Represents a {@link Query} testing the creation date of a content.
025 */
026public class CreationDateQuery implements Query
027{
028    
029    /** The operator. */
030    protected Operator _operator;
031    /** The value to test. */
032    protected AdaptableDate _value;
033    
034    /**
035     * Build a CreationDateQuery.
036     * @param value the value.
037     */
038    public CreationDateQuery(LocalDate value)
039    {
040        this(AdaptableDate.fromDate(value));
041    }
042    
043    /**
044     * Build a CreationDateQuery.
045     * @param value the value.
046     */
047    public CreationDateQuery(AdaptableDate value)
048    {
049        this(Operator.EQ, value);
050    }
051
052    /**
053     * Build a CreationDateQuery.
054     * @param op the operator.
055     * @param value the value.
056     */
057    public CreationDateQuery(Operator op, LocalDate value)
058    {
059        this(Operator.EQ, AdaptableDate.fromDate(value));
060    }
061    
062    /**
063     * Build a CreationDateQuery.
064     * @param op the operator.
065     * @param value the value.
066     */
067    public CreationDateQuery(Operator op, AdaptableDate value)
068    {
069        _operator = op;
070        _value = value;
071    }
072    
073    /**
074     * Get the operator.
075     * @return the operator.
076     */
077    public Operator getOperator()
078    {
079        return _operator;
080    }
081    
082    /**
083     * Get the value.
084     * @return the value.
085     */
086    public AdaptableDate getValue()
087    {
088        return _value;
089    }
090    
091    @Override
092    public String build() throws QuerySyntaxException
093    {
094        StringBuilder query = new StringBuilder();
095        
096        if (_operator == Operator.EXISTS)
097        {
098            query.append(SolrFieldNames.CREATION_DATE).append(':').append(QueryHelper.EXISTS_VALUE);
099            return query.toString();
100        }
101        
102        if (_operator == Operator.NE)
103        {
104            NotQuery.appendNegation(query);
105        }
106        
107        query.append(SolrFieldNames.CREATION_DATE).append(':');
108        
109        DateQuery.appendDateValue(query, _operator, _value);
110        
111        return query.toString();
112    }
113
114    @Override
115    public int hashCode()
116    {
117        final int prime = 31;
118        int result = 1;
119        result = prime * result + ((_operator == null) ? 0 : _operator.hashCode());
120        result = prime * result + ((_value == null) ? 0 : _value.hashCode());
121        return result;
122    }
123
124    @Override
125    public boolean equals(Object obj)
126    {
127        if (this == obj)
128        {
129            return true;
130        }
131        if (obj == null)
132        {
133            return false;
134        }
135        if (getClass() != obj.getClass())
136        {
137            return false;
138        }
139        CreationDateQuery other = (CreationDateQuery) obj;
140        if (_operator != other._operator)
141        {
142            return false;
143        }
144        if (_value == null)
145        {
146            if (other._value != null)
147            {
148                return false;
149            }
150        }
151        else if (!_value.equals(other._value))
152        {
153            return false;
154        }
155        return true;
156    }
157}