KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > mapping > Property


1 //$Id: Property.java,v 1.24 2005/07/14 23:55:01 oneovthafew Exp $
2
package org.hibernate.mapping;
3
4 import java.io.Serializable JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.StringTokenizer JavaDoc;
7
8 import org.hibernate.MappingException;
9 import org.hibernate.PropertyNotFoundException;
10 import org.hibernate.EntityMode;
11 import org.hibernate.engine.CascadeStyle;
12 import org.hibernate.engine.Mapping;
13 import org.hibernate.property.Getter;
14 import org.hibernate.property.PropertyAccessor;
15 import org.hibernate.property.PropertyAccessorFactory;
16 import org.hibernate.property.Setter;
17 import org.hibernate.type.AbstractComponentType;
18 import org.hibernate.type.Type;
19 import org.hibernate.util.ArrayHelper;
20
21 /**
22  * Represents a property as part of an entity or a component.
23  *
24  * @author Gavin King
25  */

26 public class Property implements Serializable JavaDoc, MetaAttributable {
27
28     private String JavaDoc name;
29     private Value value;
30     private String JavaDoc cascade;
31     private boolean updateable = true;
32     private boolean insertable = true;
33     private boolean selectable = true;
34     private boolean optimisticLocked = true;
35     private String JavaDoc propertyAccessorName;
36     private boolean lazy;
37     private boolean optional;
38     private String JavaDoc nodeName;
39     private java.util.Map JavaDoc metaAttributes;
40     private PersistentClass persistentClass;
41     private boolean naturalIdentifier;
42     
43     public boolean isBackRef() {
44         return false;
45     }
46
47     public Type getType() throws MappingException {
48         return value.getType();
49     }
50     
51     public int getColumnSpan() {
52         return value.getColumnSpan();
53     }
54     
55     public Iterator JavaDoc getColumnIterator() {
56         return value.getColumnIterator();
57     }
58     
59     public String JavaDoc getName() {
60         return name;
61     }
62     
63     public boolean isComposite() {
64         return value instanceof Component;
65     }
66
67     public Value getValue() {
68         return value;
69     }
70     
71     public boolean isPrimitive(Class JavaDoc clazz) {
72         return getGetter(clazz).getReturnType().isPrimitive();
73     }
74
75     public CascadeStyle getCascadeStyle() throws MappingException {
76         Type type = value.getType();
77         if ( type.isComponentType() && !type.isAnyType() ) {
78             AbstractComponentType actype = (AbstractComponentType) type;
79             int length = actype.getSubtypes().length;
80             for ( int i=0; i<length; i++ ) {
81                 if ( actype.getCascadeStyle(i)!=CascadeStyle.NONE ) return CascadeStyle.ALL;
82             }
83             return CascadeStyle.NONE;
84         }
85         else if ( cascade==null || cascade.equals("none") ) {
86             return CascadeStyle.NONE;
87         }
88         else {
89             StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(cascade, ", ");
90             CascadeStyle[] styles = new CascadeStyle[ tokens.countTokens() ] ;
91             int i=0;
92             while ( tokens.hasMoreTokens() ) {
93                 styles[i++] = CascadeStyle.getCascadeStyle( tokens.nextToken() );
94             }
95             return new CascadeStyle.MultipleCascadeStyle(styles);
96         }
97     }
98
99     public String JavaDoc getCascade() {
100         return cascade;
101     }
102
103     public void setCascade(String JavaDoc cascade) {
104         this.cascade = cascade;
105     }
106
107     public void setName(String JavaDoc name) {
108         this.name = name==null ? null : name.intern();
109     }
110
111     public void setValue(Value value) {
112         this.value = value;
113     }
114
115     public boolean isUpdateable() {
116         // if the property mapping consists of all formulas,
117
// make it non-updateable
118
final boolean[] columnUpdateability = value.getColumnUpdateability();
119         return updateable && (
120                 //columnUpdateability.length==0 ||
121
!ArrayHelper.isAllFalse(columnUpdateability)
122             );
123     }
124
125     public boolean isInsertable() {
126         // if the property mapping consists of all formulas,
127
// make it insertable
128
final boolean[] columnInsertability = value.getColumnInsertability();
129         return insertable && (
130                 columnInsertability.length==0 ||
131                 !ArrayHelper.isAllFalse(columnInsertability)
132             );
133     }
134
135     public void setUpdateable(boolean mutable) {
136         this.updateable = mutable;
137     }
138
139     public void setInsertable(boolean insertable) {
140         this.insertable = insertable;
141     }
142
143     public String JavaDoc getPropertyAccessorName() {
144         return propertyAccessorName;
145     }
146
147     public void setPropertyAccessorName(String JavaDoc string) {
148         propertyAccessorName = string;
149     }
150
151     /**
152      * Approximate!
153      */

154     boolean isNullable() {
155         return value==null || value.isNullable();
156     }
157
158     public boolean isBasicPropertyAccessor() {
159         return propertyAccessorName==null || "property".equals(propertyAccessorName);
160     }
161
162     public java.util.Map JavaDoc getMetaAttributes() {
163         return metaAttributes;
164     }
165
166     public MetaAttribute getMetaAttribute(String JavaDoc attributeName) {
167         return (MetaAttribute) metaAttributes.get(attributeName);
168     }
169
170     public void setMetaAttributes(java.util.Map JavaDoc metas) {
171         this.metaAttributes = metas;
172     }
173
174     public boolean isValid(Mapping mapping) throws MappingException {
175         return getValue().isValid(mapping);
176     }
177
178     public String JavaDoc toString() {
179         return getClass().getName() + '(' + name + ')';
180     }
181     
182     public void setLazy(boolean lazy) {
183         this.lazy=lazy;
184     }
185     
186     public boolean isLazy() {
187         return lazy;
188     }
189     
190     public boolean isOptimisticLocked() {
191         return optimisticLocked;
192     }
193
194     public void setOptimisticLocked(boolean optimisticLocked) {
195         this.optimisticLocked = optimisticLocked;
196     }
197     
198     public boolean isOptional() {
199         return optional || isNullable();
200     }
201     
202     public void setOptional(boolean optional) {
203         this.optional = optional;
204     }
205
206     public PersistentClass getPersistentClass() {
207         return persistentClass;
208     }
209
210     public void setPersistentClass(PersistentClass persistentClass) {
211         this.persistentClass = persistentClass;
212     }
213
214     public boolean isSelectable() {
215         return selectable;
216     }
217     
218     public void setSelectable(boolean selectable) {
219         this.selectable = selectable;
220     }
221
222     public String JavaDoc getNodeName() {
223         return nodeName;
224     }
225
226     public void setNodeName(String JavaDoc nodeName) {
227         this.nodeName = nodeName;
228     }
229
230     public String JavaDoc getAccessorPropertyName( EntityMode mode ) {
231         if ( mode == EntityMode.DOM4J ) {
232             return nodeName;
233         }
234         else {
235             return getName();
236         }
237     }
238
239     // todo : remove
240
public Getter getGetter(Class JavaDoc clazz) throws PropertyNotFoundException, MappingException {
241         return getPropertyAccessor(clazz).getGetter(clazz, name);
242     }
243
244     // todo : remove
245
public Setter getSetter(Class JavaDoc clazz) throws PropertyNotFoundException, MappingException {
246         return getPropertyAccessor(clazz).getSetter(clazz, name);
247     }
248
249     // todo : remove
250
public PropertyAccessor getPropertyAccessor(Class JavaDoc clazz) throws MappingException {
251         return PropertyAccessorFactory.getPropertyAccessor( clazz, getPropertyAccessorName() );
252     }
253
254     public boolean isNaturalIdentifier() {
255         return naturalIdentifier;
256     }
257
258     public void setNaturalIdentifier(boolean naturalIdentifier) {
259         this.naturalIdentifier = naturalIdentifier;
260     }
261 }
262
Popular Tags