Está en la página 1de 2

Read A Flat File Using File Layout And Insert Into The Specific Component Using CI ( Bulk Insert

) Approach: Get the flat file and create a File Layout. Create the CI for the component.

Create an AE and drag the File Layout and CI for dynamic code generation in the peoplecode action

/*******************************************************************************/
Local File &FILE1; Local Record &REC1; Local SQL &SQL1; Local Rowset &RS1, &RS2; Local integer &M;

/* *****************************************************************/ Function StartSession

try
&oSession = %Session; &oSession.PSMessagesMode = 1; catch Exception &ex &LOGFILE.WriteLine(&ex.ToString()); end-try; End-Function; /*******************************************************************/ Function ImportData(&RS1 As Rowset) try &oEmPDpndtBlInsCi = &oSession.GetCompIntfc(CompIntfc.EM_P_DPNDT_BL_INS_CI);
If &oEmPDpndtBlInsCi = Null Then

errorHandler();

throw CreateException(0, 0, "GetCompIntfc failed"); End-If;

&oEmPDpndtBlInsCi.GetHistoryItems = True;

EditHistoryItems = False;

InteractiveMode = False;

&recPerInfo = CREATERECORD(Record.EM_PER_INFO); &recPerInfo1 = CreateRecord(Record.EM_PER_INFO1); &recBnkInf = CreateRecord(Record.EM_BANK_INFO); &recdpdntInf = CreateRecord(Record.EM_DPNDNT_INFO); &LOGFILE.WRITEROWSET(&RS); &RS1(1).GetRecord(1).CopyFieldsTo(&recPerInfo); &oEmPDpndtBlInsCi.EMPLID = &recPerInfo.EMPLID.Value;

rem ***** Execute Get *****;


rem ***** No rows exist for the specified keys*****;

If Not &oEmPDpndTBLInsCi.Get() Then


ERRORHANDLER(); throw CreateException(0, 0, "Get failed"); End-If;

&oEmPerInfo1Collection = &oEmPDpndtBlInsCi.EM_PER_INFO1; &RS2 = &RS1(1).GetRowset(Scroll.EM_PER_INFO1); For &i = 1 To &RS2.ActiveRowCount If &i > 1 Then &oEmPerInfo1 = &oEmPerInfo1Collection.EM_PER_INFO1.InsertItem(1); Else &oEmPerInfo1 = &oEmPerInfo1Collection.Item(1); End-If; &RS2.GetRow(&i).GetRecord(Record.EM_PER_INFO1).CopyFieldsTo(&recPerInfo1); &oEmPerInfo1.EFFDT = &recPerInfo1.EFFDT.Value; SEX MAR_STATUS &oEmBankInfoCollection = &oEmPDpndtBlInsCi.EM_BANK_INFO; &RS3 = &RS1(1).GetRowset(Scroll.EM_BANK_INFO); End-For;

For &j = 1 To &RS3.ActiveRowCount If &j > 1 Then &oEmBankInfo = &oEmBankInfoCollection.EM_BANK_INFO.InsertItem(1);


Else &oEmBankInfo = &oEmBankInfoCollection.Item(&j); End-If; End-For;

&RS3.GetRow(&j).GetRecord(Record.EM_BANK_INFO).CopyFieldsTo(&recBnkInf); &oEmBankInfo.BANK_NM = &recBnkInf.BANK_NM.Value; BANK_ACCOUNT_NUM

rem ***** End: Get/Set Component Interface Properties *****; rem ***** Execute Save *****; If Not &oEmPDpndtBlInsCi.Save() Then; rem errorHandler();
throw CreateException(0, 0, "Save failed"); End-If; &fileLog.WriteLine(&ex.ToString()); end-try; rem *****************************************************************; *;rem*****************************************************; catch Exception &ex End-Function;

rem * PeopleCode to Import Data

&FILE1

GetFile("C:\Desktop\dpndBlkIns.csv", "r", "a", %FilePath_Absolute);

&LOGFILE = GetFile("C:\ Desktop\dpndBlkIns.csv.err", "W", %FilePath_Absolute);

StartSession();
&FILE1.SetFileLayout(FileLayout.EM_P_DPNDT_IB); &LOGFILE.SetFileLayout(FileLayout.EM_P_DPNDT_IB); &RS1 = &FILE1.CreateRowset(); &RS = CreateRowset(Record.EM_PER_INFO, CreateRowset(Record.EM_PER_INFO1), CreateRowset(Record.EM_BANK_INFO), CreateRowset(Record.EM_DPNDNT_INFO)); &RS1 = &FILE1.ReadRowset(); While &RS1 <> Null; ImportData(&RS1); &RS1 = &FILE1.ReadRowset(); End-While; &FILE1.Close(); &LOGFILE.Close();

The following example creates an Array Of Array Of String, then reads in two files, one into each "column" of the array. The Names file contains names; the Numbers file contains employee numbers. The ReadLine method reads each successive line in a file, until it reaches the end of the file. Notice that the first file is opened using GetFile. The second file is not opened using GetFile, but rather with Open. After the data is read into the array, you can do processing on the data. The end of the prog writes the changes back to the files, using the WriteLine method, which includes a sym end of line char at the end of every line. Local array of array of string &BOTH; Local File &MYFILE; Local string &HOLDER; /* Create empty &BOTH array */ &BOTH = CreateArrayRept(CreateArrayRept("", 0), 0); /* Read first file into first column */ &MYFILE = GetFile("names.txt", "R"); While &MYFILE.ReadLine(&HOLDER); &BOTH.Push(&HOLDER); End-While; /* read second file into second column */ &MYFILE.Open("numbers.txt", "R"); &LINENO = 1; While &MYFILE.ReadLine(&HOLDER); If &LINENO > &BOTH.Len Then /* more number lines than names, use a null name */ &BOTH.Push(CreateArray("", &HOLDER)); Else &BOTH[&LINENO].Push(&HOLDER); End-If; &LINENO = &LINENO + 1; End-While; /* if more names than numbers, add null numbers */ For &LINENO = &LINENO to &BOTH.Len &BOTH[&LINENO].Push(""); End-For; &MYFILE.Close(); /* do processing with array */ /* write data back to files */ &MYFILE1 = GetFile("names.txt", "A"); &MYFILE2 = GetFile("numbers.txt", "A"); /* loop through array and write to files */ For &I = 1 To &BOTH.Len &STRING1 = &BOTH[&I][1]; &MYFILE1.writeline(&STRING1); &STRING2 = &BOTH[&I][2]; &MYFILE2.writeline(&STRING2); End-For; &MYFILE1.Close(); &MYFILE2.Close();

También podría gustarte