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 last modification date of a content.
025 */
026public class LastModifiedQuery 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 LastModifiedQuery.
036     * @param value the value.
037     */
038    public LastModifiedQuery(LocalDate value)
039    {
040        this(AdaptableDate.fromDate(value));
041    }
042    
043    /**
044     * Build a LastModifiedQuery.
045     * @param value the value.
046     */
047    public LastModifiedQuery(AdaptableDate value)
048    {
049        this(Operator.EQ, value);
050    }
051    
052    /**
053     * Build a LastModifiedQuery.
054     * @param op the operator.
055     * @param value the value.
056     */
057    public LastModifiedQuery(Operator op, LocalDate value)
058    {
059        this(op, AdaptableDate.fromDate(value));
060    }
061    
062    /**
063     * Build a LastModifiedQuery.
064     * @param op the operator.
065     * @param value the value.
066     */
067    public LastModifiedQuery(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.NE)
097        {
098            NotQuery.appendNegation(query);
099        }
100        
101        query.append(SolrFieldNames.LAST_MODIFIED).append(':');
102        
103        DateQuery.appendDateValue(query, _operator, _value);
104        
105        return query.toString();
106    }
107
108    @Override
109    public int hashCode()
110    {
111        final int prime = 31;
112        int result = 1;
113        result = prime * result + ((_operator == null) ? 0 : _operator.hashCode());
114        result = prime * result + ((_value == null) ? 0 : _value.hashCode());
115        return result;
116    }
117
118    @Override
119    public boolean equals(Object obj)
120    {
121        if (this == obj)
122        {
123            return true;
124        }
125        if (obj == null)
126        {
127            return false;
128        }
129        if (getClass() != obj.getClass())
130        {
131            return false;
132        }
133        LastModifiedQuery other = (LastModifiedQuery) obj;
134        if (_operator != other._operator)
135        {
136            return false;
137        }
138        if (_value == null)
139        {
140            if (other._value != null)
141            {
142                return false;
143            }
144        }
145        else if (!_value.equals(other._value))
146        {
147            return false;
148        }
149        return true;
150    }
151}