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.util.Date;
019
020import org.apache.commons.lang.time.FastDateFormat;
021
022/**
023 * Constructs an {@link Expression} corresponding to the Date comparison with a metadata.
024 */
025public class DateExpression implements Expression
026{
027    private Date _value;
028    private MetadataExpression _metadata;
029    private Operator _operator;
030
031    /**
032     * Creates the comparison Expression.
033     * @param metadata the metadata name
034     * @param operator the operator to make the comparison
035     * @param value the Date value
036     */
037    public DateExpression(String metadata, Operator operator, Date value)
038    {
039        _value = value;
040        _operator = operator;
041        _metadata = new MetadataExpression(metadata);
042    }
043    
044    /**
045     * Creates the comparison Expression.
046     * @param metadata the metadata name
047     * @param operator the operator to make the comparison
048     * @param value the Date value
049     * @param unversioned true if the metadata is unversioned, false otherwise.
050     */
051    public DateExpression(String metadata, Operator operator, Date value, boolean unversioned)
052    {
053        _value = value;
054        _operator = operator;
055        _metadata = new MetadataExpression(metadata, unversioned);
056    }
057
058    public String build()
059    {
060        return _metadata.build() + " " + _operator + " xs:dateTime('" + FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss.SSSZZ").format(_value) + "')";
061    }
062}