Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Libraries

A Groovy library is a collection of uniquely named reusable callable Groovy functions linked to our Pricefx application. In Pricefx libraries are basically logics with a library nature type. As with other logics, libraries have elements in which we can write code. They contain definitions of functions, which are used across multiple logics in the same partition. GroovyDoc is a must in this case.

Code Block
languagegroovy
themeMidnight
titleGroovy code in library logic "PriceLib" in element "RoundingUtils", which defines the rounding function
linenumbersfalse
/**
 * This function rounds given number to given number of decimal places
 * and returns the rounded value.
 * It rounds using the standard rounding rules used in math.
 *
 * @param number Number to round.
 * @param decimalPlaces To how many decimal places should the number be rounded to.
 * @return The rounded value.
 */
BigDecimal round(BigDecimal number, int decimalPlaces) {
    if (number == null) {
        return null
    }
    return number.setScale(decimalPlaces, RoundingMode.HALF_UP)
}

Library elements should follow the naming conventions, specified in the Best Practices section of the knowledge base.

  • The library logic name should have a suffix "Lib", (e.g. SharedLib)

  • The library element names should have a suffix "Utils" (e.g. DatamartUtils)

A library must be deployed to the partition before it can be utilized by other calculation logics. You can verify the presence of the library logic in the pricefx web application, ConfigurationAdministration  Logics  Calculation Logic  Groovy library.

To call the library function, type “libs.” → followed by name of the logic “SharedLib.” → followed the name of the element in the logic “RoundingUtils” → followed by the name of the function “round”.

Code Block
languagegroovy
themeMidnight
titleCall to the library function, from an element of Pricelist calculation logic
linenumbersfalse
def rounder = libs.PriceLib.RoundingUtils
def roundedNumber = rounder.round(listPrice, 2)
Info
The binding variable libs is available within all logic types.

...