Showing posts with label Write CSV file. Show all posts
Showing posts with label Write CSV file. Show all posts

Friday 11 March 2016

Fetch(or Filter) Meta Data(of Document) from Content Engine and write into CSV file

//Fetch(or Filter) Meta Data(of Document) from Content Engine  and write into CSV file

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import javax.security.auth.Subject;
import com.csvreader.CsvWriter;
import com.filenet.api.collection.IndependentObjectSet;
import com.filenet.api.core.Connection;
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 CSVReportFromCEDoc{
private static Connection conn = null;
    public static Connection getCEConnection()
    {
        try {
            String ceURI = "http://localhst:9080/wsi/FNCEWS40MTOM/"; //FileNet URL
            String userName ="p8admin"; //User Name
            String password ="filenet"; //Password
            if(conn==null){
            conn = Factory.Connection.getConnection(ceURI); //CE connection
            Subject subject = UserContext.createSubject(conn, userName, password, null); //creating subject
            UserContext uc = UserContext.get(); 
            uc.pushSubject(subject); //push subject
            }

        } catch (Exception e1) {
            e1.printStackTrace();
        }
        System.out.println("CE Connection"+conn);
        return conn;
    }
    public static void getInternalQADocuments() throws IOException{
        String osName="RAKESHOS"; //Object Store name
        try{
            Connection conn = getCEConnection();
            Domain domain = Factory.Domain.fetchInstance(conn,null, null); //fetching domain
            ObjectStore objStore = Factory.ObjectStore.fetchInstance(domain, osName,null); //fetching object store
            SearchScope searchScope = new SearchScope(objStore); // Creating search scope object
            int count=1;
            String sqlStr = "Select * FROM DocClass"; //CE Query
            SearchSQL searchSQL = new SearchSQL(sqlStr);
            System.out.println("Query ::"+sqlStr);
            //fetching Data from Content Engine
            IndependentObjectSet independentObjectSet = searchScope.fetchObjects(searchSQL, new Integer(10), null, new Boolean(true)); 
           
            File file = new File("E:\\Report.csv"); 

            if ( !file.exists() ){
                file.createNewFile();
            }
            // Use FileWriter constructor that specifies open for appending
            CsvWriter csvOutput = new CsvWriter(new FileWriter(file, true), ',');
            //Create Header for CSV
            csvOutput.write("Title");
            csvOutput.write("Doc Type");
            csvOutput.write("Country");
            csvOutput.write("State");
            csvOutput.write("Street");
            csvOutput.write("House No");
            csvOutput.write("Added On");
            csvOutput.write("Modified On");
             csvOutput.endRecord();
            
            String Title=null;
            String DocType=null;
            Date DateCreated=null;
            Date DateLastModified=null;
            String State=null;
            String Country=null;
            String Street=null;
            String HouseNo=null;
            if(!(independentObjectSet.isEmpty())){
                Iterator it=independentObjectSet.iterator();
                while(it.hasNext())    {
                    Document doc=(Document)it.next();
                   
                    //fetching properties from each document
                    Properties documentProperties = doc.getProperties();
                     Title=doc.getProperties().getStringValue("DocumentTitle");
                     System.out.println("Title:::"+Title);
                     DocType=documentProperties.getStringValue("DocType");
                     DateCreated=documentProperties.getDateTimeValue("DateCreated");
                     DateLastModified=documentProperties.getDateTimeValue("DateLastModified");
                     State=documentProperties.getStringValue("State");
                     Country=documentProperties.getStringValue("Country");
                     Street=documentProperties.getStringValue("Street");
                     HouseNo=documentProperties.getStringValue("HouseNo");
                   
                     //inserting into CSV file
                     csvOutput.write(Title);
                     csvOutput.write(DocType);
                     csvOutput.write(Country);
                     csvOutput.write(State);
                     csvOutput.write(Street);
                     csvOutput.write(HouseNo);
                     csvOutput.write(DateCreated.toString());
                     csvOutput.write(DateLastModified.toString());
                     csvOutput.endRecord();
                     count++; 
                     System.out.println("Count:::;"+count);
                    }
                }
            }
        catch(Exception e){
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws IOException {
        getInternalQADocuments();
    }
}