Monday, April 7, 2014

Deep Export from Documentum to desktop

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.documentum.com.DfClientX;
import com.documentum.com.IDfClientX;
import com.documentum.fc.client.DfQuery;
import com.documentum.fc.client.IDfClient;
import com.documentum.fc.client.IDfCollection;
import com.documentum.fc.client.IDfDocument;
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.IDfExportNode;
import com.documentum.operations.IDfExportOperation;

/**
 *
 */

/**
 * @author mpoli_fujitsu
 *
 */
public class DeepExport {

/**
* @param args
* @throws DfException
*/

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

IDfSessionManager sesMgr = getSessionMgr();
IDfSession session = sesMgr.getSession("XXXX");
IDfCollection rs = null;
IDfQuery query = new DfQuery();
query.setDQL(" SELECT   r_object_id,object_name,r_folder_path FROM   dm_folder WHERE  folder('/XXXX/Departments/Finance/Accounting/General Ledger/XXX Archived Files',descend)");
rs = query.execute(session, 0);
if (rs != null) {
while (rs.next()) {
//System.out.println(rs.getString("r_folder_path"));
String str=rs.getString("r_folder_path");
//String str1=str.substring(58);
//System.out.println(str1);
//folderCreation(str1,session);
//String str2="/XXXXX/Departments/Finance/Accounting/General Ledger".concat(str1);
try{

IDfCollection rs1 = null;
IDfQuery query1 = new DfQuery();
System.out.println(" SELECT * FROM  dm_document WHERE  folder('"+str+"')");
query1.setDQL(" SELECT * FROM  dm_document WHERE  folder('"+str+"')");
rs1 = query1.execute(session, 0);
if (rs1 != null) {
while (rs1.next()) {
System.out.println(rs1.getString("object_name"));
IDfDocument doc=(IDfDocument)session.getObject(new DfId(rs1.getString("r_object_id")));
IDfClientX clientx = new DfClientX();
IDfExportOperation eo = clientx.getExportOperation();
IDfExportNode node = (IDfExportNode) eo.add(doc);
String localpath="D:/Stock".concat(str.substring(58));
System.out.println("localpath----"+localpath);
node.setFilePath(localpath+"/"+rs1.getString("object_name")+"."+doc.getContentType());
if (eo.execute())
{
System.out.println("Export operation successful.");
}
else
{
System.out.println("Export operation failed.");
}
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}
}

private static IDfSessionManager getSessionMgr(){
IDfSessionManager sessionMgr = null;
try{

IDfClientX clientx = new DfClientX();
IDfClient client = clientx.getLocalClient();
sessionMgr = client.newSessionManager();
IDfLoginInfo loginInfoObj = clientx.getLoginInfo();
loginInfoObj.setUser("dmadmin");
loginInfoObj.setPassword("xxx);
sessionMgr.setIdentity("XXX", loginInfoObj);

}catch(DfException exp){
exp.printStackTrace();
}
return sessionMgr;

}
public static void folderCreation(String str,IDfSession session)
{
boolean dirFlag = false;
String str1="D:/Stock";
File stockDir = new File(str1.concat(str));
try {
dirFlag = stockDir.mkdirs();
} catch (SecurityException Se) {
System.out.println("Error while creating directory in Java:" + Se);
}
if (dirFlag)
System.out.println("Directory created successfully");
else
System.out.println("Directory was not created successfully");
//exportDocuments(str,session);
}


public  static void exportDocuments(String path,IDfSession session)
{
String str="/XXXXx/Departments/Finance/Accounting/General Ledger".concat(path);
try{

IDfCollection rs = null;
IDfQuery query = new DfQuery();
query.setDQL(" SELECT * FROM  dm_document WHERE  folder('"+str+"')");
rs = query.execute(session, 0);
if (rs != null) {
while (rs.next()) {
System.out.println(rs.getString("object_name"));
IDfDocument doc=(IDfDocument)session.getObject(new DfId(rs.getString("r_object_id")));
IDfClientX clientx = new DfClientX();
IDfExportOperation eo = clientx.getExportOperation();
IDfExportNode node = (IDfExportNode) eo.add(doc);
String localpath="D:/Stock".concat(path);
System.out.println("localpath----"+localpath);
node.setFilePath(localpath+"."+doc.getContentType());
if (eo.execute())
{
System.out.println("Export operation successful.");
}
else
{
System.out.println("Export operation failed.");
}
}
}
}catch(Exception e){
e.printStackTrace();
}
}
public static String checkSpecialChar(String inputString)
{
Pattern pattern = Pattern.compile("[^a-z0-9]", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputString);
boolean b = matcher.find();
if(b)
{
System.out.println();
return inputString.replaceAll("[^a-zA-Z0-9]", "-");
}else
return inputString;
}
}

No comments:

Post a Comment