KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > setup > impl > dl > dbloader > hibernate > HibernateColumnMetaData


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.setup.impl.dl.dbloader.hibernate;
10
11 import org.jboss.portal.setup.impl.dl.dbloader.ColumnMetaData;
12 import org.jboss.portal.setup.PortalSetupException;
13 import org.hibernate.type.LiteralType;
14 import org.hibernate.type.NullableType;
15 import org.hibernate.type.Type;
16 import org.hibernate.type.TypeFactory;
17 import org.hibernate.tool.hbm2ddl.ColumnMetadata;
18 import org.hibernate.dialect.Dialect;
19 import org.hibernate.mapping.Column;
20 import org.hibernate.cfg.Configuration;
21 import org.hibernate.MappingException;
22 import org.hibernate.HibernateException;
23
24
25 import javax.sound.sampled.EnumControl JavaDoc;
26 import java.sql.Types JavaDoc;
27
28
29 /**
30  * @author <a HREF="mailto:palber@novell.com">Polina Alber</a>
31  * Date: Apr 15, 2005; Time: 1:28:12 PM
32  * @since JBoss portal 2.0
33  * Class org.jboss.portal.setup.impl.dl.dbloader.hibernate.HibernateColumnMetaData
34  */

35 public class HibernateColumnMetaData extends ColumnMetaData
36 {
37    private String JavaDoc m_dbTypeName;
38    private LiteralType m_literalType;
39    private int m_size = -1;
40
41    public String JavaDoc getDbTypeName()
42    {
43       return m_dbTypeName;
44    }
45
46    public HibernateColumnMetaData(String JavaDoc columnName, String JavaDoc javaType, int columnSize)
47    {
48       super(columnName, javaType, columnSize);
49    }
50
51    public int getSize()
52    {
53       return m_size;
54    }
55
56    public LiteralType getLiteralType()
57    {
58       return m_literalType;
59    }
60
61    void setColumnTypeValues(ColumnMetadata md, Dialect dialect, Column column, Configuration cfg) throws PortalSetupException
62    {
63       m_dbTypeName = column.getSqlType(); // md.getTypeName();
64

65       try
66       {
67          m_literalType = (LiteralType)TypeFactory.heuristicType(getJavaType());
68          int sqlTypeFromLiteral = ((NullableType)m_literalType).sqlType();
69          String JavaDoc dbString = dialect.getTypeName(sqlTypeFromLiteral);
70          int firstParent = dbString.indexOf('(');
71          if (firstParent > 0)
72          {
73             m_dbTypeName = dbString.substring(0, firstParent);
74             int lastparent = dbString.indexOf(')');
75             String JavaDoc length = dbString.substring(firstParent, lastparent);
76             try
77             {
78                m_size = Integer.parseInt(length);
79             }
80             catch (Throwable JavaDoc th)
81             {
82                //skip for now
83
}
84          }
85          else
86          {
87             m_dbTypeName = dbString;
88          }
89
90       }
91       catch (MappingException me)
92       {
93          throw new PortalSetupException("failed to resolve data literal type");
94       }
95       catch (HibernateException he)
96       {
97          throw new PortalSetupException("failed to resolve data literal type");
98       }
99
100    }
101
102    int getSqlTypeIndex()
103    {
104       return m_literalType == null ? -1 : ((NullableType)m_literalType).sqlType();
105    }
106
107
108 }
109
Popular Tags