1 19 package org.apache.cayenne.reflect; 20 21 import java.util.Iterator ; 22 23 import org.apache.cayenne.CayenneRuntimeException; 24 import org.apache.cayenne.map.EmbeddedAttribute; 25 import org.apache.cayenne.map.EntityInheritanceTree; 26 import org.apache.cayenne.map.ObjAttribute; 27 import org.apache.cayenne.map.ObjEntity; 28 import org.apache.cayenne.map.ObjRelationship; 29 30 36 public abstract class PersistentDescriptorFactory implements ClassDescriptorFactory { 37 38 protected ClassDescriptorMap descriptorMap; 39 40 public PersistentDescriptorFactory(ClassDescriptorMap descriptorMap) { 41 this.descriptorMap = descriptorMap; 42 } 43 44 public ClassDescriptor getDescriptor(String entityName) { 45 ObjEntity entity = descriptorMap.getResolver().getObjEntity(entityName); 46 if (entity == null) { 47 throw new CayenneRuntimeException("Unmapped entity: " + entityName); 48 } 49 50 Class entityClass = entity.getJavaClass(); 51 return getDescriptor(entity, entityClass); 52 } 53 54 protected ClassDescriptor getDescriptor(ObjEntity entity, Class entityClass) { 55 String superEntityName = entity.getSuperEntityName(); 56 57 ClassDescriptor superDescriptor = (superEntityName != null) ? descriptorMap 58 .getDescriptor(superEntityName) : null; 59 60 PersistentDescriptor descriptor = createDescriptor(); 61 62 descriptor.setEntity(entity); 63 descriptor.setSuperclassDescriptor(superDescriptor); 64 descriptor.setObjectClass(entityClass); 65 descriptor.setPersistenceStateAccessor(new BeanAccessor( 66 entityClass, 67 "persistenceState", 68 Integer.TYPE)); 69 70 Iterator attributes = descriptor.getEntity().getDeclaredAttributes().iterator(); 72 while (attributes.hasNext()) { 73 Object attribute = attributes.next(); 74 75 if (attribute instanceof ObjAttribute) { 76 createAttributeProperty(descriptor, (ObjAttribute) attribute); 77 } 78 else if (attribute instanceof EmbeddedAttribute) { 79 EmbeddedAttribute embedded = (EmbeddedAttribute) attribute; 80 Iterator embeddedAttributes = embedded.getAttributes().iterator(); 81 while (embeddedAttributes.hasNext()) { 82 createEmbeddedAttributeProperty( 83 descriptor, 84 embedded, 85 (ObjAttribute) embeddedAttributes.next()); 86 } 87 } 88 } 89 90 Iterator it = descriptor.getEntity().getDeclaredRelationships().iterator(); 92 while (it.hasNext()) { 93 94 ObjRelationship relationship = (ObjRelationship) it.next(); 95 if (relationship.isToMany()) { 96 createToManyProperty(descriptor, relationship); 97 } 98 else { 99 createToOneProperty(descriptor, relationship); 100 } 101 } 102 103 EntityInheritanceTree inheritanceTree = descriptorMap 104 .getResolver() 105 .lookupInheritanceTree(descriptor.getEntity()); 106 indexSubclassDescriptors(descriptor, inheritanceTree); 107 108 return descriptor; 109 } 110 111 protected PersistentDescriptor createDescriptor() { 112 return new PersistentDescriptor(); 113 } 114 115 protected void createAttributeProperty( 116 PersistentDescriptor descriptor, 117 ObjAttribute attribute) { 118 Class propertyType = attribute.getJavaClass(); 119 Accessor accessor = createAccessor(descriptor, attribute.getName(), propertyType); 120 descriptor.addDeclaredProperty(new SimpleAttributeProperty( 121 descriptor, 122 accessor, 123 attribute)); 124 } 125 126 protected void createEmbeddedAttributeProperty( 127 PersistentDescriptor descriptor, 128 EmbeddedAttribute embeddedAttribute, 129 ObjAttribute attribute) { 130 131 Class embeddableClass = embeddedAttribute.getJavaClass(); 132 133 String propertyPath = attribute.getName(); 134 int lastDot = propertyPath.lastIndexOf('.'); 135 if (lastDot <= 0 || lastDot == propertyPath.length() - 1) { 136 throw new IllegalArgumentException ("Invalid embeddable path: " + propertyPath); 137 } 138 139 String embeddableName = propertyPath.substring(lastDot + 1); 140 141 Accessor embeddedAccessor = createAccessor(descriptor, embeddedAttribute 142 .getName(), embeddableClass); 143 Accessor embeddedableAccessor = createEmbeddableAccessor( 144 embeddableClass, 145 embeddableName, 146 attribute.getJavaClass()); 147 148 Accessor accessor = new EmbeddedFieldAccessor( 149 embeddableClass, 150 embeddedAccessor, 151 embeddedableAccessor); 152 descriptor.addDeclaredProperty(new SimpleAttributeProperty( 153 descriptor, 154 accessor, 155 attribute)); 156 } 157 158 protected abstract void createToOneProperty( 159 PersistentDescriptor descriptor, 160 ObjRelationship relationship); 161 162 protected abstract void createToManyProperty( 163 PersistentDescriptor descriptor, 164 ObjRelationship relationship); 165 166 protected void indexSubclassDescriptors( 167 PersistentDescriptor descriptor, 168 EntityInheritanceTree inheritanceTree) { 169 170 if (inheritanceTree != null) { 171 172 Iterator it = inheritanceTree.getChildren().iterator(); 173 while (it.hasNext()) { 174 EntityInheritanceTree child = (EntityInheritanceTree) it.next(); 175 descriptor.addSubclassDescriptor(descriptorMap.getDescriptor(child 176 .getEntity() 177 .getName())); 178 179 indexSubclassDescriptors(descriptor, child); 180 } 181 } 182 } 183 184 187 protected Accessor createAccessor( 188 PersistentDescriptor descriptor, 189 String propertyName, 190 Class propertyType) throws PropertyException { 191 return new FieldAccessor(descriptor.getObjectClass(), propertyName, propertyType); 192 } 193 194 197 protected Accessor createEmbeddableAccessor( 198 Class embeddableClass, 199 String propertyName, 200 Class propertyType) { 201 return new FieldAccessor(embeddableClass, propertyName, propertyType); 202 } 203 } 204 | Popular Tags |