import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import lotus.domino.*;
public class JavaAgent extends AgentBase {
public PublicFunction F = new PublicFunction();
private String driverName= "";
private String dbURL = "";
private String userName = "";
private String userPwd = "";
public void NotesMain() {
Database db = null;
Database sysdb = null;
Document doc = null;
Session session = null;
AgentContext agentContext = null;
try {
session = getSession();
agentContext = session.getAgentContext();
db = agentContext.getCurrentDatabase();
doc = agentContext.getDocumentContext();
insertIntoDatabase(db,sysdb,session, doc);
doc.replaceItemValue("alertMsg", "alert('已经成功写入数据库!')");
} catch (Exception e) {
e.printStackTrace();
try {
doc.replaceItemValue("alertMsg", "Hanhe.alert('写入数据库失败!');");
} catch (NotesException e1) {
e1.printStackTrace();
}
} finally {
try {
if (doc != null)doc.recycle();
if(db != null)db.recycle();
if(sysdb != null)sysdb.recycle();
if(session != null)session.recycle();
if(agentContext != null)agentContext.recycle();
} catch (NotesException e) {
e.printStackTrace();
}
}
}
/**
* @param db
* @param session
* @param doc
* @throws Exception
*/
private void insertIntoDatabase(Database db,Database sysdb,Session session,Document doc) throws Exception {
Connection dbConn = null;
PreparedStatement ps = null;
PreparedStatement ps2 = null;
Statement st = null;
Statement st2 = null;
View v_view = null;
ViewEntryCollection vc_vec = null;
ViewEntryCollection vc_vec2 = null;
ViewEntry ve1 = null;
ViewEntry ve2 = null;
ViewEntry ve3 = null;
ViewEntry ve4 = null;
Document tdoc = null;
View views = null;
View v_view2 = null;
Document tdoc2=null;
Document tdoc3=null;
try{
sysdb = session.getDatabase(db.getServer(), "oadata/AIS_Org.nsf"); //数据字典
driverName= F.GetSysdictionary(sysdb,"SQL_DriverName"); //加载JDBC驱动
dbURL = F.GetSysdictionary(sysdb, "SQlUrl"); //连接服务器和数据库
userName = F.GetSysdictionary(sysdb, "sqlName"); //默认用户名
userPwd = F.GetSysdictionary(sysdb, "sqlpassword"); //密码
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
插入之前先删除表
String sql2="delete from T_RIC_cejia_Basic";
st = dbConn.createStatement();
st.addBatch(sql2);
st.executeBatch();
dbConn.commit();
String sql4="delete from T_RIC_cejia_Item";
st2 = dbConn.createStatement();
st2.addBatch(sql4);
st2.executeBatch();
dbConn.commit();
添加数据
String sql = "Insert into T_RIC_cejia_Basic(FID,FCode,FDraftsman,FProductName,FProlevel,FBrand,FProdegree,FSpecification,FMaterialfactory,Ffactory,FMaterialNo,Memo,FPakCoding,FPakName,FPaktotal,FPakaccounted,FLevel,FCost,FOtherfees,FLiquor,FUnitprice,FAntifakeprice,FBaseprice,FSanjiucost,Ffinalprice,FTax,FGross,FOthercost,FAccounted,FPeoCost,FQuality,FWineprice,FMfees,FProfit,FAmount,ShowDo)values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
String sql3=" Insert into T_RIC_cejia_Item( FID,FName,FMaterial,FSpecifications,FSupplier,FNum,FPrice,FMeasuring) values(?,?,?,?,?,?,?,?)";
dbConn.setAutoCommit(false);
ps = dbConn.prepareStatement(sql);
ps2 = dbConn.prepareStatement(sql3);
v_view = db.getView("v_list_mainform"); //主表单视图
vc_vec = v_view.getAllEntries();
ve1 = vc_vec.getFirstEntry();
int i=1;
int y=1;
v_view2 = db.getView("v_list_BaoZhuangWuDoc"); //行项目视图
views = db.getView("v_key_sapNo"); //sap物料号视图
while(ve1 != null){
tdoc = ve1.getDocument();
///获取数据
//System.out.println("编号 =" + tdoc.getItemValueString("REQUESTNUMBER"));
String FCode = tdoc.getItemValueString("REQUESTNUMBER");
String FDraftsman = tdoc.getItemValueString("CallShortName");
String FProductName = tdoc.getItemValueString("productName");
String FProlevel = tdoc.getItemValueString("JiuZhiYaoQiu");
String FBrand = tdoc.getItemValueString("ShengBaoDanWei");
String FProdegree = tdoc.getItemValueString("DuShu");
String FSpecification = tdoc.getItemValueString("GuiGe")+"*"+
tdoc.getItemValueString("GuiGe2")+"*"+ tdoc.getItemValueString("GuiGe3");
String FMaterialfactory = tdoc.getItemValueString("BaoCaiGongChang");
String Wuliao = tdoc.getItemValueString("MAKTX");
tdoc2 = views.getDocumentByKey(Wuliao,true);
if(tdoc2!=null)
{
String FMaterialNo = tdoc2.getItemValueString("MATNR"); ///sap物料号
String Ffactory = tdoc2.getItemValueString("WERKS"); ///工厂编号
String Memo = tdoc.getItemValueString("Mome");
String FPakCoding = tdoc.getItemValueString("BCTNo");
String FPakName = tdoc.getItemValueString("BCTName");
String FPaktotal = tdoc.getItemValueString("bczj");
String FPakaccounted = tdoc.getItemValueString("bczb");
String FLevel = tdoc.getItemValueString("Manpower");
String FCost = tdoc.getItemValueString("ManpowerRate");
String FOtherfees = tdoc.getItemValueString("ManpowerRateOther");
String FLiquor = tdoc.getItemValueString("CountRate");
String FUnitprice = tdoc.getItemValueString("bczj");
String FAntifakeprice = tdoc.getItemValueString("BCFWFee");
String FBaseprice = tdoc.getItemValueString("RankPrice");
String FSanjiucost = tdoc.getItemValueString("SanJiuPrice");
String Ffinalprice = tdoc.getItemValueString("ShouJia");
String FTax = tdoc.getItemValueString("Tax");
String FGross = tdoc.getItemValueString("MaoLiLv");
String FOthercost =
"介质折盒费 0.02元 水电费 0.01元 低易办公 0.01元 其他(含折旧、修理)0.01元 杂件 0.01 合计 0.06元 (元/瓶)";
String FAccounted = tdoc.getItemValueString("bczb");
String FPeoCost = tdoc.getItemValueString("ManpowerRate");
String FQuality = tdoc.getItemValueString("CountRate");
String FWineprice = tdoc.getItemValueString("NiangJiuPrice");
String FMfees = tdoc.getItemValueString("ZaFei");
String FProfit = tdoc.getItemValueString("MaoLi"); //包材物名称
String FAmount = tdoc.getItemValueString("ShouJia"); //材质
System.out.println("意见 =" + tdoc.getItemValueString("HowDo_Crouse"));
String showDo = tdoc.getItemValueString("HowDo_Crouse"); //办理意见
///插入数据
ps.setInt(1, i);
ps.setString(2, FCode);
ps.setString(3, FDraftsman);
ps.setString(4, FProductName);
ps.setString(5, FProlevel);
ps.setString(6, FBrand);
ps.setString(7, FProdegree);
ps.setString(8, FSpecification);
ps.setString(9, FMaterialfactory);
ps.setString(10, Ffactory);
ps.setString(11, FMaterialNo);
ps.setString(12, Memo);
ps.setString(13, FPakCoding);
ps.setString(14, FPakName);
if(!FPaktotal.equals(""))
{
ps.setDouble(15, Double.parseDouble(FPaktotal));
}
else
{
ps.setDouble(15,0.00);
}
if(!FPakaccounted.equals(""))
{
ps.setDouble(16, Double.parseDouble(FPakaccounted));
}
else
{
ps.setDouble(16,0.00);
}
ps.setString(17, FLevel);
if(!FCost.equals(""))
{
ps.setDouble(18, Double.parseDouble(FCost));
}
else
{
ps.setDouble(18,0.00);
}
if(!FOtherfees.equals(""))
{
ps.setDouble(19, Double.parseDouble(FOtherfees));
}
else
{
ps.setDouble(19,0.00);
}
if(!FLiquor.equals(""))
{
ps.setDouble(20, Double.parseDouble(FLiquor));
}
else
{
ps.setDouble(20,0.00);
}
if(!FUnitprice.equals(""))
{
ps.setDouble(21, Double.parseDouble(FUnitprice));
}
else
{
ps.setDouble(21,0.00);
}
if(!FAntifakeprice.equals(""))
{
ps.setDouble(22, Double.parseDouble(FAntifakeprice));
}
else
{
ps.setDouble(22,0.00);
}
if(!FBaseprice.equals(""))
{
ps.setDouble(23, Double.parseDouble(FBaseprice));
}
else
{
ps.setDouble(23,0.00);
}
if(!FSanjiucost.equals(""))
{
ps.setDouble(24, Double.parseDouble(FSanjiucost));
}
else
{
ps.setDouble(24,0.00);
}
if(!Ffinalprice.equals(""))
{
ps.setDouble(25, Double.parseDouble(Ffinalprice));
}
else
{
ps.setDouble(25,0.00);
}
if(!FTax.equals(""))
{
ps.setDouble(26, Double.parseDouble(FTax));
}
else
{
ps.setDouble(26,0.00);
}
if(!FGross.equals(""))
{
ps.setDouble(27, Double.parseDouble(FGross));
}
else
{
ps.setDouble(27,0.00);
}
ps.setString(28, FOthercost);
if(!FAccounted.equals(""))
{
ps.setDouble(29, Double.parseDouble(FAccounted));
}
else
{
ps.setDouble(29,0.00);
}
if(!FPeoCost.equals(""))
{
ps.setDouble(30, Double.parseDouble(FPeoCost));
}
else
{
ps.setDouble(30,0.00);
}
if(!FQuality.equals(""))
{
ps.setDouble(31, Double.parseDouble(FQuality));
}
else
{
ps.setDouble(31,0.00);
}
if(!FWineprice.equals(""))
{
ps.setDouble(32, Double.parseDouble(FWineprice));
}
else
{
ps.setDouble(32,0.00);
}
if(!FMfees.equals(""))
{
ps.setDouble(33, Double.parseDouble(FMfees));
}
else
{
ps.setDouble(33,0.00);
}
if(!FProfit.equals(""))
{
ps.setDouble(34, Double.parseDouble(FProfit));
}
else
{
ps.setDouble(34,0.00);
}
if(!FAmount.equals(""))
{
ps.setDouble(35, Double.parseDouble(FAmount));
}
else
{
ps.setDouble(35,0.00);
}
ps.setString(36, showDo);
ps.addBatch();
tdoc2.recycle();
插入行项目数据 通过KEYUNID拿到ParentID
System.out.println("ParentID =" + tdoc.getItemValueString("KEYUNID"));
String ParentID = tdoc.getItemValueString("KEYUNID");
vc_vec2 = v_view2.getAllEntriesByKey(ParentID,true);
ve3 = vc_vec2.getFirstEntry();
while(ve3 != null){
tdoc3=ve3.getDocument();
System.out.println("编号 =" + tdoc3.getItemValueString("BaoZhuangYuanSu"));
String FName = tdoc3.getItemValueString("BaoZhuangYuanSu"); //包材物名称
String FMaterial = tdoc3.getItemValueString("CaiZhi"); //材质
String FSpecifications = tdoc3.getItemValueString("GuiGeBianHao"); //规格/编号
String FSupplier = tdoc3.getItemValueString("VendorBianHao_Show"); //供应商
String FNum = tdoc3.getItemValueString("shuliang"); //数量
String FPrice = tdoc3.getItemValueString("BaoJia"); //报价
String FMeasuring = tdoc3.getItemValueString("CeSuanJia"); //测价
///插入数据
ps2.setInt(1, y);
ps2.setString(2, FName);
ps2.setString(3, FMaterial);
ps2.setString(4, FSpecifications);
ps2.setString(5, FSupplier);
ps2.setString(6, FNum);
ps2.setString(7, FPrice);
ps2.setString(8, FMeasuring);
ps2.addBatch();
tdoc3.recycle();
ve4 = vc_vec2.getNextEntry(ve3);
ve3.recycle();
ve3 = ve4;
y++;
}
}
tdoc.recycle();
ve2 = vc_vec.getNextEntry(ve1);
ve1.recycle();
ve1 = ve2;
i++;
}
ps.executeBatch();
ps2.executeBatch();
dbConn.commit();
}
catch(Exception e){
e.printStackTrace();
throw e;
}
finally{
if(ps != null)ps.close();
if(ps2 != null)ps2.close();
if(st != null)st.close();
if(dbConn != null)dbConn.close();
if(tdoc2 != null) {tdoc2.recycle();}
if(tdoc3 != null) {tdoc3.recycle();}
if(tdoc != null) {tdoc.recycle();}
if(ve2 !=null) ve2.recycle();
if(ve1 !=null) ve1.recycle();
if(vc_vec !=null) vc_vec.recycle();
if(ve4 !=null) ve4.recycle();
if(ve3 !=null) ve3.recycle();
if(vc_vec2 !=null) vc_vec2.recycle();
if(views !=null) views.recycle();
if(v_view !=null) v_view.recycle();
if(v_view2 !=null) v_view2.recycle();
}
}
}