EMC Developer Network

Convert a Virtual Document Structure into a XML structure using XDQL

January 2006

Software Environment

FeatureTested on
Operating SystemWindows 2000 Server SP4
CompilerSun JDK 1.4.2_08
RuntimeSun JRE 1.4.2_08
DFC5.2.x,5.3, 5.3 SP1
Content Server5.3 SP1

Abstract

This snippet obtains the virtual document as an XML tree using XDQL .

Snippet



/*
 * Created on Jan 25, 2006
 *
 * EMC Documentum Developer Program 2005
 */
package com.documentum.devprog.snippets;

import com.documentum.fc.common.DfException;
import com.documentum.fc.common.IDfLoginInfo;

import com.documentum.fc.client.IDfClient;
import com.documentum.fc.client.IDfQuery;
import com.documentum.fc.client.IDfSession;
import com.documentum.fc.client.IDfSessionManager;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.documentum.com.DfClientX;
import com.documentum.com.IDfClientX;
import com.documentum.xml.xdql.IDfXmlQuery;


public class XDQLVirtualDocument
{
   
    public static void main(String[] args)
    {
        try
        {
            String username = "dmadmin";
            String password = "dmadmin";
            String repoName = "devprog53sp1";
            String vdocId = "09000001805f0344";
            
            IDfSessionManager sessMgr = createSessionManager();
            addIdentity(sessMgr,username,password,repoName);
            nestVDoc(sessMgr,repoName,vdocId);
            
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }

    }
    
    private static void nestVDoc(IDfSessionManager sessMgr,String repo,String vdocId) throws DfException,IOException
    {
        //When trying to get the vdoc structure, it is necessary to include the 'DEPTH'
        //attribute(keyword) in the DQL query.
        StringBuffer bufQuery = new StringBuffer(32);
        bufQuery.append("select r_object_id,object_name,DEPTH from dm_document IN DOCUMENT ID('");
        bufQuery.append(vdocId);
        bufQuery.append("') DESCEND");
        String strQuery = bufQuery.toString();        
        System.out.println("Query: " + strQuery);
        
        IDfClientX clientX = new DfClientX();
        
        IDfXmlQuery xmlQuery = clientX.getXmlQuery();
        xmlQuery.setDql(strQuery);
        //The following method call causes the formatting of the XML to follow
        //the Virtual Document structure
        xmlQuery.setVirtualDocumentNested(true);
        
        IDfSession sess = null;
        try
        {
            sess = sessMgr.getSession(repo);            
            xmlQuery.execute(IDfQuery.DF_READ_QUERY,sess);
            String xmlData = xmlQuery.getXMLString();
            
            File fl = new File("virtualDocument.xml");
            FileOutputStream fout = new FileOutputStream(fl);
            fout.write(xmlData.getBytes());
            fout.close();
            System.out.println("Wrote XDQL Results to File: " + fl.getAbsolutePath());
            
        }
        finally
        {
            if(sess != null)
            {
                sessMgr.release(sess);
            }
        }
    }
    
    
    /**
     * 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 DfException
    {
        IDfClientX clientX = new DfClientX();
        IDfClient localClient = clientX.getLocalClient();
        IDfSessionManager sessMgr = localClient.newSessionManager();
        return sessMgr;
    }

    /**
     * Adds a new identity to the session manager.
     */
    private static void addIdentity(IDfSessionManager sm, String username,
            String password, String repoName) throws DfException
    {
        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(repoName))
        {
            sm.clearIdentity(repoName);
        }

        sm.setIdentity(repoName, li);
    }

}