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