/** * * @param modelClasses * @param dbType this is a name of a DBMS as returned by JDBC driver, such as Oracle, MySQL, etc. */ private void registerModels(String dbName, Set<Class<? extends Model>> modelClasses, String dbType) { for (Class<? extends Model> modelClass : modelClasses) { MetaModel mm = new MetaModel(dbName, modelClass, dbType); metaModels.addMetaModel(mm, modelClass); LogFilter.log(LOGGER, LogLevel.INFO, "Registered model: {}", modelClass); } }
/** * Returns edges for a join. An edge is a table in a many to many relationship that is not a join. * * We have to go through all the associations here because join tables, (even if the model exists) will not * have associations to the edges. * * @param join name of join table; * @return edges for a join */ protected List<String> getEdges(String join) { return metaModels.getEdges(join); }
/** * Provides a MetaModel of a model representing a table. * * @param table name of table represented by this MetaModel. * @return MetaModel of a model representing a table. */ public MetaModel getMetaModel(String table) { return metaModels.getMetaModel(table); }
private void discoverMany2ManyAssociationsFor(String source, String dbName) { for (String potentialJoinTable : metaModels.getTableNames(dbName)) { String target = Inflector.getOtherName(source, potentialJoinTable); if (target != null && getMetaModel(target) != null && hasForeignKeys(potentialJoinTable, source, target)) { Class<? extends Model> sourceModelClass = metaModels.getModelClass(source); Class<? extends Model> targetModelClass = metaModels.getModelClass(target); Association associationSource = new Many2ManyAssociation(sourceModelClass, targetModelClass, potentialJoinTable, getMetaModel(source).getFKName(), getMetaModel(target).getFKName()); getMetaModel(source).addAssociation(associationSource); } } }
private void processManyToManyOverrides(Many2Many many2manyAnnotation, Class<? extends Model> modelClass){ Class<? extends Model> otherClass = many2manyAnnotation.other(); String source = metaModels.getTableName(modelClass); String target = metaModels.getTableName(otherClass); String join = many2manyAnnotation.join(); String sourceFKName = many2manyAnnotation.sourceFKName(); String targetFKName = many2manyAnnotation.targetFKName(); String otherPk; String thisPk; try { Method m = modelClass.getMethod("getMetaModel"); MetaModel mm = (MetaModel) m.invoke(modelClass); thisPk = mm.getIdName(); m = otherClass.getMethod("getMetaModel"); mm = (MetaModel) m.invoke(otherClass); otherPk = mm.getIdName(); } catch (Exception e) { throw new InitException("failed to determine PK name in many to many relationship", e); } Association many2many1 = new Many2ManyAssociation(modelClass, otherClass, join, sourceFKName, targetFKName, otherPk); metaModels.getMetaModel(source).addAssociation(many2many1); Association many2many2 = new Many2ManyAssociation(otherClass, modelClass, join, targetFKName, sourceFKName, thisPk); metaModels.getMetaModel(target).addAssociation(many2many2); }
Set<Class<? extends Model>> modelClasses = ModelFinder.getModelsForDb(dbName); registerModels(dbName, modelClasses, dbType); String[] tables = metaModels.getTableNames(dbName);
if (metaModels.getMetaModel(modelClass) == null) { registerModel(dbName, modelClass, databaseProductName); String[] tables = metaModels.getTableNames(dbName);
protected String getTableName(Class<? extends Model> modelClass) { init(MetaModel.getDbName(modelClass)); String tableName = metaModels.getTableName(modelClass); if (tableName == null) { throw new DBException("failed to find metamodel for " + modelClass + ". Are you sure that a corresponding table exists in DB?"); } return tableName; }
private void registerColumnMetadata(String table, Map<String, ColumnMetadata> metaParams) { metaModels.setColumnMetadata(table, metaParams); } }
/** * Returns model class for a table name, null if not found. * * @param table table name * @param suppressException true to suppress exception * @return model class for a table name, null if not found.s */ protected Class<? extends Model> getModelClass(String table, boolean suppressException) { Class<? extends Model> modelClass = metaModels.getModelClass(table); if(modelClass == null && !suppressException){ throw new InitException("failed to locate meta model for: " + table + ", are you sure this is correct table name?"); }else{ return modelClass; } }
ModelRegistry modelRegistryOf(Class<? extends Model> modelClass) { return metaModels.getModelRegistry(modelClass); }
protected String metadataToJSON() { return metaModels.toJSON(); }
private boolean loadStaticMetadata() { try { Enumeration<URL> urls = Registry.instance().getClass().getClassLoader().getResources("activejdbc_metadata.json"); staticMetadataStatus = urls.hasMoreElements() ? STATIC_METADATA_LOADED : STATIC_METADATA_CHECKED; while(urls.hasMoreElements()) { URL url = urls.nextElement(); LogFilter.log(LOGGER, LogLevel.INFO, "Loading metadata from: {}", url.toExternalForm()); metaModels.fromJSON(Util.read(url.openStream())); } return staticMetadataStatus == STATIC_METADATA_LOADED; } catch(IOException e) { throw new InitException(e); } }
/** * Discover many to many associations. * * @param source name of table for which associations are searched. */ private void discoverOne2ManyAssociationsFor(String source, String dbName) { MetaModel sourceMM = getMetaModel(source); for (String target : metaModels.getTableNames(dbName)) { MetaModel targetMM = getMetaModel(target); String sourceFKName = getMetaModel(source).getFKName(); if (targetMM != sourceMM && targetMM.hasAttribute(sourceFKName)) { Class<? extends Model> sourceModelClass = metaModels.getModelClass(source); Class<? extends Model> targetModelClass = metaModels.getModelClass(target); targetMM.addAssociation(new BelongsToAssociation(targetModelClass, sourceModelClass, sourceFKName)); sourceMM.addAssociation(new OneToManyAssociation(sourceModelClass, targetModelClass, sourceFKName)); } } }
List<Class<? extends Model>> modelClasses = ModelFinder.getModelsForDb(dbName); registerModels(dbName, modelClasses, dbType); String[] tables = metaModels.getTableNames(dbName);
protected String getTableName(Class<? extends Model> modelClass) { init(MetaModel.getDbName(modelClass), modelClass); String tableName = metaModels.getTableName(modelClass); if (tableName == null) { throw new DBException("failed to find metamodel for " + modelClass + ". Are you sure that a corresponding table exists in DB?"); } return tableName; }
private void registerColumnMetadata(String table, Map<String, ColumnMetadata> metaParams) { metaModels.setColumnMetadata(table, metaParams); } }
/** * Returns model class for a table name, null if not found. * * @param table table name * @param suppressException true to suppress exception * @return model class for a table name, null if not found.s */ protected Class<? extends Model> getModelClass(String table, boolean suppressException) { Class modelClass = metaModels.getModelClass(table); if(modelClass == null && !suppressException) throw new InitException("failed to locate meta model for: " + table + ", are you sure this is correct table name?"); return modelClass; }
public Set<Class<? extends Model>> getModelClasses(String dbName) { Set<Class<? extends Model>> modelClasses = new HashSet<Class<? extends Model>>(); for (String tableName : metaModels.getTableNames(dbName)) { modelClasses.add(metaModels.getModelClass(tableName)); } return modelClasses; }
public MetaModel getMetaModel(Class<? extends Model> modelClass) { String dbName = MetaModel.getDbName(modelClass); init(dbName); return metaModels.getMetaModel(modelClass); }