Calculated Fields

(November 2012 working on Liferay versions 6.0.x, 6.1.x & 6.2.x)

 

      I could not believe, when Davide Rossi proposed to contribute XMLPortletFactory with a development I was thinking of, long time ago. The "calculated fields". 

      Who hasn't been in need of displaying a value in their screens, without having to store it? Many possiblilities come to my mind, for example, If I have the price and the tax percentage (VAT,IVA,...etc), why should I have to store the total amount? I only want to display it, it is a calculated field, redundancy here is the key, and programmers deal with it all the time.

        So just imagine, you have a table where there are columns for "price", "percentDiscount" and "percentVAT" and you need to display the calculated total, and every time some of the previous three fields change, the calculated total changes accordingly.

         The final result of this programming effort is very simple to apply to our XMLPortletFactory project, you just have to include a field in your xml definition like this:

 

<field>
    <name>calculatedTotal</name>
    <title>Total</title> 
    <type> 
        <calculated>(price-(price*(percentDiscount/100)))+(price*(percentVAT/100))</calculated>
    </type>
    <showFieldInView>true</showFieldInView>
    <required>true</required>
</field>
 

 

          This definition leads to the following results, see screen captures of it:
 

           1.-  Inserting a new row: price = 100, percentDiscount = 12 and percentVAT = 12

 
          2.- Result in view
 
 
 
          Find here the xml definition for this example.  CalculatedFields.xml
 
         Find here the war created with this definition. (Liferay SDK 6.1.1-ce-ga2) Calculated-portlet-6.1.1.1.war
 
          Pd.: I know the formula for discount and taxes is wrong, I just want to show the simpliest example possible.