2014年7月3日 星期四

Domino 7.0 透過JCO以BAPI新增SAP流程式製程單報工的範例

範例如下

(如果是JCO ver 3以上, 則必須以JcoContext.begin()與JcoContext.end()來包住兩個execute才能真正commit喔)

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.Function commFunct;
private JCO.Client client ;
private Log log;
private boolean isError=false;
private String errMsg="";
private String resultCode;
private String resultMsg ;
private String wgroup ;
private String ponumber ;
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_ACC_ACTIVITY_ALLOC_POST" );
function = new JCO.Function( ftemplate );
params = function.getImportParameterList();
JCO.Structure structure1 = params.getStructure("DOC_HEADER");
    JCO.Table table = function.getTableParameterList().getTable("DOC_ITEMS"); //it is taken from the response value of metadata
System.out.println("No of Columns: "+ table.getNumColumns());
 //-----------------------------------------------------------------------------------------------------------------------------
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+", "");
Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int thismonth = cal.get(Calendar.MONTH);
int thisyear = cal.get(Calendar.YEAR);
structure1.setValue("TW00", "CO_AREA");
structure1.setValue(date, "DOCDATE");
structure1.setValue(date, "POSTGDATE");
structure1.setValue(doc.getItemValueString("DOC_HDR_TX").replaceAll("\\s+", ""), "DOC_HDR_TX");
structure1.setValue("ROOT", "USERNAME");
structure1.setValue(thismonth, "VAL_PERIOD");
structure1.setValue(thisyear, "VAL_FISYEAR");
     System.out.println("Trying to execute append row");
     wgroup=doc.getItemValueString("WorkGroup").replaceAll("\\s+", "");
         table.appendRow(); // Add  rows to internal table for OHV
           table.setValue(wgroup,"SEND_CCTR");
           table.setValue("OHV","ACTTYPE");
           table.setValue(doc.getItemValueDouble("Amount"),"ACTVTY_QTY");
           table.setValue("STD","ACTIVITYUN");
           table.setValue(sn,"REC_ORDER");
           table.appendRow(); // Add  rows to internal table for OHF
           table.setValue(wgroup,"SEND_CCTR");
           table.setValue("OHF","ACTTYPE");
           table.setValue(doc.getItemValueDouble("Amount"),"ACTVTY_QTY");
           table.setValue("STD","ACTIVITYUN");
           table.setValue(sn,"REC_ORDER");        
           table.appendRow(); // Add  rows to internal table for LH
           table.setValue(wgroup,"SEND_CCTR");
           table.setValue("LH","ACTTYPE");
           table.setValue(doc.getItemValueDouble("Amount"),"ACTVTY_QTY");
           table.setValue("STD","ACTIVITYUN");
           table.setValue(sn,"REC_ORDER");        
//table.setValue(query_input_column4,new java.util.Date(query_input_column4_value));
System.out.println("Begin Process PO No: " + sn );
log.logAction("Begin ProcessPO No: " + sn );
function.getImportParameterList().setValue(structure1,"DOC_HEADER");
function.getTableParameterList().setValue(table,"DOC_ITEMS");
IFunctionTemplate ftemplate1 = repository.getFunctionTemplate( "BAPI_TRANSACTION_COMMIT" );
commFunct = new JCO.Function( ftemplate1 );
commFunct.getImportParameterList().setValue("10", "WAIT");
client.execute( function );
              client.execute(commFunct);
JCO.Table returnTable1 = function.getTableParameterList().getTable("RETURN");
returnTable1.setRow(1);
String sn2 = (String)function.getExportParameterList().getValue("DOC_NO");
String sn1=returnTable1.getString("TYPE")+":"+returnTable1.getString("MESSAGE");
String ck=returnTable1.getString("TYPE");
System.out.println("FeedBack: " + sn1 );
log.logAction("FeedBack: " + sn1 );
if (ck.equals("S"))
{
System.out.println("Get Doc No: " + sn2 );
log.logAction("Get Doc No: " + sn2 );
                  returnStructure = (JCO.Structure)commFunct.getExportParameterList().getValue("RETURN");
                  String sn3=returnStructure.getString("TYPE")+":"+returnStructure.getString("MESSAGE");
                  System.out.println("FeedBack: " + sn3);
log.logAction("FeedBack: " + sn3);
                  System.out.println("Function BAPI_TRANSACTION_COMMIT executed .");
session.setEnvironmentVar("AgentResult","OK");
}
else
{
System.out.println("It's failed to create DOC....");
log.logAction("It's failedto create DOC...");
session.setEnvironmentVar("AgentResult","產生報工文件時,發生錯誤 !!");
}
log.logAction("BAPI_ACC_ACTIVITY_ALLOC_POST: " + 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();
}
}
}

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();
}
}
}