Import a Single File using the Import Operation
| November 2005 | |
|
Software Environment
| Feature | Tested on |
| Operating System | Windows 2000 Server SP4 |
| Compiler | Sun JDK 1.4.2_08 |
| Runtime | Sun JRE 1.4.2_08 |
| DFC | 5.2.x,5.3, 5.3 SP1 |
| Content Server | 5.3 |
Abstract
This code snippet imports a single file into a repository using the DFC import operation.
Snippet
/*
* Created on Oct 10, 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.fc.client.IDfSysObject;
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 ImportOperation
{
public static void main(String[] args)
{
String username = "dmadmin";
String password = "dmadmin";
String repoName = "devprog";
String filename = "c:\\devprog\\folderPolicyService.jar";
String destFldrId = "";
try
{
IDfSessionManager sessMgr = createSessionManager();
addIdentity(sessMgr,username,password,repoName);
importSingleFile(sessMgr,repoName,filename,destFldrId);
}
catch(DfException ex)
{
ex.printStackTrace();
}
}
/**
* Imports a single file in the repository
*
*/
private static void importSingleFile(IDfSessionManager sessMgr,String repoName,String filename,String destFldrId) throws DfException
{
IDfSession sess = null;
try
{
IDfClientX clientX = new DfClientX();
IDfImportOperation impOper = clientX.getImportOperation();
IDfFile localFile = clientX.getFile(filename);
IDfImportNode impNode = (IDfImportNode) impOper.add(localFile);
sess = sessMgr.getSession(repoName);
impOper.setSession(sess);
IDfId destId = new DfId(destFldrId);
impOper.setDestinationFolderId(destId);
if(impOper.execute())
{
System.out.println("Import Operation Succeeded");
IDfDocument newObj = (IDfDocument)impNode.getNewObject();
//you can set any custom 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++)
{
System.out.println(errList.getString(i));
}
}
}
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
|