/** INTERNAL: */
  public Vector getForeignKeyRows(AbstractRecord row) {
    Vector subRows = new Vector();
    if (getForeignKeyGroupingElement() == null) {
      if (this.getSourceForeignKeyFields().size() > 0) {
        Object values = row.getValues((DatabaseField) this.getSourceForeignKeyFields().get(0));

        if (values != null) {
          if (values instanceof Vector) {
            int valuesSize = ((Vector) values).size();
            for (int j = 0; j < valuesSize; j++) {
              XMLRecord newRecord = new DOMRecord("test");
              newRecord.setSession(((XMLRecord) row).getSession());
              newRecord.put(this.getSourceForeignKeyFields().get(0), ((Vector) values).get(j));
              subRows.add(newRecord);
            }
          } else {
            XMLRecord newRecord = new DOMRecord("test");
            newRecord.setSession(((XMLRecord) row).getSession());
            newRecord.put(getSourceForeignKeyFields().get(0), values);
            subRows.add(newRecord);
          }
        }
      }
    } else {
      subRows = (Vector) row.getValues(getForeignKeyGroupingElement());
    }
    return subRows;
  }
  /**
   * 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);
      }
    }
  }
Exemplo n.º 3
0
 /** INTERNAL: Build the result value for the row. */
 public Object buildObject(AbstractRecord row) {
   if (this.resultType == AUTO) {
     List values = row.getValues();
     if (values.size() == 1) {
       return row.getValues().get(0);
     } else {
       return row.getValues().toArray();
     }
   } else if (this.resultType == ARRAY) {
     return row.getValues().toArray();
   } else if (this.resultType == ATTRIBUTE) {
     // Use get with field for XML records.
     Object value = row.get(row.getFields().get(0));
     if (getValueConverter() != null) {
       value = getValueConverter().convertDataValueToObjectValue(value, this.session);
     }
     return value;
   }
   return row;
 }
 /**
  * INTERNAL: Extract the primary key values from the row, then create an
  * org.eclipse.persistence.internal.oxm.Reference instance and stored it on the session's
  * org.eclipse.persistence.internal.oxm.ReferenceResolver.
  */
 public Object readFromRowIntoObject(
     AbstractRecord databaseRow,
     JoinedAttributeManager joinManager,
     Object targetObject,
     ObjectBuildingQuery sourceQuery,
     AbstractSession executionSession)
     throws DatabaseException {
   ClassDescriptor descriptor = sourceQuery.getSession().getClassDescriptor(getReferenceClass());
   ContainerPolicy cp = this.getContainerPolicy();
   Vector pkFieldNames = referenceDescriptor.getPrimaryKeyFieldNames();
   Vector primaryKeyValues = new Vector();
   primaryKeyValues.setSize(pkFieldNames.size());
   HashMap primaryKeyMap = new HashMap();
   // for each source xmlField, get the value from the row and store
   for (Iterator fieldIt = getFields().iterator(); fieldIt.hasNext(); ) {
     XMLField fld = (XMLField) fieldIt.next();
     XMLField tgtFld = (XMLField) getSourceToTargetKeyFieldAssociations().get(fld);
     Object fieldValue = databaseRow.getValues(fld);
     if ((fieldValue == null)
         || (fieldValue instanceof String)
         || !(fieldValue instanceof Vector)) {
       return cp.containerInstance();
     }
     // fix for bug# 5687430
     // need to get the actual type of the target (i.e. int, String, etc.)
     // and use the converted value when checking the cache.
     XMLConversionManager xmlConversionManager =
         (XMLConversionManager) executionSession.getDatasourcePlatform().getConversionManager();
     Vector newValues = new Vector();
     for (Iterator valIt = ((Vector) fieldValue).iterator(); valIt.hasNext(); ) {
       for (StringTokenizer stok = new StringTokenizer((String) valIt.next());
           stok.hasMoreTokens(); ) {
         Object value =
             xmlConversionManager.convertObject(
                 stok.nextToken(), descriptor.getTypedField(tgtFld).getType());
         if (value != null) {
           newValues.add(value);
         }
       }
     }
     primaryKeyMap.put(tgtFld.getXPath(), newValues);
   }
   // store the Reference instance on the resolver for use during mapping resolution phase
   ReferenceResolver resolver = ReferenceResolver.getInstance(sourceQuery.getSession());
   if (resolver != null) {
     resolver.addReference(new Reference(this, targetObject, referenceClass, primaryKeyMap));
   }
   return null;
 }
 /** Build a collection of database rows from the Record returned from the interaction. */
 public Vector buildRows(Record record, EISAccessor accessor) {
   Vector rows = null;
   if (record == null) {
     return new Vector(0);
   }
   AbstractRecord row =
       accessor.getEISPlatform().createDatabaseRowFromDOMRecord(record, this, accessor);
   if (getOutputResultPath().length() > 0) {
     Vector values = (Vector) row.getValues(getOutputResultPath());
     if (values == null) {
       values = new Vector(0);
     }
     rows = values;
   } else {
     rows = new Vector(1);
     rows.add(row);
   }
   return rows;
 }
  public Object valueFromRow(
      AbstractRecord row,
      JoinedAttributeManager joinManager,
      ObjectBuildingQuery sourceQuery,
      CacheKey cacheKey,
      AbstractSession executionSession,
      boolean isTargetProtected,
      Boolean[] wasCacheUsed)
      throws DatabaseException {
    ContainerPolicy cp = this.getContainerPolicy();

    Object fieldValue = row.getValues(this.getField());

    // BUG#2667762 there could be whitespace in the row instead of null
    if ((fieldValue == null) || (fieldValue instanceof String)) {
      if (reuseContainer) {
        Object currentObject = ((XMLRecord) row).getCurrentObject();
        Object container = getAttributeAccessor().getAttributeValueFromObject(currentObject);
        return container != null ? container : cp.containerInstance();
      } else {
        return cp.containerInstance();
      }
    }

    Vector nestedRows =
        this.getDescriptor().buildNestedRowsFromFieldValue(fieldValue, executionSession);
    if (nestedRows == null) {
      if (reuseContainer) {
        Object currentObject = ((XMLRecord) row).getCurrentObject();
        Object container = getAttributeAccessor().getAttributeValueFromObject(currentObject);
        return container != null ? container : cp.containerInstance();
      } else {
        return cp.containerInstance();
      }
    }

    Object result = null;
    if (reuseContainer) {
      Object currentObject = ((XMLRecord) row).getCurrentObject();
      Object container = getAttributeAccessor().getAttributeValueFromObject(currentObject);
      result = container != null ? container : cp.containerInstance();
    } else {
      result = cp.containerInstance(nestedRows.size());
    }

    for (Enumeration stream = nestedRows.elements(); stream.hasMoreElements(); ) {
      XMLRecord nestedRow = (XMLRecord) stream.nextElement();
      Object objectToAdd;
      if (getNullPolicy().valueIsNull((Element) nestedRow.getDOM())) {
        objectToAdd = null;
      } else {
        objectToAdd =
            buildObjectFromNestedRow(
                nestedRow, joinManager, sourceQuery, executionSession, isTargetProtected);
      }
      cp.addInto(objectToAdd, result, sourceQuery.getSession());
      if (null != getContainerAccessor()) {
        Object currentObject = ((XMLRecord) row).getCurrentObject();
        if (this.inverseReferenceMapping.getContainerPolicy() == null) {
          getContainerAccessor().setAttributeValueInObject(objectToAdd, currentObject);
        } else {
          Object backpointerContainer =
              getContainerAccessor().getAttributeValueFromObject(objectToAdd);
          if (backpointerContainer == null) {
            backpointerContainer =
                this.inverseReferenceMapping.getContainerPolicy().containerInstance();
            getContainerAccessor().setAttributeValueInObject(objectToAdd, backpointerContainer);
          }
          this.inverseReferenceMapping
              .getContainerPolicy()
              .addInto(currentObject, backpointerContainer, executionSession);
        }
      }
    }
    return result;
  }
 /**
  * INTERNAL: Merge the provided row into this row. Existing field values in this row will be
  * replaced with values from the provided row. Fields not in this row will be added from provided
  * row. Values not in provided row will remain in this row.
  */
 public void mergeFrom(AbstractRecord row) {
   for (int index = 0; index < row.size(); ++index) {
     this.put(row.getFields().get(index), row.getValues().get(index));
   }
 }
  public Object valueFromRow(
      AbstractRecord row,
      JoinedAttributeManager joinManager,
      ObjectBuildingQuery query,
      AbstractSession executionSession) {
    ContainerPolicy cp = this.getContainerPolicy();

    Object fieldValue = row.getValues(this.getField());
    if (fieldValue == null) {
      if (reuseContainer) {
        Object currentObject = ((XMLRecord) row).getCurrentObject();
        Object container = getAttributeAccessor().getAttributeValueFromObject(currentObject);
        return container != null ? container : cp.containerInstance();
      } else {
        return cp.containerInstance();
      }
    }

    Vector fieldValues = this.getDescriptor().buildDirectValuesFromFieldValue(fieldValue);
    if (fieldValues == null) {
      if (reuseContainer) {
        Object currentObject = ((XMLRecord) row).getCurrentObject();
        Object container = getAttributeAccessor().getAttributeValueFromObject(currentObject);
        return container != null ? container : cp.containerInstance();
      } else {
        return cp.containerInstance();
      }
    }

    Object result = null;
    if (reuseContainer) {
      Object currentObject = ((XMLRecord) row).getCurrentObject();
      Object container = getAttributeAccessor().getAttributeValueFromObject(currentObject);
      result = container != null ? container : cp.containerInstance();
    } else {
      result = cp.containerInstance(fieldValues.size());
    }

    for (Enumeration stream = fieldValues.elements(); stream.hasMoreElements(); ) {
      Object element = stream.nextElement();

      // PERF: Direct variable access.
      // Object value = row.get(field);
      // Object fieldValue = null;
      XMLUnmarshaller unmarshaller = ((XMLRecord) row).getUnmarshaller();
      if (element instanceof String) {
        if (this.isSwaRef() && (unmarshaller.getAttachmentUnmarshaller() != null)) {
          fieldValue =
              unmarshaller.getAttachmentUnmarshaller().getAttachmentAsDataHandler((String) element);
        } else if (!this.isSwaRef()) {
          // should be base64
          byte[] bytes =
              ((XMLConversionManager)
                      executionSession.getDatasourcePlatform().getConversionManager())
                  .convertSchemaBase64ToByteArray(element);
          fieldValue = bytes;
        }
      } else {
        // this was an element, so do the XOP/SWAREF/Inline binary cases for an element
        XMLRecord record = (XMLRecord) element;
        record.setSession(executionSession);

        if ((unmarshaller.getAttachmentUnmarshaller() != null)
            && unmarshaller.getAttachmentUnmarshaller().isXOPPackage()
            && !this.isSwaRef()
            && !this.shouldInlineBinaryData()) {
          // look for the include element:
          String xpath = XMLConstants.EMPTY_STRING;

          //  need a prefix for XOP
          String prefix = null;
          NamespaceResolver descriptorResolver =
              ((XMLDescriptor) getDescriptor()).getNamespaceResolver();
          if (descriptorResolver != null) {
            prefix = descriptorResolver.resolveNamespaceURI(XMLConstants.XOP_URL);
          }
          if (prefix == null) {
            prefix = XMLConstants.XOP_PREFIX;
          }
          NamespaceResolver tempResolver = new NamespaceResolver();
          tempResolver.put(prefix, XMLConstants.XOP_URL);
          xpath = prefix + XMLConstants.COLON + INCLUDE + "/@href";
          XMLField field = new XMLField(xpath);
          field.setNamespaceResolver(tempResolver);
          String includeValue = (String) record.get(field);
          if (element != null && includeValue != null) {
            if ((getAttributeElementClass() == ClassConstants.ABYTE)
                || (getAttributeElementClass() == ClassConstants.APBYTE)) {
              fieldValue =
                  unmarshaller.getAttachmentUnmarshaller().getAttachmentAsByteArray(includeValue);
            } else {
              fieldValue =
                  unmarshaller.getAttachmentUnmarshaller().getAttachmentAsDataHandler(includeValue);
            }
          } else {
            // If we didn't find the Include element, check for inline
            fieldValue = record.get(XMLConstants.TEXT);
            // should be a base64 string
            fieldValue =
                ((XMLConversionManager)
                        executionSession.getDatasourcePlatform().getConversionManager())
                    .convertSchemaBase64ToByteArray(fieldValue);
          }
        } else if ((unmarshaller.getAttachmentUnmarshaller() != null) && isSwaRef()) {
          String refValue = (String) record.get(XMLConstants.TEXT);
          if (refValue != null) {
            fieldValue =
                unmarshaller.getAttachmentUnmarshaller().getAttachmentAsDataHandler(refValue);
          }
        } else {
          fieldValue = record.get(XMLConstants.TEXT);
          // should be a base64 string
          fieldValue =
              ((XMLConversionManager)
                      executionSession.getDatasourcePlatform().getConversionManager())
                  .convertSchemaBase64ToByteArray(fieldValue);
        }
      }
      Object attributeValue = fieldValue;
      if (getValueConverter() != null) {
        if (getValueConverter() instanceof XMLConverter) {
          attributeValue =
              ((XMLConverter) getValueConverter())
                  .convertDataValueToObjectValue(fieldValue, executionSession, unmarshaller);
        } else {
          attributeValue =
              getValueConverter().convertDataValueToObjectValue(fieldValue, executionSession);
        }
      }

      cp.addInto(attributeValue, result, query.getSession());
    }
    return result;
  }