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();
    }
}

Java code to get last modified date of files from directory

//Java code to get last modified date of files from directory

import java.io.File;
import java.io.FilenameFilter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.activation.MimetypesFileTypeMap;

public class FilterFilesFromFolder {
    
   
    public static <lang> void main(String a[]){
        File file = new File("E:/sample/");
     
        //filtering txt from folder
        FilenameFilter textFilter = new FilenameFilter()
        {
            public boolean accept(File dir, String name) {
                String lowercaseName = name.toLowerCase();
                if (lowercaseName.endsWith(".txt")) {
                    return true;
                } else {
                    return false;
                }
            }
        };
       
        File[] files = file.listFiles(textFilter);
       
        long min = 0;
        String mimeType;
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        Date d = new Date();
        System.out.println("System Date :"+ sdf.format(d));
        for(File f: files){
            System.out.println("File Name ="+f.getName());
            System.out.println("Date Last Madified ="+ sdf.format(f.lastModified()));
            mimeType = new MimetypesFileTypeMap().getContentType(f);
            System.out.println("MIME Type ="+ new MimetypesFileTypeMap().getContentType(f));
           
            //getting last one hour modified files
            try
            {
                 long cd = d.getTime();
                 long lmd = f.lastModified();
                 long diff= cd-lmd;
                 System.out.println("Diff ="+diff);
                min = diff/(60 * 1000);
                 System.out.println("Diff in Min = "+min);
            }
            catch(Exception e)
            {
                System.out.println("Error in Date Diffrence = " + e);
            }
            if (min <= 60)
            {
            System.out.println("send mail");   
            }
        }
    }
}