~ubuntu-branches/ubuntu/wily/libhibernate3-java/wily-proposed

« back to all changes in this revision

Viewing changes to src/org/hibernate/mapping/Property.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-10-14 14:43:34 UTC
  • Revision ID: james.westby@ubuntu.com-20071014144334-eamc8i0q10gs1aro
Tags: upstream-3.2.5
ImportĀ upstreamĀ versionĀ 3.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//$Id: Property.java 10247 2006-08-11 18:49:33Z steve.ebersole@jboss.com $
 
2
package org.hibernate.mapping;
 
3
 
 
4
import java.io.Serializable;
 
5
import java.util.HashMap;
 
6
import java.util.Iterator;
 
7
import java.util.StringTokenizer;
 
8
 
 
9
import org.hibernate.MappingException;
 
10
import org.hibernate.PropertyNotFoundException;
 
11
import org.hibernate.EntityMode;
 
12
import org.hibernate.engine.CascadeStyle;
 
13
import org.hibernate.engine.Mapping;
 
14
import org.hibernate.property.Getter;
 
15
import org.hibernate.property.PropertyAccessor;
 
16
import org.hibernate.property.PropertyAccessorFactory;
 
17
import org.hibernate.property.Setter;
 
18
import org.hibernate.type.AbstractComponentType;
 
19
import org.hibernate.type.Type;
 
20
import org.hibernate.util.ArrayHelper;
 
21
 
 
22
/**
 
23
 * Represents a property as part of an entity or a component.
 
24
 *
 
25
 * @author Gavin King
 
26
 */
 
27
public class Property implements Serializable, MetaAttributable {
 
28
 
 
29
        private String name;
 
30
        private Value value;
 
31
        private String cascade;
 
32
        private boolean updateable = true;
 
33
        private boolean insertable = true;
 
34
        private boolean selectable = true;
 
35
        private boolean optimisticLocked = true;
 
36
        private PropertyGeneration generation = PropertyGeneration.NEVER;
 
37
        private String propertyAccessorName;
 
38
        private boolean lazy;
 
39
        private boolean optional;
 
40
        private String nodeName;
 
41
        private java.util.Map metaAttributes;
 
42
        private PersistentClass persistentClass;
 
43
        private boolean naturalIdentifier;
 
44
        
 
45
        public boolean isBackRef() {
 
46
                return false;
 
47
        }
 
48
 
 
49
        public Type getType() throws MappingException {
 
50
                return value.getType();
 
51
        }
 
52
        
 
53
        public int getColumnSpan() {
 
54
                return value.getColumnSpan();
 
55
        }
 
56
        
 
57
        public Iterator getColumnIterator() {
 
58
                return value.getColumnIterator();
 
59
        }
 
60
        
 
61
        public String getName() {
 
62
                return name;
 
63
        }
 
64
        
 
65
        public boolean isComposite() {
 
66
                return value instanceof Component;
 
67
        }
 
68
 
 
69
        public Value getValue() {
 
70
                return value;
 
71
        }
 
72
        
 
73
        public boolean isPrimitive(Class clazz) {
 
74
                return getGetter(clazz).getReturnType().isPrimitive();
 
75
        }
 
76
 
 
77
        public CascadeStyle getCascadeStyle() throws MappingException {
 
78
                Type type = value.getType();
 
79
                if ( type.isComponentType() && !type.isAnyType() ) {
 
80
                        AbstractComponentType actype = (AbstractComponentType) type;
 
81
                        int length = actype.getSubtypes().length;
 
82
                        for ( int i=0; i<length; i++ ) {
 
83
                                if ( actype.getCascadeStyle(i)!=CascadeStyle.NONE ) return CascadeStyle.ALL;
 
84
                        }
 
85
                        return CascadeStyle.NONE;
 
86
                }
 
87
                else if ( cascade==null || cascade.equals("none") ) {
 
88
                        return CascadeStyle.NONE;
 
89
                }
 
90
                else {
 
91
                        StringTokenizer tokens = new StringTokenizer(cascade, ", ");
 
92
                        CascadeStyle[] styles = new CascadeStyle[ tokens.countTokens() ] ;
 
93
                        int i=0;
 
94
                        while ( tokens.hasMoreTokens() ) {
 
95
                                styles[i++] = CascadeStyle.getCascadeStyle( tokens.nextToken() );
 
96
                        }
 
97
                        return new CascadeStyle.MultipleCascadeStyle(styles);
 
98
                }
 
99
        }
 
100
 
 
101
        public String getCascade() {
 
102
                return cascade;
 
103
        }
 
104
 
 
105
        public void setCascade(String cascade) {
 
106
                this.cascade = cascade;
 
107
        }
 
108
 
 
109
        public void setName(String name) {
 
110
                this.name = name==null ? null : name.intern();
 
111
        }
 
112
 
 
113
        public void setValue(Value value) {
 
114
                this.value = value;
 
115
        }
 
116
 
 
117
        public boolean isUpdateable() {
 
118
                // if the property mapping consists of all formulas, 
 
119
                // make it non-updateable
 
120
                final boolean[] columnUpdateability = value.getColumnUpdateability();
 
121
                return updateable && ( 
 
122
                                //columnUpdateability.length==0 ||
 
123
                                !ArrayHelper.isAllFalse(columnUpdateability)
 
124
                        );
 
125
        }
 
126
 
 
127
        public boolean isInsertable() {
 
128
                // if the property mapping consists of all formulas, 
 
129
                // make it insertable
 
130
                final boolean[] columnInsertability = value.getColumnInsertability();
 
131
                return insertable && (
 
132
                                columnInsertability.length==0 ||
 
133
                                !ArrayHelper.isAllFalse(columnInsertability)
 
134
                        );
 
135
        }
 
136
 
 
137
    public PropertyGeneration getGeneration() {
 
138
        return generation;
 
139
    }
 
140
 
 
141
    public void setGeneration(PropertyGeneration generation) {
 
142
        this.generation = generation;
 
143
    }
 
144
 
 
145
    public void setUpdateable(boolean mutable) {
 
146
                this.updateable = mutable;
 
147
        }
 
148
 
 
149
        public void setInsertable(boolean insertable) {
 
150
                this.insertable = insertable;
 
151
        }
 
152
 
 
153
        public String getPropertyAccessorName() {
 
154
                return propertyAccessorName;
 
155
        }
 
156
 
 
157
        public void setPropertyAccessorName(String string) {
 
158
                propertyAccessorName = string;
 
159
        }
 
160
 
 
161
        /**
 
162
         * Approximate!
 
163
         */
 
164
        boolean isNullable() {
 
165
                return value==null || value.isNullable();
 
166
        }
 
167
 
 
168
        public boolean isBasicPropertyAccessor() {
 
169
                return propertyAccessorName==null || "property".equals(propertyAccessorName);
 
170
        }
 
171
 
 
172
        public java.util.Map getMetaAttributes() {
 
173
                return metaAttributes;
 
174
        }
 
175
 
 
176
        public MetaAttribute getMetaAttribute(String attributeName) {
 
177
                return metaAttributes==null?null:(MetaAttribute) metaAttributes.get(attributeName);
 
178
        }
 
179
 
 
180
        public void setMetaAttributes(java.util.Map metas) {
 
181
                this.metaAttributes = metas;
 
182
        }
 
183
 
 
184
        public boolean isValid(Mapping mapping) throws MappingException {
 
185
                return getValue().isValid(mapping);
 
186
        }
 
187
 
 
188
        public String toString() {
 
189
                return getClass().getName() + '(' + name + ')';
 
190
        }
 
191
        
 
192
        public void setLazy(boolean lazy) {
 
193
                this.lazy=lazy;
 
194
        }
 
195
        
 
196
        public boolean isLazy() {
 
197
                if ( value instanceof ToOne ) {
 
198
                        // both many-to-one and one-to-one are represented as a
 
199
                        // Property.  EntityPersister is relying on this value to
 
200
                        // determine "lazy fetch groups" in terms of field-level
 
201
                        // interception.  So we need to make sure that we return
 
202
                        // true here for the case of many-to-one and one-to-one
 
203
                        // with lazy="no-proxy"
 
204
                        //
 
205
                        // * impl note - lazy="no-proxy" currently forces both
 
206
                        // lazy and unwrap to be set to true.  The other case we
 
207
                        // are extremely interested in here is that of lazy="proxy"
 
208
                        // where lazy is set to true, but unwrap is set to false.
 
209
                        // thus we use both here under the assumption that this
 
210
                        // return is really only ever used during persister
 
211
                        // construction to determine the lazy property/field fetch
 
212
                        // groupings.  If that assertion changes then this check
 
213
                        // needs to change as well.  Partially, this is an issue with
 
214
                        // the overloading of the term "lazy" here...
 
215
                        ToOne toOneValue = ( ToOne ) value;
 
216
                        return toOneValue.isLazy() && toOneValue.isUnwrapProxy();
 
217
                }
 
218
                return lazy;
 
219
        }
 
220
        
 
221
        public boolean isOptimisticLocked() {
 
222
                return optimisticLocked;
 
223
        }
 
224
 
 
225
        public void setOptimisticLocked(boolean optimisticLocked) {
 
226
                this.optimisticLocked = optimisticLocked;
 
227
        }
 
228
        
 
229
        public boolean isOptional() {
 
230
                return optional || isNullable();
 
231
        }
 
232
        
 
233
        public void setOptional(boolean optional) {
 
234
                this.optional = optional;
 
235
        }
 
236
 
 
237
        public PersistentClass getPersistentClass() {
 
238
                return persistentClass;
 
239
        }
 
240
 
 
241
        public void setPersistentClass(PersistentClass persistentClass) {
 
242
                this.persistentClass = persistentClass;
 
243
        }
 
244
 
 
245
        public boolean isSelectable() {
 
246
                return selectable;
 
247
        }
 
248
        
 
249
        public void setSelectable(boolean selectable) {
 
250
                this.selectable = selectable;
 
251
        }
 
252
 
 
253
        public String getNodeName() {
 
254
                return nodeName;
 
255
        }
 
256
 
 
257
        public void setNodeName(String nodeName) {
 
258
                this.nodeName = nodeName;
 
259
        }
 
260
 
 
261
        public String getAccessorPropertyName( EntityMode mode ) {
 
262
                if ( mode == EntityMode.DOM4J ) {
 
263
                        return nodeName;
 
264
                }
 
265
                else {
 
266
                        return getName();
 
267
                }
 
268
        }
 
269
 
 
270
        // todo : remove
 
271
        public Getter getGetter(Class clazz) throws PropertyNotFoundException, MappingException {
 
272
                return getPropertyAccessor(clazz).getGetter(clazz, name);
 
273
        }
 
274
 
 
275
        // todo : remove
 
276
        public Setter getSetter(Class clazz) throws PropertyNotFoundException, MappingException {
 
277
                return getPropertyAccessor(clazz).getSetter(clazz, name);
 
278
        }
 
279
 
 
280
        // todo : remove
 
281
        public PropertyAccessor getPropertyAccessor(Class clazz) throws MappingException {
 
282
                return PropertyAccessorFactory.getPropertyAccessor( clazz, getPropertyAccessorName() );
 
283
        }
 
284
 
 
285
        public boolean isNaturalIdentifier() {
 
286
                return naturalIdentifier;
 
287
        }
 
288
 
 
289
        public void setNaturalIdentifier(boolean naturalIdentifier) {
 
290
                this.naturalIdentifier = naturalIdentifier;
 
291
        }
 
292
}