Friday, 26 May 2017

Java code for Downloading Document Content (Attachments) (FileNet Content Engine API)

//Java code for Downloading Document Content (Attachments) (FileNet Content Engine API)

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import javax.security.auth.Subject;
import com.filenet.api.collection.ContentElementList;
import com.filenet.api.collection.IndependentObjectSet;
import com.filenet.api.constants.RefreshMode;
import com.filenet.api.core.Connection;
import com.filenet.api.core.ContentTransfer;
import com.filenet.api.core.Document;
import com.filenet.api.core.Domain;
import com.filenet.api.core.Factory;
import com.filenet.api.core.ObjectStore;
import com.filenet.api.property.Properties;
import com.filenet.api.query.SearchSQL;
import com.filenet.api.query.SearchScope;
import com.filenet.api.util.UserContext;

public class DownloadContent {

private static Connection conn = null;
public static Connection getCEConn()
{

try {

String ceURI = "http://localhost:9080/wsi/FNCEWS40MTOM/";
String userName ="username";
String password ="password";
if(conn==null){
conn = Factory.Connection.getConnection(ceURI);
Subject subject = UserContext.createSubject(conn, userName, password, null);
UserContext uc = UserContext.get();
uc.pushSubject(subject);
}

} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("CE Conn :: "+conn);
return conn;
}
public static void downloadDoc(String osName) throws IOException{

try{
//get the CE document

Connection conn = getCEConn();
Domain domain = Factory.Domain.fetchInstance(conn,null, null);
ObjectStore objStore = Factory.ObjectStore.fetchInstance(domain, osName,null);
SearchScope searchScope = new SearchScope(objStore);

int count=1;
String sqlStr = "Select * FROM DocClass where Title='abcd'";
SearchSQL searchSQL = new SearchSQL(sqlStr);
System.out.println("Query ::"+sqlStr);
IndependentObjectSet independentObjectSet = searchScope.fetchObjects(searchSQL, new Integer(10), null, new Boolean(true)); 
String docTitle=null;
if(!(independentObjectSet.isEmpty())){
Iterator it=independentObjectSet.iterator();
while(it.hasNext()) {
Document doc=(Document)it.next();
Properties documentProperties = doc.getProperties();
docTitle=documentProperties.getStringValue("DocumentTitle");
System.out.println("Doc Title :: "+ docTitle);
ContentElementList docContentList = doc.get_ContentElements();
Iterator iter = docContentList.iterator();
String filepath = "D:\\";
FileOutputStream fos = new FileOutputStream(filepath+docTitle);
count++; 
while (iter.hasNext() )
{
   ContentTransfer ct = (ContentTransfer) iter.next();
   // Print element sequence number and content type of the element.
   System.out.println("\nElement Sequence number: " + ct.get_ElementSequenceNumber().intValue() + "\n" +"Content type: " + ct.get_ContentType() + "\n");
   InputStream stream = ct.accessContentStream();
   byte[] buffer = new byte[4096000];
   int bytesRead = 0;
   while ((bytesRead = stream.read(buffer)) != -1) {
    System.out.print(".");
    fos.write(buffer,0,bytesRead);
   }
   System.out.println("done!");
       fos.close();
   stream.close();
    }
}
System.out.println("Count:::;"+count);
System.out.println("Done");
}
}
catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
downloadDoc("ObjectStore");
}


}

No comments:

Post a Comment