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(String value, boolean onlyDate) { 050 this.onlyDate = onlyDate; 051 if (onlyDate) { 052 this.value = dateParser.parseDateTime(value); 053 } else { 054 // workaround to allow space instead of T after the date part 055 if (value.charAt(10) == ' ') { 056 char[] s = value.toCharArray(); 057 s[10] = 'T'; 058 value = new String(s); 059 } 060 this.value = dateTimeParser.parseDateTime(value); 061 } 062 } 063 064 public Calendar toCalendar() { 065 return value.toGregorianCalendar(); 066 } 067 068 public java.sql.Date toSqlDate() { 069 return new java.sql.Date(value.toDate().getTime()); 070 } 071 072 @Override 073 public String toString() { 074 if (onlyDate) { 075 String s = dateFormatter.print(value); 076 return new StringBuffer(s.length() + 7).append("DATE '").append(s).append("'").toString(); 077 } else { 078 String s = dateTimeFormatter.print(value); 079 return new StringBuffer(s.length() + 12).append("TIMESTAMP '").append(s).append("'").toString(); 080 } 081 } 082 083 @Override 084 public String asString() { 085 if (onlyDate) { 086 return dateFormatter.print(value); 087 } else { 088 return dateTimeFormatter.print(value); 089 } 090 } 091 092 @Override 093 public void accept(IVisitor visitor) { 094 visitor.visitDateLiteral(this); 095 } 096 097 @Override 098 public boolean equals(Object obj) { 099 if (obj == this) { 100 return true; 101 } 102 if (obj instanceof DateLiteral) { 103 return value.equals(((DateLiteral) obj).value); 104 } 105 return false; 106 } 107 108 @Override 109 public int hashCode() { 110 return value.hashCode(); 111 } 112 113 public static String dateTime(DateLiteral date) { 114 return dateTimeFormatter.print(date.value); 115 } 116 117 public static String date(DateLiteral date) { 118 return dateFormatter.print(date.value); 119 } 120}