Showing posts with label App Class. Show all posts
Showing posts with label App Class. Show all posts

How to use StringBuilder class in PeopleCode

Unlike Java, C#.net and other object oriented languages (OOP) PeopleCode API does not deliver built in application class or a StringBuilder class for concatenating many strings together in a loop.
However, recently I found out that PeopleSoft has delivered few utility application classes in their diagnostic plug-in application packages.
The main class we are interesting here is StringBuffer Class which provides basic functionality to work with a large amount of string concatenation operations within PeopleCode.
The StringBuffer class is not as comprehensive as StringBuilder class in Java or C#.net but it is good enough to do the string operations you want.

Here is the class definition (note: the image is an indication only)

class StringBuffer
   method StringBuffer(&InitialValue As string, &MaxSize As integer);
   method Append(&New As string);
   method Reset();
   property string Value get set;
   property integer Length readonly;
private
   instance array of string &Pieces;
   instance integer &MaxLength;
end-class;

method StringBuffer
   /+ &InitialValue as String, +/
   /+ &MaxSize as Integer +/
   &Pieces = CreateArray(&InitialValue);
   &MaxLength = &MaxSize;
   &Length = 0;
end-method;

method Reset
   &Pieces = CreateArrayRept("", 0);
   &Length = 0;
end-method;

method Append
   /+ &New as String +/
   Local integer &TempLength = &Length + Len(&New);
   If &Length > &MaxLength Then
      throw CreateException(210, 44, "Message Not Found", String(&MaxLength));
   End-If;
   &Length = &TempLength;
   &Pieces.Push(&New);
end-method;

get Value
   /+ Returns String +/
   Local string &Temp = &Pieces.Join("", "", "", &Length);
   /* collapse array now */
   &Pieces = CreateArrayRept("", 0);
   &Pieces.Push(&Temp); /* start out with this combo string */
   Return &Temp;
end-get;

set Value
   /+ &NewValue as String +/
   /* Ditch our current value */
   %This.Reset();
   &Pieces.Push(&NewValue);
end-set;

The StringBuffer class is a simple (but useful) class which has

  • two private instances - a string array (&Pieces) and an integer variable (&MaxLength);
  • two properties - a read-write string property (Value) and read-only integer property (Length)
  • 2 methods plus constructor method.

The operation inside the class is self explanatory. The Append method can be used to add strings to the end of a string represented by the current StringBuffer. The following example initializes a StringBuffer to "Hello this is my first string!" and then appends some text to the end of the object. The &MaxSize in the constructor must specify a non-negative integer value
Before you can use this class in your PeopleCode program, you must import it into your program, using an import statement. The application package PT_DIAGNOSTICS contains the StringBuffer class. The import statement should be as follows:

import PT_DIAGNOSTICS:*;
Lets see an example;

try
   Local PT_DIAGNOSTICS:StringBuffer &MyStringBuffer = create PT_DIAGNOSTICS:StringBuffer("Hello this is my first string!", 1);
   &MyStringBuffer.Append(" This is really good.");
catch Exception &c1
   WinMessage(&c1.ToString(), 0);
end-try;

/* to get the value of string use &MyStringBuffer.Value */
WinMessage(&MyStringBuffer.Value, 0);

Even though StringBuffer class provides basic functionality to manipulate strings, it comes in handy if you are dealing with files in PeopleCode for example;
While &file.ReadLine(&line)
   &S.Append(&line);
   &S.Append(&separator);

Useful Utility App Class (PeopleCode) Methods

Useful Utility App Class (PeopleCode) Methods
Sometimes we need to write our own methods or functions for data type conversions or separate certain repeated logic so that we can use them over and over again. This re-usability of code can be achieved via PeopleCode functions or using Application class methods. I personally like to use Application Classes to create these kind of common utility methods so that I can get the benefit of object-oriented programming. Below is an example of a Utility class which has few methods for data Type conversions. This class defines following methods;

1. String to Number conversion
2. String "YYYY-MM-DD" to date conversions
3. String "DD-MM-YYYY" to date conversion
4. String "YYYY-MM-DD" to Date conversion
5. DateTime to string conversion


/*Declare Class*/
class MyUtil
/* Type Conversions */
   method stringToNumber(&inString As string) Returns number;
   method stringToDate(&inString As string, &strFormat As string) Returns date;
   method stringToDateTime(&inString As string) Returns datetime;
   method datetimeToString(&inDatetime As datetime) Returns string;
end-class
 
/* String to Number conversion */
method stringToNumber
   /+ &inString as String +/
   /+ Returns Number +/
   
   Return Value(&inString)
end-method;

/* String "YYYY-MM-DD" or "DD-MM-YYYY" to Date conversion */
method stringToDate
   /+ &inString as String +/
   /+ &strFormat as String +/
   /+ Returns Date +/
   
   Local number &Year, &Month, &Day;
   Local date &date;
   
   If All(&inString) Then
   
    If &strFormat = "YYYY-MM-DD" Then
    
    &Year = Value(Substring(&inString, 1, 4));
    &Month = Value(Substring(&inString, 6, 2));
    &Day = Value(Substring(&inString, 9, 2));
    
    Return (Date3(&Year, &Month, &Day));
    End-If;
   
      If &strFormat = "DD-MM-YYYY" Then
    
    &Day = Value(Substring(&inString, 1, 2));
    &Month = Value(Substring(&inString, 4, 2));
    &Year = Value(Substring(&inString, 7, 4));
    
    Return (Date3(&Year, &Month, &Day));
    End-If;
 Else
      /* Return Null Date */
      Return &date;
   End-If;   
end-method;

/* String "YYYY-MM-DD" to DateTime conversion */
method stringToDateTime
   /+ &inString as String +/
   /+ Returns DateTime +/
   
   Local datetime &dateT;
   
   If All(&inString) Then
      Return (DateTimeValue(&inString));
   Else
      /* Return Null Date */
      Return &dateT;
   End-If;
   
end-method;

method datetimeToString
   /+ &inDatetime as DateTime +/
   /+ Returns String +/
   
   Local string &strDatetime;
   Local datetime &nullDatetime;
   
   If &inDatetime <> &nullDatetime Then
      &strDatetime = DateTimeToLocalizedString(&inDatetime, "MM/dd/yyyy HH:mm:ss");
      &strDatetime = &strDatetime | ".000000";
      Return &strDatetime;
   Else
      /* Return Null Date */
      Return "";
   End-If;
   
end-method;