001/*
002 *  Copyright 2010 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.plugins.repository.query.expression;
017
018import java.time.LocalDate;
019import java.time.ZonedDateTime;
020import java.util.Date;
021
022import org.apache.commons.lang.time.FastDateFormat;
023
024import org.ametys.core.util.DateUtils;
025
026/**
027 * Constructs an {@link Expression} corresponding to the Date comparison with a metadata.
028 */
029public class DateExpression implements Expression
030{
031    private Date _value;
032    private MetadataExpression _metadata;
033    private Operator _operator;
034
035    /**
036     * Creates the comparison Expression.
037     * @param metadata the metadata name
038     * @param operator the operator to make the comparison
039     * @param value the Date value
040     */
041    public DateExpression(String metadata, Operator operator, Date value)
042    {
043        _value = value;
044        _operator = operator;
045        _metadata = new MetadataExpression(metadata);
046    }
047    
048    /**
049     * Creates the comparison Expression.
050     * @param metadata the metadata name
051     * @param operator the operator to make the comparison
052     * @param value the Date value
053     * @param context the expression context
054     */
055    public DateExpression(String metadata, Operator operator, Date value, ExpressionContext context)
056    {
057        _value = value;
058        _operator = operator;
059        _metadata = new MetadataExpression(metadata, context);
060    }
061    
062    /**
063     * Creates the comparison Expression
064     * @param metadata the metadata name
065     * @param operator the operator to make the comparison
066     * @param value the LocalDate value
067     */
068    public DateExpression(String metadata, Operator operator, LocalDate value)
069    {
070        this(metadata, operator, DateUtils.asDate(value));
071    }
072
073    /**
074     * Creates the comparison Expression
075     * @param metadata the metadata name
076     * @param operator the operator to make the comparison
077     * @param value the LocalDate value
078     * @param context the expression context
079     */
080    public DateExpression(String metadata, Operator operator, LocalDate value, ExpressionContext context)
081    {
082        this(metadata, operator, DateUtils.asDate(value), context);
083    }
084
085    /**
086     * Creates the comparison Expression
087     * @param metadata the metadata name
088     * @param operator the operator to make the comparison
089     * @param value the ZonedDateTime value
090     */
091    public DateExpression(String metadata, Operator operator, ZonedDateTime value)
092    {
093        this(metadata, operator, DateUtils.asDate(value));
094    }
095
096    /**
097     * Creates the comparison Expression
098     * @param metadata the metadata name
099     * @param operator the operator to make the comparison
100     * @param value the ZonedDateTime value
101     * @param context the expression context
102     */
103    public DateExpression(String metadata, Operator operator, ZonedDateTime value, ExpressionContext context)
104    {
105        this(metadata, operator, DateUtils.asDate(value), context);
106    }
107
108    public String build()
109    {
110        return _metadata.build() + " " + _operator + " xs:dateTime('" + FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSSZZ").format(_value) + "')";
111    }
112}