/** * ** Returns an array of root DBFactoryTree nodes based on the specified top-level DBFactory. * ** @param initialFactories The root DBFactories which are traversed to create the * DBFactoryTree. If null ** all root DBFactories will be traversed. ** @return An array of rot * DBFactoryTree nodes. */ public static DBFactoryTree[] getDBFactoryTree(DBFactory<? extends DBRecord> initialFactories[]) { Set<String> addedTables = new HashSet<String>(); DBFactoryTree rootNode = new DBFactoryTree(); /* start with initial factories */ if (initialFactories != null) { for (int i = 0; i < initialFactories.length; i++) { DBFactory<? extends DBRecord> dbf = initialFactories[i]; DBFactoryTree._traverseDBFactoryTree(0, dbf, rootNode, addedTables); } } /* traverse remaining DBFactories, if any */ OrderedMap<String, DBFactory<? extends DBRecord>> dbFactMap = new OrderedMap<String, DBFactory<? extends DBRecord>>(DBAdmin.getTableFactoryMap()); for (Iterator<String> i = dbFactMap.keyIterator(); i.hasNext(); ) { String tn = i.next(); DBFactory<? extends DBRecord> dbFact = (DBFactory<? extends DBRecord>) dbFactMap.get(tn); DBFactoryTree._traverseDBFactoryTree(0, dbFact, rootNode, addedTables); } /* return list of root children */ DBFactoryTree roots[] = rootNode.getChildren(); if (roots != null) { // clear our temporary root node from the table factory roots for (int i = 0; i < roots.length; i++) { roots[i].setParentNode(null); } } return roots; }
/** * ** Returns an array of root DBFactoryTree nodes based on the specified top-level DBFactory. * ** @param initialFactory The root DBFactory which is traversed to create the DBFactoryTree. If * null ** all root DBFactories will be traversed. ** @return An array of rot DBFactoryTree nodes. * If the 'initialFactory' is null, the returned ** DBFactoryTree array will have at most 1 * element. */ @SuppressWarnings("unchecked") public static DBFactoryTree[] getDBFactoryTree(DBFactory<? extends DBRecord> initialFactory) { if (initialFactory != null) { return DBFactoryTree.getDBFactoryTree( new DBFactory[] {initialFactory}); // "unchecked conversion" } else { return DBFactoryTree.getDBFactoryTree((DBFactory<? extends DBRecord>[]) null); } }
/** * ** Traverses the DBFactory dependency tree, creating a DBFactoryTree ** @param level The * current tree level ** @param dbFact The current DBFactory to add ** @param parentNode The * parent node to which a new DBFactoryNode child will be added ** @param addedTables A set of * table names added to the current DBFactoryTree * */ private static void _traverseDBFactoryTree( int level, DBFactory<? extends DBRecord> dbFact, DBFactoryTree parentNode, Set<String> addedTables) { /* no DBFactory? */ if (dbFact == null) { Print.logError("Null DBFactory!"); return; } String utableName = dbFact.getUntranslatedTableName(); /* already added? */ if (addedTables.contains(utableName)) { return; } addedTables.add(utableName); /* add this node */ // Print.logInfo(StringTools.replicateString(" ",level) + dbFact.getUntranslatedTableName()); DBFactoryTree dbFactNode = new DBFactoryTree(level, parentNode, dbFact); parentNode.addChild(dbFactNode); /* find dependent children */ DBFactory<? extends DBRecord> childFact[] = dbFact.getChildFactories(); for (int i = 0; i < childFact.length; i++) { int index = childFact[i].getParentTables().indexOf(utableName); if (level == index) { DBFactoryTree._traverseDBFactoryTree(level + 1, childFact[i], dbFactNode, addedTables); } else if (!addedTables.contains(childFact[i].getUntranslatedTableName())) { Print.logWarn( "Skipping table in heiarchy: " + utableName + " ==> " + childFact[i].getUntranslatedTableName()); } } }
/** ** Returns an OrderedMap of defined primary keys ** @return map of defined primary keys */ public Map<DBField, Object> getKeyMap() { Map<DBField, Object> keyMap = new OrderedMap<DBField, Object>(); // ordered key list if (this.dbFactory != null) { DBField keyField[] = this.dbFactory.getKeyFields(); for (int i = 0; i < keyField.length; i++) { String key = keyField[i].getName(); Object val = null; if (this.dbRecord != null) { // all key values will be defined val = this.dbRecord.getFieldValue(key); } else { // use parent keys DBFactoryTree parent = this.getParentNode(); for (; (parent != null) && (val == null); parent = parent.getParentNode()) { DBRecord dbr = parent.getDBRecord(); if (dbr == null) { // stop at the first undefined ancestor break; } else if (dbr.hasField(key)) { // try getting key value from ancestor DBField parFld = dbr.getField(key); if ((parFld != null) && parFld.isPrimaryKey()) { // primary key fields only val = dbr.getFieldValue(key); } } } } // save key DBField and value if (val != null) { keyMap.put(keyField[i], val); } else { keyMap.put(keyField[i], null); } } } return keyMap; }