KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > cfg > Ejb3DiscriminatorColumn


1 //$Id: Ejb3DiscriminatorColumn.java,v 1.7 2005/06/16 19:07:13 epbernard Exp $
2
package org.hibernate.cfg;
3
4 import java.util.Map JavaDoc;
5 import javax.persistence.DiscriminatorColumn;
6 import javax.persistence.DiscriminatorType;
7
8 import org.hibernate.AssertionFailure;
9 import org.hibernate.annotations.DiscriminatorFormula;
10 import org.hibernate.mapping.Join;
11
12 /**
13  * Discriminator column
14  *
15  * @author Emmanuel Bernard
16  */

17 public class Ejb3DiscriminatorColumn extends Ejb3Column {
18
19
20     private static final String JavaDoc DEFAULT_DISCRIMINATOR_COLUMN_NAME = "TYPE";
21     private static final String JavaDoc DEFAULT_DISCRIMINATOR_TYPE = "string";
22
23     private String JavaDoc discriminatorTypeName;
24
25     public Ejb3DiscriminatorColumn() {
26         //discriminator default value
27
super();
28         setColumnName(DEFAULT_DISCRIMINATOR_COLUMN_NAME);
29         setNullable(false);
30         setDiscriminatorTypeName(DEFAULT_DISCRIMINATOR_TYPE);
31     }
32
33     public Ejb3DiscriminatorColumn(boolean isImplicit, String JavaDoc sqlType, int length, String JavaDoc name, boolean nullable, boolean unique, boolean insertable,
34                                    boolean updatable, String JavaDoc secondaryTableName, Map JavaDoc<String JavaDoc, Join> joins, PropertyHolder propertyHolder,
35                                    String JavaDoc discriminatorTypeName, Mappings mappings) {
36         super();
37         setImplicit(isImplicit);
38         setSqlType(sqlType);
39         setLength(length);
40         setColumnName(name);
41         setNullable(nullable);
42         setUnique(unique);
43         setInsertable(insertable);
44         setUpdatable(updatable);
45         setSecondaryTableName(secondaryTableName);
46         setJoins(joins);
47         setPropertyHolder(propertyHolder);
48         setMappings(mappings);
49         this.discriminatorTypeName = discriminatorTypeName;
50         bind();
51     }
52
53     public String JavaDoc getDiscriminatorTypeName() {
54         return discriminatorTypeName;
55     }
56
57     public void setDiscriminatorTypeName(String JavaDoc discriminatorTypeName) {
58         this.discriminatorTypeName = discriminatorTypeName;
59     }
60
61     public static Ejb3DiscriminatorColumn buildDiscriminatorColumn(
62             DiscriminatorType type, DiscriminatorColumn discAnn, DiscriminatorFormula discFormulaAnn, ExtendedMappings mappings
63             ) {
64         Ejb3DiscriminatorColumn discriminatorColumn = new Ejb3DiscriminatorColumn();
65         discriminatorColumn.setMappings(mappings);
66         discriminatorColumn.setImplicit(true);
67 // String discrSqlType = null;
68
// String discrColumnName = DEFAULT_DISCRIMINATOR_COLUMN_NAME;
69
// int discrLength = 255;
70
// boolean discrNullable = false;
71
// String discrTypeName = "string";
72
// boolean isImplicit = true;
73
if (discFormulaAnn != null) {
74             discriminatorColumn.setImplicit(false);
75             discriminatorColumn.setFormula( discFormulaAnn.value() );
76         }
77         else if (discAnn != null) {
78             discriminatorColumn.setImplicit(false);
79             if (! AnnotationBinder.isDefault( discAnn.columnDefinition() ) ) discriminatorColumn.setSqlType( discAnn.columnDefinition() );
80             if (! AnnotationBinder.isDefault( discAnn.name() ) ) discriminatorColumn.setColumnName( discAnn.name() );
81             discriminatorColumn.setNullable( discAnn.nullable() );
82         }
83 // boolean overrideDefaultValues = discAnn != null;
84
// if (overrideDefaultValues) {
85
// isImplicit = false;
86
// discrSqlType = "".equals( discAnn.columnDefinition() ) ? null : discAnn.columnDefinition();
87
// discrColumnName = "".equals( discAnn.name() ) ? DEFAULT_DISCRIMINATOR_COLUMN_NAME : discAnn.name();
88
// discrNullable = discAnn.nullable();
89
//
90
// }
91
if (DiscriminatorType.CHAR.equals(type) ) {
92             discriminatorColumn.setDiscriminatorTypeName("character");
93             discriminatorColumn.setImplicit(false);
94         }
95         else if (DiscriminatorType.INTEGER.equals(type) ) {
96             discriminatorColumn.setDiscriminatorTypeName("integer");
97             discriminatorColumn.setImplicit(false);
98         }
99         else if (DiscriminatorType.STRING.equals(type) || type == null ) {
100             if (discAnn != null) discriminatorColumn.setLength( discAnn.length() );
101             discriminatorColumn.setDiscriminatorTypeName("string");
102 // discrLength = overrideDefaultValues ? discAnn.length() : discrLength;
103
// discrTypeName = "string";
104
} else {
105             throw new AssertionFailure("Unknown discriminator type: " + type);
106         }
107 // discriminatorColumn = new Ejb3DiscriminatorColumn(isImplicit, discrSqlType, discrLength, discrColumnName, discrNullable,
108
// false, false, false, (String) null, null, null, discrTypeName, mappings);
109
discriminatorColumn.bind();
110         return discriminatorColumn;
111     }
112 }
113
Popular Tags