quinta-feira, 16 de fevereiro de 2006

Class Attributes with OCL (Derivation OCL)

Its amazing how ECO let us solve some requests with just a few minutes. Today I have to add an field that calculates an sugested value for the products of my client in its entry form, so the solution was simple, I added an derived attribute in my Product class with the follow OCL:

self.BuyOrderItem.BuyOrder->orderBy(InsertedDate)->last.BuyOrderItem->Select(ee.Product = self).Value + (self.BuyOrderItem.BuyOrder->orderBy(InsertedDate)->last.BuyOrderItem->Select(ee.Product = self).Value / 100.safeCast(Decimal) * self.Profit)

Model Description: The model has a class called Product that has an attribute called Profit and has an relationship with class BuyOrderItem that has the product payed value and has an relationship with the class BuyOrder that has an attribute called InsertedDate.

OCL Explanation:
We have accessed trought the Product class (self) the Buy Orders (BuyOrder) and have order it by its Inserted Date (InsertedDate), after that we get its last BuyOrder and select its BuyOrderItem that has our product (->Select(ee.Product = self) and get its payed value.
Ok now we have calc the sugested value so lets sum that value plus that value div by 100 multiplied by the product profit attribute that we have in our product class. To do that just sum the first value that we have get, the OCL should be like that:

self.BuyOrderItem.BuyOrder->orderBy(InsertedDate)->last.BuyOrderItem->Select(ee.Product = self).Value + (self.BuyOrderItem.BuyOrder->orderBy(InsertedDate)->last.BuyOrderItem->Select(ee.Product = self).Value

Now we divide by 100 and multiply by our Profit value, so it will be like that:
self.BuyOrderItem.BuyOrder->orderBy(InsertedDate)->last.BuyOrderItem->Select(ee.Product = self).Value + (self.BuyOrderItem.BuyOrder->orderBy(InsertedDate)->last.BuyOrderItem->Select(ee.Product = self).Value / 100.safeCastDecimal * self.Profit)

Ps.: 100.safeCast was used becuse ECO still has some bugs when working with decimal types, dont forget to get the decimal suport unit at the borland website.

The objective was to do that the product class has an calculated attribute tha get the las value payed of a product plus the profit value that the client has especified for the product.

so the problem was Solved ;)

Nenhum comentário: