001/* 002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * Florent Guillaume 011 */ 012 013package org.nuxeo.ecm.core.query.sql.model; 014 015import java.util.Calendar; 016import java.util.Locale; 017 018import org.joda.time.DateTime; 019import org.joda.time.format.DateTimeFormatter; 020import org.joda.time.format.ISODateTimeFormat; 021 022/** 023 * @author Florent Guillaume 024 */ 025public class DateLiteral extends Literal { 026 027 private static final long serialVersionUID = 279219479611055690L; 028 029 public static final DateTimeFormatter dateParser = ISODateTimeFormat.dateParser().withLocale(Locale.getDefault()); 030 031 public static final DateTimeFormatter dateTimeParser = ISODateTimeFormat.dateOptionalTimeParser().withOffsetParsed(); 032 033 public static final DateTimeFormatter dateFormatter = ISODateTimeFormat.date(); 034 035 public static final DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.dateTime(); 036 037 // Direct access from org.nuxeo.ecm.core.search.backend.compass.QueryConverter 038 public final DateTime value; 039 040 public final boolean onlyDate; 041 042 public DateLiteral(String value, boolean onlyDate) { 043 this.onlyDate = onlyDate; 044 if (onlyDate) { 045 this.value = dateParser.parseDateTime(value); 046 } else { 047 // workaround to allow space instead of T after the date part 048 if (value.charAt(10) == ' ') { 049 char[] s = value.toCharArray(); 050 s[10] = 'T'; 051 value = new String(s); 052 } 053 this.value = dateTimeParser.parseDateTime(value); 054 } 055 } 056 057 public Calendar toCalendar() { 058 return value.toGregorianCalendar(); 059 } 060 061 public java.sql.Date toSqlDate() { 062 return new java.sql.Date(value.toDate().getTime()); 063 } 064 065 @Override 066 public String toString() { 067 if (onlyDate) { 068 String s = dateFormatter.print(value); 069 return new StringBuffer(s.length() + 7).append("DATE '").append(s).append("'").toString(); 070 } else { 071 String s = dateTimeFormatter.print(value); 072 return new StringBuffer(s.length() + 12).append("TIMESTAMP '").append(s).append("'").toString(); 073 } 074 } 075 076 @Override 077 public String asString() { 078 if (onlyDate) { 079 return dateFormatter.print(value); 080 } else { 081 return dateTimeFormatter.print(value); 082 } 083 } 084 085 @Override 086 public void accept(IVisitor visitor) { 087 visitor.visitDateLiteral(this); 088 } 089 090 @Override 091 public boolean equals(Object obj) { 092 if (obj == this) { 093 return true; 094 } 095 if (obj instanceof DateLiteral) { 096 return value.equals(((DateLiteral) obj).value); 097 } 098 return false; 099 } 100 101 @Override 102 public int hashCode() { 103 return value.hashCode(); 104 } 105 106 public static String dateTime(DateLiteral date) { 107 return dateTimeFormatter.print(date.value); 108 } 109 110 public static String date(DateLiteral date) { 111 return dateFormatter.print(date.value); 112 } 113}