001/*
002 *  Copyright 2016 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;
019import java.time.LocalTime;
020
021import org.ametys.core.util.date.AdaptableDate;
022
023/**
024 * Represents a {@link Query} testing a date range.
025 */
026public class DateRangeQuery extends AbstractRangeQuery<AdaptableDate>
027{
028    /**
029     * Build a date range query.
030     * @param fieldPath The field path.
031     * @param from The lower end of the range.
032     * @param to The upper end of the range.
033     */
034    public DateRangeQuery(String fieldPath, LocalDate from, LocalDate to)
035    {
036        this(fieldPath, AdaptableDate.fromDate(from), AdaptableDate.fromDate(to));
037    }
038    
039    /**
040     * Build a date range query.
041     * @param fieldPath The field path.
042     * @param from The lower end of the range.
043     * @param to The upper end of the range.
044     */
045    public DateRangeQuery(String fieldPath, AdaptableDate from, AdaptableDate to)
046    {
047        this(fieldPath, from, to, true, true);
048    }
049    
050    /**
051     * Build a date range query.
052     * @param fieldPath The field path.
053     * @param from The lower end of the range.
054     * @param to The upper end of the range.
055     * @param includeFrom Whether to include the lower end or not.
056     * @param includeTo Whether to include the upper end or not.
057     */
058    public DateRangeQuery(String fieldPath, LocalDate from, LocalDate to, boolean includeFrom, boolean includeTo)
059    {
060        this(fieldPath, AdaptableDate.fromDate(from), AdaptableDate.fromDate(to), includeFrom, includeTo);
061    }
062    
063    /**
064     * Build a date range query.
065     * @param fieldPath The field path.
066     * @param from The lower end of the range.
067     * @param to The upper end of the range.
068     * @param includeFrom Whether to include the lower end or not.
069     * @param includeTo Whether to include the upper end or not.
070     */
071    public DateRangeQuery(String fieldPath, AdaptableDate from, AdaptableDate to, boolean includeFrom, boolean includeTo)
072    {
073        super(fieldPath + "_dt", from, to, includeFrom, includeTo);
074    }
075    
076    @Override
077    public String lowerBoundForQuery(AdaptableDate value)
078    {
079        return value.resolveDate()
080                    .atTime(includeLowerBound() ? LocalTime.MIN : LocalTime.MAX)
081                    .format(AbstractDateOperatorQuery.DATE_FORMATTER);
082    }
083    
084    @Override
085    public String upperBoundForQuery(AdaptableDate value)
086    {
087        return value.resolveDate()
088                    .atTime(includeUpperBound() ? LocalTime.MIN : LocalTime.MAX)
089                    .format(AbstractDateOperatorQuery.DATE_FORMATTER);
090    }
091}