001: /*
002: * Copyright 2006 Davide Deidda
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: /*
018: * DateTimeType.java
019: *
020: * Created on 29 novembre 2003, 12.31
021: */
022:
023: package it.biobytes.ammentos.fieldtypes;
024:
025: import java.sql.*;
026: import java.text.*;
027: import java.util.*;
028:
029: import it.biobytes.ammentos.*;
030:
031: /**
032: *
033: * @author davide
034: */
035: public class DateTimeType implements FieldType {
036: private static SimpleDateFormat m_format;
037: private static List m_validFormats;
038:
039: static {
040: m_validFormats = new ArrayList();
041: m_format = new SimpleDateFormat("dd/MM/yyyy HH:mm");
042: m_validFormats.add(m_format);
043:
044: m_validFormats.add(new SimpleDateFormat("dd/MM/yyyy"));
045: m_validFormats.add(new SimpleDateFormat("HH:mm"));
046: }
047:
048: public Object loadValue(java.sql.ResultSet rs, Field field)
049: throws SQLException {
050: return (java.sql.Timestamp) rs.getTimestamp(field.getName());
051: }
052:
053: public Object parseValue(String str) throws PersistenceException {
054: Iterator it = m_validFormats.iterator();
055: java.util.Date res = null;
056: while ((res == null) && (it.hasNext())) {
057: DateFormat df = (DateFormat) it.next();
058: try {
059: res = df.parse(str);
060: } catch (Exception e) {
061: }
062: }
063:
064: if (res == null) {
065: throw new PersistenceException("Unparseable date: " + str);
066: }
067:
068: return res;
069: }
070:
071: public void setParamValue(Object fieldValue,
072: java.sql.PreparedStatement pstmt, int paramIndex)
073: throws SQLException {
074: pstmt.setTimestamp(paramIndex, new java.sql.Timestamp(
075: ((java.util.Date) fieldValue).getTime()));
076: }
077:
078: public String formatValue(Object value) {
079: String res = null;
080: if (value != null)
081: res = m_format.format((java.util.Date) value);
082: return res;
083: }
084:
085: public Object generateValue() throws PersistenceException {
086: throw new PersistenceException(
087: "unable to generate automatic value for type SqlDateTime");
088: }
089:
090: public boolean isNumeric() {
091: return false;
092: }
093:
094: public Object[] getPossibleValues() {
095: return null;
096: }
097:
098: /**
099: * Returns the sum of the provided values, or null if summing is not possible
100: *
101: * @param value1 First value to add
102: * @param value2 Second value to add
103: */
104: public Object addValues(Object value1, Object value2) {
105: return null;
106: }
107:
108: /**
109: * Gets the underlying mappes Java class represented from elements of this
110: * type
111: *
112: * @return the Java class mapped from this FieldType
113: */
114: public Class getMappedClass() {
115: return java.util.Date.class;
116: }
117:
118: public int getSqlType() {
119: return Types.TIMESTAMP;
120: }
121: }
|