用JAVA读取Domain的数据库

来源:互联网 发布:aegisub for mac字体 编辑:程序博客网 时间:2024/05/21 19:23
  去年,由于做一个项目是针对DOMINO的系统,所以抽出时间研究了一个这个当初是莲花公司的传家宝。
     其实,DOMINO有自己的LOTUSSRIPT,但是大家都用JAVA习惯了,当然它也提供了JAVA的接口,所以我们参考一下它的开发文档,写了一下JAVA操作DOMNIO的数据库的例子。



public class LotusReadDB {

  private static String user = "test";
  private static String passwd = "123456789";
  private static String dominoHost = "192.168.8.17";
 
  private static String TESTDB1="mail.box";//用户的邮箱数据库
  private static String TESTDB1VIEW="mail";//用户的邮箱视图
 
  private static String TESTDB2="mtdata/mtstore.nsf";//MTC邮件跟踪的数据库
  private static String TESTDB2VIEW="alluser";

  Vector rowDataParent=new Vector();
 
  Vector columnNames=new Vector();
 
 Database db=null;
 
  /**
   * 取得数据库
   * @param accessDB
   * @return
  * @throws NotesException
   */
  Database getDB(String accessDB) throws NotesException{
      // String accessDB = "mail.box";
      /**数据库的路径是相对于Data目录下的
       *mtdata/mtstore.nsf
       *mail/songlb.nsf
       * **/

      lotus.domino.Session s = lotus.domino.NotesFactory.createSession(
          dominoHost, user, passwd);

     // System.out.println(s.getServerName() + " :" + s.getNotesVersion() +
                        // ":user " + s.getUserName());

      lotus.domino.Database db = s.getDatabase(null, accessDB);
      if (!db.isOpen())
      {
        db.open();
      }

      //打出数据库的标题和大小

      //System.out.println("title:" + db.getTitle() + "      size:" + db.getSize());
      return db;
  }
 
/**
   *  取某个数据库的某张表
   * @param accessDB String     数据库名称
   * @param accessView String     表名称
   */
  public  void getDBDatas(String accessDB,String accessView)
  {
    Vector rowDataSon=new Vector();
    try
    {


     db=this.getDB(accessDB);//得到数据库

      View view = (View) db.getView(accessView);//取得自己想要的MAIL视图

      System.out.print(view.getName() + "(");

      columnNames = view.getColumns(); //得到所有列名
     
      Document doc=view.getFirstDocument();
      StringBuffer strBuf=new StringBuffer();
     
     while(doc!=null){

         Vector values = doc.getColumnValues();

         for (int j = 0; j < columnNames.size(); j++) {
             String columnName = ((String) columnNames.elementAt(j) .toString()).trim();
            
             if (columnName.length() != 0) {
                 System.out.println("cloumn class:"+values.get(j).getClass());
                 ViewColumn column = (ViewColumn) columnNames.elementAt(j);
//                 strBuf=strBuf.append("/t[" + columnName + "]:");
//                 //strBuf=strBuf.append(column.getItemName());
//                 strBuf=strBuf.append(values.get(j).toString());
                 System.out.println("/t[" + columnName + "]:" + values.get(j));
                
             }
            
         }
         System.out.println(")");
        
         System.out
         .println("----------------------------------------------------------------");
        
        
//         for(int p=0;p<doc.getItems().size();p++){
//             Item item=(Item)doc.getItems().get(p);
//             System.out.println("*"+p+""+item.getName()+"------"+item.getValues());
//         }
        
        
        
         doc = view.getNextDocument(doc);
      }
     
     
     

     

   
     
//      Document docF = view.getFirstDocument();
//
//      Vector allItems = docF.getItems();
//      for (int t = 0; t < allItems.size(); t++)
//      {
//        System.out.println("********* " + t + " Start ***********");
//        Item item = (Item) allItems.get(t);
//        System.out.println("--item.getName():" + item.getName());
//        System.out.println("--item.getText():" + item.getText());
//        //System.out.println("--item.getValueString():" + item.getValueString());
//        System.out.println("********* " + t + " end ***********");
//        System.out.println("");
//        System.out.println("");
//      }
      System.out.println("");
      System.out.println("");

    }
    catch (lotus.domino.NotesException e)
    {
      e.printStackTrace();
     // System.out.println("Domino Server 可能没有启动!");
    }
    catch (Exception ex)
    {ex.printStackTrace();
      //System.out.println("Domino Server 可能没有启动!");
    }

  }
 
  /**
   * 取得所有行数据
   * @return
   */
  public Object[][] getRowData(){
      int row=rowDataParent.size();
      int col=columnNames.size();
      Object[][] temp=new Object[row][col];
      for(int i=0;i<row;i++){
              temp[i]=((Vector)rowDataParent.get(i)).toArray();
      }
      return temp;
  }
 
  public Object[] getColumnName(){
      return this.columnNames.toArray();
  }
 
  public LotusReadDB()
  {
  }
 
 
  /**
   * Example 得全部 document
   * @param db
 * @throws NotesException
 * @throws ClassNotFoundException
 * @throws IOException
   */
  public void getAllDocument(View view) throws NotesException, IOException, ClassNotFoundException{
      Document doc=view.getFirstDocument();
      int t=0;
      Item item=null;
      while(doc!=null){
          Vector vt=doc.getItems();
         // for(int i=0;i<vt.size();i++){
             
              //PostedDate
             
              item = doc.getFirstItem("PostedDate");
              if (item != null)
                System.out.println("PostedDate: "+"[ " + item.getText() + " ]");
             
             
            
              System.out.println(doc.getColumnValues().toString());
             

//              RichTextItem body =
//                    (RichTextItem)doc.getFirstItem("Body");
//              if (body == null)
//                System.out.println("No body");
//              else
//                System.out.println( body.getFormattedText(true, 0, 0));

             
         // }
         
          doc=view.getNextDocument(doc);
      }
  }
 
  /**
   * Example 得全部 document
   * @param db
   * @throws NotesException
   */
  public void getAllDocument(Database db) throws NotesException{
      DocumentCollection dc = db.getAllDocuments();
      Document doc = dc.getFirstDocument();
      while (doc != null) {
        System.out.println("******");
        System.out.println(
          doc.getItemValueString("Subject"));
        Enumeration e = doc.getAuthors().elements();
        while (e.hasMoreElements())
          System.out.println("Author: " + e.nextElement());
        System.out.println("Created: " + doc.getCreated());
        if (doc.isNewNote())
          System.out.println("Is new note");
        if (doc.isResponse())
          System.out.println("Parent UNID: " +               doc.getParentDocumentUNID());
        if (doc.isSigned()) {
          System.out.println("Signed by: " +               doc.getSigner());
          System.out.println("Verified by: " +               doc.getVerifier()); }
        System.out.println("Last accessed: " +               doc.getLastAccessed());
        System.out.println("Verified by: " +               doc.getLastModified());
        System.out.println("Note ID: " +               doc.getNoteID());
        System.out.println("Universal ID: " +    doc.getUniversalID());
        doc = dc.getNextDocument(); }   
  }
 
  /**
   * Example 得到指定View  的全部entry
   * @param view
   * @throws NotesException
   */
  public void getEntryByView(View view) throws NotesException{
      ViewNavigator nav = (ViewNavigator) view.createViewNav();
      String typeCol = null;
      ViewEntry entry = nav.getFirst();
      while (entry != null) {
        if (entry.isCategory()) typeCol = "category";
        else if (entry.isDocument()) typeCol = "document";
        else if (entry.isTotal()) typeCol = "total";
        System.out.println("Entry at position" +
        entry.getPosition('.') + " is a " + typeCol);
        System.out.println("/tIndent level: " +
          entry.getIndentLevel());
        System.out.println
        ("/tColumn indent level: " +
          entry.getColumnIndentLevel());
        System.out.println("/tNote ID: " + entry.getNoteID());
        if (entry.isDocument()) {
          System.out.println
          ("/tUniversal ID: " + entry.getUniversalID());
          System.out.println
          ("/tFT search score: " + entry.getFTSearchScore());
          if (entry.isConflict())
            System.out.println("/tConflict document");
          System.out.println("/tSubject: /"" +
          entry.getDocument().getItemValueString("Subject") +
            "/""); }
        if (entry.getParent() instanceof ViewNavigator)
          System.out.println("/tParent is ViewNavigator");
        else if (entry.getParent() instanceof
          ViewEntryCollection)
          System.out.println("/tParent is ViewEntryCollection");
        else System.out.println("/tParent is View");
        System.out.println("/tSibling count: " +
        entry.getSiblingCount());
        System.out.println("/tChild count: " +
        entry.getChildCount());
        System.out.println
        ("/tDescendant count: " + entry.getDescendantCount());
        if (entry.isValid()) System.out.println("/tEntry is Valid");
        else System.out.println("/tEntry is deletion stub");
        entry = nav.getNext(); }
  }
 
  /**
   * Example 得到指定数据的所有view
   * @param db
 * @throws NotesException
   */
  public void getAllView(Database db) throws NotesException{
      String title = db.getTitle();
      Vector views = db.getViews();
      int size = views.size();
      System.out.println("Database /"" + title +
      "/" has " + size + " views");
      for (int i = 0; i<size; i++) {
        String name =
           ((View)views.elementAt(i)).getName();
        System.out.println
        ("View #" + (i+1) + ": " + name); }   
  }
 
 
  /**
   * Example 得到指定数据的所有FORM
 * @throws NotesException
   *
   *
   */
  public void getAllForm(Database db) throws NotesException {

        Vector forms = db.getForms();
        System.out.println("Forms in /"" + db.getTitle() + "/":");
        for (int i = 0; i < forms.size(); i++) {
           
            Form form = (Form) forms.elementAt(i);
           
            System.out.println("/nForm name : " + form.getName());
           
            Vector alias = form.getAliases();
           
            if (alias.size() != 0) {
                System.out.println("/tAlias/t: " + alias.elementAt(0));
                for (int j = 1; j < alias.size(); j++)
                    System.out.println("/t /" /"/t: " + alias.elementAt(j));
            }
           
            System.out.println("/tProtect Readers/t: "
                    + form.isProtectReaders());
            System.out.println("/tProtect Users/t: " + form.isProtectUsers());
            System.out.println("/tSub Form     /t: " + form.isSubForm());
            Vector users = form.getFormUsers();
            if (users.size() != 0) {
                System.out.println("/tForm users/t: " + users.elementAt(0));
                for (int j = 1; j < users.size(); j++)
                    System.out.println("/t   /" /"      /t: "
                            + users.elementAt(j));
            }
            Vector readers = form.getReaders();
            if (readers.size() != 0) {
                System.out.println("/tForm Readers/t: " + readers.elementAt(0));
                for (int j = 1; j < readers.size(); j++)
                    System.out.println("/t   /"  /"       /t: "
                            + readers.elementAt(j));
            }
            Vector fields = form.getFields();
            if (fields.size() != 0) {
                System.out.println("/tFields     /t: " + fields.elementAt(0));
                for (int j = 1; j < fields.size(); j++)
                    System.out.println("/t  /"/"         /t: "
                            + fields.elementAt(j));
            }
        }
    }
 
  /**
   * 得到ITEM的所有属性.
   * @param db
 * @throws NotesException
   */
  public void getAllItemP(Database db) throws NotesException{
      DocumentCollection dc = db.getAllDocuments();
      Document doc = dc.getFirstDocument();
      Enumeration items = doc.getItems().elements();
      while (items.hasMoreElements()) {
        System.out.println("******");
        Item item = (Item)items.nextElement();
        System.out.println("Name: " + item.getName());
        String type = "Undefined";
        switch (item.getType()) {
          case Item.ATTACHMENT :
            type = "Attachment"; break;
          case Item.EMBEDDEDOBJECT :
            type = "Embedded object"; break;
          case Item.ERRORITEM :
            type = "Error item"; break;
          case Item.NAMES :
            type = "Names"; break;
          case Item.AUTHORS :
            type = "Authors"; break;
          case Item.READERS :
            type = "Readers"; break;
          case Item.NOTELINKS :
            type = "Note links"; break;
          case Item.NOTEREFS :
            type = "Note references"; break;
          case Item.NUMBERS :
            type = "Numbers"; break;
          case Item.RICHTEXT :
            type = "Rich text"; break;
          case Item.TEXT :
            type = "Text"; break;
          case Item.SIGNATURE :
            type = "Signature"; break;
          case Item.DATETIMES :
            type = "Date-times"; break;
          case Item.UNAVAILABLE :
            type = "Unavailable"; break;
          case Item.UNKNOWN :
            type = "Unknown"; break;
          case Item.USERDATA :
            type = "User data"; break;
          case Item.USERID :
            type = "User ID"; break;
          }
        System.out.println("Type: " + type);
        if (item.isEncrypted())
          System.out.println("Is encrypted");
        if (item.isSigned())
          System.out.println("Is signed");
        if (item.isSummary())
          System.out.println("Is summary");
        if (item.isProtected())
          System.out.println("Is protected");
        System.out.println("Text:/n" + item.getText());
      }
  }
 
  public static void main(String[]  arg) throws NotesException, IOException, ClassNotFoundException{
    LotusReadDB readDB=new LotusReadDB();

    Database db=readDB.getDB(TESTDB2);
    //-------------example get  all form
    //readDB.getAllForm(db);
   
    //get "(Tracking Request)" 的form
    //Form form=db.getForm("(ReturnNonReceipt)");
    //System.out.println(form.getFields().toString()+"     "+form.getName());
  
    System.out.println();
  
    //-------------example get  all form
    //readDB.getAllView(db);
   
    View mailV=db.getView(TESTDB2VIEW);


    //readDB.getEntryByView(mailV);
   
    //---------------得到全部document
    //readDB.getAllDocument(db);
   
    //readDB.getAllDocument(mailV);
   
    readDB.getDBDatas(TESTDB2,TESTDB2VIEW);
   
    //readDB.getAllItemP(db);
  }
}
原创粉丝点击