/** * 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); }
public MetaModel getMetaModel(Class<? extends Model> modelClass) { String dbName = MetaModel.getDbName(modelClass); init(dbName); return metaModels.getMetaModel(modelClass); }
/** * An edge is a table in a many to many relationship that is not a join. * * @param join join table * * @return edges for a join. */ protected List<String> getEdges(String join) { List<String> results = new ArrayList<>(); for (Many2ManyAssociation a : many2ManyAssociations) { if (a.getJoin().equalsIgnoreCase(join)) { results.add(getMetaModel(a.getSourceClass()).getTableName()); results.add(getMetaModel(a.getTargetClass()).getTableName()); } } return results; }
private void processOverridesHasMany(Class<? extends Model> modelClass, HasMany hasManyAnnotation) { if(hasManyAnnotation != null){ Class<? extends Model> childClass = hasManyAnnotation.child(); String foreignKeyName = hasManyAnnotation.foreignKeyName(); if (metaModels.getMetaModel(childClass).hasAssociation(modelClass, OneToManyAssociation.class)) { LogFilter.log(LOGGER, LogLevel.WARNING, "Redundant annotations used: @BelongsTo and @HasMany on a " + "relationship between Model {} and Model {}.", modelClass.getName(), childClass.getName()); return; } Association hasMany = new OneToManyAssociation(modelClass, childClass, foreignKeyName); Association belongsTo = new BelongsToAssociation(childClass, modelClass, foreignKeyName); metaModels.getMetaModel(modelClass).addAssociation(hasMany); metaModels.getMetaModel(childClass).addAssociation(belongsTo); } }
private void processOverridesBelongsTo(Class<? extends Model> modelClass, BelongsTo belongsToAnnotation) { if(belongsToAnnotation != null){ Class<? extends Model> parentClass = belongsToAnnotation.parent(); String foreignKeyName = belongsToAnnotation.foreignKeyName(); if (metaModels.getMetaModel(parentClass).hasAssociation(modelClass, OneToManyAssociation.class)) { LogFilter.log(LOGGER, LogLevel.WARNING, "Redundant annotations used: @BelongsTo and @HasMany on a " + "relationship between Model {} and Model {}.", modelClass.getName(), parentClass.getName()); return; } Association hasMany = new OneToManyAssociation(parentClass, modelClass, foreignKeyName); Association belongsTo = new BelongsToAssociation(modelClass, parentClass, foreignKeyName); metaModels.getMetaModel(parentClass).addAssociation(hasMany); metaModels.getMetaModel(modelClass).addAssociation(belongsTo); } }
private void processPolymorphic(BelongsToPolymorphic belongsToPolymorphic, Class<? extends Model> modelClass ) { Class<? extends Model>[] parentClasses = belongsToPolymorphic.parents(); String[] typeLabels = belongsToPolymorphic.typeLabels(); if (typeLabels.length > 0 && typeLabels.length != parentClasses.length) { throw new InitException("must provide all type labels for polymorphic associations"); } for (int i = 0, parentClassesLength = parentClasses.length; i < parentClassesLength; i++) { Class<? extends Model> parentClass = parentClasses[i]; String typeLabel = typeLabels.length > 0 ? typeLabels[i] : parentClass.getName(); BelongsToPolymorphicAssociation belongsToPolymorphicAssociation = new BelongsToPolymorphicAssociation(modelClass, parentClass, typeLabel, parentClass.getName()); metaModels.getMetaModel(modelClass).addAssociation(belongsToPolymorphicAssociation); OneToManyPolymorphicAssociation oneToManyPolymorphicAssociation = new OneToManyPolymorphicAssociation(parentClass, modelClass, typeLabel); metaModels.getMetaModel(parentClass).addAssociation(oneToManyPolymorphicAssociation); } }
private void processUnrelatedTo(UnrelatedTo unrelatedTo, Class<? extends Model> modelClass) { Class<? extends Model>[] related = unrelatedTo.value(); for (Class<? extends Model> relatedClass : related) { MetaModel relatedMM = metaModels.getMetaModel(relatedClass); MetaModel thisMM = metaModels.getMetaModel(modelClass); if(relatedMM != null){ Association association = relatedMM.getAssociationForTarget(modelClass); relatedMM.removeAssociationForTarget(modelClass); if(association != null){ LogFilter.log(LOGGER, LogLevel.INFO, "Removed association: " + association); } } Association association = thisMM.getAssociationForTarget(relatedClass); thisMM.removeAssociationForTarget(relatedClass); if(association != null){ LogFilter.log(LOGGER, LogLevel.INFO, "Removed association: " + association); } } }
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); }
/** * 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); }
/** * 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); }
public MetaModel getMetaModel(Class<? extends Model> modelClass) { String dbName = MetaModel.getDbName(modelClass); init(dbName, modelClass); return metaModels.getMetaModel(modelClass); }
public MetaModel getMetaModel(Class<? extends Model> modelClass) { String dbName = MetaModel.getDbName(modelClass); init(dbName); return metaModels.getMetaModel(modelClass); }
/** * An edge is a table in a many to many relationship that is not a join. * * @param join join table * * @return edges for a join. */ protected List<String> getEdges(String join) { List<String> results = new ArrayList<>(); for (Many2ManyAssociation a : many2ManyAssociations) { if (a.getJoin().equalsIgnoreCase(join)) { results.add(getMetaModel(a.getSourceClass()).getTableName()); results.add(getMetaModel(a.getTargetClass()).getTableName()); } } return results; } }
private void processOverridesHasMany(Class<? extends Model> modelClass, HasMany hasManyAnnotation) { if(hasManyAnnotation != null){ Class<? extends Model> childClass = hasManyAnnotation.child(); String foreignKeyName = hasManyAnnotation.foreignKeyName(); if (metaModels.getMetaModel(childClass).hasAssociation(modelClass, OneToManyAssociation.class)) { LogFilter.log(LOGGER, LogLevel.WARNING, "Redundant annotations used: @BelongsTo and @HasMany on a " + "relationship between Model {} and Model {}.", modelClass.getName(), childClass.getName()); return; } Association hasMany = new OneToManyAssociation(modelClass, childClass, foreignKeyName); Association belongsTo = new BelongsToAssociation(childClass, modelClass, foreignKeyName); metaModels.getMetaModel(modelClass).addAssociation(hasMany); metaModels.getMetaModel(childClass).addAssociation(belongsTo); } }
private void processOverridesBelongsTo(Class<? extends Model> modelClass, BelongsTo belongsToAnnotation) { if(belongsToAnnotation != null){ Class<? extends Model> parentClass = belongsToAnnotation.parent(); String foreignKeyName = belongsToAnnotation.foreignKeyName(); if (metaModels.getMetaModel(parentClass).hasAssociation(modelClass, OneToManyAssociation.class)) { LogFilter.log(LOGGER, LogLevel.WARNING, "Redundant annotations used: @BelongsTo and @HasMany on a " + "relationship between Model {} and Model {}.", modelClass.getName(), parentClass.getName()); return; } Association hasMany = new OneToManyAssociation(parentClass, modelClass, foreignKeyName); Association belongsTo = new BelongsToAssociation(modelClass, parentClass, foreignKeyName); metaModels.getMetaModel(parentClass).addAssociation(hasMany); metaModels.getMetaModel(modelClass).addAssociation(belongsTo); } }
private void processPolymorphic(BelongsToPolymorphic belongsToPolymorphic, Class<? extends Model> modelClass ) { Class<? extends Model>[] parentClasses = belongsToPolymorphic.parents(); String[] typeLabels = belongsToPolymorphic.typeLabels(); if (typeLabels.length > 0 && typeLabels.length != parentClasses.length) { throw new InitException("must provide all type labels for polymorphic associations"); } for (int i = 0, parentClassesLength = parentClasses.length; i < parentClassesLength; i++) { Class<? extends Model> parentClass = parentClasses[i]; String typeLabel = typeLabels.length > 0 ? typeLabels[i] : parentClass.getName(); BelongsToPolymorphicAssociation belongsToPolymorphicAssociation = new BelongsToPolymorphicAssociation(modelClass, parentClass, typeLabel, parentClass.getName()); metaModels.getMetaModel(modelClass).addAssociation(belongsToPolymorphicAssociation); OneToManyPolymorphicAssociation oneToManyPolymorphicAssociation = new OneToManyPolymorphicAssociation(parentClass, modelClass, typeLabel); metaModels.getMetaModel(parentClass).addAssociation(oneToManyPolymorphicAssociation); } }
private void processUnrelatedTo(UnrelatedTo unrelatedTo, Class<? extends Model> modelClass) { Class<? extends Model>[] related = unrelatedTo.value(); for (Class<? extends Model> relatedClass : related) { MetaModel relatedMM = metaModels.getMetaModel(relatedClass); MetaModel thisMM = metaModels.getMetaModel(modelClass); if(relatedMM != null){ Association association = relatedMM.getAssociationForTarget(modelClass); relatedMM.removeAssociationForTarget(modelClass); if(association != null){ LogFilter.log(LOGGER, LogLevel.INFO, "Removed association: " + association); } } Association association = thisMM.getAssociationForTarget(relatedClass); thisMM.removeAssociationForTarget(relatedClass); if(association != null){ LogFilter.log(LOGGER, LogLevel.INFO, "Removed association: " + association); } } }
private void processOverridesBelongsTo(Class<? extends Model> modelClass, BelongsTo belongsToAnnotation) { if(belongsToAnnotation != null){ Class<? extends Model> parentClass = belongsToAnnotation.parent(); String foreignKeyName = belongsToAnnotation.foreignKeyName(); Association hasMany = new OneToManyAssociation(getTableName(parentClass), getTableName(modelClass), foreignKeyName); Association belongsTo = new BelongsToAssociation(getTableName(modelClass), getTableName(parentClass), foreignKeyName); metaModels.getMetaModel(parentClass).addAssociation(hasMany); metaModels.getMetaModel(modelClass).addAssociation(belongsTo); } }
private void processManyToManyOverrides(Many2Many many2manyAnnotation, Class<? extends Model> modelClass){ Class<? extends Model> otherClass = many2manyAnnotation.other(); String source = getTableName(modelClass); String target = 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); }
if (metaModels.getMetaModel(modelClass) == null) { registerModel(dbName, modelClass, databaseProductName);