package com.ora.jsp.tags.sql.value;
import java.lang.reflect.*;
import java.text.*;
import java.sql.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import com.ora.jsp.sql.value.*;
import com.ora.jsp.tags.sql.ValueTagParent;
import com.ora.jsp.util.*;
/**
* This class is a custom action intended to be used in the body of
* a sqlQuery or an sqlUpdate action element. It adds the specified
* value to its parent's value list.
*
* @author Hans Bergsten, Gefion software <hans@gefionsoftware.com>
* @version 1.0
*/
public class TimestampValueTag extends ValueTag {
private Timestamp value;
/**
* Sets the value property.
*/
public void setValue(java.util.Date value) {
// Convert the java.util.Date to a java.sql.Timestamp
this.value = new Timestamp(value.getTime());
}
/**
* Gets the value, specified by the value attribute, the stringValue
* attribute, the param attribute, or the name and property attributes,
* and adds it to the parent's value list. If the value is a String
* and a pattern attribute is specified, the String is converted
* into the appropriate type before being added to the parent's
* value list.
*/
public int doEndTag() throws JspException {
if (stringValue != null) {
value = toTimestamp(stringValue, pattern);
}
else if (param != null) {
String paramValue = getParameter(param);
value = toTimestamp(paramValue, pattern);
}
else if (name != null) {
value = getTimestamp(name, property, pattern);
}
ValueTagParent parent =
(ValueTagParent) findAncestorWithClass(this, ValueTagParent.class);
if (parent == null) {
throw new JspException("The sqlTimestampValue action is not " +
"enclosed by a supported action type");
}
parent.addValue(new TimestampValue(value));
return EVAL_PAGE;
}
/**
* Releases all instance variables.
*/
public void release() {
value = null;
super.release();
}
private Timestamp toTimestamp(String stringValue, String pattern)
throws JspException {
Timestamp timestampValue = null;
try {
java.util.Date date = StringFormat.toDate(stringValue, pattern);
timestampValue = new Timestamp(date.getTime());
}
catch (ParseException e) {
throw new JspException(stringValue + " can not be converted to " +
" a BigDecimal using pattern: " + pattern + ". Message: " +
e.getMessage());
}
return timestampValue;
}
private Timestamp getTimestamp(String beanName, String propertyName,
String pattern) throws JspException {
Timestamp timestampValue = null;
Object bean = getBean(beanName);
Method readMethod = getPropertyReadMethod(bean, propertyName);
Class returnType = readMethod.getReturnType();
Object value = getValue(bean, readMethod, propertyName);
if (Timestamp.class.isAssignableFrom(returnType)) {
timestampValue = (Timestamp) value;
}
else if (String.class.isAssignableFrom(returnType)) {
timestampValue = toTimestamp((String) value, pattern);
}
else {
throw new JspException("Read method for the " + propertyName +
" property in the bean named " + beanName + " is not of type " +
" String or Timestamp");
}
return timestampValue;
}
} |