Category: PeopleCode

Arrays Question

This post is to address a question posed in one of the comments on another post.  The answer is a little too in-depth for a comment reply.  Also, I had been wanting to blog more about arrays anyway.

Here is the question:

Hi i have seen your comment on Arrays.
i need your guidence on creating array and using it in Peoplecode App package.
I have some logic written in SQR. Same thing i want create in App Package which can be used.
Let me brief you my requirement.
i have several parameters to be stored in an array which i will get from different validations and SQL tables for employees.which i will store it in a array. After finishing validations for all the employees i have pass those employee id and details some other system so i want to replicate in App package using Array can you guide me how i can design it in App package.

I also decided to use my step-by-step that I just completed.  So, this answer will build on that post.

Listening to the requirements, I don’t think that we need to extend the array object like the post that this comment was on.  At most, I think we might want to create an employee object that will store all of the fields (or parameters as the requirements call them) needed.

So, I am creating a new Application Package called BLG_BLOGGING.  Then, I am inserting a new Application Class called “EmployeeObject”:

Then, I created three properties in that class to hold three parameters relating to an employee.  I did not specify the “get” or “set” keywords so I don’t have to create getter and setter methods.  This is the easiest way to add properties to a class because you don’t have to write any code for the properties.  Here is the code:

class EmployeeObject
   property string EmployeeID;
   property string FirstName;
   property string LastName;
end-class;

Then, in the Application Engine program, you have to import your new class.  This statement does nothing more than tell the program you are going to use this class later on.

import BLG_BLOGGING:EmployeeObject;

Here are the different variables that we will need.

Local File &log;
Local BLG_BLOGGING:EmployeeObject &emp;
Local array of BLG_BLOGGING:EmployeeObject &ary;
Local number &x;

This code creates an empty array.  We need to pass it a copy of the employee object just so it knows what data type will be stored in the array.  It will not actually store anything in the array at this point.

&emp = create BLG_BLOGGING:EmployeeObject();
&ary = CreateArrayRept(&emp, 0);

Here is where we load the first employee into the array.  We set all of the properties, and then, we use the push() method to insert it into the array.

&emp.EmployeeID = "001";
&emp.FirstName = "Bob";
&emp.LastName = "Tomato";
&ary.Push(&emp);
&log.WriteLine("Added Bob Tomato -- length = " | &ary.Len);

Then, we repeat that with another employee.

&emp = create BLG_BLOGGING:EmployeeObject();
&emp.EmployeeID = "002";
&emp.FirstName = "Larry";
&emp.LastName = "Cucumber";
&ary.Push(&emp);
&log.WriteLine("Added Larry Cucumber -- length = " | &ary.Len);

Finally, we add a third employee just to give us some data.

&emp = create BLG_BLOGGING:EmployeeObject();
&emp.EmployeeID = "003";
&emp.FirstName = "Lunt";
&emp.LastName = "Squash";
&ary.Push(&emp);
&log.WriteLine("Added Lunt Squash -- length = " | &ary.Len);

Then, we need to loop through the array to show what it contains.  The Get() method accesses one of the elements in the array.  It does not remove the element from the array.

&log.WriteLine("");
&log.WriteLine("Employees: ");
For &x = 1 To &ary.Len
     &emp = &ary.Get(&x);
     &log.WriteLine("  " | &emp.EmployeeID | ") " | &emp.LastName | ", " | &emp.FirstName);
End-For;

Final Solution

Here is the Application Package, Employee Object:

class EmployeeObject
   property string EmployeeID;
   property string FirstName;
   property string LastName;
end-class;

Here is the App Engine Program:

import BLG_BLOGGING:EmployeeObject;

Local File &log;
Local BLG_BLOGGING:EmployeeObject &emp;
Local array of BLG_BLOGGING:EmployeeObject &ary;
Local number &x;

&log = GetFile("c:\temp\log.txt", "W", "A", %FilePath_Absolute);

&emp = create BLG_BLOGGING:EmployeeObject();
&ary = CreateArrayRept(&emp, 0);

&emp.EmployeeID = "001";
&emp.FirstName = "Bob";
&emp.LastName = "Tomato";
&ary.Push(&emp);
&log.WriteLine("Added Bob Tomato -- length = " | &ary.Len);

&emp = create BLG_BLOGGING:EmployeeObject();
&emp.EmployeeID = "002";
&emp.FirstName = "Larry";
&emp.LastName = "Cucumber";
&ary.Push(&emp);
&log.WriteLine("Added Larry Cucumber -- length = " | &ary.Len);

&emp = create BLG_BLOGGING:EmployeeObject();
&emp.EmployeeID = "003";
&emp.FirstName = "Lunt";
&emp.LastName = "Squash";
&ary.Push(&emp);
&log.WriteLine("Added Lunt Squash -- length = " | &ary.Len);

&log.WriteLine("");
&log.WriteLine("Employees: ");
For &x = 1 To &ary.Len
     &emp = &ary.Get(&x);
     &log.WriteLine("  " | &emp.EmployeeID | ") " | &emp.LastName | ", " | &emp.FirstName);
End-For;

&log.Close();

Here is the final output:

Added Bob Tomato -- length = 1
Added Larry Cucumber -- length = 2
Added Lunt Squash -- length = 3

Employees:
  001) Tomato, Bob
  002) Cucumber, Larry
  003) Squash, Lunt

Step-by-Step: App Engine for Testing PeopleCode

This is a how-to post that I intend to refer back to from time to time.  The goal is to create a simple Application Engine program into which we can drop some PeopleCode and see how it works.  Assuming we don’t need any of the online pieces, this is much easier than going through all of the steps to create a page and register it so we can see it online.

Step 1: Create a new Application Engine Program

In Application Designer, click Ctrl + N or use the File > New menu option.  This will open the “New” dialog, and you can choose Application Engine program from the list.

You new program should look like this:

Step 2: Disable Restart

This step is very important.  If you don’t disable the restart and your program crashes, you will have to go through a few extra steps before you can rerun it.

First, click on the properties button while your program is in focus (you can also use the File > Definition Properties menu):

This should bring up the Properties dialog.  Then, go to the Advanced tab.  Check the “Disable Restart” option.

Step 3: Add an Action

First, click on the “Step 1” step to select it.  I usually click anywhere in the gray, and this should turn it black.

Next, click on the Add Action button, or you can use the Insert > Action menu.

Finally, change the type from SQL to PeopleCode.

Step 4: Save the program

At this point, you need to save before you can add PeopleCode.  You can use Ctrl + S, click on the Save icon on the toolbar, or you can use the File > Save menu.

Step 5: Enter the PeopleCode

First, open the PeopleCode program by double clicking anywhere on the gray of the PeopleCode action.  Or, you can right click on it and choose the “View PeopleCode” option.

Next, you will probably want to open a file to show output from your PeopleCode testing.  You can use this PeopleCode:

Local File &log;
&log = GetFile("c:\temp\log.txt", "W", "A", %Filepath_Absolute);

Then, you can print to that file with the writeline() method.  For now, we will just print Hello, World.

&log.WriteLine("Hello, World!");

Finally, you will probably want to close your file:

&log.Close();

Here is what it all looks like:

Make sure to save once you make these changes.

Step 6: Run the Program

Again, after you have saved, go back to the main program window where you can see the Main section, Step 1, and your new PeopleCode action.  Then, click the run icon.

In the dialog, Check the Output log to file and uncheck Run Minimized.  The output log to file allows you to see what happened.  Otherwise, the window will close before you see what happened.  The run minimized isn’t a big deal, but if the program doesn’t run minimized you see it pop up and go away better.  When the program goes away, you know it is done running.

Finally, when it is done, check the output.  If you used the paths that I did your output should be in the c:\temp directory.  You should have two files.  The first, is the main output from the program.  Check this to make sure the program ran to success:

The second is the log that your PeopleCode created.  For now, it should just say, “Hello, World”.

JSON

Here is another one of those technologies that I would love to play with but have to wait for a client that actually needs it: JSON.  I found Jim’s post on JSON very interesting and would love to have the need to come back to it.

One other solution that might be worth considering is this library for Java.  You would have to compile it and place it on the class path, but you could access it from PeopleCode.  Or, I wonder if it would be worth implementing the library in a PeopleCode Application Package?

And in conjunction, you can’t mention JSON, without mentioning jQuery.

JDBC From PeopleCode — Disadvantage/Advantage

Jim Marion’s post on JDBC made me think a little more.  (By the way, thanks, Jim, for linking me.)  The one disadvantage about accessing the PeopleSoft database via JDBC is that you have to supply the password to make the connection.

I have thought about trying to read the password from the application server or batch server configuration file, but if I remember right it only has the connect id and the connect password.  With the connect password, I might be able to use it to read the Access id and password from the database, but I have would have to be able to unencrypt it.

One advantage that SQL Server might have is that you can use Window’s security.  Assuming that the account running the application server or batch server has access to the database, you could just use the integrated security instead of an actual user name or password.

The advantage that JDBC has is that you don’t have to know the number of fields/columns that you want until run time.  With both the SQL object and SQLExec, you have to have a variable for each field you return or a return that contains all of those fields.

For example, I am trying to loop through a group of tables in a linked server and copy all of their fields to a table in the current database.  I have a problem using INSERT … SELECT, and I have to read the values in and then write them out.  I can’t figure another way to do it other than use JDBC.

Please correct me if I am wrong on any of this.  Maybe these thoughts will give someone else an idea that I overlooked.

Step By Step Virtual PS Install: Database Creation

This is a continuation of my virtual PeopleSoft installation.  In this step, we will create the database in an already installed Oracle Database system.  So, this assumes that the Oracle Database software is already installed.  Loading the PeopleTools data will be covered in the next step.  To see the complete list of steps, click here.

Read More

Deleting Files from PeopleCode

We have an interface file that we want to delete for security reasons once we have processed the file.  But, deleting it from the Application  Engine was not as straight-forward as I would have thought.

This does not work:

RemoveDirectory("c:\temp\mytextfile.txt", %FilePath_Absolute);

So, Java to the rescue — this does:

Local JavaObject &javaFile;
&javaFile = CreateJavaObject("java.io.File", "c:\temp\mytextfile.txt");
&javaFile.delete();

Update (8/20/2014):

Back when I wrote this post, I missed seeing the .Delete() method of the File API object.  That would be the better way to make this work.  Here’s an example:

Local File &file;
&file = GetFile("c:\temp\mytextfile.txt", "W", "A", %FilePath_Absolute);
&file.Delete();

(Thanks Tim and Andrew for pointing this out.)

Resources

Determine Last Update Date of a File

We have had trouble with our interface picking up the wrong file.  The client thinks they have put the file in the right place, but the server is looking at a different file for some reason.  Placing the date/time in the standard output helps identify these situations.  Here is how you can do that:

Local JavaObject &javaFile;
Local JavaObject &javaFormat;

&javaFile = CreateJavaObject(“java.io.File”, “c:\temp\myFile.txt”);
&javaFormat = CreateJavaObject(“java.text.SimpleDateFormat”, “yyyy/MM/dd hh:mm:ss aa”);
MessageBox(0, ” “, 0, 0, File Timestamp: %1”, &javaFormat.format(&javaFile.lastModified()));

Writing to Access Databases

We are thinking about connecting to an Access database for one of our interfaces.  Here is some PeopleCode that I threw together to make sure that it was possible.

I tested the code in a VMWare instance with HCM8.9/Tools 8.46, Windows Server 2003.  The instance did not have Microsoft Access installed, and I changed the path (c:\temp\AccessTest.mdb below) to a network (UNC) path where the computer had Access installed.

Local JavaObject &class;
Local JavaObject &connection;
Local JavaObject &driverManager;
Local JavaObject &statement;

&class = GetJavaClass("java.lang.Class");
&class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

&driverManager = GetJavaClass("java.sql.DriverManager");
&connection = &driverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\temp\AccessTest.mdb;DriverID=22;READONLY=false");
&statement = &connection.createStatement();
&statement.executeUpdate("insert into tblTest(TestMessage) values ('testing')");

Hope it helps.

Resources