先實做此一內建Function
FUNCTION EXTRACT_TABLE_DATA .
*"----------------------------------------------------------------------
*"*"區域介面:
*" TABLES
*" FIELDS STRUCTURE ZSQL_CLAUSE_ELEMENTS
*" FROMCLAUSE STRUCTURE ZSQL_CLAUSE_ELEMENTS
*" WHERECLAUSE STRUCTURE ZSQL_CLAUSE_ELEMENTS
*" DATA STRUCTURE ZTABLEROWS
*"----------------------------------------------------------------------
*"----------------------------------------------------------------------
*" Copy selected fields from QUERY_TABLE to DATA_STRUCTURE
*"----------------------------------------------------------------------
TYPE-POOLS: abap.
DATA:
columnName TYPE SO_TEXT,
fieldDataDescrRef TYPE REF TO abap_componentdescr,
numberFields TYPE i,
fieldDescr TYPE abap_componentdescr,
fieldname TYPE string,
fieldDescrTab TYPE abap_component_tab,
rowStructDescr TYPE REF TO cl_abap_structdescr,
rowReference TYPE REF TO data,
returnRowString TYPE string,
dataFieldString TYPE string,
dataline LIKE data,
fromClauseRow TYPE ZSQL_CLAUSE_ELEMENTS,
fromClauseString TYPE string,
whereClauseRow TYPE ZSQL_CLAUSE_ELEMENTS,
whereClauseString TYPE string,
fieldsRow TYPE ZSQL_CLAUSE_ELEMENTS.
FIELD-SYMBOLS:
<datarow> TYPE ANY,
<datafield> TYPE ANY.
* CREATE DataStructure with field names
* Datatypes are read from fieldnames of FIELDS input table
DESCRIBE TABLE FIELDS LINES numberFields.
LOOP AT FIELDS INTO fieldsRow.
fieldname = SY-TABIX.
* names need to be unique and must start with a char
CONCATENATE 'string' fieldname INTO fieldname.
CONDENSE fieldname.
fieldDescr-name = fieldname.
* for dictionary lookup we need to change columnnames from Open SQL
* to dictionary notation
columnName = fieldsRow-TEXT.
REPLACE FIRST OCCURRENCE OF SUBSTRING '~' IN columnName WITH '-' RESPECTING CASE.
fieldDescr-type ?= cl_abap_typedescr=>describe_by_name( columnName ).
APPEND fieldDescr TO fieldDescrTab.
ENDLOOP.
rowStructDescr = cl_abap_structdescr=>create( fieldDescrTab ).
* now we create the actual data structure in memory
create data rowReference type HANDLE rowStructDescr.
* finally we assign it to the Field-symbol used by the select statement
ASSIGN rowReference->* TO <datarow>.
* End Create DataStructure
* to simplify calls we concatenate from and whereclause into strings
* this way caller doesn't need to check word wrappings
fromClauseString = ''.
LOOP AT FROMCLAUSE INTO fromClauseRow.
CONCATENATE fromClauseString fromClauseRow-TEXT INTO fromClauseString.
ENDLOOP.
whereClauseString = ''.
LOOP AT WHERECLAUSE INTO whereClauseRow.
CONCATENATE whereClauseString whereClauseRow-TEXT INTO whereClauseString.
ENDLOOP.
* Now start actual select operation
SELECT (FIELDS) FROM (fromClauseString) INTO <datarow> WHERE (whereClauseString).
* we read all fields of the current row, cast it to string and
* concatenate it into a dataline with division chars.
CLEAR: returnRowString.
DO numberFields TIMES.
ASSIGN component sy-index of structure <datarow> to <datafield>.
dataFieldString = <datafield>.
CONCATENATE returnRowString '|' datafieldstring INTO returnRowString.
ENDDO.
dataline = returnRowString.
* finally dataline is added to the return table.
INSERT dataline INTO TABLE data.
ENDSELECT.
ENDFUNCTION.
使用範例
2015年5月4日 星期一
2015年4月29日 星期三
Domino 7 使用 ADO 做 Mysql 的 新增,修改,刪除及查詢
查詢
Sub Initialize
On Error Goto ErrHandle
Set session=New NotesSession
Set db=session.CurrentDatabase
Set ws=New NotesUIWorkspace
Set agent = session.CurrentAgent
Set currentLog = New NotesLog( db.Title+" - Agent - "+agent.Name+" on "+db.Server )
Set profile=db.GetProfileDocument("SystemProfile")
Call currentLog.OpenNotesLog( db.Server, profile.LogPath(0) )
Dim conn As Variant
Set conn = CreateObject ("ADODB.Connection")
ConnAdmin = "DRIVER={MySQL ODBC 5.1 Driver}; SERVER="+profile.SqlServer(0)+"; DATABASE="+profile.SqlDatabase(0)+";UID="+profile.SqlAccount(0)+";PASSWORD="+profile.SqlPasswd(0)+"; OPTION=3"
conn.open ConnAdmin
Set uidoc=ws.CurrentDocument
Set doc=uidoc.Document
Set RSTT = CreateObject("ADODB.Recordset")
'SQL = "SELECT * FROM mm_budget WHERE MM_MARA_MATNR='"+doc.ItemNo(0)+"'"
SQL = "SELECT * FROM mm_budget WHERE MM_MARA_MATNR='"+uidoc.FieldGetText("ItemNo")+"' AND BGTDATE>'" +uidoc.FieldGetText("Result")+"'"
rstt.CursorLocation=3
rstt.open SQL, conn, 1,3
rstt.Sort = "BGTDATE DESC"
Call uidoc.FieldClear("A")
Call uidoc.FieldClear("B")
Call uidoc.FieldClear("C")
Call uidoc.FieldClear("D")
Call uidoc.FieldClear("E")
Call uidoc.FieldClear("F")
Do While Not rstt.eof
' For i = 0 To 5
' this.field=rstt.Fields(i).field
' this.value=rstt.Fields(i).value
' Set item = doc.AppendItemValue (field, value)
' Next
Call uidoc.FieldAppendText("A",Cstr(rstt.Fields(0).value)+Chr(10)+Chr(13))
Call uidoc.FieldAppendText("B",Cstr(rstt.Fields(4).value)+Chr(10)+Chr(13))
Call uidoc.FieldAppendText("C",Cstr(rstt.Fields(2).value)+Chr(10)+Chr(13))
Call uidoc.FieldAppendText("D",Cstr(rstt.Fields(5).value)+Chr(10)+Chr(13))
Call uidoc.FieldAppendText("E",Cstr(rstt.Fields(3).value)+Chr(10)+Chr(13))
Call uidoc.FieldAppendText("F",Cstr(rstt.Fields(1).value)+Chr(10)+Chr(13))
RSTT.MoveNext
Loop
rstt.close
conn.close
ErrHandle:
Print "Error" & Str(Err) & ": " & Error$ & " on line " & Cstr(Erl)
Call currentLog.LogError(0,"不明錯誤,請查詢Log 記錄 : " & "Error" & Str(Err) & ": " & Error$ & " on line " & Cstr(Erl))
End Sub
新增
Sub Initialize
On Error Goto ErrHandle
Set session=New NotesSession
Set db=session.CurrentDatabase
Set ws=New NotesUIWorkspace
Set agent = session.CurrentAgent
Set currentLog = New NotesLog( db.Title+" - Agent - "+agent.Name+" on "+db.Server )
Set profile=db.GetProfileDocument("SystemProfile")
Call currentLog.OpenNotesLog( db.Server, profile.LogPath(0) )
Dim avarNew As Variant
avarNew = Split(profile.RecNo(0), "|")
Dim conn As Variant
Set conn = CreateObject ("ADODB.Connection")
ConnAdmin = "DRIVER={MySQL ODBC 5.1 Driver}; SERVER="+profile.SqlServer(0)+"; DATABASE="+profile.SqlDatabase(0)+";UID="+profile.SqlAccount(0)+";PASSWORD="+profile.SqlPasswd(0)+"; OPTION=3"
conn.open ConnAdmin
Set uidoc=ws.CurrentDocument
Set doc=uidoc.Document
SQL = "INSERT INTO `mis`.`mm_budget` (`BGTTYPE`, `BGTDATE`, `QTY`, `MM_MARA_MATNR`, `MM_KOS_KOSTL`) VALUES ('"+avarNew(4)+"', '"+avarNew(1)+"', '"+avarNew(3)+"', '"+avarNew(0)+"', '"+avarNew(2)+"')"
Call currentlog.LogAction(SQL)
conn.execute(SQL)
conn.close
ErrHandle:
Print "Error" & Str(Err) & ": " & Error$ & " on line " & Cstr(Erl)
Call currentLog.LogError(0,"不明錯誤,請查詢Log 記錄 : " & "Error" & Str(Err) & ": " & Error$ & " on line " & Cstr(Erl))
End Sub
刪除
Sub Initialize
On Error Goto ErrHandle
Set session=New NotesSession
Set db=session.CurrentDatabase
Set ws=New NotesUIWorkspace
Set agent = session.CurrentAgent
Set currentLog = New NotesLog( db.Title+" - Agent - "+agent.Name+" on "+db.Server )
Set profile=db.GetProfileDocument("SystemProfile")
Call currentLog.OpenNotesLog( db.Server, profile.LogPath(0) )
Dim conn As Variant
Set conn = CreateObject ("ADODB.Connection")
ConnAdmin = "DRIVER={MySQL ODBC 5.1 Driver}; SERVER="+profile.SqlServer(0)+"; DATABASE="+profile.SqlDatabase(0)+";UID="+profile.SqlAccount(0)+";PASSWORD="+profile.SqlPasswd(0)+"; OPTION=3"
conn.open ConnAdmin
Set uidoc=ws.CurrentDocument
Set doc=uidoc.Document
SQL = "DELETE FROM `mis`.`mm_budget` WHERE `BGTID`='"+profile.RecNo(0)+"';"
Call currentlog.LogAction(SQL)
conn.execute(SQL)
conn.close
ErrHandle:
Print "Error" & Str(Err) & ": " & Error$ & " on line " & Cstr(Erl)
Call currentLog.LogError(0,"不明錯誤,請查詢Log 記錄 : " & "Error" & Str(Err) & ": " & Error$ & " on line " & Cstr(Erl))
End Sub
修改
Sub Initialize
On Error Goto ErrHandle
Set session=New NotesSession
Set db=session.CurrentDatabase
Set ws=New NotesUIWorkspace
Set agent = session.CurrentAgent
Set currentLog = New NotesLog( db.Title+" - Agent - "+agent.Name+" on "+db.Server )
Set profile=db.GetProfileDocument("SystemProfile")
Call currentLog.OpenNotesLog( db.Server, profile.LogPath(0) )
Dim avarNew As Variant
avarNew = Split(profile.RecNo(0), "|")
Dim conn As Variant
Set conn = CreateObject ("ADODB.Connection")
ConnAdmin = "DRIVER={MySQL ODBC 5.1 Driver}; SERVER="+profile.SqlServer(0)+"; DATABASE="+profile.SqlDatabase(0)+";UID="+profile.SqlAccount(0)+";PASSWORD="+profile.SqlPasswd(0)+"; OPTION=3"
conn.open ConnAdmin
Set uidoc=ws.CurrentDocument
Set doc=uidoc.Document
SQL = "UPDATE `mis`.`mm_budget` SET `MM_MARA_MATNR`='"+DoTrim(avarNew(1))+"',`MM_KOS_KOSTL`='"+DoTrim(avarNew(3))+"',`BGTTYPE`='"+DoTrim(avarNew(5))+"',`BGTDATE`='"+DoTrim(avarNew(2))+"', `QTY`='"+DoTrim(avarNew(4))+"' WHERE `BGTID`='"+DoTrim(avarNew(0))+"';"
Call currentlog.LogAction(SQL)
conn.execute(SQL)
conn.close
ErrHandle:
Print "Error" & Str(Err) & ": " & Error$ & " on line " & Cstr(Erl)
Call currentLog.LogError(0,"不明錯誤,請查詢Log 記錄 : " & "Error" & Str(Err) & ": " & Error$ & " on line " & Cstr(Erl))
End Sub
Sub Initialize
On Error Goto ErrHandle
Set session=New NotesSession
Set db=session.CurrentDatabase
Set ws=New NotesUIWorkspace
Set agent = session.CurrentAgent
Set currentLog = New NotesLog( db.Title+" - Agent - "+agent.Name+" on "+db.Server )
Set profile=db.GetProfileDocument("SystemProfile")
Call currentLog.OpenNotesLog( db.Server, profile.LogPath(0) )
Dim conn As Variant
Set conn = CreateObject ("ADODB.Connection")
ConnAdmin = "DRIVER={MySQL ODBC 5.1 Driver}; SERVER="+profile.SqlServer(0)+"; DATABASE="+profile.SqlDatabase(0)+";UID="+profile.SqlAccount(0)+";PASSWORD="+profile.SqlPasswd(0)+"; OPTION=3"
conn.open ConnAdmin
Set uidoc=ws.CurrentDocument
Set doc=uidoc.Document
Set RSTT = CreateObject("ADODB.Recordset")
'SQL = "SELECT * FROM mm_budget WHERE MM_MARA_MATNR='"+doc.ItemNo(0)+"'"
SQL = "SELECT * FROM mm_budget WHERE MM_MARA_MATNR='"+uidoc.FieldGetText("ItemNo")+"' AND BGTDATE>'" +uidoc.FieldGetText("Result")+"'"
rstt.CursorLocation=3
rstt.open SQL, conn, 1,3
rstt.Sort = "BGTDATE DESC"
Call uidoc.FieldClear("A")
Call uidoc.FieldClear("B")
Call uidoc.FieldClear("C")
Call uidoc.FieldClear("D")
Call uidoc.FieldClear("E")
Call uidoc.FieldClear("F")
Do While Not rstt.eof
' For i = 0 To 5
' this.field=rstt.Fields(i).field
' this.value=rstt.Fields(i).value
' Set item = doc.AppendItemValue (field, value)
' Next
Call uidoc.FieldAppendText("A",Cstr(rstt.Fields(0).value)+Chr(10)+Chr(13))
Call uidoc.FieldAppendText("B",Cstr(rstt.Fields(4).value)+Chr(10)+Chr(13))
Call uidoc.FieldAppendText("C",Cstr(rstt.Fields(2).value)+Chr(10)+Chr(13))
Call uidoc.FieldAppendText("D",Cstr(rstt.Fields(5).value)+Chr(10)+Chr(13))
Call uidoc.FieldAppendText("E",Cstr(rstt.Fields(3).value)+Chr(10)+Chr(13))
Call uidoc.FieldAppendText("F",Cstr(rstt.Fields(1).value)+Chr(10)+Chr(13))
RSTT.MoveNext
Loop
rstt.close
conn.close
ErrHandle:
Print "Error" & Str(Err) & ": " & Error$ & " on line " & Cstr(Erl)
Call currentLog.LogError(0,"不明錯誤,請查詢Log 記錄 : " & "Error" & Str(Err) & ": " & Error$ & " on line " & Cstr(Erl))
End Sub
新增
Sub Initialize
On Error Goto ErrHandle
Set session=New NotesSession
Set db=session.CurrentDatabase
Set ws=New NotesUIWorkspace
Set agent = session.CurrentAgent
Set currentLog = New NotesLog( db.Title+" - Agent - "+agent.Name+" on "+db.Server )
Set profile=db.GetProfileDocument("SystemProfile")
Call currentLog.OpenNotesLog( db.Server, profile.LogPath(0) )
Dim avarNew As Variant
avarNew = Split(profile.RecNo(0), "|")
Dim conn As Variant
Set conn = CreateObject ("ADODB.Connection")
ConnAdmin = "DRIVER={MySQL ODBC 5.1 Driver}; SERVER="+profile.SqlServer(0)+"; DATABASE="+profile.SqlDatabase(0)+";UID="+profile.SqlAccount(0)+";PASSWORD="+profile.SqlPasswd(0)+"; OPTION=3"
conn.open ConnAdmin
Set uidoc=ws.CurrentDocument
Set doc=uidoc.Document
SQL = "INSERT INTO `mis`.`mm_budget` (`BGTTYPE`, `BGTDATE`, `QTY`, `MM_MARA_MATNR`, `MM_KOS_KOSTL`) VALUES ('"+avarNew(4)+"', '"+avarNew(1)+"', '"+avarNew(3)+"', '"+avarNew(0)+"', '"+avarNew(2)+"')"
Call currentlog.LogAction(SQL)
conn.execute(SQL)
conn.close
ErrHandle:
Print "Error" & Str(Err) & ": " & Error$ & " on line " & Cstr(Erl)
Call currentLog.LogError(0,"不明錯誤,請查詢Log 記錄 : " & "Error" & Str(Err) & ": " & Error$ & " on line " & Cstr(Erl))
End Sub
刪除
Sub Initialize
On Error Goto ErrHandle
Set session=New NotesSession
Set db=session.CurrentDatabase
Set ws=New NotesUIWorkspace
Set agent = session.CurrentAgent
Set currentLog = New NotesLog( db.Title+" - Agent - "+agent.Name+" on "+db.Server )
Set profile=db.GetProfileDocument("SystemProfile")
Call currentLog.OpenNotesLog( db.Server, profile.LogPath(0) )
Dim conn As Variant
Set conn = CreateObject ("ADODB.Connection")
ConnAdmin = "DRIVER={MySQL ODBC 5.1 Driver}; SERVER="+profile.SqlServer(0)+"; DATABASE="+profile.SqlDatabase(0)+";UID="+profile.SqlAccount(0)+";PASSWORD="+profile.SqlPasswd(0)+"; OPTION=3"
conn.open ConnAdmin
Set uidoc=ws.CurrentDocument
Set doc=uidoc.Document
SQL = "DELETE FROM `mis`.`mm_budget` WHERE `BGTID`='"+profile.RecNo(0)+"';"
Call currentlog.LogAction(SQL)
conn.execute(SQL)
conn.close
ErrHandle:
Print "Error" & Str(Err) & ": " & Error$ & " on line " & Cstr(Erl)
Call currentLog.LogError(0,"不明錯誤,請查詢Log 記錄 : " & "Error" & Str(Err) & ": " & Error$ & " on line " & Cstr(Erl))
End Sub
修改
Sub Initialize
On Error Goto ErrHandle
Set session=New NotesSession
Set db=session.CurrentDatabase
Set ws=New NotesUIWorkspace
Set agent = session.CurrentAgent
Set currentLog = New NotesLog( db.Title+" - Agent - "+agent.Name+" on "+db.Server )
Set profile=db.GetProfileDocument("SystemProfile")
Call currentLog.OpenNotesLog( db.Server, profile.LogPath(0) )
Dim avarNew As Variant
avarNew = Split(profile.RecNo(0), "|")
Dim conn As Variant
Set conn = CreateObject ("ADODB.Connection")
ConnAdmin = "DRIVER={MySQL ODBC 5.1 Driver}; SERVER="+profile.SqlServer(0)+"; DATABASE="+profile.SqlDatabase(0)+";UID="+profile.SqlAccount(0)+";PASSWORD="+profile.SqlPasswd(0)+"; OPTION=3"
conn.open ConnAdmin
Set uidoc=ws.CurrentDocument
Set doc=uidoc.Document
SQL = "UPDATE `mis`.`mm_budget` SET `MM_MARA_MATNR`='"+DoTrim(avarNew(1))+"',`MM_KOS_KOSTL`='"+DoTrim(avarNew(3))+"',`BGTTYPE`='"+DoTrim(avarNew(5))+"',`BGTDATE`='"+DoTrim(avarNew(2))+"', `QTY`='"+DoTrim(avarNew(4))+"' WHERE `BGTID`='"+DoTrim(avarNew(0))+"';"
Call currentlog.LogAction(SQL)
conn.execute(SQL)
conn.close
ErrHandle:
Print "Error" & Str(Err) & ": " & Error$ & " on line " & Cstr(Erl)
Call currentLog.LogError(0,"不明錯誤,請查詢Log 記錄 : " & "Error" & Str(Err) & ": " & Error$ & " on line " & Cstr(Erl))
End Sub
2015年4月22日 星期三
Domino利用JCO來透過RFC_RAED_TABLE取得SAP資料的範例
很多取SAP資料的小應用其實都不需要自己寫RFC,下面這個例子是讓用戶輸入料號及批號來取得品名及可用量和有效期。
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 function1;
private JCO.Function function2;
private JCO.Client client ;
private Log log;
private boolean isError=false;
private String errMsg="";
private String resultCode;
private String resultMsg ;
private String cnname ;
private String quantity ;
private String valid_date ;
private Document doc;
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( "RFC_READ_TABLE" );
function = new JCO.Function( ftemplate );
function1 = new JCO.Function( ftemplate );
function2 = new JCO.Function( ftemplate );
System.out.println("Begin Load Note Procsess Documents....");
log.logAction("Begin Load Note Procsess Documents....");
if (doc != null) {
String sn=doc.getItemValueString("ItemNo").replaceAll("\\s+", "").trim();
String sn2=doc.getItemValueString("Batch").replaceAll("\\s+", "").trim();
System.out.println("Begin Process ItemNo: " + sn + "-Batch-" + sn2);
log.logAction("Begin Process ItemNo: " + sn + "-Btach-" + sn2);
//取中文名稱
function.getImportParameterList().setValue(",","DELIMITER");
function.getImportParameterList().setValue("MAKT","QUERY_TABLE");
JCO.ParameterList inputTables = function.getTableParameterList();
JCO.Table table = inputTables.getTable("OPTIONS");
table.appendRows(1);
table.setValue("MATNR EQ '"+ sn +"' ", "TEXT");
function.setTableParameterList(inputTables);
client.execute( function );
JCO.Table returnTable1 = function.getTableParameterList().getTable("DATA");
int records = returnTable1.getNumRows();
if (records == 0) {
cnname = "N/A";
}
else {
returnTable1.setRow(1);
String[] aArray = returnTable1.getString("WA").split(",");
cnname=aArray[3];
}
//取庫存量
function1.getImportParameterList().setValue(",","DELIMITER");
function1.getImportParameterList().setValue("MCHB","QUERY_TABLE");
JCO.ParameterList inputTables1 = function1.getTableParameterList();
JCO.Table table1 = inputTables1.getTable("OPTIONS");
table1.appendRows(1);
table1.setValue("MATNR EQ '"+ sn +"' AND CHARG EQ '"+ sn2 +"'", "TEXT");
function1.setTableParameterList(inputTables1);
client.execute( function1);
JCO.Table returnTable2 = function1.getTableParameterList().getTable("DATA");
int records2 = returnTable2.getNumRows();
if (records2 == 0) {
quantity = "N/A";
}
else {
returnTable2.setRow(1);
System.out.println("Get Data: " + returnTable2.getString("WA"));
String[] bArray = returnTable2.getString("WA").split(",");
quantity=bArray[13];
}
//取有效期
function2.getImportParameterList().setValue(",","DELIMITER");
function2.getImportParameterList().setValue("MCH1","QUERY_TABLE");
JCO.ParameterList inputTables2 = function2.getTableParameterList();
JCO.Table table2 = inputTables2.getTable("OPTIONS");
table2.appendRows(1);
table2.setValue("MATNR EQ '"+ sn +"' AND CHARG EQ '"+ sn2 +"'", "TEXT");
function2.setTableParameterList(inputTables2);
client.execute( function2);
JCO.Table returnTable3 = function2.getTableParameterList().getTable("DATA");
int records3 = returnTable3.getNumRows();
if (records3 == 0) {
valid_date = "N/A";
}
else {
returnTable3.setRow(1);
System.out.println("Get Data: " + returnTable3.getString("WA"));
String[] cArray = returnTable3.getString("WA").split(",");
valid_date = cArray[9];
}
session.setEnvironmentVar("AgentResult",cnname+","+quantity+","+valid_date);
}
}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();
}
}
}
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 function1;
private JCO.Function function2;
private JCO.Client client ;
private Log log;
private boolean isError=false;
private String errMsg="";
private String resultCode;
private String resultMsg ;
private String cnname ;
private String quantity ;
private String valid_date ;
private Document doc;
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( "RFC_READ_TABLE" );
function = new JCO.Function( ftemplate );
function1 = new JCO.Function( ftemplate );
function2 = new JCO.Function( ftemplate );
System.out.println("Begin Load Note Procsess Documents....");
log.logAction("Begin Load Note Procsess Documents....");
if (doc != null) {
String sn=doc.getItemValueString("ItemNo").replaceAll("\\s+", "").trim();
String sn2=doc.getItemValueString("Batch").replaceAll("\\s+", "").trim();
System.out.println("Begin Process ItemNo: " + sn + "-Batch-" + sn2);
log.logAction("Begin Process ItemNo: " + sn + "-Btach-" + sn2);
//取中文名稱
function.getImportParameterList().setValue(",","DELIMITER");
function.getImportParameterList().setValue("MAKT","QUERY_TABLE");
JCO.ParameterList inputTables = function.getTableParameterList();
JCO.Table table = inputTables.getTable("OPTIONS");
table.appendRows(1);
table.setValue("MATNR EQ '"+ sn +"' ", "TEXT");
function.setTableParameterList(inputTables);
client.execute( function );
JCO.Table returnTable1 = function.getTableParameterList().getTable("DATA");
int records = returnTable1.getNumRows();
if (records == 0) {
cnname = "N/A";
}
else {
returnTable1.setRow(1);
String[] aArray = returnTable1.getString("WA").split(",");
cnname=aArray[3];
}
//取庫存量
function1.getImportParameterList().setValue(",","DELIMITER");
function1.getImportParameterList().setValue("MCHB","QUERY_TABLE");
JCO.ParameterList inputTables1 = function1.getTableParameterList();
JCO.Table table1 = inputTables1.getTable("OPTIONS");
table1.appendRows(1);
table1.setValue("MATNR EQ '"+ sn +"' AND CHARG EQ '"+ sn2 +"'", "TEXT");
function1.setTableParameterList(inputTables1);
client.execute( function1);
JCO.Table returnTable2 = function1.getTableParameterList().getTable("DATA");
int records2 = returnTable2.getNumRows();
if (records2 == 0) {
quantity = "N/A";
}
else {
returnTable2.setRow(1);
System.out.println("Get Data: " + returnTable2.getString("WA"));
String[] bArray = returnTable2.getString("WA").split(",");
quantity=bArray[13];
}
//取有效期
function2.getImportParameterList().setValue(",","DELIMITER");
function2.getImportParameterList().setValue("MCH1","QUERY_TABLE");
JCO.ParameterList inputTables2 = function2.getTableParameterList();
JCO.Table table2 = inputTables2.getTable("OPTIONS");
table2.appendRows(1);
table2.setValue("MATNR EQ '"+ sn +"' AND CHARG EQ '"+ sn2 +"'", "TEXT");
function2.setTableParameterList(inputTables2);
client.execute( function2);
JCO.Table returnTable3 = function2.getTableParameterList().getTable("DATA");
int records3 = returnTable3.getNumRows();
if (records3 == 0) {
valid_date = "N/A";
}
else {
returnTable3.setRow(1);
System.out.println("Get Data: " + returnTable3.getString("WA"));
String[] cArray = returnTable3.getString("WA").split(",");
valid_date = cArray[9];
}
session.setEnvironmentVar("AgentResult",cnname+","+quantity+","+valid_date);
}
}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年8月19日 星期二
ASP.NET MVC 用自訂Action Filter來做Log
參考這篇
張小呆的碎碎唸
稍微改成log到資料庫並記錄下當時所處理的資料.
public class LogActionFilterAttribute : ActionFilterAttribute
{
public string ControllerName { get; set; }
public string ActionName { get; set; }
private ApplicationDbContext db = new ApplicationDbContext();
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
string uid = filterContext.HttpContext.User.Identity.Name.ToString();
string originController = filterContext.RouteData.Values["controller"].ToString();
string originAction = filterContext.RouteData.Values["action"].ToString();
string originArea = String.Empty;
if (filterContext.RouteData.DataTokens.ContainsKey("area"))
originArea = filterContext.RouteData.DataTokens["area"].ToString();
//這邊利用傳遞參數的名稱來判定要如取出相關資料與以記錄, 所以Action的參數名稱就必須要有可鑑別性
string result = "";
if (filterContext.ActionParameters.ContainsKey("empv"))
{
empEditViewModels viewModel = (empEditViewModels)filterContext.ActionParameters["empv"];
result = viewModel.emp1.eid + viewModel.emp1.cname + " 職稱: " + viewModel.emp1.title + " 部門編號: " + viewModel.emp1.dept;
};
if (filterContext.ActionParameters.ContainsKey("emp"))
{
emp emp1 = (emp)filterContext.ActionParameters["emp"];
result = emp1.eid + emp1.cname + " 職稱: " + emp1.title + " 部門編號: " +emp1.dept;
};
if (filterContext.ActionParameters.ContainsKey("id"))
{
int id1 = (int)filterContext.ActionParameters["id"];
result = " 序號: " + id1.ToString();
};
//紀錄相關資料到資料庫
actlog logmodel = new actlog()
{
App = ControllerName+originController,
Act = ActionName+originAction,
Pepo = String.IsNullOrEmpty(SessionHelper.RealName) ? uid : SessionHelper.RealName,
Ext ="進入網頁 : "+result,
Tm = DateTime.Now
};
db.actlogs.Add(logmodel);
db.SaveChanges();
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
}
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
}
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
string uid = filterContext.HttpContext.User.Identity.Name.ToString();
actlog logmodel = new actlog()
{
App = ControllerName,
Act = ActionName,
Pepo = String.IsNullOrEmpty(SessionHelper.RealName) ? uid : SessionHelper.RealName,
Ext = "傳回的動作結果執行完成",
Tm = DateTime.Now
};
db.actlogs.Add(logmodel);
db.SaveChanges();
}
}
在control中使用範例 :
[HttpPost]
[ValidateAntiForgeryToken]
[LogActionFilter(ControllerName = "員工資料管理", ActionName = "新增完成")]
public ActionResult Create([Bind(Include = "id,eid,cname,dept,title")] emp emp, int? page, int? itemsPerPage, string sortOrder, string currentFilter)
{
ViewBag.DropDownList = new SelectList(db.deps, "Id", "title");
ViewBag.CurrentPage = page;
ViewBag.CurrentItemsPerPage = itemsPerPage;
ViewBag.OldSortParm = sortOrder;
ViewBag.CurrentFilter = currentFilter;
if (ModelState.IsValid)
{
db.emps.Add(emp);
db.SaveChanges();
return RedirectToAction("Index", new { page = ViewBag.CurrentPage, itemsPerPage = ViewBag.CurrentItemsPerPage, sortOrder = ViewBag.OldSortParm, searchString = "", CurrentFilter = ViewBag.CurrentFilter });
}
return View(emp);
}
紀錄結果 :
張小呆的碎碎唸
稍微改成log到資料庫並記錄下當時所處理的資料.
public class LogActionFilterAttribute : ActionFilterAttribute
{
public string ControllerName { get; set; }
public string ActionName { get; set; }
private ApplicationDbContext db = new ApplicationDbContext();
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
string uid = filterContext.HttpContext.User.Identity.Name.ToString();
string originController = filterContext.RouteData.Values["controller"].ToString();
string originAction = filterContext.RouteData.Values["action"].ToString();
string originArea = String.Empty;
if (filterContext.RouteData.DataTokens.ContainsKey("area"))
originArea = filterContext.RouteData.DataTokens["area"].ToString();
//這邊利用傳遞參數的名稱來判定要如取出相關資料與以記錄, 所以Action的參數名稱就必須要有可鑑別性
string result = "";
if (filterContext.ActionParameters.ContainsKey("empv"))
{
empEditViewModels viewModel = (empEditViewModels)filterContext.ActionParameters["empv"];
result = viewModel.emp1.eid + viewModel.emp1.cname + " 職稱: " + viewModel.emp1.title + " 部門編號: " + viewModel.emp1.dept;
};
if (filterContext.ActionParameters.ContainsKey("emp"))
{
emp emp1 = (emp)filterContext.ActionParameters["emp"];
result = emp1.eid + emp1.cname + " 職稱: " + emp1.title + " 部門編號: " +emp1.dept;
};
if (filterContext.ActionParameters.ContainsKey("id"))
{
int id1 = (int)filterContext.ActionParameters["id"];
result = " 序號: " + id1.ToString();
};
//紀錄相關資料到資料庫
actlog logmodel = new actlog()
{
App = ControllerName+originController,
Act = ActionName+originAction,
Pepo = String.IsNullOrEmpty(SessionHelper.RealName) ? uid : SessionHelper.RealName,
Ext ="進入網頁 : "+result,
Tm = DateTime.Now
};
db.actlogs.Add(logmodel);
db.SaveChanges();
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
}
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
}
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
string uid = filterContext.HttpContext.User.Identity.Name.ToString();
actlog logmodel = new actlog()
{
App = ControllerName,
Act = ActionName,
Pepo = String.IsNullOrEmpty(SessionHelper.RealName) ? uid : SessionHelper.RealName,
Ext = "傳回的動作結果執行完成",
Tm = DateTime.Now
};
db.actlogs.Add(logmodel);
db.SaveChanges();
}
}
在control中使用範例 :
[HttpPost]
[ValidateAntiForgeryToken]
[LogActionFilter(ControllerName = "員工資料管理", ActionName = "新增完成")]
public ActionResult Create([Bind(Include = "id,eid,cname,dept,title")] emp emp, int? page, int? itemsPerPage, string sortOrder, string currentFilter)
{
ViewBag.DropDownList = new SelectList(db.deps, "Id", "title");
ViewBag.CurrentPage = page;
ViewBag.CurrentItemsPerPage = itemsPerPage;
ViewBag.OldSortParm = sortOrder;
ViewBag.CurrentFilter = currentFilter;
if (ModelState.IsValid)
{
db.emps.Add(emp);
db.SaveChanges();
return RedirectToAction("Index", new { page = ViewBag.CurrentPage, itemsPerPage = ViewBag.CurrentItemsPerPage, sortOrder = ViewBag.OldSortParm, searchString = "", CurrentFilter = ViewBag.CurrentFilter });
}
return View(emp);
}
紀錄結果 :
在authentication mode="Forms"下使用 membership defaultProvider="ADMembershipProvider", 搭配Fluent Security的測試及如何使用AD群組來做權限控管
參考這篇,
Fluent Security - 在MVC集中管理頁面權限的套件
真的不錯用, 但受到Form登入無法直接使用 AD的群組當Roles的影響.
所以只能用上
configue.ForAllControllers().Ignore();
configue.For<HomeController>().DenyAnonymousAccess();
configue.For<HomeController>().DenyAuthenticatedAccess();
而一直找不到使用角色的辦法..
configue.For<HomeController>(hc => hc.Contact()).RequireAnyRole("Everyone");
只好回頭用自訂 action filter的辦法來處理 AD的Group.
1.加入參考
C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.DirectoryServices.AccountManagement.dll
2. Using System.DirectoryServices.AccountManagement;
3.使用者登入時取回AD Group資料塞入Session, 給自訂Action Filter及View使用.
[HttpPost]
public ActionResult Login(LoginModel model, string returnUrl)
{
if (!this.ModelState.IsValid)
{
return this.View(model);
}
if (Membership.ValidateUser(model.UserName, model.Password))
{
//取回AD Group資料塞入Session
var context = new PrincipalContext(ContextType.Domain, "Adimmune");
var userPrincipal = UserPrincipal.FindByIdentity(context,
IdentityType.SamAccountName,
model.UserName);
var UGS = userPrincipal.GetAuthorizationGroups();
string uGroup = "";
foreach (var ug in UGS)
uGroup = uGroup + ug.Name + ";";
SessionHelper.UserGroup = uGroup;
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
if (this.Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
return this.Redirect(returnUrl);
}
return this.RedirectToAction("Index", "Home");
}
this.ModelState.AddModelError(string.Empty, "您提供的Windows帳號或密碼並不正確,如有疑問請與資訊人員聯絡.");
return this.View(model);
}
//!!切記要在LogOff時清空SessionHelper.UserGroup,否則會影響權限控管
public ActionResult LogOff()
{
SessionHelper.UserGroup = "";
SessionHelper.RealName = "";
FormsAuthentication.SignOut();
return this.RedirectToAction("Index", "Home");
}
4.自訂Action Filter.
public class AuthorizeADAttribute : AuthorizeAttribute
{
public string Groups { get; set; }
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
/* Return true immediately if the authorization is not
locked down to any particular AD group */
if (String.IsNullOrEmpty(Groups))
return true;
// Get the AD groups
var groups = Groups.Split(',').ToList<string>();
// Verify that the user is in the given AD group (if any)
foreach (var group in groups)
if (SessionHelper.UserGroup.Contains(group+";"))
return true;
return false;
}
5. Control使用範例
[AuthorizeAD(Groups = "IT")]
public class empsController : Controller
{
..................................
}
6 View使用範例
if (Session["UserGroup"].ToString().Contains("webAdmin01;"))
{
<ul class="nav pull-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">後台管理<b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="@Url.Action("Index", "emps")"><i class="icon-off"></i>員工</a></li>
<li><a href="@Url.Action("Index", "deps")"><i class="icon-off"></i>部門</a></li>
<li><a href="@Url.Action("Index", "exms")"><i class="icon-off"></i>評核類別</a></li>
<li><a href="@Url.Action("Index", "ots")"><i class="icon-off"></i>評核時間</a></li>
<li><a href="@Url.Action("Index", "ts")"><i class="icon-off"></i>評核紀錄</a></li>
<li><a href="@Url.Action("Index", "actlogs")"><i class="icon-off"></i>Logs</a></li>
</ul>
</li>
</ul>
}
Fluent Security - 在MVC集中管理頁面權限的套件
真的不錯用, 但受到Form登入無法直接使用 AD的群組當Roles的影響.
所以只能用上
configue.ForAllControllers().Ignore();
configue.For<HomeController>().DenyAnonymousAccess();
configue.For<HomeController>().DenyAuthenticatedAccess();
而一直找不到使用角色的辦法..
configue.For<HomeController>(hc => hc.Contact()).RequireAnyRole("Everyone");
只好回頭用自訂 action filter的辦法來處理 AD的Group.
1.加入參考
C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.DirectoryServices.AccountManagement.dll
2. Using System.DirectoryServices.AccountManagement;
3.使用者登入時取回AD Group資料塞入Session, 給自訂Action Filter及View使用.
[HttpPost]
public ActionResult Login(LoginModel model, string returnUrl)
{
if (!this.ModelState.IsValid)
{
return this.View(model);
}
if (Membership.ValidateUser(model.UserName, model.Password))
{
//取回AD Group資料塞入Session
var context = new PrincipalContext(ContextType.Domain, "Adimmune");
var userPrincipal = UserPrincipal.FindByIdentity(context,
IdentityType.SamAccountName,
model.UserName);
var UGS = userPrincipal.GetAuthorizationGroups();
string uGroup = "";
foreach (var ug in UGS)
uGroup = uGroup + ug.Name + ";";
SessionHelper.UserGroup = uGroup;
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
if (this.Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
return this.Redirect(returnUrl);
}
return this.RedirectToAction("Index", "Home");
}
this.ModelState.AddModelError(string.Empty, "您提供的Windows帳號或密碼並不正確,如有疑問請與資訊人員聯絡.");
return this.View(model);
}
//!!切記要在LogOff時清空SessionHelper.UserGroup,否則會影響權限控管
public ActionResult LogOff()
{
SessionHelper.UserGroup = "";
SessionHelper.RealName = "";
FormsAuthentication.SignOut();
return this.RedirectToAction("Index", "Home");
}
4.自訂Action Filter.
public class AuthorizeADAttribute : AuthorizeAttribute
{
public string Groups { get; set; }
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
/* Return true immediately if the authorization is not
locked down to any particular AD group */
if (String.IsNullOrEmpty(Groups))
return true;
// Get the AD groups
var groups = Groups.Split(',').ToList<string>();
// Verify that the user is in the given AD group (if any)
foreach (var group in groups)
if (SessionHelper.UserGroup.Contains(group+";"))
return true;
return false;
}
5. Control使用範例
[AuthorizeAD(Groups = "IT")]
public class empsController : Controller
{
..................................
}
6 View使用範例
if (Session["UserGroup"].ToString().Contains("webAdmin01;"))
{
<ul class="nav pull-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">後台管理<b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="@Url.Action("Index", "emps")"><i class="icon-off"></i>員工</a></li>
<li><a href="@Url.Action("Index", "deps")"><i class="icon-off"></i>部門</a></li>
<li><a href="@Url.Action("Index", "exms")"><i class="icon-off"></i>評核類別</a></li>
<li><a href="@Url.Action("Index", "ots")"><i class="icon-off"></i>評核時間</a></li>
<li><a href="@Url.Action("Index", "ts")"><i class="icon-off"></i>評核紀錄</a></li>
<li><a href="@Url.Action("Index", "actlogs")"><i class="icon-off"></i>Logs</a></li>
</ul>
</li>
</ul>
}
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();
}
}
}
(如果是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();
}
}
}
這篇比上次叫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();
}
}
}
訂閱:
文章 (Atom)