Java syntax in Activity

Though Pega suggest us not to use java , certain times we are forced to use java for the complex solutions.
For a guy like me , who did not do java as curriculum always depends on google for the java code.
This post is going to help me to identify the syntax of certain java code which I have used

Click the button to copy the syntax and paste in the Java beautifier for alignment
https://codebeautify.org/javaviewer

Use case Add Log message
Syntax
  oLog.infoForced("Log message Here");
Use case Add try and catch block
Syntax
  try {
   oLog.infoForced("try block executed");
   } catch (Exception ex) {
    oLog.error("catch block executed", ex.toString());
} 
Use case To get current Date and Time
Syntax
  Date date1 = java.util.Calendar.getInstance().getTime();
      oLog.infoForced("Current Date Time"+ date1);
Use case To get current Date and Time
Syntax
  Date date1 = java.util.Calendar.getInstance().getTime();
      oLog.infoForced("Current Date Time"+ date1);
Use case Convert Bigdecimal(input) to date format
Syntax
  DateFormat df = new SimpleDateFormat("dd/MMM/YYYY");
      String date = df.format(input);
      oLog.infoForced("Converted Date format"+ date);
Use case To create a Clipboard page object from step page/ Page/ datapage
Syntax
ClipboardPage sp1 = myStepPage;
ClipboardPage sp2 = tools.getStepPage();
ClipboardPage cp  tools.findPage("PageName");
ClipboardPage dp  tools.findPage("D_PageName");
Use case To create a Clipboard property object from step page/ Page/ datapage
Syntax
ClipboardPage cp  tools.findPage("PageName");
String ABC = cp.getProperty("pyLabel").toString();
Use case To Loop over PageList / datapage - (D_PageName.pxResults(1).PNAME)
Syntax
String DatapageName = "D_PageName";
ClipboardPage dpage tools.findPage(DatapageName);
if (dpage != null) {
    ClipboardProperty Results = dpage.getIfPresent("pxResults");
    Iterator it = Results.iterator();
    while (it.hasNext()) {
        ClipboardProperty next = (ClipboardProperty) it.next();
        ClipboardPage nextPage = next.getPageValue();
        ClipbaordProperty propertyname = nextPage.getIfPresent("PNAME");
    	oLog.infoForced("Print Property"+ propertyname);
    }
}
Use case For Loop Logic
Syntax
int size = 10000;
    int size = sp.getProperty("PropertyName").size();
    for (int i=1 ; i <= size ; i++)
    {
    oLog.infoForced("For logic"+ i);
    }

Java to check the files in server

This post shows me how to check the files and folders in the server where pega is installed.

Since I don't have access to Pega production server directly , I have used Java here to display the list of files and folders in Pega logs.

This will help me to confirm if the file listener files are present in path or not.


try { oLog.infoForced("[FilesInDirectory] try block started "); java.io.File fileContents[] = new java.io.File(Path).listFiles(); if (fileContents != null) { for (int i = 0; i < fileContents.length; i++) { if (fileContents[i].isDirectory()) { oLog.infoForced("[FilesInDirectory] Folder: " + fileContents[i]); } else { oLog.infoForced("[FilesInDirectory] File: " + fileContents[i]); } } } oLog.infoForced("[FilesInDirectory] try block Ended "); } catch (Exception ex) { oLog.error("[FilesInDirectory] catch block expected", ex); }

try {
   oLog.infoForced("[FilesInDirectory] try block started ");
    java.io.File fileContents[] = new java.io.File(Path).listFiles();
    if (fileContents != null) {
    for (int i = 0; i < fileContents.length; i++) {
       if (fileContents[i].isDirectory()) {
    oLog.infoForced("[FilesInDirectory] Folder: " + fileContents[i]); 
} else {
                oLog.infoForced("[FilesInDirectory] File: " + fileContents[i]);
            }
        }
    }
    oLog.infoForced("[FilesInDirectory] try block Ended ");

} catch (Exception ex) {
    oLog.error("[FilesInDirectory] catch block expected", ex);
} 

Create a New Activity in Pega with parameter "Path" to pass to java step and Paste the above Java Code.

This activity contains only two steps .
1) First to set the Param.Path to Local.Path
2) Run the Java

Since I am running on my Local machine (windows) , I am passing the path as D:\InstallationApps\PRPCPersonalEdition\tomcat . If Its Linux ,we can just pass /opt

Now check the logs for the list of files and folders.