/** ** 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; }
/** * ** Sets the DBRecord for this node ** @param record The DBRecord to set ** @return True if the * DBRecord was successfully set, false otherwise */ public boolean setDBRecord(DBRecord<?> record) { /* pre-checks */ this.dbRecord = null; if (record == null) { return false; } else if (!this.hasDBFactory()) { // TODO: we probably could just retrieve the DBFactory from the DBRecord Print.logError("DBFactory is not defined!"); return false; } /* check DBFactory */ DBFactory<?> rcdFact = DBRecord.getFactory(record); if (!this.getDBFactory().equals(rcdFact)) { Print.logError("Invalid DBFactory for specified DBRecord!"); return false; } /* set DBRecord */ this.dbRecord = record; return true; }
/** * ** Gets the DBRecord associated with this key ** @param reload If the record should be reloaded * before it is returned ** @return The DBRecord associated with this key */ public gDBR getDBRecord(boolean reload) { // returns null if there is an error /* create record */ if (this.record == null) { try { this.record = DBRecord._createDBRecord(this); } catch (DBException dbe) { // Implementation error (this should never occur) // an NPE will likely follow Print.logStackTrace("Implementation error - cant' create DB record", dbe); return null; } } /* reload */ if (reload) { // 'reload' is ignored if key does not exist this.record.reload(); } /* return record (never null) */ return this.record; }