refactor modelbuilder

This commit is contained in:
wea_ondara
2022-11-23 17:23:09 +01:00
parent 3c85ef2c84
commit 6f0743889b

View File

@@ -1,17 +1,15 @@
package jef.model;
import jef.Database;
import jef.model.annotations.processors.AnnotationProcessor;
import jef.model.constraints.ForeignKeyConstraint;
import jef.model.constraints.IndexConstraint;
import jef.model.constraints.KeyConstraint;
import jef.model.constraints.PrimaryKeyConstraint;
import jef.model.constraints.UniqueKeyConstraint;
import jef.mysql.MysqlDatabase;
import jef.serializable.SerializableObject;
import jef.util.Check;
import jef.util.Util;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -45,7 +43,7 @@ public class ModelBuilder {
}
private static ModelBuilder from0(Class<? extends DbContext> context, ModelBuilderOptions options) {
var mb = new ModelBuilder(new ArrayList<>());
var mb = new ModelBuilder();
EntityInitializer.initEntities(mb, context);
EntityDefaultConstructorChecker.checkEntities(mb);
PrimaryKeyInitializer.initPrimaryKeys(mb);
@@ -56,29 +54,18 @@ public class ModelBuilder {
processor.apply(mb);
}
Util.ThrowableBiFunction<Database, DbContextOptions, DbContext> init;//TODO create a function for initializing db contexts
try {
var ctor = context.getDeclaredConstructor(Database.class, DbContextOptions.class);
init = ctor::newInstance;
} catch (NoSuchMethodException e) {
try {
var ctor = context.getDeclaredConstructor(DbContextOptions.class);
init = (d, o) -> ctor.newInstance(o);
} catch (NoSuchMethodException e2) {
try {
var ctor = context.getDeclaredConstructor();
init = (d, o) -> ctor.newInstance();
} catch (NoSuchMethodException e3) {
throw new RuntimeException(e);
}
}
}
try {
DbContext instance = init.apply(new MysqlDatabase(null, null), options.getContextOptions());//TODO
var instance = context.getDeclaredConstructor().newInstance();
instance.onModelCreate(mb);
instance.onModelValidate(mb);
} catch (Throwable e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException("Error while instantiating DbContext '" + context.getName() + "'", e);
} catch (InstantiationException e) {
throw new RuntimeException("Failed to instantiate DbContext '" + context.getName() + "'", e);
} catch (IllegalAccessException e) {
throw new RuntimeException("Default constructor of DbContext '" + context.getName() + "' must be publicly accessible", e);
} catch (NoSuchMethodException e) {
throw new RuntimeException("DbContext '" + context.getName() + "' does not have a default constructor", e);
}
return mb;