001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
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 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019
020package org.nuxeo.ecm.core.query.sql.model;
021
022import java.util.Calendar;
023import java.util.Locale;
024
025import org.joda.time.DateTime;
026import org.joda.time.format.DateTimeFormatter;
027import org.joda.time.format.ISODateTimeFormat;
028
029/**
030 * @author Florent Guillaume
031 */
032public class DateLiteral extends Literal {
033
034    private static final long serialVersionUID = 279219479611055690L;
035
036    public static final DateTimeFormatter dateParser = ISODateTimeFormat.dateParser().withLocale(Locale.getDefault());
037
038    public static final DateTimeFormatter dateTimeParser = ISODateTimeFormat.dateOptionalTimeParser().withOffsetParsed();
039
040    public static final DateTimeFormatter dateFormatter = ISODateTimeFormat.date();
041
042    public static final DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.dateTime();
043
044    // Direct access from org.nuxeo.ecm.core.search.backend.compass.QueryConverter
045    public final DateTime value;
046
047    public final boolean onlyDate;
048
049    public DateLiteral(DateTime value) {
050        this.value = value;
051        this.onlyDate = false;
052    }
053
054    public DateLiteral(String value, boolean onlyDate) {
055        this.onlyDate = onlyDate;
056        if (onlyDate) {
057            this.value = dateParser.parseDateTime(value);
058        } else {
059            // workaround to allow space instead of T after the date part
060            if (value.charAt(10) == ' ') {
061                char[] s = value.toCharArray();
062                s[10] = 'T';
063                value = new String(s);
064            }
065            this.value = dateTimeParser.parseDateTime(value);
066        }
067    }
068
069    public Calendar toCalendar() {
070        return value.toGregorianCalendar();
071    }
072
073    public java.sql.Date toSqlDate() {
074        return new java.sql.Date(value.toDate().getTime());
075    }
076
077    @Override
078    public String toString() {
079        if (onlyDate) {
080            String s = dateFormatter.print(value);
081            return new StringBuffer(s.length() + 7).append("DATE '").append(s).append("'").toString();
082        } else {
083            String s = dateTimeFormatter.print(value);
084            return new StringBuffer(s.length() + 12).append("TIMESTAMP '").append(s).append("'").toString();
085        }
086    }
087
088    @Override
089    public String asString() {
090        if (onlyDate) {
091            return dateFormatter.print(value);
092        } else {
093            return dateTimeFormatter.print(value);
094        }
095    }
096
097    @Override
098    public void accept(IVisitor visitor) {
099        visitor.visitDateLiteral(this);
100    }
101
102    @Override
103    public boolean equals(Object obj) {
104        if (obj == this) {
105            return true;
106        }
107        if (obj instanceof DateLiteral) {
108            return value.equals(((DateLiteral) obj).value);
109        }
110        return false;
111    }
112
113    @Override
114    public int hashCode() {
115        return value.hashCode();
116    }
117
118    public static String dateTime(DateLiteral date) {
119        return dateTimeFormatter.print(date.value);
120    }
121
122    public static String date(DateLiteral date) {
123        return dateFormatter.print(date.value);
124    }
125}