001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Portions Copyright Apache Software Foundation.
007: *
008: * The contents of this file are subject to the terms of either the GNU
009: * General Public License Version 2 only ("GPL") or the Common Development
010: * and Distribution License("CDDL") (collectively, the "License"). You
011: * may not use this file except in compliance with the License. You can obtain
012: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
013: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
014: * language governing permissions and limitations under the License.
015: *
016: * When distributing the software, include this License Header Notice in each
017: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
018: * Sun designates this particular file as subject to the "Classpath" exception
019: * as provided by Sun in the GPL Version 2 section of the License file that
020: * accompanied this code. If applicable, add the following below the License
021: * Header, with the fields enclosed by brackets [] replaced by your own
022: * identifying information: "Portions Copyrighted [year]
023: * [name of copyright owner]"
024: *
025: * Contributor(s):
026: *
027: * If you wish your version of this file to be governed by only the CDDL or
028: * only the GPL Version 2, indicate your decision by adding "[Contributor]
029: * elects to include this software in this distribution under the [CDDL or GPL
030: * Version 2] license." If you don't indicate a single choice of license, a
031: * recipient has the option to distribute your version of this file under
032: * either the CDDL, the GPL Version 2 or to extend the choice of license to
033: * its licensees as provided above. However, if you add GPL Version 2 code
034: * and therefore, elected the GPL Version 2 license, then the option applies
035: * only if the new code is made subject to such option by the copyright
036: * holder.
037: */
038: package org.apache.taglibs.standard.tag.common.sql;
039:
040: import javax.servlet.jsp.JspException;
041: import javax.servlet.jsp.JspTagException;
042: import javax.servlet.jsp.jstl.sql.SQLExecutionTag;
043: import javax.servlet.jsp.tagext.TagSupport;
044:
045: import org.apache.taglibs.standard.resources.Resources;
046:
047: /**
048: * <p>Tag handler for <Param> in JSTL, used to set
049: * parameter values for a SQL statement.</p>
050: *
051: * @author Justyna Horwat
052: */
053:
054: public abstract class DateParamTagSupport extends TagSupport {
055:
056: //*********************************************************************
057: // Private constants
058:
059: private static final String TIMESTAMP_TYPE = "timestamp";
060: private static final String TIME_TYPE = "time";
061: private static final String DATE_TYPE = "date";
062:
063: //*********************************************************************
064: // Protected state
065:
066: protected String type;
067: protected java.util.Date value;
068:
069: //*********************************************************************
070: // Constructor
071:
072: public DateParamTagSupport() {
073: super ();
074: init();
075: }
076:
077: private void init() {
078: value = null;
079: type = null;
080: }
081:
082: //*********************************************************************
083: // Tag logic
084:
085: public int doEndTag() throws JspException {
086: SQLExecutionTag parent = (SQLExecutionTag) findAncestorWithClass(
087: this , SQLExecutionTag.class);
088: if (parent == null) {
089: throw new JspTagException(Resources
090: .getMessage("SQL_PARAM_OUTSIDE_PARENT"));
091: }
092:
093: if (value != null) {
094: convertValue();
095: }
096:
097: parent.addSQLParameter(value);
098: return EVAL_PAGE;
099: }
100:
101: //*********************************************************************
102: // Private utility methods
103:
104: private void convertValue() throws JspException {
105:
106: if ((type == null) || (type.equalsIgnoreCase(TIMESTAMP_TYPE))) {
107: if (!(value instanceof java.sql.Timestamp)) {
108: value = new java.sql.Timestamp(value.getTime());
109: }
110: } else if (type.equalsIgnoreCase(TIME_TYPE)) {
111: if (!(value instanceof java.sql.Time)) {
112: value = new java.sql.Time(value.getTime());
113: }
114: } else if (type.equalsIgnoreCase(DATE_TYPE)) {
115: if (!(value instanceof java.sql.Date)) {
116: value = new java.sql.Date(value.getTime());
117: }
118: } else {
119: throw new JspException(Resources.getMessage(
120: "SQL_DATE_PARAM_INVALID_TYPE", type));
121: }
122: }
123: }
|