package io.ebeaninternal.server.deploy.visitor;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import io.ebeaninternal.server.deploy.BeanProperty;
import io.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import io.ebeaninternal.server.deploy.BeanPropertyAssocOne;
import io.ebeaninternal.server.deploy.InheritInfo;
import io.ebeaninternal.server.deploy.InheritInfoVisitor;
public class VisitProperties {
public static void visit(BeanDescriptor<?> descriptor, BeanPropertyVisitor visitor) {
new VisitProperties().visitProperties(descriptor, visitor);
}
protected void visitProperties(BeanDescriptor<?> desc, BeanPropertyVisitor propertyVisitor) {
BeanProperty idProp = desc.getIdProperty();
if (idProp != null) {
visit(propertyVisitor, idProp);
}
BeanPropertyAssocOne<?> unidirectional = desc.getUnidirectional();
if (unidirectional != null) {
visit(propertyVisitor, unidirectional);
}
BeanProperty[] propertiesNonTransient = desc.propertiesNonTransient();
for (BeanProperty p : propertiesNonTransient) {
if (p.isDDLColumn()) {
visit(propertyVisitor, p);
}
}
visitInheritanceProperties(desc, propertyVisitor);
propertyVisitor.visitEnd();
}
protected void visit(BeanPropertyVisitor pv, BeanProperty p) {
if (p instanceof BeanPropertyAssocMany<?>) {
pv.visitMany((BeanPropertyAssocMany<?>) p);
} else if (p instanceof BeanPropertyAssocOne<?>) {
BeanPropertyAssocOne<?> assocOne = (BeanPropertyAssocOne<?>) p;
if (assocOne.isEmbedded()) {
pv.visitEmbedded(assocOne);
BeanProperty[] embProps = assocOne.getProperties();
for (BeanProperty embProp : embProps) {
pv.visitEmbeddedScalar(embProp, assocOne);
}
} else if (assocOne.isOneToOneExported()) {
pv.visitOneExported(assocOne);
} else {
pv.visitOneImported(assocOne);
}
} else {
pv.visitScalar(p);
}
}
protected void visitInheritanceProperties(BeanDescriptor<?> descriptor, BeanPropertyVisitor pv) {
InheritInfo inheritInfo = descriptor.getInheritInfo();
if (inheritInfo != null && inheritInfo.isRoot()) {
inheritInfo.visitChildren(new InheritChildVisitor(this, pv));
}
}
protected static class InheritChildVisitor implements InheritInfoVisitor {
private final VisitProperties owner;
private final BeanPropertyVisitor pv;
protected InheritChildVisitor(VisitProperties owner, BeanPropertyVisitor pv) {
this.owner = owner;
this.pv = pv;
}
@Override
public void visit(InheritInfo inheritInfo) {
for (BeanProperty beanProperty : inheritInfo.desc().propertiesLocal()) {
if (beanProperty.isDDLColumn()) {
owner.visit(pv, beanProperty);
}
}
}
}
}