001/*
002 *  Copyright 2015 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 * Query on the Dublin Core date metadata.
025 */
026public class DublinCoreDateQuery implements Query
027{
028    
029    /** The value to test. */
030    protected AdaptableDate _value;
031    
032    /** The binary operator to use. */
033    protected Operator _operator;
034    
035    /**
036     * Create a query on the Dublin Core date metadata with EQ operator.
037     * @param value The value to test.
038     */
039    public DublinCoreDateQuery(LocalDate value)
040    {
041        this(AdaptableDate.fromDate(value));
042    }
043    
044    /**
045     * Create a query on the Dublin Core date metadata with EQ operator.
046     * @param value The value to test.
047     */
048    public DublinCoreDateQuery(AdaptableDate value)
049    {
050        this(value, Operator.EQ);
051    }
052    
053    /**
054     * Create a query on the Dublin Core date metadata.
055     * @param value The value to test.
056     * @param operator The binary operator to use, can be one of EQ, NE, GT, GE, LT and LE.
057     */
058    public DublinCoreDateQuery(LocalDate value, Operator operator)
059    {
060        this(AdaptableDate.fromDate(value), operator);
061    }
062    
063    /**
064     * Create a query on the Dublin Core date metadata.
065     * @param value The value to test.
066     * @param operator The binary operator to use, can be one of EQ, NE, GT, GE, LT and LE.
067     */
068    public DublinCoreDateQuery(AdaptableDate value, Operator operator)
069    {
070        _value = value;
071        _operator = operator;
072    }
073    
074    @Override
075    public String build() throws QuerySyntaxException
076    {
077        StringBuilder query = new StringBuilder();
078        
079        if (_operator == Operator.NE)
080        {
081            NotQuery.appendNegation(query);
082        }
083        
084        query.append(SolrFieldNames.DC_DATE).append(':');
085        
086        DateQuery.appendDateValue(query, _operator, _value);
087        
088        return query.toString();
089    }
090
091    @Override
092    public int hashCode()
093    {
094        final int prime = 31;
095        int result = 1;
096        result = prime * result + ((_operator == null) ? 0 : _operator.hashCode());
097        result = prime * result + ((_value == null) ? 0 : _value.hashCode());
098        return result;
099    }
100
101    @Override
102    public boolean equals(Object obj)
103    {
104        if (this == obj)
105        {
106            return true;
107        }
108        if (obj == null)
109        {
110            return false;
111        }
112        if (getClass() != obj.getClass())
113        {
114            return false;
115        }
116        DublinCoreDateQuery other = (DublinCoreDateQuery) obj;
117        if (_operator != other._operator)
118        {
119            return false;
120        }
121        if (_value == null)
122        {
123            if (other._value != null)
124            {
125                return false;
126            }
127        }
128        else if (!_value.equals(other._value))
129        {
130            return false;
131        }
132        return true;
133    }
134}