2014年7月2日 星期三

Lotus Domino 7.0利用Java呼叫BAPI的範例

這次我們讓Notes的支援工時報工單可以利用BAPI來取回工單的材料說明及實際核發日期來做為簽核的參考.

這篇比上次叫RFC的範例多了
1.取回Structure並輸入參數的例子(有Import及Table)
2.BAPI傳回的Table取值的範例

import lotus.domino.*;
import java.util.*;
import com.sap.mw.jco.*;
public class SAPApprove extends AgentBase {
private Session session;
private AgentContext agentContext;
private Database db;
private String sapIP,sapClient,sapAccount,sapPasswd,sapLang,sapSysNo;
private JCO.Function function;
private JCO.Client client ;
private Log log;
private boolean isError=false;
private String errMsg="";
private String resultCode;
private String resultMsg ;
private JCO.Structure returnStructure;
private Document doc;
private JCO.ParameterList params;

public void init(Session session,AgentContext agentContext) throws NotesException{
try {
this.session=session;
this.agentContext=agentContext;
this.db=agentContext.getCurrentDatabase();
Document profile=db.getProfileDocument("SystemProfile",null);
sapIP=profile.getItemValueString("SAPIP");
sapClient=profile.getItemValueString("SAPClient");
sapAccount=profile.getItemValueString("SAPAccount");
sapPasswd=profile.getItemValueString("SAPPasswd");
sapLang=profile.getItemValueString("SAPCode");
sapSysNo=profile.getItemValueString("SAPSysNO");
String logPath=profile.getItemValueString("LogPath");
log = session.createLog( db.getTitle()+" 's Agent "+agentContext.getCurrentAgent().getName()+" on " + db.getServer());
log.openNotesLog(db.getServer(),logPath);

Agent agent = agentContext.getCurrentAgent();
doc = db.getDocumentByID(agent.getParameterDocID());

System.out.println("Load Profile OK.");
}catch(Exception e) {
e.printStackTrace();
try{
session.setEnvironmentVar("AgentResult","載入SAP 設定時,發生錯誤 !!");
}catch ( Exception ex){}
}
}


public void processApprove() {
boolean isok;
Item notifyitem;
try{
System.out.println("Begin JCO Connection....");
log.logAction("Begin JCO Connection....");
client = JCO.createClient(sapClient, sapAccount, sapPasswd, sapLang, sapIP, sapSysNo);
client.connect();
JCO.Repository repository = new JCO.Repository( "Notes", client );
JCO.Attributes attr = client.getAttributes();
IFunctionTemplate ftemplate = repository.getFunctionTemplate( "BAPI_PROCORD_GET_DETAIL" );
function = new JCO.Function( ftemplate );
//取回structure來設定輸入參數
params = function.getImportParameterList();
JCO.Structure structure1 = params.getStructure("ORDER_OBJECTS");
structure1.setValue("1", "HEADER");

//用Table傳參數的例子
//     JCoTable table = function.getTableParameterList().getTable(targetTableUnderBAPI); //it is taken from the response value of metadata
//System.out.println("No of Columns: "+ table.getNumColumns());
//      System.out.println("Trying to execute append row");
//        table.appendRow();
 //          table.setValue(query_input_column1,query_input_column1_value);
 //        table.setValue(query_input_column2,query_input_column2_value);
 //      table.setValue(query_input_column3,query_input_column3_value);
 //table.setValue(query_input_column4,new java.util.Date(query_input_column4_value));
 //-----------------------------------------------------------------------------------------------------------------------------

System.out.println("Begin Load Not Procsess Documents....");
log.logAction("Begin Load Not Procsess Documents....");
if (doc != null) {
String sn="00"+doc.getItemValueString("PONumber").replaceAll("\\s+", "");
System.out.println("Begin Process PO No: " + sn );
log.logAction("Begin ProcessPO No: " + sn );
function.getImportParameterList().setValue(sn,"NUMBER");
function.getImportParameterList().setValue(structure1,"ORDER_OBJECTS");
client.execute( function );
returnStructure = (JCO.Structure)function.getExportParameterList().getValue("RETURN");
//取回結果的Table並取值
JCO.Table returnTable1 = function.getTableParameterList().getTable("HEADER");
int records = returnTable1.getNumRows();
if (records == 0) {
session.setEnvironmentVar("AgentResult","無此工單");
session.setEnvironmentVar("AgentResult2", "");
}
else {
String rMessage="";
for (int i=0;i<records;i++) {
returnTable1.setRow(i);
rMessage =  rMessage + returnTable1.getString("MATERIAL_TEXT") + "; "; }
returnTable1.setRow(1);
// Date RelaseDate= returnTable1.getDate("ACTUAL_RELEASE_DATE");
String dateString = returnTable1.getString("ACTUAL_RELEASE_DATE");
session.setEnvironmentVar("AgentResult",rMessage);
session.setEnvironmentVar("AgentResult2", dateString);
}
log.logAction("BAPI_PROCORD_GET_DETAIL PONumber: " + sn);
}
}catch(java.lang.NoClassDefFoundError e){
e.printStackTrace();
try{
session.setEnvironmentVar("AgentResult","SAP 元件未安裝!!");
}catch ( Exception ex){}
}
catch(Exception e) {
e.printStackTrace();
try{
log.logError(0,e.getLocalizedMessage());
session.setEnvironmentVar("AgentResult","代理程式發生不明錯誤!!");
}catch(Exception err){}
}finally{
try{
  JCO.releaseClient(client);
log.close();
}catch(Exception err){}
}
}

public void NotesMain() {

try {
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
SAPApprove sapobj=new SAPApprove();
sapobj.init(session,agentContext);

Agent agent = agentContext.getCurrentAgent();

sapobj.processApprove();

} catch(Exception e) {
e.printStackTrace();
}
}
}

沒有留言:

張貼留言