Saturday, February 7, 2009

Import Data from Files Using Application Engine

You can use application engine to import files into tables in your PeopleSoft Database. Prior to version 8, the only way to programmatically do this was by using SQR. However, you now have the ability to create a File Layout Definition object and using Peoplecode, you can load a file directly into your database.

Application Engine is extremely powerful when utilized correctly. For example, you can utilize functionality such as peoplecode, SQL, Do While, Do Until, and Do Select. Therefore, you have the ability to manipulate the data just as you were capable of doing using SQR.

I'm going to walk through loading data from a flat file into a table using Application Engine. This example is a very simple one and is easy to follow. So lets get started!

First and foremost, you will need to create a File Layout Definition.

For this example we are using a table that we have defined in Application Designer named "N_SDN_TBL". This table is constructed as follows:

---------------------- --------------
N_SDN_ID decimal 9,10
N_SDN_NAME char 50
N_SDN_TYPE char 12
N_SDN_SNCTION_PGM char 20
N_SDN_TITLE char 180
N_SDN_VSSL_CALLSGN char 8
N_SDN_VESSEL_TYPE char 25
N_SDN_VSSLTONNAGE char 14
N_SDN_GRT_REG_TON char 8
N_SDN_VESSEL_FLAG char 40
N_SDN_VSSL_OWNER char 150
N_SDN_REMARKS char 254

When you define a File Layout, you can define it with a format of FIXED, CSV, or XML. In this Example we are using Fixed. From Application Designer, simply create a new File Layout object. Right click on the File Layout and Insert a Record. For this example we choose N_SDN_TBL. Once you choose the record App Designer will add the record and its fields to the layout.

The Next step is to adjust the start and ending positions for each field. Simply double click on a field and enter the start and ending position for this field the way it is in your flat file. Once you go through and complete this process for each field you can save your file layout and start building your application engine program.

All we need for this program are two simple steps.

The first step will be a simple SQL step to truncate the table N_SDN_TBL.Second, we add another step for PeopleCode. The PeopleCode for loading this data into the table is written below.


/**********************************************************
FUNCTION TO IMPORT DATA
**********************************************************/
Function ImportSegment(&RS2 As Rowset, &RSParent As Rowset)
Local Rowset &RS1, &RSP;
Local string &RecordName;
Local Record &REC2, &RECP;
Local SQL &SQL1;
Local integer &I, &L;
&SQL1 = CreateSQL("%Insert(:1)");
&RecordName = "RECORD." | &RS2.DBRecordName;
&REC2 = CreateRecord(@(&RecordName));
&RECP = &RSParent(1).GetRecord(@(&RecordName));
For &I = 1 To &RS2.ActiveRowCount
&RS2(&I).GetRecord(1).CopyFieldsTo(&REC2);
If (EditRecord(&REC2)) Then
&SQL1.Execute(&REC2);
&RS2(&I).GetRecord(1).CopyFieldsTo(&RECP);
For &L = 1 To &RS2.GetRow(&I).ChildCount
&RS1 = &RS2.GetRow(&I).GetRowset(&L);
If (&RS1 <> Null) Then
&RSP = &RSParent.GetRow(1).GetRowset(&L);
ImportSegment(&RS1, &RSP);
End-If;
End-For;
If &RSParent.ActiveRowCount > 0 Then
&RSParent.DeleteRow(1);
End-If;
Else
&LOGFILE.WriteRowset(&RS);
&LOGFILE.WriteLine("****Correct error in this record and delete all error messages");
&LOGFILE.WriteRecord(&REC2);
For &L = 1 To &RS2.GetRow(&I).ChildCount
&RS1 = &RS2.GetRow(&I).GetRowset(&L);
If (&RS1 <> Null) Then
&LOGFILE.WriteRowset(&RS1);
End-If;
End-For;
End-If;
End-For;
End-Function;



Rem *****************************************************************;
rem * Define our Objects *;
rem *****************************************************************;
Local File &FILE1
Local Record &REC1
Local SQL &SQL1
Local Rowset &RS1
Local integer &M


/******************************************************************/
/* LOAD A FILE CALLED SDN.FF */
/******************************************************************/

/* CREATE A HANDLE TO THE FILE AND THE LOGFILE SETTING THE ATTRIBUTES */

&FILE1 = GetFile("D:\FS800\N_INTERFACES\SDN\SDN.FF", "r", "a", %FilePath_Absolute);
&LOGFILE = GetFile("D:\FS800\N_INTERFACES\SDN\SDN.FF.err", "W", %FilePath_Absolute);

/* LOAD OUR FILELAYOUT TO THE FILE HANDLE */

&FILE1.SetFileLayout(FileLayout.N_SDN_TBL);
&LOGFILE.SetFileLayout(FileLayout.N_SDN_TBL);

/*************************************************************
CREATE A ROWSET FOR THE FILE AND ONE FOR THE RECORD THAT WE
WILL BE LOADING THEN READ EACH ROW OF THE FILE AND INSERT
IT INTO OUR TABLE
**************************************************************/

&RS1 = &FILE1.CreateRowset();
&RS = CreateRowset(Record.N_SDN_TBL);
&SQL1 = CreateSQL("%Insert(:1)");
&RS1 = &FILE1.ReadRowset();

While &RS1 <> Null;
ImportSegment(&RS1, &RS);
&RS1 = &FILE1.ReadRowset();
End-While;

&FILE1.Close();
&LOGFILE.Close();

I hope that this example was helpful in providing you a simple, quick, and easy way
to load data from a fixed field length flat file into a table. There really is no need to continue to do these types of applications using SQR. You should start programming using Application engine for any type of process that does not require a fancy report. Remember, Application Engine programs are objects that you can upgrade. SQR's are not.

2 comments:

  1. How to do the same if we have multiple files and 1record to write the data?

    ReplyDelete
  2. Trik Jitu Menang Bermain Situs Judi Permainan Sabung Ayam Online Yang Jarang Diketahui Kebanyakan Orang Klik Di Sini

    Agen Sabung Ayam Online Terbaik Dan Juga Terpercaya http://www.bakarayam.co

    Informasi Terlengkap Mengenai Sabung Ayam

    http://ayambakar33033.blogspot.com/2018/08/makna-sabung-ayam-leghorn-dalam-dunia.html/

    ReplyDelete