Validations in groovy for business object

We can write simple or complex validations at business objects level in groovy

  • In some cases we want to check if age > 18.
  • In some case we want to check for the object row the status is approved then no changes should happen.
  • In some cases we want to allow only the same user to edit the record.
  • In some case we want to validate using some external API ( to check availability of quantity or price ).

For all these we can use validations using groovy.

General idea is to create a object function using groovy to validate and use this object function in before insert trigger or before update trigger.

Let us say we have a Business Object Employee with fields (id, first name, last name, age) and we want to have validation that age should be at least 18 years.

Object function code.

if ( age < 18 ) {
  throw new oracle.jbo.ValidationException('Age should not be less than 18');
}

Let us say we have a Business Object ApprovalBO with fields (id, approval name, status {draft, created, pending approval, rejected, approved} ) and we want to restrict user to not edit any data once the record status is set to approved or rejected.

def origStatus = getOriginalAttributeValue('status')
if ( origStatus == 'Approved' || origStatus == 'Rejected' ) {
  throw new oracle.jbo.ValidationException('Changes not allowed for this record as status is ' + origStatus);
}

Let us say we have a Business Object Transaction with fields (id, transaction name, created by, updated by) and we want to have validation that only created by user can update the value of record.

def secCtx = adf.context.getSecurityContext()
def user = secCtx.getUserName()

if ( createdBy != user ) {
  throw new oracle.jbo.ValidationException('Only created user can update the record');
}

Let us say we have a Business Object Inventory with fields (id, inventory name, quantity, price) and we want to have validation against a external api. We can configure the external api using service connections. We can make rest call in groovy and validate against it.

def priceSvc = newService('priceService');
def responseObj = priceSvc.getQuantityAndPrice();
def quantityApi = responseObj.quantity;
def priceApi = responseObj.price;

if ( quantity > quantityApi ) {
  throw new oracle.jbo.ValidationException('Quantity entered is greater that avalible quantity from api (' + quantityApi + ')');
}

if ( price > priceApi ) {
  throw new oracle.jbo.ValidationException('Price entered is greater that avalible price from api (' + priceApi  + ')');
}

Reference – https://docs.oracle.com/en/cloud/paas/integration-cloud/visual-developer/trigger-rules-business-objects-1.html

https://docs.oracle.com/en/cloud/paas/app-builder-cloud/visual-builder-groovy/groovy-tips-and-techniques.html#groovy-tips-and-techniques

How to rename file in MFT

In MFT, when file is moved from source to transfer, we can rename the file.

This will be useful in scenarios when the file name is different than what is expected

Sometime during decryption the extension is lost, we can add a different extension

Sometimes we have to append a source to handle files from different sources differently

We can use RenameRegExp to rename the file

This uses same method as java replace. The input is Regular expression

Common use cases and examples

to completely rename the file –

String sourceRegExp = "([A-Za-z0-9_]+)([A-Za-z0-9.]*)";
String targetRegExp = "input_file.csv";
Input Output
ABC_SOME_BIG_FILE_NAME.csvinput_file.csv
ABC_SOME_OTHER_FILE_NAME.csvinput_file.csv
SOME_FILE_NAME_WITHOUT_EXTENSIONinput_file.csv

To completely change the extension

String sourceRegExp = "([A-Za-z0-9_]+)([A-Za-z0-9.]*)";
String targetRegExp = "$1.csv";
InputOutput
some_data_1.txt some_data_1.csv
some_data_2.dat some_data_2.csv
some_data_3. some_data_3.csv
some_data_4.txt.pgp some_data_4.csv
some_data_5.txt.gpg some_data_5.csv
some_data_6.csv some_data_6.csv
some_data_7 some_data_7.csv

To add a prefix to the file name.

String sourceRegExp = "([A-Za-z0-9_.]+)";
String targetRegExp = "source_$1";
InputOutput
some_data_1.txt source_some_data_1.txt
some_data_2.dat source_some_data_2.dat
some_data_3. source_some_data_3.
some_data_4.txt.pgp source_some_data_4.txt.pgp
some_data_5.txt.gpg source_some_data_5.txt.gpg
some_data_6.csv source_some_data_6.csv
some_data_7 source_some_data_7

Sample Java code to test regular expression if the input / output is as expected:

public class RenameString {
    public static void main(String[] args){

        TestCase[] testCases  = new TestCase[7];
      
        testCases[0] = new TestCase("some_data.txt", "some_data.csv");
        testCases[1] = new TestCase("some_data.dat", "some_data.csv");
        testCases[2] = new TestCase("some_data.", "some_data.csv");
        testCases[3] = new TestCase("some_data.txt.pgp", "some_data.csv");
        testCases[4] = new TestCase("some_data.txt.gpg", "some_data.csv");
        testCases[5] = new TestCase("some_data.csv", "some_data.csv");
        testCases[6] = new TestCase("some_data", "some_data.csv");
    
        for ( TestCase t : testCases) {
            String output = renameFileNameExtension(t.inputStr);

            System.out.print(t.inputStr + " --- " + t.expectedOutputStr + " --- " + output + " --- " );

            if ( output.equals(t.expectedOutputStr) ) {
                System.out.println("success");
            }
            else {
                System.out.println("failed");
            }
        }
    }

    public static String renameFileNameExtension(String inputStr) {
        
        String sourceRegExp = "([A-Za-z0-9_]+)([A-Za-z0-9.]*)";
        String targetRegExp = "$1.csv";

        String output = inputStr.replaceAll(sourceRegExp, targetRegExp);

        return output;
    }

}

class TestCase {
    String inputStr;
    String expectedOutputStr;

    TestCase(String inputStr, String expectedOutputStr){
        this.inputStr = inputStr;
        this.expectedOutputStr = expectedOutputStr;
    }
}

You can test Regular expression using this website https://regex101.com/ and selecting Java language.