Пример #1
0
 protected void _deleteDependencies() throws DBException {
   DBField[] keyFlds = this.getKeyFields();
   DBFieldValues fldVals = this.getFieldValues();
   DBFactory fact = this.getFactory();
   DBFactory childFact[] = fact.getChildFactories();
   for (int i = 0; i < childFact.length; i++) {
     DBRecordKey key = childFact[i].createKey(); // an empty key
     for (int k = 0; k < keyFlds.length; k++) {
       String fldName = keyFlds[k].getName();
       if (fldVals.hasFieldValue(fldName)) {
         Object fldValue = fldVals.getFieldValue(fldName);
         key.setFieldValue(fldName, fldValue);
       } else {
         throw new DBException("Missing dependent key fields!");
       }
     }
     // Do not perform recursive dependency deletion!
     // - 'key' is an incomplete (partial key only), and dependency deletion would fail
     // - all dependent children should already be specified by "getChildFactories()"
     try {
       int whereKeyType = DBWhere.KEY_PARTIAL_ALL; // Should use ALL available partial keys.
       key._delete(null, whereKeyType); // primary key delete
     } catch (SQLException sqe) {
       throw new DBException("Record deletion", sqe);
     }
   }
 }
Пример #2
0
  /**
   * ** Returns true if the parent records in their respective parent tables exist. ** @return True
   * if the parent records exist.
   */
  public boolean parentsExist() throws DBException {
    DBFactory<gDBR> dbFact = this.getFactory();
    DBFieldValues myFldVals = this.getFieldValues();
    java.util.List<String> parentList = dbFact.getParentTables();
    for (String parentTable : parentList) {

      /* get parent table DBFactory */
      Print.logInfo("[%s] Parent table: %s", this.getTableName(), parentTable);
      DBFactory parentFact = DBFactory.getFactoryByName(parentTable);
      if (parentFact == null) {
        Print.logError("Unexpected error finding parent table: " + parentTable);
        return false;
      }

      /* create parent record key with fields from this key */
      DBRecordKey parentKey = parentFact.createKey(); // an empty key
      DBField parentKeyFlds[] = parentFact.getKeyFields();
      for (DBField pkf : parentKeyFlds) {
        String pfn = pkf.getName();

        /* get this DBField */
        DBField myKeyFld = this.getField(pfn);
        if (myKeyFld == null) {
          Print.logError("Unexpected error finding field: [" + this.getTableName() + "] " + pfn);
          return false;
        }

        /* get parent key field value */
        Object pkv = myFldVals.getFieldValue(pfn);
        if (pkv == null) {
          Print.logError("Unexpected error finding parent field: [" + parentTable + "] " + pfn);
          return false;
        }
        if (myKeyFld.isDefaultValue(pkv)) {
          Print.logInfo("This key contains a global value, skipping parent check: " + parentTable);
          parentKey = null;
          break;
        }
        parentKey.setFieldValue(pfn, pkv);
      }

      /* check parent existence */
      if ((parentKey != null) && !parentKey.exists()) {
        Print.logError("Parent record does not exist: [" + parentTable + "] " + parentKey);
        return false;
      }
    }
    return true;
  }
Пример #3
0
 /**
  * ** Returns true if this key fully defines all key fields ** @return True if this key fully
  * defines all key fields
  */
 public boolean isFullKey() {
   DBField kfld[] = this.getKeyFields();
   if (ListTools.isEmpty(kfld)) {
     return false;
   } else {
     DBFieldValues fldVals = this.getFieldValues(); // hasPartialKey
     for (int i = 0; i < kfld.length; i++) {
       String fldName = kfld[i].getName();
       if (!fldVals.hasFieldValue(fldName)) {
         return false;
       }
     }
     return true;
   }
 }
Пример #4
0
 /**
  * ** Returns a string representation of this object ** @return The string representation of this
  * object
  */
 public String toString() {
   DBField kf[] = this.getKeyFields();
   if (kf.length == 0) {
     return "<null>";
   } else {
     DBFieldValues fv = this.getFieldValues();
     StringBuffer sb = new StringBuffer();
     for (int i = 0; i < kf.length; i++) {
       if (i > 0) {
         sb.append(",");
       }
       sb.append(fv.getFieldValueAsString(kf[i].getName()));
     }
     return sb.toString();
   }
 }
Пример #5
0
  /**
   * ** Returns true if this object is equivilent to the specified object ** @param other The other
   * object ** @return True if <code>other</code> is the same class and all fields and ** field
   * values are the same
   */
  public boolean equals(Object other) {
    if (other == null) {

      return false;

    } else if (this.getClass().equals(other.getClass())) {

      /* get key fields */
      DBField thisKfld[] = this.getKeyFields();
      DBField othrKfld[] = ((DBRecordKey) other).getKeyFields();
      if (thisKfld.length != othrKfld.length) {
        return false;
      }

      /* compare field values */
      DBFieldValues thisFval = this.getFieldValues();
      DBFieldValues othrFval = ((DBRecordKey) other).getFieldValues();
      for (int i = 0; (i < thisKfld.length); i++) {
        if (!thisKfld[i].equals(othrKfld[i])) {
          return false;
        }
        Object thisKey = thisFval.getFieldValue(thisKfld[i].getName());
        Object othrKey = othrFval.getFieldValue(othrKfld[i].getName());
        if ((thisKey == null) || (othrKey == null)) {
          if (thisKey != othrKey) {
            return false;
          }
        } else if (!thisKey.equals(othrKey)) {
          return false;
        }
      }

      /* equals */
      return true;
    }

    return false;
  }
Пример #6
0
  /**
   * ** Return the 'WHERE' clause for this key [CHECK] ** @param altIndexName The alternate index
   * name. If null or blank, uses ** primary keys instead ** @param whereKeyType The where key type.
   * One of the constants from DBWhere ** @return The 'WHERE' clause for this key
   */
  protected String _getWhereClause(
      String altIndexName, int whereKeyType) // boolean fullKeyRequired)
      throws DBException {

    /* key fields */
    boolean usePrimaryKey = StringTools.isBlank(altIndexName);
    DBField keyFlds[] = usePrimaryKey ? this.getKeyFields() : this.getAltKeyFields(altIndexName);
    if (ListTools.isEmpty(keyFlds)) {
      throw new DBException("No keys found!");
    }

    /* WHERE */
    DBWhere dwh = new DBWhere(this.getFactory());
    DBFieldValues fldVals = this.getFieldValues();
    int keyCnt = 0;
    boolean hasPartialKey = false;
    for (int i = 0; i < keyFlds.length; i++) {
      String fldName = keyFlds[i].getName();
      if (fldVals.hasFieldValue(fldName)) {
        if (!hasPartialKey || (whereKeyType == DBWhere.KEY_PARTIAL_ALL)) {
          String fev = dwh.EQ(fldName, fldVals.getFieldValueAsString(fldName));
          if (keyCnt > 0) {
            dwh.append(dwh.AND_(fev));
          } else {
            dwh.append(fev);
          }
          keyCnt++;
        } else {
          // whereKeyType == DBWhere.KEY_PARTIAL_FIRST, and we found a subsequent key
          String m =
              "Additional partial key in 'WHERE' clause! ["
                  + this.getTableName()
                  + "."
                  + fldName
                  + "]";
          // throw new DBException(m); // TODO:
          Print.logWarn("******************************************************************");
          Print.logWarn(m);
          // Print.logWarn(StringTools.join(keyFlds,","));
          // Print.logStackTrace(m);
          Print.logWarn("******************************************************************");
        }
      } else if ((i == 0) && (whereKeyType != DBWhere.KEY_PARTIAL_ALL_EMPTY)) { //
        // missing first key
        if (keyFlds[i].isAutoIncrement()) {
          // first key is an "auto_increment" and it is not present
          // assume that we are expecting the DB server to create this value for us, thus the key
          // dow not exist
          // However, there is nothing we can do about this here.
          String m =
              "First key field for 'WHERE' clause is 'auto_increment' and field is not present ["
                  + this.getTableName()
                  + "."
                  + fldName
                  + "]";
          throw new DBException(m);
        } else {
          String m =
              "Missing first key field for 'WHERE' clause! ["
                  + this.getTableName()
                  + "."
                  + fldName
                  + "]";
          throw new DBException(m);
        }
      } else if (whereKeyType == DBWhere.KEY_FULL) {
        // missing a key when all keys are required
        String m = "Missing key for 'WHERE' clause! [" + this.getTableName() + "." + fldName + "]";
        throw new DBException(m);
      } else {
        // only a portion of the key has been specified.
        // This is a common occurance deleting an Account/Device with sub-dependencies
        // Print.logWarn("Key field not specified: " + this.getTableName() + "." + fldName);
        hasPartialKey = true;
      }
    }

    return (keyCnt > 1) ? dwh.WHERE(dwh.toString()) : dwh.WHERE_(dwh.toString());
  }