/** Adds superclass properties to the descriptor, applying proper overrides. */ protected void indexSuperclassProperties(final PersistentDescriptor descriptor) { ClassDescriptor superDescriptor = descriptor.getSuperclassDescriptor(); if (superDescriptor != null) { superDescriptor.visitProperties( new PropertyVisitor() { public boolean visitAttribute(AttributeProperty property) { // decorate super property to return an overridden attribute descriptor.addSuperProperty(new AttributePropertyDecorator(descriptor, property)); return true; } public boolean visitToMany(ToManyProperty property) { descriptor.addSuperProperty(property); return true; } public boolean visitToOne(ToOneProperty property) { descriptor.addSuperProperty(property); return true; } }); } }
protected void createEmbeddedAttributeProperty( PersistentDescriptor descriptor, EmbeddedAttribute embeddedAttribute, ObjAttribute attribute) { Class<?> embeddableClass = embeddedAttribute.getJavaClass(); String propertyPath = attribute.getName(); int lastDot = propertyPath.lastIndexOf('.'); if (lastDot <= 0 || lastDot == propertyPath.length() - 1) { throw new IllegalArgumentException("Invalid embeddable path: " + propertyPath); } String embeddableName = propertyPath.substring(lastDot + 1); EmbeddableDescriptor embeddableDescriptor = createEmbeddableDescriptor(embeddedAttribute); Accessor embeddedAccessor = createAccessor(descriptor, embeddedAttribute.getName(), embeddableClass); Accessor embeddedableAccessor = createEmbeddableAccessor(embeddableDescriptor, embeddableName, attribute.getJavaClass()); Accessor accessor = new EmbeddedFieldAccessor(embeddableDescriptor, embeddedAccessor, embeddedableAccessor); descriptor.addDeclaredProperty(new SimpleAttributeProperty(descriptor, accessor, attribute)); }
private void appendDeclaredRootDbEntity(PersistentDescriptor descriptor, ObjEntity entity) { DbEntity dbEntity = entity.getDbEntity(); if (dbEntity != null) { // descriptor takes care of weeding off duplicates, which are likely // in cases // of non-horizontal inheritance descriptor.addRootDbEntity(dbEntity); } }
protected void indexSubclassDescriptors( PersistentDescriptor descriptor, EntityInheritanceTree inheritanceTree) { if (inheritanceTree != null) { for (EntityInheritanceTree child : inheritanceTree.getChildren()) { ObjEntity childEntity = child.getEntity(); descriptor.addSubclassDescriptor( childEntity.getClassName(), descriptorMap.getDescriptor(childEntity.getName())); indexSubclassDescriptors(descriptor, child); } } }
protected ClassDescriptor getDescriptor(ObjEntity entity, Class<?> entityClass) { String superEntityName = entity.getSuperEntityName(); ClassDescriptor superDescriptor = (superEntityName != null) ? descriptorMap.getDescriptor(superEntityName) : null; PersistentDescriptor descriptor = createDescriptor(); descriptor.setEntity(entity); descriptor.setSuperclassDescriptor(superDescriptor); descriptor.setObjectClass(entityClass); descriptor.setPersistenceStateAccessor( new BeanAccessor(entityClass, "persistenceState", Integer.TYPE)); // only include this entity attributes and skip superclasses... for (ObjAttribute attribute : descriptor.getEntity().getDeclaredAttributes()) { if (attribute instanceof EmbeddedAttribute) { EmbeddedAttribute embedded = (EmbeddedAttribute) attribute; for (ObjAttribute objAttribute : embedded.getAttributes()) { createEmbeddedAttributeProperty(descriptor, embedded, objAttribute); } } else { createAttributeProperty(descriptor, attribute); } } // only include this entity relationships and skip superclasses... for (ObjRelationship relationship : descriptor.getEntity().getDeclaredRelationships()) { if (relationship.isToMany()) { String collectionType = relationship.getCollectionType(); if (collectionType == null || ObjRelationship.DEFAULT_COLLECTION_TYPE.equals(collectionType)) { createToManyListProperty(descriptor, relationship); } else if (collectionType.equals("java.util.Map")) { createToManyMapProperty(descriptor, relationship); } else if (collectionType.equals("java.util.Set")) { createToManySetProperty(descriptor, relationship); } else if (collectionType.equals("java.util.Collection")) { createToManyCollectionProperty(descriptor, relationship); } else { throw new IllegalArgumentException( "Unsupported to-many collection type: " + collectionType); } } else { createToOneProperty(descriptor, relationship); } } EntityInheritanceTree inheritanceTree = descriptorMap.getResolver().getInheritanceTree(descriptor.getEntity().getName()); descriptor.setEntityInheritanceTree(inheritanceTree); indexSubclassDescriptors(descriptor, inheritanceTree); indexQualifiers(descriptor, inheritanceTree); appendDeclaredRootDbEntity(descriptor, descriptor.getEntity()); indexRootDbEntities(descriptor, inheritanceTree); indexSuperclassProperties(descriptor); descriptor.sortProperties(); return descriptor; }
/** Creates an accessor for the property. */ protected Accessor createAccessor( PersistentDescriptor descriptor, String propertyName, Class<?> propertyType) throws PropertyException { return new FieldAccessor(descriptor.getObjectClass(), propertyName, propertyType); }
protected void indexQualifiers( final PersistentDescriptor descriptor, EntityInheritanceTree inheritanceTree) { Expression qualifier; if (inheritanceTree != null) { qualifier = inheritanceTree.qualifierForEntityAndSubclasses(); } else { qualifier = descriptor.getEntity().getDeclaredQualifier(); } if (qualifier != null) { // using map instead of a Set to collect attributes, as // ObjEntity.getAttribute may return a decorator for attribute on // each call, resulting in dupes final Map<String, ObjAttribute> attributes = new HashMap<>(); final DbEntity dbEntity = descriptor.getEntity().getDbEntity(); qualifier.traverse( new TraversalHelper() { @Override public void startNode(Expression node, Expression parentNode) { if (node.getType() == Expression.DB_PATH) { String path = node.getOperand(0).toString(); final DbAttribute attribute = dbEntity.getAttribute(path); if (attribute != null) { ObjAttribute objectAttribute = descriptor.getEntity().getAttributeForDbAttribute(attribute); if (objectAttribute == null) { objectAttribute = new ObjAttribute(attribute.getName()) { @Override public DbAttribute getDbAttribute() { return attribute; } }; // we semi-officially DO NOT support inheritance // descriptors based on related entities, so // here we // assume that DbAttribute is rooted in the root // DbEntity, and no relationship is involved. objectAttribute.setDbAttributePath(attribute.getName()); objectAttribute.setType(TypesMapping.getJavaBySqlType(attribute.getType())); } attributes.put(objectAttribute.getName(), objectAttribute); } } else if (node.getType() == Expression.OBJ_PATH) { String path = node.getOperand(0).toString(); ObjAttribute attribute = descriptor.getEntity().getAttribute(path); attributes.put(path, attribute); } } }); descriptor.setDiscriminatorColumns(attributes.values()); descriptor.setEntityQualifier(qualifier); } }
protected void createAttributeProperty(PersistentDescriptor descriptor, ObjAttribute attribute) { Class<?> propertyType = attribute.getJavaClass(); Accessor accessor = createAccessor(descriptor, attribute.getName(), propertyType); descriptor.addDeclaredProperty(new SimpleAttributeProperty(descriptor, accessor, attribute)); }