* Clean up and unify DbMapping constructors a bit.

* Make typename, app, and props fields final.
* Issue prominent warnings if parent proto can't be resolved or
  relational extended type doesn't have a _prototype mapping.
* Determine in update() if this is a relational mapping or not.
This commit is contained in:
hns 2006-08-10 21:28:58 +00:00
parent 5924f93f81
commit 47b101827a
2 changed files with 38 additions and 38 deletions

View file

@ -30,13 +30,13 @@ import java.util.*;
*/ */
public final class DbMapping { public final class DbMapping {
// DbMappings belong to an application // DbMappings belong to an application
protected Application app; protected final Application app;
// prototype name of this mapping // prototype name of this mapping
private String typename; private final String typename;
// properties from where the mapping is read // properties from where the mapping is read
private ResourceProperties props; private final ResourceProperties props;
// name of data dbSource to which this mapping writes // name of data dbSource to which this mapping writes
private DbSource dbSource; private DbSource dbSource;
@ -120,19 +120,14 @@ public final class DbMapping {
// Set of mappings that depend on us and should be forwarded last data change events // Set of mappings that depend on us and should be forwarded last data change events
HashSet dependentMappings = new HashSet(); HashSet dependentMappings = new HashSet();
// flag that tells us whether this maps to a relational db
private boolean relational = false;
/** /**
* Create an empty DbMapping * Create an empty DbMapping
*/ */
public DbMapping(Application app) { public DbMapping(Application app, String typename) {
this.app = app; this(app, typename, null);
this.typename = null;
prop2db = new HashMap();
db2prop = new HashMap();
parentInfo = null;
idField = null;
} }
/** /**
@ -146,12 +141,14 @@ public final class DbMapping {
prop2db = new HashMap(); prop2db = new HashMap();
db2prop = new HashMap(); db2prop = new HashMap();
columnMap = new HashMap(); columnMap = new HashMap();
parentInfo = null; parentInfo = null;
idField = null; idField = null;
this.props = props; this.props = props;
readBasicProperties();
if (props != null) {
readBasicProperties();
}
} }
/** /**
@ -173,14 +170,14 @@ public final class DbMapping {
dbSource = app.getDbSource(dbSourceName); dbSource = app.getDbSource(dbSourceName);
if (dbSource == null) { if (dbSource == null) {
app.logEvent("*** Data Source for prototype " + typename + app.logError("*** Data Source for prototype " + typename +
" does not exist: " + dbSourceName); " does not exist: " + dbSourceName);
app.logEvent("*** accessing or storing a " + typename + app.logError("*** accessing or storing a " + typename +
" object will cause an error."); " object will cause an error.");
} else if (tableName == null) { } else if (tableName == null) {
app.logEvent("*** No table name specified for prototype " + typename); app.logError("*** No table name specified for prototype " + typename);
app.logEvent("*** accessing or storing a " + typename + app.logError("*** accessing or storing a " + typename +
" object will cause an error."); " object will cause an error.");
// mark mapping as invalid by nulling the dbSource field // mark mapping as invalid by nulling the dbSource field
dbSource = null; dbSource = null;
@ -230,7 +227,10 @@ public final class DbMapping {
if (extendsProto != null) { if (extendsProto != null) {
parentMapping = app.getDbMapping(extendsProto); parentMapping = app.getDbMapping(extendsProto);
if (parentMapping != null) { if (parentMapping == null) {
app.logError("*** Parent mapping for prototype " + typename +
" does not exist: " + extendsProto);
} else {
if (parentMapping.needsUpdate()) { if (parentMapping.needsUpdate()) {
parentMapping.update(); parentMapping.update();
} }
@ -245,9 +245,18 @@ public final class DbMapping {
dbSourceName = null; dbSourceName = null;
dbSource = null; dbSource = null;
} }
// set/update relational flag
relational = dbSourceName != null || parentMapping.isRelational();
} }
} else { } else {
parentMapping = null; parentMapping = null;
// set/update relational flag
relational = dbSourceName != null;
}
if (inheritsStorage() && getPrototypeField() == null) {
app.logError("*** Prototype not stored for extended relational type " + typename);
app.logError("*** objects fetched from db will have base prototype!");
} }
// check if there is an extension-id specified inside the type.properties // check if there is an extension-id specified inside the type.properties
@ -401,7 +410,8 @@ public final class DbMapping {
} }
/** /**
* Looks up the prototype-name identified by the given integer value * Looks up the prototype name identified by the given id, returing
* our own type name if it can't be resolved
* @param id the id specified for the prototype * @param id the id specified for the prototype
* @return the name of the extending prototype * @return the name of the extending prototype
*/ */
@ -765,7 +775,7 @@ public final class DbMapping {
private void initGroupbyMapping() { private void initGroupbyMapping() {
// if a prototype is defined for groupby nodes, use that // if a prototype is defined for groupby nodes, use that
// if mapping doesn' exist or isn't defined, create a new (anonymous internal) one // if mapping doesn' exist or isn't defined, create a new (anonymous internal) one
groupbyMapping = new DbMapping(app); groupbyMapping = new DbMapping(app, subRelation.groupbyPrototype);
// If a mapping is defined, make the internal mapping inherit from // If a mapping is defined, make the internal mapping inherit from
// the defined named prototype. // the defined named prototype.
@ -780,8 +790,6 @@ public final class DbMapping {
} else { } else {
groupbyMapping.propRelation = subRelation.getGroupbyPropertyRelation(); groupbyMapping.propRelation = subRelation.getGroupbyPropertyRelation();
} }
groupbyMapping.typename = subRelation.groupbyPrototype;
} }
/** /**
@ -916,15 +924,7 @@ public final class DbMapping {
* not what we want. * not what we want.
*/ */
public boolean isRelational() { public boolean isRelational() {
if (dbSourceName != null) { return relational;
return true;
}
if (parentMapping != null) {
return parentMapping.isRelational();
}
return false;
} }
/** /**
@ -1378,10 +1378,9 @@ public final class DbMapping {
* db. * db.
*/ */
public String getStorageTypeName() { public String getStorageTypeName() {
if ((tableName == null) && (parentMapping != null)) { if (inheritsStorage()) {
return parentMapping.getStorageTypeName(); return parentMapping.getStorageTypeName();
} }
return (dbSourceName == null) ? null : typename; return (dbSourceName == null) ? null : typename;
} }
@ -1397,7 +1396,8 @@ public final class DbMapping {
// note: tableName and dbSourceName are nulled out in update() if they // note: tableName and dbSourceName are nulled out in update() if they
// are inherited from the parent mapping. This way we know that // are inherited from the parent mapping. This way we know that
// storage is not inherited if either of them is not null. // storage is not inherited if either of them is not null.
return parentMapping != null && tableName == null && dbSourceName == null; return relational && parentMapping != null
&& tableName == null && dbSourceName == null;
} }
/** /**

View file

@ -734,7 +734,7 @@ public final class Relation {
// create a synthetic DbMapping that describes how to fetch the // create a synthetic DbMapping that describes how to fetch the
// collection's child objects. // collection's child objects.
if (virtualMapping == null) { if (virtualMapping == null) {
virtualMapping = new DbMapping(ownType.app); virtualMapping = new DbMapping(ownType.app, null);
virtualMapping.subRelation = getVirtualSubnodeRelation(); virtualMapping.subRelation = getVirtualSubnodeRelation();
virtualMapping.propRelation = getVirtualPropertyRelation(); virtualMapping.propRelation = getVirtualPropertyRelation();
} }