| Line | Hits | Note | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * $Id: DateUserType.java 1441 2009-05-06 08:59:10Z amandel $ | ||
| 3 | * | ||
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. | ||
| 5 | * | ||
| 6 | * Redistribution and use in source and binary forms, with or without | ||
| 7 | * modification, are permitted provided that the following conditions are | ||
| 8 | * met: | ||
| 9 | * | ||
| 10 | * * Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * * Redistributions in binary form must reproduce the above | ||
| 13 | * copyright notice, this list of conditions and the following | ||
| 14 | * disclaimer in the documentation and/or other materials | ||
| 15 | * provided with the distribution. | ||
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of | ||
| 17 | * its contributors may be used to endorse or promote products | ||
| 18 | * derived from this software without specific prior written | ||
| 19 | * permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS | ||
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR | ||
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | ||
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | ||
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | package org.jcoderz.commons.util; | ||
| 34 | |||
| 35 | import java.sql.PreparedStatement; | ||
| 36 | import java.sql.ResultSet; | ||
| 37 | import java.sql.SQLException; | ||
| 38 | import java.sql.Timestamp; | ||
| 39 | import java.sql.Types; | ||
| 40 | |||
| 41 | import org.hibernate.HibernateException; | ||
| 42 | import org.jcoderz.commons.types.Date; | ||
| 43 | |||
| 44 | /** | ||
| 45 | * This is the Hibernate user type that maps a {@link Date} to a | ||
| 46 | * timestamp in the Database. | ||
| 47 | * | ||
| 48 | * @author Andreas Mandel | ||
| 49 | */ | ||
| 50 | 0 | public class DateUserType | |
| 51 | extends UserTypeBase | ||
| 52 | { | ||
| 53 | private static final long serialVersionUID = 1L; | ||
| 54 | private static final int SQL_TYPE = Types.TIMESTAMP; | ||
| 55 | 0 | private static final int[] SQL_TYPES = {SQL_TYPE}; | |
| 56 | |||
| 57 | /** | ||
| 58 | * Hibernate <tt>org.jcoderz.commons.types.Date</tt> type as mapped | ||
| 59 | * from this UserType. | ||
| 60 | * @return this UserType as org.hibernate.type.Type. | ||
| 61 | */ | ||
| 62 | public static org.hibernate.type.Type getType () | ||
| 63 | { | ||
| 64 | 0 | return TypeHolder.TYPE; | |
| 65 | } | ||
| 66 | |||
| 67 | /** {@inheritDoc} */ | ||
| 68 | public Object nullSafeGet ( | ||
| 69 | ResultSet resultSet, String[] types, Object owner) | ||
| 70 | (1) | throws HibernateException, SQLException | |
| 71 | { | ||
| 72 | 0 | final Timestamp timestamp = resultSet.getTimestamp(types[0]); | |
| 73 | final Date result; | ||
| 74 | 0 | if (timestamp == null || resultSet.wasNull()) | |
| 75 | { | ||
| 76 | 0 | result = null; | |
| 77 | } | ||
| 78 | else | ||
| 79 | { | ||
| 80 | 0 | result = Date.fromSqlTimestamp(timestamp); | |
| 81 | } | ||
| 82 | 0 | return result; | |
| 83 | } | ||
| 84 | |||
| 85 | /** {@inheritDoc} */ | ||
| 86 | public void nullSafeSet (PreparedStatement statement, Object value, | ||
| 87 | int index) | ||
| 88 | (2) | throws HibernateException, SQLException | |
| 89 | { | ||
| 90 | 0 | if (value != null) | |
| 91 | { | ||
| 92 | 0 | statement.setTimestamp(index, ((Date) value).toSqlTimestamp()); | |
| 93 | } | ||
| 94 | else | ||
| 95 | { | ||
| 96 | 0 | statement.setNull(index, SQL_TYPE); | |
| 97 | } | ||
| 98 | 0 | } | |
| 99 | |||
| 100 | /** {@inheritDoc} */ | ||
| 101 | public Class returnedClass () | ||
| 102 | { | ||
| 103 | 0 | return Date.class; | |
| 104 | } | ||
| 105 | |||
| 106 | /** {@inheritDoc} */ | ||
| 107 | public int[] sqlTypes () | ||
| 108 | { | ||
| 109 | 0 | return (int[]) SQL_TYPES.clone(); | |
| 110 | } | ||
| 111 | |||
| 112 | /** | ||
| 113 | * Class to lazy initialize the Hibernate Type adapter. | ||
| 114 | */ | ||
| 115 | 0 | (3) | private static class TypeHolder |
| 116 | { | ||
| 117 | 0 | private static final org.hibernate.type.Type TYPE | |
| 118 | = new org.hibernate.type.CustomType(DateUserType.class, null); | ||
| 119 | } | ||
| 120 | } |