1 21 22 package org.continuent.sequoia.controller.sql.macros; 23 24 import java.sql.Date ; 25 import java.sql.Time ; 26 import java.sql.Timestamp ; 27 28 import org.continuent.sequoia.common.xml.DatabasesXmlTags; 29 30 36 public class DateMacro extends AbstractMacro 37 { 38 private int dateFormat; 39 private long clockResolution; 40 41 42 public static final int DATE_DATE = 1; 43 44 public static final int DATE_TIME = 2; 45 46 public static final int DATE_TIMESTAMP = 3; 47 48 55 public DateMacro(String macroName, int dateFormat, long timeResolution) 56 { 57 super(macroName); 58 this.dateFormat = dateFormat; 59 clockResolution = timeResolution; 60 if (clockResolution < 0) 61 throw new RuntimeException ( 62 "Invalid negative value for clock resolution in date macros"); 63 } 64 65 68 public String generateMacroValue(long currentTimeInMs) 69 { 70 if (clockResolution > 0) 71 currentTimeInMs = currentTimeInMs - (currentTimeInMs % clockResolution); 72 73 switch (dateFormat) 74 { 75 case DATE_DATE : 76 return "{d '" + new Date (currentTimeInMs).toString() + "'}"; 77 case DATE_TIME : 78 return "{t '" + new Time (currentTimeInMs).toString() + "'}"; 79 case DATE_TIMESTAMP : 80 return "{ts '" + new Timestamp (currentTimeInMs).toString() + "'}"; 81 default : 82 throw new RuntimeException ( 83 "Unexpected replacement strategy for date macro (" + dateFormat 84 + ")"); 85 } 86 } 87 88 95 public static final int getDateFormat(String dateFormat) 96 throws IllegalArgumentException  97 { 98 if (dateFormat.equals(DatabasesXmlTags.VAL_date)) 99 return DATE_DATE; 100 else if (dateFormat.equals(DatabasesXmlTags.VAL_time)) 101 return DATE_TIME; 102 else if (dateFormat.equals(DatabasesXmlTags.VAL_timestamp)) 103 return DATE_TIMESTAMP; 104 else 105 throw new IllegalArgumentException ("Invalid date format tag: " 106 + dateFormat); 107 } 108 109 116 public static final String getStringDateFormat(int dateFormat) 117 throws IllegalArgumentException  118 { 119 switch (dateFormat) 120 { 121 case DATE_DATE : 122 return DatabasesXmlTags.VAL_date; 123 case DATE_TIME : 124 return DatabasesXmlTags.VAL_time; 125 case DATE_TIMESTAMP : 126 return DatabasesXmlTags.VAL_timestamp; 127 default : 128 throw new IllegalArgumentException ("Invalid date number format: " 129 + dateFormat); 130 } 131 } 132 133 136 public String getXml() 137 { 138 return "<" + DatabasesXmlTags.ELT_DateMacro + " " 139 + DatabasesXmlTags.ATT_macroName + "=\"" + macroName + "\" " 140 + DatabasesXmlTags.ATT_dateFormat + "=\"" 141 + getStringDateFormat(dateFormat) + "\" " 142 + DatabasesXmlTags.ATT_timeResolution + "=\"" 143 + String.valueOf(clockResolution) + "\" />"; 144 } 145 146 } 147 | Popular Tags |