lotus Domino中java代理通过JDBC与关系数据库集成(事务回滚例子)

import lotus.domino.*;
import java.util.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.*;
import java.math.*;
import java.io.PrintWriter;
import java.util.Date;
import java.sql.Statement;
import java.text.SimpleDateFormat;

public class JavaAgent extends AgentBase {
    String StrNoNew="";//新的字符串
    String No="";//简称
    String SPString="//.";//拆分答
    String l="1";//指定第几个数组
    String IntStr=""; //指定第几个数组的数据
    String Level="";//部门级别
    String ParentStr="";//上级数据
    String JTcode="";//集团数据
    String Comcode="";//公司数据
    String FullnameStr="";//公司数据
 

 public void NotesMain() {
  
  Connection con = null;
  ResultSet res = null;
  ResultSet ress = null; 
  try {
   Session session = getSession();
   AgentContext agentContext = session.getAgentContext();
   Agent agent = agentContext.getCurrentAgent();
   Database db = agentContext.getCurrentDatabase();
   //Document doc = agentContext.getDocumentContext();
   
   //获取主代理的doc文档信息
   String paramid = agent.getParameterDocID();
   Document doc = db.getDocumentByID(paramid); 
   
   Database sysdb =session.getDatabase(db.getServer(),"XXdata/XXXXX.nsf");
   String ServerName=""; //JDBC连接服务器
   String UserName=""; //JDBC连接用户名
   String UserPWD=""; //JDBC连接密码
   String dbURL=""; //JDBC连接的URL
   String JDBCSQL=""; //JDBC的SQL语句
   //得到SRM连接信息
   
   View JDBCview = sysdb.getView("v_key_f_sysdictionary");
   Document JDBCdoc = JDBCview.getDocumentByKey("DCMS中间库链接URL");
   
   if(JDBCdoc!=null){
     dbURL = JDBCdoc.getItemValueString("itemvalue");
   }
   JDBCdoc = JDBCview.getDocumentByKey("中间库链接用户名");
   if(JDBCdoc!=null){
     UserName = JDBCdoc.getItemValueString("itemvalue");
   }
   JDBCdoc = JDBCview.getDocumentByKey("中间库链接密码");
   if(JDBCdoc!=null){
     UserPWD = JDBCdoc.getItemValueString("itemvalue");
   }
   Class.forName("oracle.jdbc.driver.OracleDriver");
      
   con=DriverManager.getConnection(dbURL,UserName,UserPWD);
   con.setAutoCommit(false);   //禁止自动提交,设置回滚点
   
   
   Date dateNow=new Date();
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     String strdateNow = sdf.format(dateNow);
     
     int AllOrderNum; //拆分订单总数
     int LockSum;  //锁住文档数
     int PassSum;  //拆分订单通过数量
        int BackSum;  //拆分订单退回起草(不通过)数量
        int InsertSum;  //写入过中间库数量
        AllOrderNum=0;
        LockSum=0;
        PassSum=0;
        BackSum=0;
        InsertSum=0;
     
   //找相关拆分订单A>>>
   Document Odoc = null;
                View Oview=db.getView("v_all_ByOrderCode");
                  ViewEntryCollection Ocn = Oview.getAllEntriesByKey(doc.getItemValueString("OrderCode"), false);
                   ViewEntry Oentry = null;
                    if(Ocn.getCount()>0){
                    AllOrderNum = Ocn.getCount();
                       for(int ii = 1; ii <= Ocn.getCount(); ii++){                         
                Oentry = Ocn.getNthEntry(ii);
                           if (Oentry!=null){
                              if (Oentry.isValid()){
                               Odoc = Oentry.getDocument();                
                    if (Odoc != null) {  
                     //找相关拆分订单B>>>                     
                                          
                     //如果发现文档被锁住,则解锁
                     if (Odoc.hasItem("$Writers")){
                      LockSum += 1;
                     }

                     
                     //如果评审通过则PassSum+1
                     if(Odoc.getItemValueString("IsFromDCMS").equals("1")==true && ((Odoc.getItemValueString("tLiuCheng_Name").equals("内销特单流程(有资料)")==true && Double.parseDouble(Odoc.getItemValueString("Status_Num"))>8) || (Odoc.getItemValueString("tLiuCheng_Name").equals("内销特单流程(B2B有资料)")==true && Double.parseDouble(Odoc.getItemValueString("Status_Num"))>5)) ){
                      PassSum += 1;
                     }                                                                                                             
                     
                     //如果评审不通过则BackSum+1              
                     if(Odoc.getItemValueString("OrderDelType").equals("1")==true){
                      BackSum += 1;
                     }
                     
                     //如果mainform文档有写入中间库标致(通过或取消)
                     if(Odoc.getItemValueString("InsertType").equals("1")==true || Odoc.getItemValueString("InsertQuXiaoType").equals("1")==true){
                      InsertSum += 1;
                     }
                     
        //找相关拆分订单B<<<
                    }
                               }
                           }
                       } 
                    }
                    //找相关拆分订单A<<<<
                   
               if(LockSum==0 && InsertSum==0 && AllOrderNum>0 && (PassSum+BackSum)==AllOrderNum){            
                     System.out.println("写入订单信息!单号:"+doc.getItemValueString("REQUESTNUMBER").trim()+"<<-BEGIN->>");
     
                 //申请单头
     String InsertSQL_H = "";
     String InsertDate = "";   //写入时间
     String SERIALNUMBER = ""; //接口流水号  
     String OrderCode = "";   //订单编号
     String Attr22 = "";   //备注信息
     String InsertMarkStr = "";  //写入成功或失败信息
     String InsertMarkItem = "";  //写入成功或失败标记字段名
   
     InsertDate = strdateNow;
     SERIALNUMBER = strdateNow+doc.getItemValueString("OrderCode").trim();
     OrderCode = doc.getItemValueString("OrderCode").trim();
     
     InsertSQL_H = "";
          InsertSQL_H = InsertSQL_H + "insert into ZOPPORTORDFROMOA (PORTID,PortStatus,InsertDate,SERIALNUMBER,OrderCode,Attr22,Status)";
     InsertSQL_H = InsertSQL_H + " VALUES (";   
     InsertSQL_H = InsertSQL_H + "ZOPPORTORDFROMOASEQ.nextval" + ",'";
     InsertSQL_H = InsertSQL_H + "40" + "',";
     InsertSQL_H = InsertSQL_H + "sysdate" +",'";   
     InsertSQL_H = InsertSQL_H + SERIALNUMBER + "','";
     InsertSQL_H = InsertSQL_H + OrderCode + "','";     
     
     Statement stmt;
                     stmt = con.createStatement();
     
                if(PassSum==0){
     //所有拆分订单不通过    
     
     InsertSQL_H = InsertSQL_H + Attr22 + "','";  //备注
     InsertSQL_H = InsertSQL_H + "4" + "')";   //未通过:4,通过:16
   
     PreparedStatement ps1 = con.prepareStatement(InsertSQL_H);
     stmt.executeUpdate(InsertSQL_H);  
   
     con.commit(); //事务提交        
     InsertMarkStr = "写入取消订单信息成功!";
     InsertMarkItem = "InsertQuXiaoType";

                }else{
                 //找相已经通过审核的关拆分订单A>>>
   Document Pdoc = null;
                View Pview=db.getView("v_all_ByOrderCode_Pass");
                  ViewEntryCollection Pcn = Oview.getAllEntriesByKey(doc.getItemValueString("OrderCode"), false);
                   ViewEntry Pentry = null;
                    if(Pcn.getCount()>0){
                       for(int kk = 1; kk <= Pcn.getCount(); kk++){
                Pentry = Pcn.getNthEntry(kk);
                           if (Pentry!=null){
                              if (Pentry.isValid()){
                               Pdoc = Pentry.getDocument();                
                    if (Pdoc != null) {  
                     //找相已经通过审核的关拆分订单B>>>
                               
   //找行项目A>>>
   Document cdoc = null;
                View viewa=db.getView("v_list_SubDoc");
                  ViewEntryCollection dcn = viewa.getAllEntriesByKey(Pdoc.getItemValueString("KEY_UNID"), false);
                   ViewEntry entry = null;
                    if(dcn.getCount()>0){
                       for(int jj = 1; jj <= dcn.getCount(); jj++){
                entry = dcn.getNthEntry(jj);
                           if (entry!=null){
                              if (entry.isValid()){
                               cdoc = entry.getDocument();                
                    if (cdoc != null) { 
                     //找行项目B>>>
   String InsertSQL = "";
   
   String LineNum = cdoc.getItemValueString("LineNum").trim();  
   String ProdCode = cdoc.getItemValueString("WuLiao").trim();  
   String OrderQty = cdoc.getItemValueString("Amount").trim();  
   String DeliveryDate = cdoc.getItemValueString("SubDeliveryDate").trim();
   
   //如果行项目的备注不为空
   if(cdoc.getItemValueString("note").trim().equals("")==false){ 
    Attr22 = Attr22 + LineNum + "_" + ProdCode + "_" + cdoc.getItemValueString("note").trim() + "<br>";  //备注
   }
   
   
   //判断是否有打删除标记
   String StrDelType="";
   if(cdoc.getItemValueString("DelType").trim().equals("1")==true){
    StrDelType="0";
   }else{
    StrDelType="10";
   }

   InsertSQL = "";
        InsertSQL = InsertSQL + "insert into ZOPPORTORDDETAILFROMOA (PORTDETAILID,SERIALNUMBER,OrderCode,LineNum,linestatus,ProdCode,OrderQty,DeliveryDate)";
   InsertSQL = InsertSQL + " VALUES (";   
   InsertSQL = InsertSQL + "ZOPPORTORDDETAILFROMOASEQ.nextval" + ",'";
   InsertSQL = InsertSQL + SERIALNUMBER + "','";
   InsertSQL = InsertSQL + OrderCode + "','";
   InsertSQL = InsertSQL + LineNum + "','";
   InsertSQL = InsertSQL + StrDelType + "','";   //默认“10”,行关闭“0”
   InsertSQL = InsertSQL + ProdCode + "','";
   InsertSQL = InsertSQL + OrderQty + "',";   
   InsertSQL = InsertSQL + "to_date('"+DeliveryDate+"','yyyy-mm-dd'))"; 
   //System.out.println(InsertSQL);
   PreparedStatement ps2 = con.prepareStatement(InsertSQL);     
   stmt.executeUpdate(InsertSQL);   
             //找行项目B<<<<              
                    }
                               }
                           }
                       } 
                    }
                    //找行项目A<<<<
                             
                     //找相已经通过审核的关拆分订单B<<<
                    }
                               }
                           }
                       } 
                    }
                    //找相已经通过审核的关拆分订单A<<<<
                   
                   
                    InsertSQL_H = InsertSQL_H + Attr22 + "','";  //备注    
                InsertSQL_H = InsertSQL_H + "16" + "')";   //未通过:4,通过:16
   
    PreparedStatement ps1 = con.prepareStatement(InsertSQL_H);  
                   
    stmt.executeUpdate(InsertSQL_H); 
            
    con.commit(); //事务提交
    InsertMarkStr = "写入订单信息成功!";
    InsertMarkItem = "InsertType";
    
                }
                
                
                //如果InsertMarkStr值不为空,说明写入中间库(评审通过或不通过)信息成功!>>>>>>>>>
                if(InsertMarkStr.equals("")==false){
                 //写入成功后,再次查找相关的拆分订单,将成功标志写入到各订单!
   //找相关需要写入成功标志的拆分订单A>>>
   Document Idoc = null;
                View Iview=db.getView("v_all_ByOrderCode_Pass");
                  ViewEntryCollection Icn = Oview.getAllEntriesByKey(doc.getItemValueString("OrderCode"), false);
                   ViewEntry Ientry = null;
                    if(Icn.getCount()>0){
                       for(int ll = 1; ll <= Icn.getCount(); ll++){
                Ientry = Icn.getNthEntry(ll);
                           if (Ientry!=null){
                              if (Ientry.isValid()){
                               Idoc = Ientry.getDocument();                
                    if (Idoc != null) {  
                     //找相关需要写入成功标志的拆分订单B>>>
                     
                     System.out.println(InsertMarkStr+"单号:"+Idoc.getItemValueString("REQUESTNUMBER"));
                                          
                     Idoc.replaceItemValue(InsertMarkItem,"1");    //写入通过或取消标志
                     Idoc.save(true,false);
                     //System.out.println(InsertMarkItem+":"+Idoc.getItemValueString(InsertMarkItem));
                     //找相关需要写入成功标志的拆分订单B<<<
                    }
                               }
                           }
                       } 
                    }
                    //找相关需要写入成功标志的拆分订单A<<<<
                }
                //如果InsertMarkStr值不为空,说明写入中间库(评审通过或不通过)信息成功!<<<<<<<<<
                
                
               }
              
   
   
  } catch(Exception e) {
   e.printStackTrace();
   try {
                    con.rollback(); //操作不成功则回滚
                    System.out.println("写入订单信息失败!");

           }catch(Exception ex) {
    ex.printStackTrace();
            }
  }finally {
   try {
    if (res != null) {
     res.close();
     res = null;
    }
    if (con != null) {
     con.close();
     con = null;
    }
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
 }
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值