1 package org.hibernate.mapping; 3 4 import java.util.ArrayList ; 5 import java.util.HashMap ; 6 import java.util.Iterator ; 7 import java.util.Map ; 8 9 import org.hibernate.EntityMode; 10 import org.hibernate.FetchMode; 11 import org.hibernate.MappingException; 12 import org.hibernate.engine.CascadeStyle; 13 import org.hibernate.tuple.TuplizerLookup; 14 import org.hibernate.type.ComponentType; 15 import org.hibernate.type.EmbeddedComponentType; 16 import org.hibernate.type.Type; 17 import org.hibernate.util.JoinedIterator; 18 import org.hibernate.util.ReflectHelper; 19 20 25 public class Component extends SimpleValue implements MetaAttributable { 26 27 private ArrayList properties = new ArrayList (); 28 private String componentClassName; 29 private boolean embedded; 30 private String parentProperty; 31 private PersistentClass owner; 32 private boolean dynamic; 33 private Map metaAttributes; 34 private String nodeName; 35 private boolean isKey; 36 37 private java.util.Map tuplizerImpls; 38 39 public int getPropertySpan() { 40 return properties.size(); 41 } 42 public Iterator getPropertyIterator() { 43 return properties.iterator(); 44 } 45 public void addProperty(Property p) { 46 properties.add(p); 47 } 48 public void addColumn(Column column) { 49 throw new UnsupportedOperationException ("Cant add a column to a component"); 50 } 51 public int getColumnSpan() { 52 int n=0; 53 Iterator iter = getPropertyIterator(); 54 while ( iter.hasNext() ) { 55 Property p = (Property) iter.next(); 56 n+= p.getColumnSpan(); 57 } 58 return n; 59 } 60 public Iterator getColumnIterator() { 61 Iterator [] iters = new Iterator [ getPropertySpan() ]; 62 Iterator iter = getPropertyIterator(); 63 int i=0; 64 while ( iter.hasNext() ) { 65 iters[i++] = ( (Property) iter.next() ).getColumnIterator(); 66 } 67 return new JoinedIterator(iters); 68 } 69 70 public Component(PersistentClass owner) throws MappingException { 71 super( owner.getTable() ); 72 this.owner = owner; 73 } 74 75 public Component(Component component) throws MappingException { 76 super( component.getTable() ); 77 this.owner = component.getOwner(); 78 } 79 80 public Component(Join join) throws MappingException { 81 super( join.getTable() ); 82 this.owner = join.getPersistentClass(); 83 } 84 85 public Component(Collection collection) throws MappingException { 86 super( collection.getCollectionTable() ); 87 this.owner = collection.getOwner(); 88 } 89 90 public void setTypeByReflection(String propertyClass, String propertyName) {} 91 92 public boolean isEmbedded() { 93 return embedded; 94 } 95 96 public String getComponentClassName() { 97 return componentClassName; 98 } 99 100 public Class getComponentClass() throws MappingException { 101 try { 102 return ReflectHelper.classForName(componentClassName); 103 } 104 catch (ClassNotFoundException cnfe) { 105 throw new MappingException("component class not found: " + componentClassName, cnfe); 106 } 107 } 108 109 public PersistentClass getOwner() { 110 return owner; 111 } 112 113 public String getParentProperty() { 114 return parentProperty; 115 } 116 117 public void setComponentClassName(String componentClass) { 118 this.componentClassName = componentClass; 119 } 120 121 public void setEmbedded(boolean embedded) { 122 this.embedded = embedded; 123 } 124 125 public void setOwner(PersistentClass owner) { 126 this.owner = owner; 127 } 128 129 public void setParentProperty(String parentProperty) { 130 this.parentProperty = parentProperty; 131 } 132 133 public boolean isDynamic() { 134 return dynamic; 135 } 136 137 public void setDynamic(boolean dynamic) { 138 this.dynamic = dynamic; 139 } 140 141 public Type getType() throws MappingException { 142 final int span = getPropertySpan(); 143 String [] names = new String [span]; 144 org.hibernate.type.Type[] types = new org.hibernate.type.Type[span]; 145 boolean[] nullabilities = new boolean[span]; 146 CascadeStyle[] cascade = new CascadeStyle[span]; 147 FetchMode[] joinedFetch = new FetchMode[span]; 148 Iterator props = getPropertyIterator(); 149 int j=0; 150 while ( props.hasNext() ) { 151 Property prop = (Property) props.next(); 152 names[j] = prop.getName(); 153 types[j] = prop.getType(); 154 cascade[j] = prop.getCascadeStyle(); 155 joinedFetch[j] = prop.getValue().getFetchMode(); 156 nullabilities[j] = prop.isNullable(); 157 j++; 158 } 159 160 TuplizerLookup tuplizers = TuplizerLookup.create(this); 161 162 if ( isEmbedded() ) { 163 return new EmbeddedComponentType( 164 names, 165 types, 166 nullabilities, 167 joinedFetch, 168 cascade, 169 isKey, 170 tuplizers 171 ); 172 } 173 else { 174 return new ComponentType( 175 names, 176 types, 177 nullabilities, 178 joinedFetch, 179 cascade, 180 isKey, 181 tuplizers 182 ); 183 } 184 185 } 186 187 public void setTypeUsingReflection(String className, String propertyName) 188 throws MappingException { 189 } 190 191 public java.util.Map getMetaAttributes() { 192 return metaAttributes; 193 } 194 public MetaAttribute getMetaAttribute(String attributeName) { 195 return (MetaAttribute) metaAttributes.get(attributeName); 196 } 197 198 public void setMetaAttributes(java.util.Map metas) { 199 this.metaAttributes = metas; 200 } 201 202 public Object accept(ValueVisitor visitor) { 203 return visitor.accept(this); 204 } 205 206 public boolean[] getColumnInsertability() { 207 boolean[] result = new boolean[ getColumnSpan() ]; 208 Iterator iter = getPropertyIterator(); 209 int i=0; 210 while ( iter.hasNext() ) { 211 Property prop = (Property) iter.next(); 212 boolean[] chunk = prop.getValue().getColumnInsertability(); 213 if ( prop.isInsertable() ) { 214 System.arraycopy(chunk, 0, result, i, chunk.length); 215 } 216 i+=chunk.length; 217 } 218 return result; 219 } 220 221 public boolean[] getColumnUpdateability() { 222 boolean[] result = new boolean[ getColumnSpan() ]; 223 Iterator iter = getPropertyIterator(); 224 int i=0; 225 while ( iter.hasNext() ) { 226 Property prop = (Property) iter.next(); 227 boolean[] chunk = prop.getValue().getColumnUpdateability(); 228 if ( prop.isUpdateable() ) { 229 System.arraycopy(chunk, 0, result, i, chunk.length); 230 } 231 i+=chunk.length; 232 } 233 return result; 234 } 235 236 public String getNodeName() { 237 return nodeName; 238 } 239 240 public void setNodeName(String nodeName) { 241 this.nodeName = nodeName; 242 } 243 244 public boolean isKey() { 245 return isKey; 246 } 247 248 public void setKey(boolean isKey) { 249 this.isKey = isKey; 250 } 251 252 public boolean hasPojoRepresentation() { 253 return componentClassName!=null; 254 } 255 256 public void addTuplizer(EntityMode entityMode, String implClassName) { 257 if ( tuplizerImpls == null ) { 258 tuplizerImpls = new HashMap (); 259 } 260 tuplizerImpls.put( entityMode, implClassName ); 261 } 262 263 public String getTuplizerImplClassName(EntityMode mode) { 264 if ( tuplizerImpls == null ) return null; 265 return ( String ) tuplizerImpls.get( mode ); 266 } 267 268 public Property getProperty(String propertyName) throws MappingException { 269 Iterator iter = getPropertyIterator(); 270 while ( iter.hasNext() ) { 271 Property prop = (Property) iter.next(); 272 if ( prop.getName().equals(propertyName) ) { 273 return prop; 274 } 275 } 276 throw new MappingException("component property not found: " + propertyName); 277 } 278 279 public String toString() { 280 return getClass().getName() + '(' + properties.toString() + ')'; 281 } 282 283 } 284 | Popular Tags |