예제 #1
0
 /** INTERNAL: Add the field-value pair to the document. */
 public void add(DatabaseField key, Object value) {
   if (null == value) {
     return;
   }
   XMLField xmlField = convertToXMLField(key);
   XPathFragment lastFragment = xmlField.getLastXPathFragment();
   XMLConversionManager xcm =
       (XMLConversionManager) session.getDatasourcePlatform().getConversionManager();
   if (lastFragment.nameIsText()) {
     String stringValue = (String) xcm.convertObject(value, String.class);
     characters(stringValue);
   } else if (lastFragment.isAttribute()) {
     String stringValue = (String) xcm.convertObject(value, String.class);
     attribute(lastFragment, xmlField.getNamespaceResolver(), stringValue);
   } else {
     element(lastFragment);
   }
 }
 /**
  * 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;
 }
  /**
   * INTERNAL: Create (if necessary) and populate a reference object that will be used during the
   * mapping reference resolution phase after unmarshalling is complete.
   *
   * @param record
   * @param xmlField
   * @param object
   * @param session
   * @return
   */
  public void buildReference(
      UnmarshalRecord record, XMLField xmlField, Object object, AbstractSession session) {
    ReferenceResolver resolver = ReferenceResolver.getInstance(session);
    if (resolver == null) {
      return;
    }

    Object srcObject = record.getCurrentObject();
    Reference reference = resolver.getReference(this, srcObject);
    // if reference is null, create a new instance and set it on the resolver
    if (reference == null) {
      reference = new Reference(this, srcObject, getReferenceClass(), new HashMap());
      resolver.addReference(reference);
    }

    XMLField tgtFld = (XMLField) getSourceToTargetKeyFieldAssociations().get(xmlField);
    String tgtXPath = tgtFld.getXPath();
    Vector pks;
    HashMap primaryKeyMap = reference.getPrimaryKeyMap();
    if (primaryKeyMap.containsKey(tgtXPath)) {
      pks = (Vector) primaryKeyMap.get(tgtXPath);
    } else {
      pks = new Vector();
      primaryKeyMap.put(tgtXPath, pks);
    }

    ClassDescriptor descriptor = session.getClassDescriptor(getReferenceClass());
    Class type = descriptor.getTypedField(tgtFld).getType();
    XMLConversionManager xmlConversionManager =
        (XMLConversionManager) session.getDatasourcePlatform().getConversionManager();
    for (StringTokenizer stok = new StringTokenizer((String) object); stok.hasMoreTokens(); ) {
      Object value = xmlConversionManager.convertObject(stok.nextToken(), type);
      if (value != null) {
        pks.add(value);
      }
    }
  }