// Caching model (tables, table-references, search-references,
// list-references)
// Changed to use the SessionHandler directly because the dal
// layer uses the ModelProvider, so otherwise there will be a
// cyclic relation.
final SessionFactoryController sessionFactoryController = new ModelSessionFactoryController();
final Session session = sessionFactoryController.getSessionFactory().openSession();
final Transaction tx = session.beginTransaction();
try {
log.debug("Read model from db");
tables = list(session, Table.class);
// read the columns in one query and assign them to the table
final List<Column> cols = readColumns(session);
assignColumnsToTable(cols);
refTable = list(session, RefTable.class);
refSearch = list(session, RefSearch.class);
refList = list(session, RefList.class);
modules = retrieveModules(session);
tables = removeInvalidTables(tables);
for (final RefTable rt : refTable) {
refTableMap.put(rt.getId(), rt);
}
for (final RefSearch rs : refSearch) {
// note mapped by reference id
refSearchMap.put(rs.getReference(), rs);
}
// this map stores the mapped tables
tablesByTableName = new HashMap<String, Table>();
for (final Table t : tables) {
if (t.isView()) {
continue;
}
// tables are stored case insensitive!
tablesByTableName.put(t.getTableName().toLowerCase(), t);
}
log.debug("Setting referencetypes for columns");
for (final Table t : tablesByTableName.values()) {
t.setReferenceTypes(ModelProvider.instance);
}
log.debug("Setting List Values for columns");
for (final RefList rl : refList) {
rl.setAllowedValue();
}
model = new ArrayList<Entity>();
entitiesByName = new HashMap<String, Entity>();
entitiesByClassName = new HashMap<String, Entity>();
entitiesByTableName = new HashMap<String, Entity>();
entitiesByTableId = new HashMap<String, Entity>();
entitiesWithTreeType = new ArrayList<Entity>();
for (final Table t : tablesByTableName.values()) {
log.debug("Building model for table " + t.getTableName());
final Entity e = new Entity();
e.initialize(t);
model.add(e);
entitiesByClassName.put(e.getClassName(), e);
entitiesByName.put(e.getName(), e);
entitiesByTableName.put(t.getTableName().toUpperCase(), e);
entitiesByTableId.put(t.getId(), e);
if (e.getTreeType() != null) {
entitiesWithTreeType.add(e);
}
}
// in the second pass set all the referenceProperties
// and targetEntities
// uses global member tablesByTableName
setReferenceProperties();
// add virtual property for the case that the
// id property is also a reference (a foreign key)
// In this case hibernate requires two mappings
// one for the id (a string) and for the reference
// in addition the id generation strategy should be set
// to foreign.
log.debug("Setting virtual property for many-to-one id's");
setVirtualPropertiesForReferenceId();
buildUniqueConstraints(session, sessionFactoryController);
final Map<String, Boolean> colMandatories = getColumnMandatories(session,
sessionFactoryController);
// initialize the name and also set the mandatory value on the basis
// of the real not-null in the database!
for (final Entity e : model) {
for (final Property p : e.getProperties()) {
p.initializeName();
if (p.getColumnName() != null) {
final Boolean mandatory = colMandatories.get(createColumnMandatoryKey(e.getTableName(),
p.getColumnName()));
if (mandatory != null) {
p.setMandatory(mandatory);
} else {
log.warn("Column " + p + " mandatory is null");
}
}
}
// dumpPropertyNames(e);
}
} finally {
log.debug("Closing session and sessionfactory used during model read");
tx.commit();
session.close();
sessionFactoryController.getSessionFactory().close();
}
clearLists();
}