java 访问Domino LOtus 数据库

mport lotus.domino.*;
import java.util.Properties;
import java.util.Vector;
import java.sql.*;
import java.io.*;
import pde.dao.ConnectDB;
import pde.FtpUpfile;

public class ExportNotesData {
    InitConfig initconfig = null ;
    ConnectDB connectDB = null ;
    Session LotusSession = null ;
    Database db = null ;
    View view = null ;
    Document doc = null ;
    String I_STORAGEID = "" ;
    String TblID = "" ;
    String AmsFields = "" ;
    String AmsFieldValues = "" ;
    String RecID = "" ;
    String C_YWID = "" ;
    String IdeaGUIDFileName = "";
    String tmpFilePath = System.getProperty("user.dir")+" \\tempFiles\\" ;
    int YWIndex = 0 ;
    FtpUpfile ftpUpfile = null ;
 

    public static void main(String[] args)
    {
        ExportNotesData eNotesData = new ExportNotesData();
    }
    public ExportNotesData()
    {
        initconfig = new InitConfig();
        int ExpRecCount = 0 ;
        try
        {
            System.out.println(tmpFilePath) ;
            java.io.File myFilePath = new java.io.File(tmpFilePath);
            if (!myFilePath.exists()) {
                myFilePath.mkdir();
            }
            ftpUpfile = new FtpUpfile(initconfig.FTPServer, Integer.parseInt(initconfig.FTPPort), initconfig.FTPUser, initconfig.FTPPwd) ;
            ftpUpfile.login() ;
            connectDB = new ConnectDB(initconfig.DBDriverClass, initconfig.DBDriverUrl,
                                      initconfig.DBUser, initconfig.DBPassword) ;
            TblID = connectDB.getFieldValue("SYS_TBL", "ID", "where C_TYPE = 'USET' and C_TBLNAME = '"+initconfig.DBTable+"'") ;
            I_STORAGEID = connectDB.getFieldValue("sys_ftp_http", "ID", "where i_default = 1") ;
            LotusSession = getSession(initconfig.LotusServer, initconfig.LotusDIIOPPort,
                                      initconfig.LotusUser, initconfig.LotusPwd);
            System.out.println(LotusSession+":"+initconfig.LotusServer+":"+initconfig.LotusUser) ;

            db = LotusSession.getDatabase(initconfig.LotusServer, initconfig.LotusDataBase);
            view = db.getView(initconfig.LotusView) ;
            Document doc = view.getFirstDocument();
            Item uitem = null;
            while (doc != null)
            {
                AmsFields = "" ;
                AmsFieldValues = "" ;
                C_YWID = connectDB.getCreateGUID() ;
                ExpRecCount += 1 ;
                YWIndex = 0 ;
                RecID = (connectDB.getMaxID(initconfig.DBTable) + 1) +"" ;
                System.out.println("导出记录数: "+ExpRecCount+"-----------DB记录ID: "+RecID+"---------") ;
                String IdeaFileName =  "意见列表.html" ;
                IdeaGUIDFileName = getGUIDFileName(IdeaFileName) ;
                Vector AllItems = doc.getItems();      //获得域名
                for (int i =0 ; i < AllItems.size(); i++)
                {
                    uitem = (Item)AllItems.get(i) ;
                    ProcesItems(doc, uitem) ;
                }
                AmsFields = AmsFields + "原文数量,C_YWID" ;
                AmsFieldValues = AmsFieldValues + YWIndex +",'"+C_YWID+"'" ;
                connectDB.SaveInfo("\""+initconfig.DBTable+"\"", AmsFields, AmsFieldValues) ;
                ftpUpfile.ftpclient.ascii(); //上传完文件必须执行
                doc = view.getNextDocument(doc);
            }
            connectDB.CloseCon() ;
            ftpUpfile.logout() ;
        }
        catch(Exception e)
        {
            e.printStackTrace() ;
        }

    }

//连接远程主机
    public Session getSession(String sip,String port, String username,String password)
    {
        lotus.domino.Session s;
        String sior;
        try
        {
            sior = NotesFactory.getIOR(sip+":"+port);
            s = NotesFactory.createSessionWithIOR(sior, username, password);
        }
        catch (Exception ex)
        {
            s = null;
        }
        return s;
    }


    public void ProcesItems(Document doc, Item uitem)
    {
        RichTextItem ruitem = null;
        try
        {
            String itemName = uitem.toString() ;
            int itemType = uitem.getType() ;
            String itemValue = "" ;

            switch(itemType) {
               case Item.TEXT:         //1280
               {
                       int itemIndex = initconfig.LotusFields.indexOf(itemName) ;    //判断是否是要导的域
                       if (itemIndex > -1)
                       {
                           if (doc.getItemValue(itemName).size() > 0)
                               itemValue = ObjToStr(doc.getItemValue(itemName).get(0)) ;
                           String uAmsField = ObjToStr(initconfig.FieldAttrs.get(itemName)) ;
                           AmsFields += uAmsField+"," ;
                           AmsFieldValues += "'"+itemValue+"',";
                       }
                       break;
               }
               case Item.RICHTEXT:     //1
               {
                    ProcesRichTextItem(doc, uitem) ;
                    break;
               }
               default:
               {
                    break;
               }
            }
        }
        catch(Exception e)
        {
            e.printStackTrace() ;
        }

    }

 

    public void  ProcesRichTextItem(Document doc, Item uitem)
    {
        try
        {
            EmbeddedObject eo = null;
            java.io.InputStream is = null;
            RichTextItem ruitem = (RichTextItem)uitem ;

            if (uitem.toString().equals("RCover"))
            {
                RichTextNavigator rtnav = ruitem.createNavigator() ;
                if (rtnav.getFirstElement(RichTextItem.RTELEM_TYPE_DOCLINK) != null)
                {
                    do
                    {
                          Document uDoc = getLinkDocument(rtnav) ;
                          Item uUitem = null;
                          Vector uAllItems = uDoc.getItems();      //获得域名
                          for (int i =0 ; i < uAllItems.size(); i++)
                          {
                              uUitem = (Item)uAllItems.get(i) ;
                              ProcesChildItems(uDoc, uUitem) ;
                          }
                          String uFormName = uDoc.getItemValueString("Form") ;
                          String uDocID = uDoc.getUniversalID() ;
                          String uDate = "to_date('"+connectDB.getCurrentDate()+"','yyyy-MM-dd HH24:mi:ss')" ;
                          String uValues = "'"+C_YWID+"', '"+uFormName+"', '"+uDocID+"', "+uDate ;
                          connectDB.SaveInfo("OA_IMPDOCIDS", "YWID,FORMNAME,DOCID,IMPDATETIME", uValues) ;
                    }
                    while(rtnav.findNextElement()) ;
                }
                else
                    System.out.println("没有doclink 在 RCover") ;
            }

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


    /*
     *
    */
    public void ProcesChildItems(Document doc, Item uitem)
    {
        try
        {
            String itemName = uitem.toString() ;
            int itemType = uitem.getType() ;
            String itemValue = "" ;

            switch(itemType) {

               case Item.TEXT:         //1280
               {
                       if (doc.getItemValue(itemName) != null && doc.getItemValue(itemName).size() > 0)
                       {
                           itemValue = ObjToStr(doc.getItemValue(itemName).get(0)) ;
                           //判断是否是流程跟踪
                           int itemIndex = initconfig.LotusFlowTrackFields.indexOf(itemName) ;
                           if (itemIndex > -1)
                           {
                               if (!itemValue.trim().equals(""))
                               {
                                   SaveFlowTrack(itemValue) ;
                               }
                           }
                           else if (!itemName.equals("TDispInnerUsers") && !itemName.equals("sManuTemplateFields"))   //判断不写入的域
                           {
                               String uValues = "'"+C_YWID+"', '"+itemName+"', '"+itemValue+"', 1" ;
                               connectDB.SaveInfo("OA_IMPDOCCONTENTS", "YWID, FIELDNAME, FIELDVALUE, IORDER", uValues) ;
                           }
                       }
                       break;
               }
               case Item.RICHTEXT:     //1
               {
                    ProcesChildRichTextItem(doc, uitem) ;
                    break;
               }
               default:
               {
                    break;
               }
            }
        }
        catch(Exception e)
        {
            e.printStackTrace() ;
        }

    }

public void  ProcesChildRichTextItem(Document doc, Item uitem)
    {
        try
        {
            EmbeddedObject eo = null;
            java.io.InputStream is = null;
            RichTextItem ruitem = (RichTextItem)uitem ;
            if ( ruitem.getEmbeddedObjects().size() > 0)
            {
                String YWName = ruitem.getText().substring(3) ;
                eo = doc.getAttachment(YWName);
                 if(eo != null)
                 {
                     is = eo.getInputStream();
                     String YWSize = eo.getFileSize()+"";
                     if(is.available()>0) {
                            String YWType = getYWType(ruitem.toString()) ;
                            String UpLoadDate = "to_date('"+connectDB.getCurrentDate()+"','yyyy-MM-dd HH24:mi:ss')" ;
                            String UpLoadMan = "中软OA" ;
                            String uGUIDFileName = getGUIDFileName(YWName) ;
                            String YWFields = "C_YWID,C_TYPE,C_FILENAME,F_SIZE,C_UPLOADEMP,C_UPLOADDATE,I_STORAGEID,C_GUIDNAME,I_ORDER" ;
                            String YWValues = "'"+C_YWID+"','"+YWType+"','"+YWName+"','"+YWSize+"','"+UpLoadMan+"',"+UpLoadDate+",'"+I_STORAGEID+"','"+uGUIDFileName+"',"+YWIndex ;
                            connectDB.SaveInfo("SYS_LINK", YWFields, YWValues) ;
                            ftpUpfile.upFile(is, getFTPPath()+uGUIDFileName) ;
                            is.close() ;
                            YWIndex += 1 ;
                            if (ruitem.toString().equals("zwnr"))
                            {
                                String luFile = tmpFilePath+YWName ;
                                eo.extractFile(luFile) ;
                                RemoveDocTag(luFile) ;
                                File file = new File(luFile) ;
                                if (file.exists())
                                {
                                    YWSize = file.length()+"" ;
                                    YWType = "正文" ;
                                    uGUIDFileName = getGUIDFileName(YWName) ;
                                    UpLoadDate = "to_date('"+connectDB.getCurrentDate()+"','yyyy-MM-dd HH24:mi:ss')" ;
                                    YWFields = "C_YWID,C_TYPE,C_FILENAME,F_SIZE,C_UPLOADEMP,C_UPLOADDATE,I_STORAGEID,C_GUIDNAME,I_ORDER" ;
                                    YWValues = "'"+C_YWID+"','"+YWType+"','"+uGUIDFileName+"','"+YWSize+"','"+UpLoadMan+"',"+UpLoadDate+",'"+I_STORAGEID+"','"+uGUIDFileName+"',"+YWIndex ;
                                    connectDB.SaveInfo("SYS_LINK", YWFields, YWValues) ;
                                    ftpUpfile.upFile(tmpFilePath+YWName, getFTPPath()+uGUIDFileName) ;
                                    YWIndex += 1 ;
                                    //file.delete() ;
                                }
                            }
                     }
                 }
            }
        }
        catch(Exception e)
        {
            e.printStackTrace() ;
        }
    }

    public void SaveFlowTrack(String FlowTrack)
    {
        try
        {
            String YWType = "流程跟踪" ;
            String YWName = "流程跟踪.html" ;
            String UpLoadMan = "中软OA" ;
            String UpLoadDate = "to_date('"+connectDB.getCurrentDate()+"','yyyy-MM-dd HH24:mi:ss')" ;
            String uGUIDFileName = getGUIDFileName(YWName) ;
            String YWFields = "C_YWID,C_TYPE,C_FILENAME,F_SIZE,C_UPLOADEMP,C_UPLOADDATE,I_STORAGEID,C_GUIDNAME,I_ORDER" ;
            String YWValues = "'"+C_YWID+"','"+YWType+"','"+uGUIDFileName+"','100','"+UpLoadMan+"',"+UpLoadDate+",'"+I_STORAGEID+"','"+uGUIDFileName+"',"+YWIndex ;
            File file = new File(tmpFilePath+YWName) ;
            if (!file.exists())
                file.createNewFile();
            PrintWriter IdeaPrint = new PrintWriter(new FileWriter(tmpFilePath+YWName, true)) ;
            IdeaPrint.write(FlowTrack) ;
            IdeaPrint.close() ;
            connectDB.SaveInfo("SYS_LINK", YWFields, YWValues) ;
            ftpUpfile.upFile(tmpFilePath+YWName, getFTPPath()+uGUIDFileName) ;
            file.delete() ;
            YWIndex += 1 ;
        }
        catch(Exception e)
        {
            e.printStackTrace() ;
        }
    }

    public void DeleteFile(String uFile)
    {
        try
        {
            File file = new File(uFile) ;
            if (file.exists())
            {
               file.delete() ;
            }
        }
        catch(Exception e)
        {
            e.printStackTrace() ;
        }
    }

    public String getGUIDFileName(String YWName)
    {
        try
        {
             String uFileExt = YWName.substring(YWName.lastIndexOf(".")+1, YWName.length()) ;
             String myGUID = connectDB.getCreateGUID() ;
             int tmpInt = Integer.parseInt(RecID) / 2000 + 1 ;
             String uGUIDNAME = myGUID+"_"+RecID+"_"+TblID+"_"+tmpInt+"."+uFileExt ;
             return uGUIDNAME ;
        }
        catch(Exception e)
        {
            e.printStackTrace() ;
            return "" ;
        }
    }

    public String getNoPathGUIDFileName(String YWName)
    {
        try
        {
             String uFileExt = YWName.substring(YWName.lastIndexOf(".")+1, YWName.length()) ;
             String myGUID = connectDB.getCreateGUID() ;
             String uGUIDNAME = myGUID+"."+uFileExt ;
             return uGUIDNAME ;
        }
        catch(Exception e)
        {
            e.printStackTrace() ;
            return "" ;
        }
    }

    public String getFTPPath()
    {
        try
        {
             int tmpInt = Integer.parseInt(RecID) / 2000 + 1 ;
             String uFtpPath = "/"+TblID+"/"+tmpInt+"/" ;
             return uFtpPath ;
        }
        catch(Exception e)
        {
            e.printStackTrace() ;
            return "" ;
        }
    }
    public String getYWType(String itemName)
    {
        String YWType = "" ;
        if (itemName.equals("zwdg"))  YWType = "底稿" ;
        if (itemName.equals("zwnr"))  YWType = "定稿" ;
        if (itemName.startsWith("fjnr"))  YWType = "附件" ;
        return YWType ;
    }
    public String ObjToStr(Object obj)
    {
        String s = "" ;
        if (obj != null)
           s = obj.toString() ;
        return s ;
    }
 
    public void RemoveDocTag(String filePath)
    {
        Runtime rt=Runtime.getRuntime();
        try
        {
            rt.exec("RemoveDocTag.exe "+filePath);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(java 访问Domino LOtus 数据库)