EMC Developer Network

Import Multiple Files using the Import Operation

November 2005

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

Abstract

This code snippet imports multiple files using the DFC Import Operation. It also sets a custom object type and name for each file.

Snippet


/*
 * Created on Nov 7, 2005
 *
 * EMC Documentum Developer Program 2005
 */
package com.documentum.devprog.snippets.operations;

import com.documentum.fc.common.DfException;
import com.documentum.fc.common.DfId;
import com.documentum.fc.common.IDfId;
import com.documentum.fc.common.IDfList;
import com.documentum.fc.common.IDfLoginInfo;

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

import com.documentum.com.DfClientX;
import com.documentum.com.IDfClientX;
import com.documentum.operations.IDfFile;
import com.documentum.operations.IDfImportNode;
import com.documentum.operations.IDfImportOperation;

public class ImportMultipleFiles
{
    
    public static void main(String[] args)
    {
        String username = "dmadmin";
        String password = "dmadmin";
        String repoName = "devprog";
        String[] filenames = {"c:\\devprog\\folderPolicyService.jar","C:\\devprog\\DevProg.log"};
        String destFldrId = "0c00000180000105";
        
        try
        {
            IDfSessionManager sessMgr = createSessionManager();
            addIdentity(sessMgr,username,password,repoName);            
            importFiles(sessMgr,repoName,filenames,destFldrId);
        }
        catch(DfException ex)
        {
            ex.printStackTrace();
        }
        

    }

    /**
     * Imports a single file in the repository
     * 
     */
    private static void importFiles(IDfSessionManager sessMgr,String repoName,String[] filenames,String destFldrId) throws DfException
    {               
        IDfSession sess = null;
        try
        {
            IDfClientX clientX = new DfClientX();
            
            IDfImportOperation impOper = clientX.getImportOperation();
            
            IDfId destId = new DfId(destFldrId);            
            //This will import all files to a single destination.
            //To import each file to a different destination
            //set call this method on the import node.
            //impOper.setDestinationFolderId(destId);
            for(int i=0;i<filenames.length;i++)
            {
                IDfFile localFile = clientX.getFile(filenames[i]);
                IDfImportNode impNode = (IDfImportNode) impOper.add(localFile);
                //You can set different destination ids here for each import node
                //This way files get imported to different destinations.
                impNode.setDestinationFolderId(destId);
                //set custom object type. If not called dm_document is used.
                impNode.setDocbaseObjectType("dp_text");
                //set custom object name. 
                impNode.setNewObjectName(localFile.getName() + "" + ((int)(Math.random()*100)));
                //The import operation determines the file format.
                //It is also possible to explicitly set the format.
                //impNode.setFormat("");
                
            }
            
            sess = sessMgr.getSession(repoName);
            impOper.setSession(sess);                        
            
            if(impOper.execute())
            {
                System.out.println("Import Operation Succeeded");
                IDfList newObjLst = impOper.getNewObjects();
                for(int i=0;i<newObjLst.getCount();i++)
                {
                    IDfDocument newObj = (IDfDocument) newObjLst.get(i);
                    //you can set any custom/standard attr values on the document now
                    //newObj.setString("my_attr","someValue");
                    //newObj.save();
                    System.out.println("Created Object: " + newObj.getObjectId());
                }                   
            }
            else
            {
                System.out.println("Import Operation Failed");
                IDfList errList = impOper.getErrors();
                for(int i=0;i<errList.getCount();i++)
                {
					IDfOperationError err = (IDfOperationError) errList.get(i);
                    System.out.println(err.getMessage());
                }
            }
        }
        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);
    }

}

	

Using the Code

The above code snippet comes with a main method and is thus a standalone program. To use it, copy-paste the entire code into a new Java class created in your IDE. Adjust the package and class name of the pasted code according to your preference. Change the username,password, repository name and any other necessary variables. Compile and run the code. For details on setting up your IDE for DFC development, refer to the article Setting up a DFC Development Environment