1 package org.hibernate.mapping; 3 4 import java.io.Serializable ; 5 import java.util.Iterator ; 6 import java.util.StringTokenizer ; 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 26 public class Property implements Serializable , MetaAttributable { 27 28 private String name; 29 private Value value; 30 private String cascade; 31 private boolean updateable = true; 32 private boolean insertable = true; 33 private boolean selectable = true; 34 private boolean optimisticLocked = true; 35 private String propertyAccessorName; 36 private boolean lazy; 37 private boolean optional; 38 private String nodeName; 39 private java.util.Map 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 getColumnIterator() { 56 return value.getColumnIterator(); 57 } 58 59 public String 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 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 tokens = new StringTokenizer (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 getCascade() { 100 return cascade; 101 } 102 103 public void setCascade(String cascade) { 104 this.cascade = cascade; 105 } 106 107 public void setName(String 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 final boolean[] columnUpdateability = value.getColumnUpdateability(); 119 return updateable && ( 120 !ArrayHelper.isAllFalse(columnUpdateability) 122 ); 123 } 124 125 public boolean isInsertable() { 126 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 getPropertyAccessorName() { 144 return propertyAccessorName; 145 } 146 147 public void setPropertyAccessorName(String string) { 148 propertyAccessorName = string; 149 } 150 151 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 getMetaAttributes() { 163 return metaAttributes; 164 } 165 166 public MetaAttribute getMetaAttribute(String attributeName) { 167 return (MetaAttribute) metaAttributes.get(attributeName); 168 } 169 170 public void setMetaAttributes(java.util.Map 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 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 getNodeName() { 223 return nodeName; 224 } 225 226 public void setNodeName(String nodeName) { 227 this.nodeName = nodeName; 228 } 229 230 public String getAccessorPropertyName( EntityMode mode ) { 231 if ( mode == EntityMode.DOM4J ) { 232 return nodeName; 233 } 234 else { 235 return getName(); 236 } 237 } 238 239 public Getter getGetter(Class clazz) throws PropertyNotFoundException, MappingException { 241 return getPropertyAccessor(clazz).getGetter(clazz, name); 242 } 243 244 public Setter getSetter(Class clazz) throws PropertyNotFoundException, MappingException { 246 return getPropertyAccessor(clazz).getSetter(clazz, name); 247 } 248 249 public PropertyAccessor getPropertyAccessor(Class 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 |