Tuesday, May 21, 2013

Asynchronous Integration broker service operation handler PeopleCode

Following is a sample PeopleCode for the Asynchronous Integration broker service operation handler, which loads message data into target record. In this Sample PeopleCode “TEST_DATA_TBL” is the record name which is target record and same used in the IB Message. This is simple example of integration broker service handler which takes care of only zero level data.

 /* Receive Message */


import PS_PT:Integration:INotificationHandler;

class TEST_HANDLER implements PS_PT:Integration:INotificationHandler
method OnNotify(&_Msg As Message);
private
method CopyFieldsToFunc(&SourceRec As Record, &TargetRec As Record);
end-class;



method OnNotify

/+ &_Msg as Message +/

/+ Extends/implements PS_PT:Integration:INotificationHandler.OnNotify +/

Local integer &i;
Local Rowset &RsMsgHdr, &RsRecHdr;
Local Row &RwMsgHdr;
Local Record &RcMsgHdr;
Local Record &RcStgHdr;
Local Message &Msg;


&Msg = &_Msg;
&RsMsgHdr = &Msg.GetRowset();
&RsRecHdr = CreateRowset(Record.TEST_DATA_TBL);
&RsMsgHdr.CopyTo(&RsRecHdr);

For &i = 1 To &RsRecHdr.ActiveRowCount

&RwMsgHdr = &RsRecHdr.GetRow(&i);
&RcMsgHdr = &RwMsgHdr.GetRecord(Record.TEST_DATA_TBL);
&RcStgHdr = CreateRecord(Record.TEST_DATA_TBL);
%This.CopyFieldsToFunc(&RcMsgHdr, &RcStgHdr);

End-For;

end-method;


method CopyFieldsToFunc

/+ &SourceRec as Record, +/

/+ &TargetRec as Record +/

Local integer &i, &j;
Local string &Empl, &EmplFound;

For &i = 1 To &SourceRec.FieldCount

For &j = 1 To &TargetRec.FieldCount

If &SourceRec.GetField(&i).Name = &TargetRec.GetField(&j).Name Then

   &TargetRec.GetField(&j).Value = &SourceRec.GetField(&i).Value;

End-If;

End-For;

End-For;



&Empl = &TargetRec.GetField(Field.EMPLID).Value;

SQLExec("SELECT 'Y' FROM PS_TEST_DATA_TBL WHERE EMPLID = :1 ", &Empl, &EmplFound);


If All(&EmplFound) Then

&TargetRec.Update();

Else

&TargetRec.Insert();

End-If;

end-method;

Generate Excel file using PeopleCode/Application engine

To generate Excel file using PeopleCode make sure that any Microsoft excel template file exists in the excel folder. For example in the below PeopleCode I am using “template.xlt” Microsoft excel template file, if you are not using any formula in the Excel sheet then in the template file data type of all the cell should be TEXT to avoide eliminating leading zero from any number which should be treated as string. And one more thing while generating excel file make sure that code is being executed in the server where Microsoft excel is installed. For example if you are generating excel file using application engine then make sure process should be executed in PSNT process scheduler server where Microsoft excel has been already installed as in Unix server it will not work and process error out. Similarly if using normal PeopleCode then make sure your Application server is in PSNT and Microsoft excel is already installed.

----------------SAMPLE CODE----------------------

Local object &oWorkApp, &oWorkBook;
Local PostReport &RPTINFO;
Local number &RPTINSTANCE;


&PrcsInstace = TEST_TREEDT_AET.PROCESS_INSTANCE;
&SetID = TEST_TREEDT_AET.SETID;
&TreeName = TEST_TREEDT_AET.TREE_NAME;
&EffDt = String(TEST_TREEDT_AET.EFFDT);
&Field = TEST_TREEDT_AET.FIELDNAME;


SQLExec("SELECT SERVERNAMERQST FROM ps_pmn_prcslist WHERE prcsinstance = :1", &PrcsInstace, &Server);


&find = 0;
&find = Find("NT", &Server);

If &find <> 0 Then
&reportPath = GetEnv("PS_HOME") |"\excel\";
&reportOutPutPath = GetEnv("PS_SERVDIR") |"\log_output\AE_TEST_TREEDTL_" |&PrcsInstace |"\" |&SetID |"-" |&TreeName|"-" |&EffDt |"-" |&PrcsInstace |".xls";
&oWorkApp = CreateObject("COM", "Excel.Application");
&oWorkBook = ObjectGetProperty(&oWorkApp, "Workbooks");
&oWorkBook.Open(&reportPath |"template.xlt");
&oWorkSheet = &oWorkApp.Worksheets("Sheet1");
&oWorkApp.ActiveWorkBook.SaveAs(&reportOutPutPath);


&oWorkSheet.Cells(1, 1).Value = "Set ID";
&oWorkSheet.Cells(1, 1).Font.Bold = True;
&oWorkSheet.Cells(1, 2).Value = &SetID;
&oWorkSheet.Cells(1, 2).Font.Bold = True;


&oWorkSheet.Cells(2, 1).Value = "Tree Name";
&oWorkSheet.Cells(2, 1).Font.Bold = True;
&oWorkSheet.Cells(2, 2).Value = &TreeName;
&oWorkSheet.Cells(2, 2).Font.Bold = True;

&oWorkSheet.Cells(3, 1).Value = "Effective Date";
&oWorkSheet.Cells(3, 1).Font.Bold = True;
&oWorkSheet.Cells(3, 2).Value = &EffDt;
&oWorkSheet.Cells(3, 2).Font.Bold = True;



&SQL1 = CreateSQL("SELECT DISTINCT DESCR254||'>' , CHARTFIELD_VALUE ,DESCR,Length(DESCR254)-Length(replace(DESCR254,'>',null))+1 FROM %TABLE(TEST_TRDT_TMP) WHERE PROCESS_INSTANCE = :1 ORDER BY 1,2", &PrcsInstace);

SQLExec("SELECT DISTINCT B.LONGNAME FROM PSDBFLDLABL A, PSDBFLDLABLLANG B WHERE A.FIELDNAME = B.FIELDNAME AND A.LABEL_ID = B.LABEL_ID AND A.DEFAULT_LABEL = '1' AND A.FIELDNAME = :1", &Field, &FieldLabel);

SQLExec("SELECT Max(Length(DESCR254)-Length(replace(DESCR254,'>',null)))+1 FROM %TABLE(TEST_TRDT_TMP) WHERE PROCESS_INSTANCE = :1 ", &PrcsInstace, &MaxNodeLevel);

If None(&FieldLabel) Then
     &FieldLabel = "Tree leaf";
End-If;


For &j = 1 To &MaxNodeLevel
   &oWorkSheet.Cells(5, &j).Value = "Node Number " |String(&j);
   &oWorkSheet.Cells(5, &j).Font.Bold = True;
End-For;

&oWorkSheet.Cells(5, &MaxNodeLevel + 1).Value = &FieldLabel;
&oWorkSheet.Cells(5, &MaxNodeLevel + 1).Font.Bold = True;
&oWorkSheet.Cells(5, &MaxNodeLevel + 2).Value = "Description";
&oWorkSheet.Cells(5, &MaxNodeLevel + 2).Font.Bold = True;

/*Write detail information*/
&i = 6;
While &SQL1.Fetch(&Path, &ChartField, &Decr, &CurrNodeLevel)

For &j = 1 To &CurrNodeLevel
&PathLength = Len(&Path);
&find = Find(">", &Path);
&NodeLevel = Left(&Path, &find - 1);
&Path = Right(&Path, &PathLength - &find);
&oWorkSheet.Cells(&i, &j).Value = &NodeLevel;
End-For;

&oWorkSheet.Cells(&i, &MaxNodeLevel + 1).VALUE = &ChartField;
&oWorkSheet.Cells(&i, &MaxNodeLevel + 2).Value = &Decr;
&i = &i + 1;

End-While;

/* Save Excel file and quit */

&oWorkApp.ActiveWorkBook.Save();
&oWorkApp.ActiveWorkBook.Close();
&oWorkApp.Quit();

Else

MessageBox(0, "", 0, 0, "Process is not being executed in NT Server");

End-If;

----------------------------------------------------------------------------------------