Wednesday, 11 May 2016

Java Sample Code for Creating Custom Object in FileNet

// Java Sample Code for Creating Custom Object in FileNet

public void CreateCustomObject(Connection conn) {


try {
Domain domain = Factory.Domain.fetchInstance(conn, null, null);
ObjectStore objStore = Factory.ObjectStore.fetchInstance(domain,"OSNAME", null);

System.out.println("del"+folderOj.get_Name());

//Creaing Custome Object and setting properties

CustomObject myObject= Factory.CustomObject.createInstance(objStore,"Student"); //Object store, Costome Object Class Name
com.filenet.api.property.Properties props = myObject.getProperties();
props.putValue("TitleName","stuInfo022");
props.putValue("StuID", "RK1234");
props.putValue("StuMailID", "p8@filenet.com");
props.putValue("age", 13);
DateFormat date = new SimpleDateFormat("dd/MM/yyyy");
Date memeberDate= date.parse("20/12/2015");
props.putValue("JoiningDate", memeberDate);
myObject.save(RefreshMode.REFRESH);
System.out.println("Customobject " + myObject.get_Name() + " created");

//Saving the Custome Object into folder
String folder= "/Test";
com.filenet.api.core.Folder folderOj= Factory.Folder.fetchInstance(objStore,folder, null);
ReferentialContainmentRelationship rel = folderOj.file(myObject,AutoUniqueName.AUTO_UNIQUE,null,DefineSecurityParentage.DO_NOT_DEFINE_SECURITY_PARENTAGE);
rel.save(RefreshMode.REFRESH);

} catch (Exception e) {
e.printStackTrace();
}
}

Java Sample code to retrieve a custom object in FileNet

//Java Sample code to retrieve a custom object in FileNet

public void FetchCEObjects(Connection conn) {



try {
Domain domain = Factory.Domain.fetchInstance(conn, null, null);
ObjectStore objStore = Factory.ObjectStore.fetchInstance(domain,"OSNAME", null);
String customeObjectPath= "/Test/stuInfo022"; // Full Path of Custome Object

CustomObject myObject= Factory.CustomObject.fetchInstance(objStore,customeObjectPath, null);

 
String TitleName= myObject.get_Name();
System.out.println(TitleName+ " Name is retrieved");
 
com.filenet.api.property.Properties props = myObject.getProperties();
 
   String StuID = props.getStringValue("StuID");
String StuMailID= props.getStringValue("ICCMailUID");
Integer age = props.getInteger32Value("age");
Date dateCreated= props.getDateTimeValue(PropertyNames.DATE_CREATED);
Date JoiningDate= props.getDateTimeValue("JoiningDate");
 
   System.out.println("Student ID: " + StuID );
System.out.println("Student age: " + age);
System.out.println("Mail ID: " + StuMailID);
System.out.println("Date created: " + dateCreated);
System.out.println("Joining Date : " + JoiningDate);

} catch (Exception e) {

e.printStackTrace();
}
}

Java Sample for Retrieving collections of individual classes meta data and individual properties meta data (FileNet Content Engine API)

// Java Sample for Retrieving collections of individual classes meta data and individual properties meta data (FileNet Content Engine API)

public void FetchPropClassDesc(Connection conn) {

try {
Domain domain = Factory.Domain.fetchInstance(conn, null, null);
ObjectStore objStore = Factory.ObjectStore.fetchInstance(domain,"OSNAME", null);


/*
ClassDescription Are collections of metadata that describe individual classes
Provide access to PropertyDescriptionobjects associated with class 
Return default permission for an object of a particular class
Determine if versioning is enabled for a particular Document class
Assign a document lifecycle policy to a document class or subclassRetrieve
*/

ClassDescriptionSet classDescriptions= objStore.get_ClassDescriptions();
ClassDescription classDesc;
Iterator it = classDescriptions.iterator();
while (it.hasNext()) 
{
classDesc= (ClassDescription)it.next();
System.out.println("Class name = " + classDesc.get_DisplayName());

}

PropertyFilter pf= new PropertyFilter();
pf.addIncludeType(0, null, null, FilteredPropertyType.ANY,1);
Document document=Factory.Document.fetchInstance(objStore, new Id("{08CA2852-111B-4E5D-B032-DBG2A653B4FD}"), pf);
String documentName= document.get_Name();
System.out.println(documentName+ " Document has been retrieved");


com.filenet.api.meta.ClassDescription classDescription= document.get_ClassDescription();
/*
Are collections of metadata that describe individual properties
Are contained in the ClassDescriptionPropertyDescriptionsproperty
Provide information on how the property needs to be represented to the user
Contain information such as choice lists 
Each instance of a PropertyDescription object describes a specific property for a single class
*/

PropertyDescriptionList propDescs= classDescription.get_PropertyDescriptions();
PropertyDescription propDesc; 
Iterator propIt= propDescs.iterator();
while (propIt.hasNext()) 
{
propDesc=(PropertyDescription)propIt.next();

System.out.println("Property name = " +propDesc.get_SymbolicName()); 
//get_SymbolicName(): A String thatrepresents the programmatic identifier for this PropertyDescription

System.out.println("Read only? " + propDesc.get_IsReadOnly().toString()); 
//get_IsReadOnly(): A Boolean value that indicates whether or not you can modify the value of the property. If true, the property value can be changed only by the server

System.out.println("Value of Property" + propDesc.get_Settability()); 
//get_Settability(): A constant that indicates when the value of a property can be setRetrieve (READ_ONLY-3, READ_WRITE-0, SETTABLE_ONLY_BEFORE_CHECKIN-1, SETTABLE_ONLY_ON_CREATE)

System.out.println("Is Value Required ? "+propDesc.get_IsValueRequired()); 
//get_IsValueRequired(): A Boolean value that indicates whether the property is required to have a value (true) or not (false)

System.out.println("Cardinality = " + propDesc.get_Cardinality()); 
//get_Cardinality() :A constant that indicates the cardinality of the PropertyDescription (ENUM-1, LIST-2, SINGLE-0)

System.out.println(""+propDesc.get_IsSystemGenerated());
/*get_IsSystemGenerated(): A Boolean value that indicates whether the property has its value set automatically by the Content Engine server (true) or not (false). 
If the value is true, the described property does one of the following:
Provides system information
Has its value determined by the server
*/

}

} catch (Exception e) {
e.printStackTrace();
}
}

Tuesday, 10 May 2016

Retrieving all documents and objects in Folder (FileNet CE API)

//Retrieving all documents and objects in Folder (FileNet CE API)

public void FetchCEObjectsInFolder(Connection conn) {  //FileNet CE Connection 

try {
Domain domain = Factory.Domain.fetchInstance(conn, null, null);
ObjectStore objStore = Factory.ObjectStore.fetchInstance(domain,"OSNAME", null);
System.out.println("Object Store ="+ objStore.get_DisplayName() );

String folder= "/Test"; // Folder name

com.filenet.api.core.Folder folderOj= Factory.Folder.fetchInstance(objStore,folder, null);
System.out.println("del"+folderOj.get_Name());

DocumentSet documents = folderOj.get_ContainedDocuments(); //For documents

ReferentialContainmentRelationshipSet refConRelSet= folderOj.get_Containees(); // for objects
Iterator it = documents.iterator(); 
while(it.hasNext())
{

//Retrieving documents
Document retrieveDoc= (Document)it.next();
String name = retrieveDoc.get_Name();
System.out.println("Document Name :: "+ name);

//Retrieving Objects
ReferentialContainmentRelationship retrieveObj=(ReferentialContainmentRelationship)it.next();
IndependentObject containee=retrieveObj.get_Head();
String className= containee.getClassName();
String displayName= retrieveObj.get_Name(); 
System.out.println("Class Name = "+ className);
System.out.println("Display Name = "+ displayName);
}
} catch (Exception e) {
e.printStackTrace();
}
}

Deleting Sub Folders and Root Folder in CE

//Deleting Sub Folders and Root Folder in CE

public void deleteCEFolder(Connection conn) {  //FileNet CE Connection

try {
Domain domain = Factory.Domain.fetchInstance(conn, null, null);
ObjectStore objStore = Factory.ObjectStore.fetchInstance(domain,"OSNAME", null);
System.out.println("Object Store ="+ objStore.get_DisplayName() );

String folder= "/NewFolder";

com.filenet.api.core.Folder folderOj= Factory.Folder.fetchInstance(objStore,folder, null);
System.out.println("del"+folderOj.get_Name());

FolderSet subFolders= folderOj.get_SubFolders();
Iterator it = subFolders.iterator();
while(it.hasNext()){

com.filenet.api.core.Folder subFolder= (com.filenet.api.core.Folder) it.next();

String name = ((com.filenet.api.core.Folder) subFolder).get_FolderName();
System.out.println("Subfolder = "+name);

//1. First Delete sub folders
subFolder.delete();
subFolder.save(RefreshMode.NO_REFRESH);
System.out.println("Subfolder = "+name+" is Deleted");
}


  //2. Delete Root folder

folderOj.delete();
folderOj.save(RefreshMode.REFRESH);
System.out.println("Root Folder "+folderOj + "is Deleted");

} catch (Exception e) {
e.printStackTrace();
}
}

Fetching folders and sub folders in CE

//Fetching folders and sub folders in CE

public void FetchCEFolder(Connection conn) {     //FileNet CE Connection 

try {
Domain domain = Factory.Domain.fetchInstance(conn, null, null);
ObjectStore objStore = Factory.ObjectStore.fetchInstance(domain,"OSNAME", null);
System.out.println("Object Store ="+ objStore.get_DisplayName() );

String folder= "/NewFolder";

// Fetching Parent folder
com.filenet.api.core.Folder folderOj= Factory.Folder.fetchInstance(objStore,folder, null);
System.out.println("del"+folderOj.get_Name());

//Fetching sub folders 
FolderSet subFolders= folderOj.get_SubFolders(); 
Iterator it = subFolders.iterator();
while(it.hasNext()){
com.filenet.api.core.Folder subFolder= (com.filenet.api.core.Folder) it.next(); 
String name = ((com.filenet.api.core.Folder) subFolder).get_FolderName();
System.out.println("Subfolder = "+name);

// Fetching hidden folders
if(subFolder.getProperties().getBooleanValue("IsHiddenContainer"))
System.out.println("Folder "+ name + "is hidden");

}

} catch (Exception e) {
e.printStackTrace();
}
}

Creating folder and sub folder in CE (CE API)

// Creating folder and sub folder in CE (CE API)

public void createCEFolder(Connection conn) {    //FileNet CE Connection 

try {
//fetching Domain
Domain domain = Factory.Domain.fetchInstance(conn, null, null);
//fetching Object Store
ObjectStore objStore = Factory.ObjectStore.fetchInstance(domain,"OSNAME", null);
System.out.println("Object Store ="+ objStore.get_DisplayName() );

//Creating Root folder 
com.filenet.api.core.Folder testFolder= Factory.Folder.createInstance(objStore,null);
com.filenet.api.core.Folder rootFolder= objStore.get_RootFolder(); 
System.out.println(rootFolder);
testFolder.set_Parent(rootFolder);
testFolder.set_FolderName("NewFolder"); //Folder Name
testFolder.save(RefreshMode.REFRESH);

//Creating Sub folder
com.filenet.api.core.Folder subFolder= testFolder.createSubFolder("newSubFolder");
subFolder.save(RefreshMode.REFRESH);

} catch (Exception e) {
e.printStackTrace();
}
}

Retrieve all Object stores in CE


// Retrieve all Object stores in CE

public void FetchOSList(Connection conn) {   //FileNet CE Connection

try {

Domain domain = Factory.Domain.fetchInstance(conn, null, null);
ObjectStoreSet osSet= domain.get_ObjectStores();
ObjectStore store;
Iterator iterator = osSet.iterator();
Iterator<ObjectStore> osIter = iterator;
while (osIter.hasNext()) {
store = (ObjectStore)osIter.next();
 
if((store.getAccessAllowed().intValue() & AccessLevel.USE_OBJECT_STORE.getValue())>0)
System.out.println(store.get_Name());
 
}

} catch (Exception e) {
e.printStackTrace();
}
}

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