2017-08-13 29 views
0

我想刪除文檔中文件的重複出現,只留下一個 文件(如果它存在多次)。如何刪除文檔中重複出現的文件,如果它在java中使用dql查詢存在多次?

以下是我的代碼。

import com.documentum.fc.client.IDfQuery; 
import com.documentum.fc.client.IDfSession; 
import com.documentum.fc.client.IDfSessionManager; 
import com.documentum.fc.common.DfException; 
import com.documentum.fc.common.DfId; 
import com.documentum.fc.common.IDfLoginInfo; 
import com.documentum.operations.IDfDeleteOperation; 

public class CountFiles { 

    // Documentum target repository where the files will be imported 
    private static final String REPO_NAME = "rep"; 



    public static void main(String[] args) throws Exception { 

     try { 
      String username = "user"; 
      String password = "pwd"; 

      System.out.println("Starting to connect ..."); 

      IDfSessionManager sessMgr = createSessionManager(); 
      addIdentity(sessMgr, username, password); 
      IDfSession sess = sessMgr.getSession(REPO_NAME); 
      System.out.println("Successfully connected to the server."); 
      queryDocumentum(sess); 


     } catch(Exception ex) { 
      System.out.println(ex); 
      ex.printStackTrace(); 
     } 
    } 


    private static void queryDocumentum(IDfSession sess) throws DfException { 
    IDfQuery query = new DfQuery(); 
String queryStr= "select count(*) from dm_document where folder('/XXX/YYY', DESCEND) and object_name = 'abc.pdf' "; 
    query.setDQL(queryStr); 
    IDfCollection coll = query.execute(sess,IDfQuery.DF_EXEC_QUERY); 
    while(coll.next()) 
    { 
     int count = coll.getValueAt(0); 
     if(count>1) 
     { 
     String qry = "delete dm_sysobject (all) objects where object_name='abc.pdf';"; 

     IDfQuery q= new DfQuery(); 
     query.setDQL(qry); 
     IDfCollection col = query.execute(sess,IDfQuery.DF_EXEC_QUERY); 


     } 
    } 
    coll.close(); 
} 
/** 
    * Creates a new session manager instance. The session manager does not have 
    * any identities associated with it. 
    * 
    * @return a new session manager object. 
    * @throws DfException 
    */ 
    private static IDfSessionManager createSessionManager() 
      throws Exception { 
     IDfClientX clientX = new DfClientX(); 
     IDfClient localClient = clientX.getLocalClient(); 
     IDfSessionManager sessMgr = localClient.newSessionManager(); 

     System.out.println("Created session manager."); 

     return sessMgr; 
    } 

    /** 
    * Adds a new identity to the session manager. 
    * 
    */ 
    private static void addIdentity(final IDfSessionManager sm, 
      final String username, final String password) 
      throws Exception { 
     IDfClientX clientX = new DfClientX(); 

     IDfLoginInfo li = clientX.getLoginInfo(); 
     li.setUser(username); 
     li.setPassword(password); 

     // check if session manager already has an identity. 
     // if yes, remove it. 
     if(sm.hasIdentity(REPO_NAME)) { 
      sm.clearIdentity(REPO_NAME); 

      System.out.println("Cleared identity on :" + REPO_NAME); 
     } 

     sm.setIdentity(REPO_NAME, li); 

     System.out.println("Set up identity for the user."); 
    } 

} 

但是我做手術的方式有些問題。這不是工作。我沒有在這裏給出文件的路徑,因爲我不知道文件的確切路徑。如果沒有給出文件的路徑,可以刪除除文件之外的所有文件。

回答

0

如果您使用Java編寫邏輯,無論如何都要查看IDfOperatiins。此外,這種做大宗材料的方法在指南中有很好的介紹。

0

我建議你DQL和邏輯下面的變化,這主要是因爲你正在使用DFC API:

DQL:

//Retrieves all the root object ids of the documents version tree where there are duplicates as their name. 
//Here I thought the latest created document is the one to keep, you can adapt it to your needs - **see ORDER by clause**. 

    select i_chronicle_id from dm_document where folder('/XXX/YYYY', DESCEND) and object_name in (select object_name from dm_document where folder('/XXX/YYYY', DESCEND) and object_name = 'abc.pdf' group by object_name having count(*) > 1) order by r_creation_date asc; 

邏輯:

//"pseudo-code" - deletes the entire version tree of the document that is not the last in the returned list 
while 'not last i_chronicle_id from the result collection' 
    //execute this dql: 
    `delete dm_document (all) objects where i_chronicle_id='<i_chronicle_id from the list>';` 

希望這有助於

艾米利亞

附:我已經針對CS 7.3

進行過測試的DQL
相關問題