DFC Code to export query result into CSV
import java.io.FileWriter;import java.io.IOException;
import java.io.PrintWriter;
import com.documentum.com.DfClientX;
import com.documentum.com.IDfClientX;
import com.documentum.fc.client.IDfClient;
import com.documentum.fc.client.IDfCollection;
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.IDfAttr;
import com.documentum.fc.common.IDfLoginInfo;
public class PrintQueryResult {
public static void main(String[] args) {
// ********************CHECK THE DFC PROPERTY*********************//
IDfSession session = null;
// ***********************CHANGE THE LOGING INFO**********************//
session = getSessionByIdentity("dmadmin", "password","repositoryName", "server", "port");
// ***********************CHANGE THE QUERY**********************//
String sQuery = "select * from dm_document";
// **CHANGE THE LOCATION OF QUERY RESULTS*****************//
String fileLocation = "C:\\Log\\Test\\";
String fileName = "dm_document";
performQuery(sQuery, fileLocation+ fileName, session);
}
private static void performQuery(String sQuery, String fileName,
IDfSession session) {
IDfCollection recordCollection;
try {
System.out.println("Started");
IDfClientX myClientx = new DfClientX();
IDfQuery q = myClientx.getQuery(); // Create query object
q.setDQL(sQuery); // Give it the query
recordCollection = q.execute(session, IDfQuery.DF_READ_QUERY); // execute
// the
// query
displayResultsInCsv(recordCollection, fileName + ".csv");
recordCollection.close();
System.out.println("Finished");
} catch (Throwable e) {
if (e instanceof DfException) {
System.out.println("DFC Exception:");
String s = ((DfException) e).getStackTraceAsString();
System.out.println(s);
} else {
System.out.println("Non-DFC Exception ");
e.printStackTrace();
}
} // end: catch()
finally {
if (session != null)
session.getSessionManager().release(session);
}
}
// Step through a collection and display results
public static void displayResultsInCsv(IDfCollection col, String fileName)
throws DfException, IOException {
FileWriter fileWriter = new FileWriter(fileName);
PrintWriter printWriter = new PrintWriter(fileWriter);
boolean isHeaderPrinted = false;
while (col.next()) {
StringBuffer resultLine = new StringBuffer();
StringBuffer headerCsv = new StringBuffer();// row num
for (int i = 0; i < col.getAttrCount(); i++) { // For attribute
IDfAttr attrib = col.getAttr(i); // factory method for
// Once Header is Printed, No need to append as header is
// printed once
if (!isHeaderPrinted) {
headerCsv.append(attrib.getName());
headerCsv.append(",");
}
// System.out.print( "\t" + attr.getName() + ": " ); // Display
if (attrib.getDataType() == IDfAttr.DM_BOOLEAN) { // Display
// value
resultLine.append(col.getBoolean(attrib.getName()));
resultLine.append(",");
} else if (attrib.getDataType() == IDfAttr.DM_DOUBLE) {
resultLine.append(col.getDouble(attrib.getName()));
resultLine.append(",");
} else if (attrib.getDataType() == IDfAttr.DM_ID) {
resultLine.append(col.getId(attrib.getName()));
resultLine.append(",");
} else if (attrib.getDataType() == IDfAttr.DM_INTEGER) {
resultLine.append(col.getInt(attrib.getName()));
resultLine.append(",");
} else if (attrib.getDataType() == IDfAttr.DM_STRING) {
String str = col.getString(attrib.getName());
resultLine.append(str.replaceAll(",", " "));
resultLine.append(",");
} else if (attrib.getDataType() == IDfAttr.DM_TIME) {
resultLine.append(col.getTime(attrib.getName()));
resultLine.append(",");
} else { // Unknown type
System.out.println("Unknown type:" + attrib.getDataType());
}
}
// Print the header only for the first Time
if (!isHeaderPrinted) {
printWriter.println(headerCsv);
isHeaderPrinted = true;
}
printWriter.println(resultLine);
}
// Flush the output to the file
printWriter.flush();
// Close the Print Writer
printWriter.close();
// Close the File Writer
fileWriter.close();
}
public static IDfSession getSessionByIdentity(String username,
String password, String repoName, String primary_host,
String primary_port) {
IDfSession sess = null;
try {
IDfClientX clientX = new DfClientX();
IDfClient localClient = clientX.getLocalClient();
IDfSessionManager sessMgr = localClient.newSessionManager();
localClient.getClientConfig().setString("primary_host", primary_host);
localClient.getClientConfig().setString("primary_port", primary_port);
IDfLoginInfo li = clientX.getLoginInfo();
li.setUser(username);
li.setPassword(password);
if (sessMgr.hasIdentity(repoName)) {
sessMgr.clearIdentity(repoName);
}
sessMgr.setIdentity(repoName, li);
sess = sessMgr.getSession(repoName);
} catch (Exception ex) {
ex.printStackTrace();
}
return sess;
}
}
No comments:
Post a Comment