KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > sql > macros > DateMacro


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2006 Continuent, Inc.
4  * Contact: sequoia@continuent.org
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Initial developer(s): Emmanuel Cecchet.
19  * Contributor(s): ______________________.
20  */

21
22 package org.continuent.sequoia.controller.sql.macros;
23
24 import java.sql.Date JavaDoc;
25 import java.sql.Time JavaDoc;
26 import java.sql.Timestamp JavaDoc;
27
28 import org.continuent.sequoia.common.xml.DatabasesXmlTags;
29
30 /**
31  * This class defines a DateMacro
32  *
33  * @author <a HREF="mailto:emmanuel.cecchet@continuent.com">Emmanuel Cecchet</a>
34  * @version 1.0
35  */

36 public class DateMacro extends AbstractMacro
37 {
38   private int dateFormat;
39   private long clockResolution;
40
41   /** Value if date macro should be replaced by an java.sql.Date value */
42   public static final int DATE_DATE = 1;
43   /** Value if date macro should be replaced by an java.sql.Time value */
44   public static final int DATE_TIME = 2;
45   /** Value if date macro should be replaced by an java.sql.Timestamp value */
46   public static final int DATE_TIMESTAMP = 3;
47
48   /**
49    * Creates a new <code>DateMacro</code> object
50    *
51    * @param macroName the name of the macro
52    * @param dateFormat the date format (see constants in this class)
53    * @param timeResolution the time resolution in ms
54    */

55   public DateMacro(String JavaDoc macroName, int dateFormat, long timeResolution)
56   {
57     super(macroName);
58     this.dateFormat = dateFormat;
59     clockResolution = timeResolution;
60     if (clockResolution < 0)
61       throw new RuntimeException JavaDoc(
62           "Invalid negative value for clock resolution in date macros");
63   }
64
65   /**
66    * @see org.continuent.sequoia.controller.sql.macros.AbstractMacro#generateMacroValue(long)
67    */

68   public String JavaDoc 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 JavaDoc(currentTimeInMs).toString() + "'}";
77       case DATE_TIME :
78         return "{t '" + new Time JavaDoc(currentTimeInMs).toString() + "'}";
79       case DATE_TIMESTAMP :
80         return "{ts '" + new Timestamp JavaDoc(currentTimeInMs).toString() + "'}";
81       default :
82         throw new RuntimeException JavaDoc(
83             "Unexpected replacement strategy for date macro (" + dateFormat
84                 + ")");
85     }
86   }
87
88   /**
89    * Convert the date format from string (xml value) to integer
90    *
91    * @param dateFormat the date format XML tag
92    * @return an int corresponding to the string description
93    * @throws IllegalArgumentException if the tag is not supported
94    */

95   public static final int getDateFormat(String JavaDoc dateFormat)
96       throws IllegalArgumentException JavaDoc
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 JavaDoc("Invalid date format tag: "
106           + dateFormat);
107   }
108
109   /**
110    * Convert the date format from int (java code) to string (xml value)
111    *
112    * @param dateFormat the date format
113    * @return a string description corresponding to that level
114    * @throws IllegalArgumentException if the date format is not supported
115    */

116   public static final String JavaDoc getStringDateFormat(int dateFormat)
117       throws IllegalArgumentException JavaDoc
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 JavaDoc("Invalid date number format: "
129             + dateFormat);
130     }
131   }
132
133   /**
134    * @see org.continuent.sequoia.common.xml.XmlComponent#getXml()
135    */

136   public String JavaDoc 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