Пример #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
  /**
   * ** Gets a virtual DBRecord from the specified remote service ** @param servReq The remote web
   * service ** @return The virtual DBRecord (cannot be saved or reloaded)
   */
  @SuppressWarnings("unchecked")
  public gDBR getVirtualDBRecord(final ServiceRequest servReq) throws DBException {
    String CMD_dbget = DBFactory.CMD_dbget;
    String TAG_Response = servReq.getTagResponse();
    String TAG_Record = DBFactory.TAG_Record;
    String ATTR_command = servReq.getAttrCommand();
    String ATTR_result = servReq.getAttrResult();

    /* send request / get response */
    Document xmlDoc = null;
    try {
      xmlDoc =
          servReq.sendRequest(
              CMD_dbget,
              new ServiceRequest.RequestBody() {
                public StringBuffer appendRequestBody(StringBuffer sb, int indent) {
                  return DBRecordKey.this.toRequestXML(sb, indent);
                }
              });
    } catch (IOException ioe) {
      Print.logException("Error", ioe);
      throw new DBException("Request read error", ioe);
    }

    /* parse 'GTSResponse' */
    Element gtsResponse = xmlDoc.getDocumentElement();
    if (!gtsResponse.getTagName().equalsIgnoreCase(TAG_Response)) {
      Print.logError("Request XML does not start with '%s'", TAG_Response);
      throw new DBException("Response XML does not begin eith '" + TAG_Response + "'");
    }

    /* request command/argument */
    String cmd = StringTools.trim(gtsResponse.getAttribute(ATTR_command));
    String result = StringTools.trim(gtsResponse.getAttribute(ATTR_result));
    if (StringTools.isBlank(result)) {
      result = StringTools.trim(gtsResponse.getAttribute("type"));
    }
    if (!result.equalsIgnoreCase("success")) {
      Print.logError("Response indicates failure");
      throw new DBException("Response does not indicate 'success'");
    }

    /* Record */
    NodeList rcdList = XMLTools.getChildElements(gtsResponse, TAG_Record);
    if (rcdList.getLength() <= 0) {
      Print.logError("No 'Record' tags");
      throw new DBException("GTSResponse does not contain any 'Record' tags");
    }
    Element rcdElem = (Element) rcdList.item(0);

    /* return DBRecord */
    gDBR dbr = (gDBR) DBFactory.parseXML_DBRecord(rcdElem);
    dbr.setVirtual(true);
    return dbr;
  }
Пример #3
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;
  }
Пример #4
0
 /** ** Encodes this DBRecordKey into XML for "GTSRequest' purposes */
 private StringBuffer toRequestXML(StringBuffer sb, int indent) {
   boolean isSoapReq = false;
   if (sb == null) {
     sb = new StringBuffer();
   }
   DBRecordKey<gDBR> recKey = this;
   String tableName = recKey.getTableName();
   DBField fld[] = recKey.getKeyFields(); // KEY fields
   DBFieldValues fldVals = recKey.getFieldValues();
   String PFX1 = XMLTools.PREFIX(isSoapReq, indent);
   sb.append(PFX1);
   sb.append(
       XMLTools.startTAG(
           isSoapReq,
           DBFactory.TAG_Record,
           XMLTools.ATTR(DBFactory.ATTR_table, tableName),
           false,
           true));
   DBFactory.writeXML_DBFields(sb, 2 * indent, fld, fldVals, isSoapReq);
   sb.append(PFX1);
   sb.append(XMLTools.endTAG(isSoapReq, DBFactory.TAG_Record, true));
   return sb;
 }
Пример #5
0
 /**
  * ** Encodes this DBRecordKey into XML ** @param sb The StringBuffer to which the DBRecord XML is
  * writen ** @param indent The number of spaces to indent ** @param sequence An optional record
  * sequence number ** @param soapXML True for SOAP XML ** @return The StringBuffer
  */
 public StringBuffer toXML(StringBuffer sb, int indent, int sequence, boolean soapXML) {
   if (sb == null) {
     sb = new StringBuffer();
   }
   String prefix = StringTools.replicateString(" ", indent);
   DBRecordKey<gDBR> recKey = this;
   String tableName = recKey.getTableName();
   DBField fld[] = recKey.getKeyFields(); // KEY fields
   DBFieldValues fldVals = recKey.getFieldValues();
   String PFX1 = XMLTools.PREFIX(soapXML, indent);
   sb.append(PFX1);
   sb.append(
       XMLTools.startTAG(
           soapXML,
           DBFactory.TAG_RecordKey,
           XMLTools.ATTR(DBFactory.ATTR_table, tableName)
               + ((sequence > 0) ? XMLTools.ATTR(DBFactory.ATTR_sequence, sequence) : ""),
           false,
           true));
   DBFactory.writeXML_DBFields(sb, 2 * indent, fld, fldVals, soapXML);
   sb.append(PFX1);
   sb.append(XMLTools.endTAG(soapXML, DBFactory.TAG_RecordKey, true));
   return sb;
 }