001/*
002 *  Copyright 2022 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.LocalDateTime;
019import java.time.ZoneOffset;
020import java.time.ZonedDateTime;
021
022import org.ametys.core.util.date.AdaptableDate;
023
024/**
025 * Represents a {@link Query} testing a datetime field.
026 */
027public class DateTimeQuery extends AbstractDateOperatorQuery
028{
029    /**
030     * Build a DateQuery testing the existence of the field.
031     * @param fieldPath the field path.
032     */
033    public DateTimeQuery(String fieldPath)
034    {
035        this(fieldPath, Operator.EXISTS, (AdaptableDate) null);
036    }
037    
038    /**
039     * Build a DateQuery.
040     * @param fieldPath the field's path
041     * @param value the value.
042     */
043    public DateTimeQuery(String fieldPath, ZonedDateTime value)
044    {
045        this(fieldPath, AdaptableDate.fromDateTime(value.withZoneSameInstant(ZoneOffset.UTC).toLocalDateTime()));
046    }
047    
048    /**
049     * Build a DateQuery.
050     * @param fieldPath the field's path
051     * @param value the value.
052     */
053    public DateTimeQuery(String fieldPath, AdaptableDate value)
054    {
055        this(fieldPath, Operator.EQ, value);
056    }
057    
058    /**
059     * Build a DateQuery.
060     * @param fieldPath the field's path
061     * @param op the operator.
062     * @param value the value.
063     */
064    public DateTimeQuery(String fieldPath, Operator op, ZonedDateTime value)
065    {
066        this(fieldPath, op, AdaptableDate.fromDateTime(value.withZoneSameInstant(ZoneOffset.UTC).toLocalDateTime()));
067    }
068    
069    /**
070     * Build a DateQuery.
071     * @param fieldPath the field's path
072     * @param op the operator.
073     * @param value the value.
074     */
075    public DateTimeQuery(String fieldPath, Operator op, AdaptableDate value)
076    {
077        super(fieldPath + "_dt", op, value);
078    }
079
080    @Override
081    protected void appendDateValue(StringBuilder query, Operator operator, AdaptableDate value)
082    {
083        LocalDateTime dateTime = value.resolveDateTime();
084        String strValue = dateTime.format(DATE_FORMATTER);
085        
086        if (operator == Operator.EQ || operator == Operator.NE)
087        {
088            query.append(strValue);
089            return;
090        }
091        
092        if (operator == Operator.GT)
093        {
094            query.append('{').append(strValue).append(" TO *]");
095        }
096        else if (operator == Operator.GE)
097        {
098            query.append('[').append(strValue).append(" TO *]");
099        }
100        else if (operator == Operator.LT)
101        {
102            query.append("[* TO ").append(strValue).append('}');
103        }
104        else if (operator == Operator.LE)
105        {
106            query.append("[* TO ").append(strValue).append(']');
107        }
108    }
109}