View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.jelly.tags.sql;
17  
18  import java.sql.Date;
19  
20  import javax.servlet.jsp.jstl.sql.SQLExecutionTag;
21  
22  import org.apache.commons.jelly.JellyTagException;
23  import org.apache.commons.jelly.TagSupport;
24  import org.apache.commons.jelly.XMLOutput;
25  import org.apache.commons.jelly.tags.Resources;
26  
27  /***
28   * <p>Tag handler for &lt;Param&gt; in JSTL, used to set
29   * parameter values for a SQL statement.</p>
30   *
31   * @author Justyna Horwat
32   */
33  
34  public class DateParamTag extends TagSupport {
35  
36      //**********************************************************************
37      // Private constants
38  
39      private static final String TIMESTAMP_TYPE = "timestamp";
40      private static final String TIME_TYPE = "time";
41      private static final String DATE_TYPE = "date";
42  
43      //*********************************************************************
44      // Protected state
45  
46      protected String type;
47      protected java.util.Date value;
48  
49      //*********************************************************************
50      // Constructor
51  
52      public DateParamTag() {
53      }
54  
55      //*********************************************************************
56      // Properties
57  
58      public void setValue(Date value) {
59          this.value = value;
60      }
61  
62      public void setType(String type) {
63          this.type = type;
64      }
65  
66      //*********************************************************************
67      // Tag logic
68  
69      public void doTag(XMLOutput output) throws JellyTagException {
70          SQLExecutionTag parent =
71              (SQLExecutionTag) findAncestorWithClass(this, SQLExecutionTag.class);
72          if (parent == null) {
73              throw new JellyTagException(Resources.getMessage("SQL_PARAM_OUTSIDE_PARENT"));
74          }
75  
76          if (value != null) {
77              convertValue();
78          }
79  
80          parent.addSQLParameter(value);
81      }
82  
83      //*********************************************************************
84      // Private utility methods
85  
86      private void convertValue() throws JellyTagException {
87  
88          if ((type == null) || (type.equalsIgnoreCase(TIMESTAMP_TYPE))) {
89              if (!(value instanceof java.sql.Timestamp)) {
90                  value = new java.sql.Timestamp(value.getTime());
91              }
92          }
93          else if (type.equalsIgnoreCase(TIME_TYPE)) {
94              if (!(value instanceof java.sql.Time)) {
95                  value = new java.sql.Time(value.getTime());
96              }
97          }
98          else if (type.equalsIgnoreCase(DATE_TYPE)) {
99              if (!(value instanceof java.sql.Date)) {
100                 value = new java.sql.Date(value.getTime());
101             }
102         }
103         else {
104             throw new JellyTagException(
105                 Resources.getMessage("SQL_DATE_PARAM_INVALID_TYPE", type));
106         }
107     }
108 }