/** INTERNAL: Write fields needed for insert into the template for with null values. */
 public void writeInsertFieldsIntoRow(AbstractRecord row, AbstractSession session) {
   if (isForeignKeyRelationship() && !isReadOnly()) {
     if (getForeignKeyGroupingElement() != null) {
       row.put(getForeignKeyGroupingElement(), null);
     } else if (this.getSourceForeignKeyFields().size() > 0) {
       row.put(getSourceForeignKeyFields().get(0), null);
     }
   } else {
     super.writeInsertFieldsIntoRow(row, session);
   }
 }
  /**
   * INTERNAL: Get the appropriate attribute value from the object and put it in the appropriate
   * field of the database row. Loop through the reference objects and extract the primary keys and
   * put them in the vector of "nested" rows.
   */
  public void writeFromObjectIntoRow(Object object, AbstractRecord row, AbstractSession session) {
    if (!isForeignKeyRelationship) {
      return;
    }

    if (((getSourceForeignKeysToTargetKeys()) == null)
        || (getSourceForeignKeysToTargetKeys().size() == 0)) {
      return;
    }

    if (this.isReadOnly()) {
      return;
    }

    AbstractRecord referenceRow =
        this.getIndirectionPolicy().extractReferenceRow(this.getAttributeValueFromObject(object));
    if (referenceRow != null) {
      // the reference objects have not been instantiated - use the value from the original row
      if (getForeignKeyGroupingElement() != null) {
        row.put(
            this.getForeignKeyGroupingElement(),
            referenceRow.getValues(this.getForeignKeyGroupingElement()));
      } else if (getSourceForeignKeyFields().size() > 0) {
        DatabaseField foreignKeyField = (DatabaseField) getSourceForeignKeyFields().get(0);
        row.put(foreignKeyField, referenceRow.getValues(foreignKeyField));
      }
      return;
    }

    ContainerPolicy cp = this.getContainerPolicy();

    // extract the keys from the objects
    Object attributeValue = this.getRealCollectionAttributeValueFromObject(object, session);
    Vector nestedRows = new Vector(cp.sizeFor(attributeValue));

    if (getForeignKeyGroupingElement() != null) {
      for (Object iter = cp.iteratorFor(attributeValue); cp.hasNext(iter); ) {
        XMLRecord nestedRow =
            this.extractKeyRowFromReferenceObject(cp.next(iter, session), session, (XMLRecord) row);
        nestedRows.addElement(nestedRow);
      }
      row.add(this.getForeignKeyGroupingElement(), nestedRows);
    } else {
      DatabaseField singleField = (DatabaseField) getSourceForeignKeyFields().get(0);
      DatabaseField pkField = (DatabaseField) getSourceForeignKeysToTargetKeys().get(singleField);
      for (Object iter = cp.iteratorFor(attributeValue); cp.hasNext(iter); ) {
        Object singleValue =
            getReferenceDescriptor()
                .getObjectBuilder()
                .extractValueFromObjectForField(cp.next(iter, session), pkField, session);
        row.add(singleField, singleValue);
      }
    }
  }
 /**
  * INTERNAL: This row is built for shallow insert which happens in case of bidirectional inserts.
  * The foreign keys must be set to null to avoid constraints.
  */
 public void writeFromObjectIntoRowForShallowInsertWithChangeRecord(
     ChangeRecord changeRecord, AbstractRecord row, AbstractSession session) {
   if (isForeignKeyRelationship() && !isReadOnly()) {
     if (getForeignKeyGroupingElement() != null) {
       row.put(getForeignKeyGroupingElement(), null);
     } else if (this.getSourceForeignKeyFields().size() > 0) {
       row.put(getSourceForeignKeyFields().get(0), null);
     }
   } else {
     super.writeFromObjectIntoRowForShallowInsertWithChangeRecord(changeRecord, row, session);
   }
 }
 /** INTERNAL: Get a value from the object and set that in the respective field of the row. */
 protected void writeFromNullObjectIntoRow(AbstractRecord record) {
   if (isReadOnly()) {
     return;
   }
   if (isForeignKeyRelationship()) {
     Enumeration foreignKeys = getForeignKeyFields().elements();
     while (foreignKeys.hasMoreElements()) {
       record.put((DatabaseField) foreignKeys.nextElement(), null);
     }
   }
   if (getTypeField() != null) {
     record.put(getTypeField(), null);
   }
 }
  /** INTERNAL: */
  @Override
  public void writeFromObjectIntoRow(
      Object object, AbstractRecord row, AbstractSession session, WriteType writeType)
      throws DescriptorException {
    if (this.isReadOnly()) {
      return;
    }

    Object attributeValue = this.getAttributeValueFromObject(object);
    if (attributeValue == null) {
      row.put(this.getField(), null);
      return;
    }

    ContainerPolicy cp = this.getContainerPolicy();

    Vector nestedRows = new Vector(cp.sizeFor(attributeValue));
    Object iter = cp.iteratorFor(attributeValue);
    if (null != iter) {
      while (cp.hasNext(iter)) {
        Object element = cp.next(iter, session);
        // convert the value - if necessary
        element =
            convertObjectValueToDataValue(element, session, ((XMLRecord) row).getMarshaller());
        if (element == null) {
          XMLNullRepresentationType nullRepresentation =
              getNullPolicy().getMarshalNullRepresentation();
          if (nullRepresentation == XMLNullRepresentationType.XSI_NIL) {
            nestedRows.add(XMLRecord.NIL);
          } else if (nullRepresentation == XMLNullRepresentationType.EMPTY_NODE) {
            Node emptyNode =
                XPathEngine.getInstance()
                    .createUnownedElement(((XMLRecord) row).getDOM(), (XMLField) field);
            DOMRecord nestedRow = new DOMRecord(emptyNode);
            nestedRows.add(nestedRow);
          }
        } else {
          nestedRows.addElement(buildCompositeRow(element, session, row, writeType));
        }
      }
    }

    Object fieldValue = null;
    if (!nestedRows.isEmpty()) {
      fieldValue =
          this.getDescriptor()
              .buildFieldValueFromNestedRows(nestedRows, getStructureName(), session);
    }
    row.put(this.getField(), fieldValue);
  }
  /** INTERNAL: Write the attribute value from the object to the row. */
  public void writeFromObjectIntoRow(Object object, AbstractRecord row, AbstractSession session) {
    // for each xmlField on this mapping
    for (Iterator fieldIt = getFields().iterator(); fieldIt.hasNext(); ) {
      XMLField xmlField = (XMLField) fieldIt.next();
      ContainerPolicy cp = getContainerPolicy();
      Object collection = getAttributeAccessor().getAttributeValueFromObject(object);
      if (collection == null) {
        return;
      }

      Object fieldValue;
      Object objectValue;
      String stringValue = "";
      QName schemaType;
      Object iterator = cp.iteratorFor(collection);
      if (usesSingleNode()) {
        while (cp.hasNext(iterator)) {
          objectValue = cp.next(iterator, session);
          fieldValue = buildFieldValue(objectValue, xmlField, session);
          if (fieldValue != null) {
            schemaType = getSchemaType(xmlField, fieldValue, session);
            String newValue = getValueToWrite(schemaType, fieldValue, session);
            if (newValue != null) {
              stringValue += newValue;
              if (cp.hasNext(iterator)) {
                stringValue += SPACE;
              }
            }
          }
        }
        if (!(stringValue.equals(""))) {
          row.put(xmlField, stringValue);
        }
      } else {
        ArrayList keyValues = new ArrayList();
        while (cp.hasNext(iterator)) {
          objectValue = cp.next(iterator, session);
          fieldValue = buildFieldValue(objectValue, xmlField, session);
          if (fieldValue != null) {
            schemaType = getSchemaType(xmlField, fieldValue, session);
            stringValue = getValueToWrite(schemaType, fieldValue, session);
            // row.add(xmlField, stringValue);
            keyValues.add(stringValue);
          }
        }
        row.put(xmlField, keyValues);
      }
    }
  }
  /** INTERNAL: Write fields needed for insert into the template for with null values. */
  public void writeInsertFieldsIntoRow(AbstractRecord record, AbstractSession session) {
    if (isReadOnly()) {
      return;
    }

    record.put(getField(), null);
  }
 /** PUBLIC: Add all of the elements. */
 public void putAll(Map map) {
   Iterator entriesIterator = map.entrySet().iterator();
   while (entriesIterator.hasNext()) {
     Map.Entry entry = (Map.Entry) entriesIterator.next();
     put(entry.getKey(), entry.getValue());
   }
 }
  /**
   * INTERNAL: This row is built for shallow insert which happens in case of bidirectional inserts.
   * The foreign keys must be set to null to avoid constraints.
   */
  public void writeFromObjectIntoRowForShallowInsertWithChangeRecord(
      ChangeRecord changeRecord, AbstractRecord record, AbstractSession session) {
    if (isReadOnly()) {
      return;
    }

    record.put(getField(), null);
  }
  /** INTERNAL: Get a value from the object and set that in the respective field of the row. */
  @Override
  public void writeFromObjectIntoRowForWhereClause(
      ObjectLevelModifyQuery query, AbstractRecord record) {
    if (isReadOnly()) {
      return;
    }
    Object object;
    if (query.isDeleteObjectQuery()) {
      object = query.getObject();
    } else {
      object = query.getBackupClone();
    }
    Object referenceObject = getRealAttributeValueFromObject(object, query.getSession());

    if (referenceObject == null) {
      writeFromNullObjectIntoRow(record);
    } else {
      if (isForeignKeyRelationship()) {
        Enumeration sourceFields = getForeignKeyFields().elements();
        ClassDescriptor descriptor = query.getSession().getDescriptor(referenceObject.getClass());
        while (sourceFields.hasMoreElements()) {
          DatabaseField sourceKey = (DatabaseField) sourceFields.nextElement();
          String targetQueryKey = (String) getSourceToTargetQueryKeyNames().get(sourceKey);
          DatabaseField targetKeyField =
              descriptor.getObjectBuilder().getFieldForQueryKeyName(targetQueryKey);
          if (targetKeyField == null) {
            throw DescriptorException.variableOneToOneMappingIsNotDefinedProperly(
                this, descriptor, targetQueryKey);
          }
          Object referenceValue =
              descriptor
                  .getObjectBuilder()
                  .extractValueFromObjectForField(
                      referenceObject, targetKeyField, query.getSession());
          record.put(sourceKey, referenceValue);
        }
      }
      if (getTypeField() != null) {
        record.put(getTypeField(), getTypeForImplementor(referenceObject.getClass()));
      }
    }
  }
  /** INTERNAL: Get a value from the object and set that in the respective field of the row. */
  public void writeFromObjectIntoRow(
      Object object, AbstractRecord record, AbstractSession session) {
    if (isReadOnly()) {
      return;
    }

    Object referenceObject = getRealAttributeValueFromObject(object, session);

    if (referenceObject == null) {
      // Fix for 2730536, must put something in modify row, even if it is null.
      record.put(getField(), null);
      return;
    }

    Ref ref =
        ((ObjectRelationalDataTypeDescriptor) getReferenceDescriptor())
            .getRef(referenceObject, session);

    record.put(getField(), ref);
  }
  /**
   * INTERNAL: Build and return the nested database row from the specified field value. The field
   * value better be an Struct.
   */
  public AbstractRecord buildNestedRowFromFieldValue(Object fieldValue) throws DatabaseException {

    AbstractRecord row = new DatabaseRecord();
    Object[] attributes = (Object[]) fieldValue;

    for (int index = 0; index < getOrderedFields().size(); index++) {
      DatabaseField field = (DatabaseField) getOrderedFields().elementAt(index);
      row.put(field, attributes[index]);
    }

    return row;
  }
  /**
   * INTERNAL: Get a value from the object and set that in the respective field of the row. If the
   * mapping id target foreign key, you must only write the type into the roe, the rest will be
   * updated when the object itself is written
   */
  @Override
  public void writeFromObjectIntoRowWithChangeRecord(
      ChangeRecord changeRecord, AbstractRecord record, AbstractSession session) {
    if (isReadOnly()) {
      return;
    }

    ObjectChangeSet changeSet =
        (ObjectChangeSet) ((ObjectReferenceChangeRecord) changeRecord).getNewValue();
    if (changeSet == null) {
      writeFromNullObjectIntoRow(record);
    } else {
      Object referenceObject = changeSet.getUnitOfWorkClone();
      if (isForeignKeyRelationship()) {
        Enumeration sourceFields = getForeignKeyFields().elements();
        ClassDescriptor descriptor = session.getDescriptor(referenceObject.getClass());
        while (sourceFields.hasMoreElements()) {
          DatabaseField sourceKey = (DatabaseField) sourceFields.nextElement();
          String targetQueryKey = (String) getSourceToTargetQueryKeyNames().get(sourceKey);
          DatabaseField targetKeyField =
              descriptor.getObjectBuilder().getFieldForQueryKeyName(targetQueryKey);
          if (targetKeyField == null) {
            throw DescriptorException.variableOneToOneMappingIsNotDefinedProperly(
                this, descriptor, targetQueryKey);
          }
          Object referenceValue =
              descriptor
                  .getObjectBuilder()
                  .extractValueFromObjectForField(referenceObject, targetKeyField, session);
          record.put(sourceKey, referenceValue);
        }
      }
      if (getTypeField() != null) {
        record.put(getTypeField(), getTypeForImplementor(referenceObject.getClass()));
      }
    }
  }
  /** INTERNAL: Get a value from the object and set that in the respective field of the row. */
  public void writeFromObjectIntoRowWithChangeRecord(
      ChangeRecord changeRecord, AbstractRecord record, AbstractSession session) {
    if (isReadOnly()) {
      return;
    }

    ObjectChangeSet changeSet =
        (ObjectChangeSet) ((ObjectReferenceChangeRecord) changeRecord).getNewValue();
    Object referenceObject = changeSet.getUnitOfWorkClone();

    if (referenceObject == null) {
      return;
    }

    Ref ref =
        ((ObjectRelationalDataTypeDescriptor) getReferenceDescriptor())
            .getRef(referenceObject, session);

    record.put(getField(), ref);
  }
 /** Build a database row from the record returned from the interaction. */
 public AbstractRecord buildRow(Record record, EISAccessor accessor) {
   if (record == null) {
     return null;
   }
   AbstractRecord row =
       accessor.getEISPlatform().createDatabaseRowFromDOMRecord(record, this, accessor);
   if (row == null) {
     return null;
   }
   if (getOutputResultPath().length() > 0) {
     row = (AbstractRecord) row.get(getOutputResultPath());
     // Handle the case were the output row is mapped into a database row of values.
   } else if (hasOutputArguments()) {
     row = createXMLRecord(getOutputRootElementName());
     for (int index = 0; index < getOutputArgumentNames().size(); index++) {
       DatabaseField field = (DatabaseField) getOutputArguments().get(index);
       row.put(field, row.get(getOutputArgumentNames().get(index)));
     }
   }
   return row;
 }
  public void writeFromObjectIntoRow(Object object, AbstractRecord row, AbstractSession session) {
    XMLRecord record = (XMLRecord) row;
    XMLMarshaller marshaller = record.getMarshaller();
    Object attributeValue = getAttributeValueFromObject(object);

    ContainerPolicy cp = this.getContainerPolicy();
    Vector elements = new Vector(cp.sizeFor(attributeValue));
    XMLField field = (XMLField) getField();
    NamespaceResolver resolver = field.getNamespaceResolver();
    boolean isAttribute = field.getLastXPathFragment().isAttribute();
    String prefix = null;
    XMLField includeField = null;
    if (!isAttribute) {
      if (record.isXOPPackage() && !isSwaRef() && !shouldInlineBinaryData()) {
        field = (XMLField) getField();

        // If the field's resolver is non-null and has an entry for XOP,
        // use it - otherwise, create a new resolver, set the XOP entry,
        // on it, and use it instead.
        // We do this to avoid setting the XOP namespace declaration on
        // a given field or descriptor's resolver, as it is only required
        // on the current element
        if (resolver != null) {
          prefix = resolver.resolveNamespaceURI(XMLConstants.XOP_URL);
        }
        if (prefix == null) {
          prefix = XMLConstants.XOP_PREFIX; // "xop";
          resolver = new NamespaceResolver();
          resolver.put(prefix, XMLConstants.XOP_URL);
        }
        includeField = new XMLField(prefix + XMLConstants.COLON + INCLUDE + "/@href");
        includeField.setNamespaceResolver(resolver);
      }
    }
    XMLField textField = new XMLField(field.getXPath() + '/' + XMLConstants.TEXT);
    textField.setNamespaceResolver(field.getNamespaceResolver());
    textField.setSchemaType(field.getSchemaType());
    // field = textField;

    boolean inline = false;
    for (Object iter = cp.iteratorFor(attributeValue); cp.hasNext(iter); ) {
      Object element = cp.next(iter, session);
      element = getValueToWrite(element, object, record, field, includeField, session);
      if (element.getClass() == ClassConstants.ABYTE) {
        inline = true;
      }
      if (element != null) {
        elements.addElement(element);
      }
    }
    Object fieldValue = null;
    if (!elements.isEmpty()) {
      fieldValue =
          this.getDescriptor()
              .buildFieldValueFromDirectValues(elements, elementDataTypeName, session);
    }
    if (inline) {
      row.put(textField, fieldValue);
    } else {
      row.put(field, fieldValue);
    }
  }