Purchase order lines

When creating an association in ECO you are not guaranteed the order in which the objects at the end of the association will be presented.  For this reason ECO provides a Boolean "ordered" property on association ends.

When you set Ordered = True in your model and then generate your database, ECO will create an additional column in your database identifying the sequence of the objects.

I have just been creating SalesInvoice / SalesInvoiceLine classes and chose to have an ordered association, basically because it is standard industry practise to identify each line of an Invoice/Order with a sequencial number, making it easily identifyable when discussing it.

It would have been nice if I could have used an OCL derived attribute with an expression similar to this:

this.SalesInvoice.lines->indexOf(self)

but unfortunately OCL doesn't seem to have a way to find the index of an object in a collection, so I had to resort to writing a code-derived attribute.

First I marked the attribute as Derived, and didn't enter any DerivationOCL for it.  Then I added a public method to the class with the following signature

public Int32 LineNumberDeriveAndSubscribe(ISubscriber reEvaluateSubscriber, ISubscriber resubscribeSubscriber)

Finally, the code for the attribute had to do the following

  1. Subscribe to the SalesInvoice property
  2. The object has no SalesInvoice, return -1
  3. Subscribe to SalesInvoice.Lines
  4. Return the index of this object in SalesInvoice.Lines

public Int32 LineNumberDeriveAndSubscribe(ISubscriber reEvaluateSubscriber, ISubscriber resubscribeSubscriber)
{
    AsIObject().Properties["SalesInvoice"].SubscribeToValue(resubscribeSubscriber);
    if (this.SalesInvoice == null)
        return -1;
    else
    {
        SalesInvoice.AsIObject().Properties["Lines"].SubscribeToValue(resubscribeSubscriber);
        return this.SalesInvoice.Lines.IndexOf(this) + 1;
    }
}

For an explanation of the difference between ReEvaluate and ReSubscribe, take a look at this article.

 

Share this article!

Follow us!

Find more helpful articles: