root/trunk/src/java/org/jcoderz/commons/util/StringUserTypeBase.java

Revision 1011, 3.7 kB (checked in by amandel, 7 months ago)

Aligned svn keyword settings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
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 */
33package org.jcoderz.commons.util;
34
35import java.sql.PreparedStatement;
36import java.sql.ResultSet;
37import java.sql.SQLException;
38import java.sql.Types;
39
40import 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 */
51public abstract class StringUserTypeBase
52    extends UserTypeBase
53{
54    private static final int SQL_TYPE = Types.VARCHAR;
55    private static final int[] SQL_TYPES = {SQL_TYPE};
56
57    /**
58     * {@inheritDoc}
59     */
60    public Object nullSafeGet (ResultSet resultSet, String[] types,
61        Object owner)
62        throws HibernateException, SQLException
63    {
64        final String value = resultSet.getString(types[0]);
65        final Object result;
66        if (value == null || resultSet.wasNull())
67        {
68            result = null;
69        }
70        else if (value.length() == 0)
71        {
72            result = getEmptyOrNull();
73        }
74        else
75        {
76            result = fromString(value);
77        }
78        return result;
79    }
80
81    /**
82     * Implement this method for the particular RestrictedString.
83     *
84     * @return either null or <code>fromString("")</code> if the
85     *   StrongType permits this.
86     */
87    public abstract Object getEmptyOrNull ();
88
89    /**
90     * Implement this method for the particular RestrictedString.
91     *
92     * @param value the String representation of the mapped class
93     * @return the instance of the mapped class
94     */
95    public abstract Object fromString (String value);
96
97    /**
98     * {@inheritDoc}
99     */
100    public void nullSafeSet (PreparedStatement statement, Object value,
101        int index)
102        throws HibernateException, SQLException
103    {
104        if (value != null)
105        {
106            statement.setString(index, value.toString());
107        }
108        else
109        {
110            statement.setNull(index, SQL_TYPE);
111        }
112    }
113
114    /**
115     * {@inheritDoc}
116     */
117    public int[] sqlTypes ()
118    {
119      return (int[]) SQL_TYPES.clone();
120    }
121}
Note: See TracBrowser for help on using the browser.