G'day all,

[snip]
>>    RelationshipHelper.lazyLoad(this,"country");
[snip]

>This does sound quite interesting, it is code you would be 
>able to share 
>with the project. I think I can see places where it would be useful.

I can't submit the whole shebang, as it relies on parts of our
applicatication framework. Here, though, is the main method (The logging
stuff is part of our framework too).... Every one is free to use, abuse
and/or gut the following code, should it be of any use to them.

HTH,

Charles.

  /**
   * If the relationship specified is not null, load the the named
   * relationship
   * @param o instance of an object
   * @param relationshipName the name of the relationship to load
   */
  public void loadRelationship(Object o, String relationshipName) {
    PersistenceBroker broker =
PersistenceBrokerFactory.defaultPersistenceBroker();

    // Get the metadata
    ClassDescriptor classDescriptor =
broker.getClassDescriptor(o.getClass());
    ObjectReferenceDescriptor refDescriptor =
classDescriptor.getObjectReferenceDescriptorByName(relationshipName);
    if (refDescriptor == null) {
      refDescriptor =
classDescriptor.getCollectionDescriptorByName(relationshipName);
    }

    if (refDescriptor == null) {
      Log.error(this, "loadRelationship - couldn't find relationship '" +
relationshipName + "'");
    } else {
      // Get the current value of the relationship
      PersistentField persistentField = refDescriptor.getPersistentField();
      Object currentValue = persistentField.get(o);

      boolean loadRelationship = (currentValue == null);
      if (loadRelationship) {
        // Use the broker to load the data, ensuring that we do a cascade
retrieve
        boolean oldCascade = refDescriptor.getCascadeRetrieve();
        refDescriptor.setCascadeRetrieve(true);
        broker.retrieveReference(o, relationshipName);
        refDescriptor.setCascadeRetrieve(oldCascade);
      } else {
        // the current value is a collection proxy, and the proxy isn't
loaded, access the iterator, which will
        // force it to be loaded
        if (currentValue instanceof CollectionProxy) {
          CollectionProxy collectionProxy = (CollectionProxy) currentValue;
          if (!collectionProxy.isLoaded()) {
            Iterator it = collectionProxy.ojbIterator();
          }
        } else {
          Log.debug(this, "loadRelationship - relationship '" +
relationshipName + "' already loaded on " + o);
        }
      }
    }
    broker.close();
  }


This email and any attachments are strictly confidential and are intended
solely for the addressee. If you are not the intended recipient you must
not disclose, forward, copy or take any action in reliance on this message
or its attachments. If you have received this email in error please notify
the sender as soon as possible and delete it from your computer systems.
Any views or opinions presented are solely those of the author and do not
necessarily reflect those of HPD Software Limited or its affiliates.

 At present the integrity of email across the internet cannot be guaranteed
and messages sent via this medium are potentially at risk.  All liability
is excluded to the extent permitted by law for any claims arising as a re-
sult of the use of this medium to transmit information by or to 
HPD Software Limited or its affiliates.



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to