| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 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.Types; |
|---|
| 39 | |
|---|
| 40 | import org.hibernate.HibernateException; |
|---|
| 41 | |
|---|
| 42 | /** |
|---|
| 43 | * |
|---|
| 44 | * This class is used by the Hibernate binding for RestrictedStrings. |
|---|
| 45 | * A subclass has to implement the <code>fromString</code> method |
|---|
| 46 | * for the particular RestrictedString. Be aware that VARCHAR is |
|---|
| 47 | * restricted to 1000 Unicode characters! |
|---|
| 48 | * |
|---|
| 49 | * @author thomas.bodemer |
|---|
| 50 | */ |
|---|
| 51 | public abstract class StringUserTypeBase |
|---|
| 52 | extends UserTypeBase |
|---|
| 53 | { |
|---|
| 54 | private static final long serialVersionUID = 1L; |
|---|
| 55 | private static final int SQL_TYPE = Types.VARCHAR; |
|---|
| 56 | private static final int[] SQL_TYPES = {SQL_TYPE}; |
|---|
| 57 | |
|---|
| 58 | /** |
|---|
| 59 | * {@inheritDoc} |
|---|
| 60 | */ |
|---|
| 61 | public Object nullSafeGet (ResultSet resultSet, String[] types, |
|---|
| 62 | Object owner) |
|---|
| 63 | throws HibernateException, SQLException |
|---|
| 64 | { |
|---|
| 65 | final String value = resultSet.getString(types[0]); |
|---|
| 66 | final Object result; |
|---|
| 67 | if (value == null || resultSet.wasNull()) |
|---|
| 68 | { |
|---|
| 69 | result = null; |
|---|
| 70 | } |
|---|
| 71 | else if (value.length() == 0) |
|---|
| 72 | { |
|---|
| 73 | result = getEmptyOrNull(); |
|---|
| 74 | } |
|---|
| 75 | else |
|---|
| 76 | { |
|---|
| 77 | result = fromString(value); |
|---|
| 78 | } |
|---|
| 79 | return result; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | /** |
|---|
| 83 | * Implement this method for the particular RestrictedString. |
|---|
| 84 | * |
|---|
| 85 | * @return either null or <code>fromString("")</code> if the |
|---|
| 86 | * StrongType permits this. |
|---|
| 87 | */ |
|---|
| 88 | public abstract Object getEmptyOrNull (); |
|---|
| 89 | |
|---|
| 90 | /** |
|---|
| 91 | * Implement this method for the particular RestrictedString. |
|---|
| 92 | * |
|---|
| 93 | * @param value the String representation of the mapped class |
|---|
| 94 | * @return the instance of the mapped class |
|---|
| 95 | */ |
|---|
| 96 | public abstract Object fromString (String value); |
|---|
| 97 | |
|---|
| 98 | /** |
|---|
| 99 | * {@inheritDoc} |
|---|
| 100 | */ |
|---|
| 101 | public void nullSafeSet (PreparedStatement statement, Object value, |
|---|
| 102 | int index) |
|---|
| 103 | throws HibernateException, SQLException |
|---|
| 104 | { |
|---|
| 105 | if (value != null) |
|---|
| 106 | { |
|---|
| 107 | statement.setString(index, value.toString()); |
|---|
| 108 | } |
|---|
| 109 | else |
|---|
| 110 | { |
|---|
| 111 | statement.setNull(index, SQL_TYPE); |
|---|
| 112 | } |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | /** |
|---|
| 116 | * {@inheritDoc} |
|---|
| 117 | */ |
|---|
| 118 | public int[] sqlTypes () |
|---|
| 119 | { |
|---|
| 120 | return (int[]) SQL_TYPES.clone(); |
|---|
| 121 | } |
|---|
| 122 | } |
|---|