Tuesday, 8 November 2016

Java code to retrieve Versions and Version Series for a CE document

//Java code to retrieve Versions and Version Series for a CE document

import java.io.IOException;
import java.util.Iterator;
import javax.security.auth.Subject;
import com.filenet.api.collection.IndependentObjectSet;
import com.filenet.api.collection.VersionableSet;
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.core.VersionSeries;
import com.filenet.api.core.Versionable;
import com.filenet.api.query.SearchSQL;
import com.filenet.api.query.SearchScope;
import com.filenet.api.util.UserContext;

public class CEUtil {
private static Connection conn = null;

public static Connection getCEConnection()
{
try {
String ceURI = "http://localhost:9080/wsi/FNCEWS40MTOM/";
String userName ="ceusr";
String password ="pwd";
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 Connection"+conn);
return conn;
}
public static void getVersions(String osName) throws IOException{

try{
Connection conn = getCEConnection();
Domain domain = Factory.Domain.fetchInstance(conn,null, null);
ObjectStore objStore = Factory.ObjectStore.fetchInstance(domain, osName,null);
SearchScope searchScope = new SearchScope(objStore);
int count=0;
String sqlStr = "Select * FROM docClass where DocumentTitle='documeny07'";
SearchSQL searchSQL = new SearchSQL(sqlStr);
System.out.println("Query ::"+sqlStr);
IndependentObjectSet independentObjectSet = searchScope.fetchObjects(searchSQL, new Integer(10), null, new Boolean(true)); 

if(!(independentObjectSet.isEmpty())){
Iterator it=independentObjectSet.iterator();

while(it.hasNext()) {
Document doc=(Document)it.next();

//get versions
VersionableSet versions = doc.get_Versions();
Versionable versionable;
Iterator it1 = versions.iterator();
while (it1.hasNext())
{
versionable = (Versionable)it1.next();
Integer majorNum= versionable.get_MajorVersionNumber();
System.out.println("Major Version"+majorNum);
Integer minorNum= versionable.get_MinorVersionNumber();
System.out.println("Minor Version"+minorNum);
String versionStatus= versionable.get_VersionStatus().toString();
System.out.println("Version Status =" + versionStatus);
}

//Get Version series
VersionSeries versionSeries = doc.get_VersionSeries();
Document releasedDoc= (Document)versionSeries.get_ReleasedVersion();
Integer majorNum= releasedDoc.get_MajorVersionNumber();
System.out.println("Major Version series"+majorNum);
Integer minorNum= releasedDoc.get_MinorVersionNumber();
System.out.println("Minor Version series"+minorNum);

//Current version details
Document CurMajorDoc= (Document)versionSeries.get_CurrentVersion();
Integer CurMajorNum= CurMajorDoc.get_MajorVersionNumber();
Integer CurMinorNum= CurMajorDoc.get_MinorVersionNumber();
System.out.println("Currentmajor version =" + CurMajorNum);
System.out.println("Currentminor version =" + CurMinorNum);

count++;
    System.out.println("Count:::"+count);
}
System.out.println("Count:::;"+count);
System.out.println("Done");
}

}
catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
getVersions("OSNAME"); //Param = Object Store
}
}

Java code to get user details from LDAP

//Java code to get user details from LDAP

import java.util.Hashtable;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

public class GetUserDetailsFromLDAP {
static String ldapSearchBase = "DC=Company,DC=internal";
private static DirContext ctx = null;
private static DirContext getActiveDirectoryContext() throws Exception {

final Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
properties.put(Context.PROVIDER_URL,"ldap://localhost:389");
properties.put(Context.SECURITY_AUTHENTICATION,"simple");
properties.put(Context.SECURITY_PRINCIPAL, "CN=userName");
properties.put(Context.SECURITY_CREDENTIALS,"Password");
return new InitialDirContext(properties);

}
public String[] getUser(String userID) throws Exception {

String DisplayName="";
String location="";
String attValue = "";
DirContext directoryContext  = getActiveDirectoryContext();
String returnedAttrs[] = {"displayName","userPrincipalName", "cn", "mail","sn","company","department","memberof"};
String returnedAttrsValues[]=new String[2];
SearchControls searchCtls = new SearchControls();

//Search Scope - 
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);//(or)SearchControls.ONELEVEL_SCOPE (or) SearchControls.OBJECT_SCOPE

searchCtls.setReturningAttributes(returnedAttrs);
String searchFilter = userID;
NamingEnumeration users = directoryContext.search(ldapSearchBase, searchFilter, searchCtls);
if(!users.hasMoreElements())
{
returnedAttrsValues[0]=userID;
}
else{
while (users.hasMoreElements())
{
SearchResult sr = (SearchResult)users.next();
Attributes attrs = sr.getAttributes();
if (attrs.size() == 0){
System.out.println("dddd");
}
else{
try {
for (NamingEnumeration ae = attrs.getAll();ae.hasMore();){
Attribute attr = (Attribute)ae.next();
System.out.println("Attribute = "+attr.toString());
String id = attr.getID();
NamingEnumeration e = attr.getAll();
if(!e.hasMore())
{
returnedAttrsValues[0]=userID;
}
else{
while(e.hasMore()){
attValue = (String)e.next();
if(id.equalsIgnoreCase("DisplayName")){
DisplayName = attValue;
if(DisplayName!=null && !DisplayName.equals("")){
returnedAttrsValues[0]=attValue;
}
else{
returnedAttrsValues[0]=userID;

}
}
else if(id.equalsIgnoreCase("physicalDeliveryOfficeName")){
location = attValue;
returnedAttrsValues[1]=attValue;
}

}
}

}
}
catch (NamingException e){
System.out.println("[LDAP] Exception while retreiving Attr from LDAP"+e.getMessage());
System.out.println("[LDAP] Exception while retreiving Attr from LDAP"+e.getRootCause());

}

}
}
}
return returnedAttrsValues;
}

public static void main(String args[]) throws Exception{
GetUserDetailsFromLDAP gug = new GetUserDetailsFromLDAP();
gug.getUser("cn=userid"); //(or) mail= abc@gmail.com (or) sn=name (or) uid=name...etc 
}
}

Java code to get all users in LDAP Group

//Java code to get all users in LDAP Group

import java.util.Hashtable;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

public class GetUsersFrormLDAPGroup {
static String ldapSearchBase = "DC=Company,DC=internal";
private static DirContext ctx = null;
private static DirContext getActiveDirectoryContext() throws Exception {

final Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
properties.put(Context.PROVIDER_URL,"ldap://localhost:389");
properties.put(Context.SECURITY_AUTHENTICATION,"simple");
properties.put(Context.SECURITY_PRINCIPAL, "CN=UserName");
properties.put(Context.SECURITY_CREDENTIALS,"Password");
return new InitialDirContext(properties);

}
public void getGroupUsers(String searchBase, String searchFilter, String returnedAttrs[], int maxResults)
{
Hashtable userEntries = null;
String member="";
try{
SearchControls searchCtls = new SearchControls();
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);      
searchCtls.setReturningAttributes(returnedAttrs);
ctx=getActiveDirectoryContext();
try{
System.out.println("Search Base: "+searchBase);
System.out.println("Search Filter: "+searchFilter);
NamingEnumeration users = ctx.search(searchBase, searchFilter, searchCtls);
if(users.hasMoreElements() == false){
System.out.println("Not find any object with this filter " + searchFilter + " and searchBase " + searchBase);
}

int k = 0;
String attValue = "";
userEntries = new Hashtable();
while (users.hasMoreElements()){
if(k >= maxResults)
break;        
SearchResult sr = (SearchResult)users.next();
Attributes attrs = sr.getAttributes();
if (attrs.size() == 0){
System.out.println("Could not find attribute " + returnedAttrs[0] + " for this object.");
}else{

try{          
for (NamingEnumeration ae = attrs.getAll();ae.hasMore();){ 
Attribute attr = (Attribute)ae.next();                
String id = attr.getID();
for (NamingEnumeration e = attr.getAll();e.hasMore();){                
attValue = (String)e.next();
if(id.equalsIgnoreCase("member"))
member = attValue;
System.out.println("member :"+member);
else
{
System.out.println("empty");
}
}
}
}catch(NamingException e){
System.out.println("Problem listing membership:"+e);          
}
}
k++;
}
}catch (NamingException e){
System.out.println("Problem searching directory: "+e);          
}     
ctx.close();
ctx=null;  
}catch (Exception namEx){
System.out.println("Exception while fetching the users from LDAP::"+namEx);      
}    

}
public static void main(String args[]) throws Exception{
GetUsersFrormLDAPGroup gug = new GetUsersFrormLDAPGroup();
String returnedAttrs[] = {"cn","member", "name"};
String searchFilter="CN=GroupName";
gug.getGroupUsers(ldapSearchBase,searchFilter, returnedAttrs, Integer.parseInt("2000"));
}
}

Thursday, 12 May 2016

Retrieving Documents and properties based on Content Based Retrieval(CBR)

//Retrieving Documents and properties based on Content Based Retrieval(CBR)

//content-based document searching

import java.util.Iterator;
import javax.security.auth.Subject;

import com.filenet.api.collection.DocumentSet;
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.query.SearchSQL;
import com.filenet.api.query.SearchScope;
import com.filenet.api.util.UserContext;

public class ceUtil {

public static void main(String[] args) {
ceUtil obj = new ceUtil();
obj.FetchCEObjects(obj.getCEConnection());


}

public Connection getCEConnection() {

Connection conn = null;
try {

String ceURI = "http://localhost:9080/wsi/FNCEWS40MTOM/";

String userName = "userName";
String password = "password";
conn = Factory.Connection.getConnection(ceURI);
Subject subject = UserContext.createSubject(conn, userName,
password, null);
UserContext uc = UserContext.get();
uc.pushSubject(subject);
} catch (Exception e1) {
e1.printStackTrace();
}
System.out.println("CE Connection" + conn);
return conn;
}

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

SearchScope search = new SearchScope(objStore);
String keyWord = "keyword in attachment";

String mySQLString= "SELECT d.this, DocumentTitle,Name FROM docClass d INNER JOIN ContentSearch v ON v.QueriedObject= d.This WHERE d.IsCurrentVersion= TRUE AND CONTAINS(d.*,'" + keyWord+ "')";
SearchSQL sql= new SearchSQL(mySQLString); 
DocumentSet documents = (DocumentSet) search.fetchObjects(sql,Integer.valueOf("50"),null, Boolean.valueOf(true));
com.filenet.api.core.Document doc; 

Iterator it = documents.iterator();
while (it.hasNext())
{
doc = (Document)it.next();
System.out.println("document="+doc.get_Name());
}

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

}


/*
Additinal Queries for Content Based search

SELECT d.This, DocumentTitle, Name FROM docClass d INNER JOIN VerityContentSearch v ON v.QueriedObject= d.This WHERE d.IsCurrentVersion= TRUE AND CONTAINS(d.*,'model <SENTENCE> Deluxe')
//Search for documents that contain the two key words in a sentence (use with <SENTENCE>).

SELECT d.This, DocumentTitle, Name FROM docClass d INNER JOIN VerityContentSearch v ON v.QueriedObject= d.This WHERE d.IsCurrentVersion= TRUE AND CONTAINS(d.*, 'tested <NEAR/25> passed')
//Search for documents that contain the two key words located close to each other (use with <NEAR>)
*/

*******************************************************************************

//Sample code for retrieving the properties based on CBR

RepositoryRowSet rowSet= search.fetchRows(sql, Integer.valueOf("50"),null,Boolean.valueOf(true));
Iterator itRow= rowSet.iterator();
RepositoryRow row; 
com.filenet.api.property.Properties props; 
com.filenet.api.property.Property prop; 
while (itRow.hasNext()) 
{
row = (RepositoryRow)itRow.next();
props = row.getProperties(); 
Iterator itProp= props.iterator(); 
while (itProp.hasNext())
{
prop =(com.filenet.api.property.Property) itProp.next();
if(prop.getPropertyName().equalsIgnoreCase("Rank"))
{
System.out.println(prop.getPropertyName() +" = " + prop.getFloat64Value().toString());
else
System.out.println(prop.getPropertyName()+" = " + prop.getStringValue());
}
}
}

Searching or Retriving Documents from Multiple Object stores in FileNet Content Engine

//Searching or Retriving Documents from Multiple Object stores in FileNet Content Engine

import java.util.Iterator;
import javax.security.auth.Subject;

import com.filenet.api.collection.DocumentSet;
import com.filenet.api.constants.MergeMode;
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.FilterElement;
import com.filenet.api.property.PropertyFilter;
import com.filenet.api.query.SearchSQL;
import com.filenet.api.query.SearchScope;
import com.filenet.api.util.UserContext;


public class ceUtil {


public static void main(String[] args) {
ceUtil obj = new ceUtil();
obj.FetchCEObjects(obj.getCEConnection());


}

public Connection getCEConnection() {

Connection conn = null;
try {

String ceURI = "http://localhost:9080/wsi/FNCEWS40MTOM/";

String userName = "username";
String password = "password";
conn = Factory.Connection.getConnection(ceURI);
Subject subject = UserContext.createSubject(conn, userName,
password, null);
UserContext uc = UserContext.get();
uc.pushSubject(subject);
} catch (Exception e1) {
e1.printStackTrace();
}
System.out.println("CE Connection" + conn);
return conn;
}

public void FetchCEObjects(Connection conn) {

try {
Domain domain = Factory.Domain.fetchInstance(conn, null, null);

ObjectStore objectStores[] = new ObjectStore[2];
objectStores[0] = Factory.ObjectStore.fetchInstance(domain,"DESIGNOS", null);
objectStores[1] = Factory.ObjectStore.fetchInstance(domain,"TARGETOS", null);
System.out.println("Object Store 1 : "+objectStores[0].get_DisplayName());
System.out.println("Object Store 2 : "+objectStores[1].get_DisplayName());

PropertyFilter pf= new PropertyFilter();
pf.addIncludeProperty(new FilterElement(Integer.getInteger("2"),null, Boolean.valueOf(true),"Name"));
SearchScope search = new SearchScope(objectStores, MergeMode.UNION);
SearchSQL sql= new SearchSQL("Select * from docClass where Creator='p8admin'");
DocumentSet documents = (DocumentSet)search.fetchObjects(sql,Integer.getInteger("50"),pf, Boolean.valueOf(true));

Document doc;
Iterator it = documents.iterator();
while (it.hasNext()){doc = (Document)it.next();
System.out.println("document="+doc.get_Name());
}

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

Searching Documents with Paging concept in FileNet Content Engine

PageIterator (Paging)  (FileNet Content Engine API)

Provides paging functionality for sets of independent objects and repository rows.


Sets of independent objects and repository rows are divided into pages;each page is a number of collection elements (objects or rows) that represent a subset of the collection elements. You can iterate a page at a time instead of one object or row at a time. 

As an example, if a page is defined as 10 elements, and the collection has a total of 22 elements, the first paging operation returns a page containing 10 elements, the second page returns the next 10 elements, and the third page returns the last 2 elements. This page iteration is especially useful for interactive applications that display a page of information at a time.


//Searching Documents with Paging concept in FileNet Content Engine

import java.util.Iterator;
import javax.security.auth.Subject;
import com.filenet.api.collection.ClassDescriptionSet;
import com.filenet.api.collection.DocumentSet;
import com.filenet.api.collection.PageIterator;
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.meta.ClassDescription;
import com.filenet.api.query.SearchSQL;
import com.filenet.api.query.SearchScope;
import com.filenet.api.util.UserContext;

public class ceUtil {

public static void main(String[] args) {
ceUtil obj = new ceUtil();
obj.FetchCEObjects(obj.getCEConnection());
}

public Connection getCEConnection() {

Connection conn = null;
try {
String ceURI = "http://localhost:9080/wsi/FNCEWS40MTOM/";
String userName = "username";
String password = "password!";
conn = Factory.Connection.getConnection(ceURI);
Subject subject = UserContext.createSubject(conn, userName,
password, null);
UserContext uc = UserContext.get();
uc.pushSubject(subject);
} catch (Exception e1) {
e1.printStackTrace();
}
System.out.println("CE Connection" + conn);
return conn;
}

public void FetchCEDocs(Connection conn) {


try {
Domain domain = Factory.Domain.fetchInstance(conn, null, null);
ObjectStore objStore = Factory.ObjectStore.fetchInstance(domain,"OS NAME", null);
String folder= "/TestEform";

SearchScope search = new SearchScope(objStore);
String sql1 = "Select * from docClass where Creator='p8admin'";
SearchSQL searchSQL= new SearchSQL(sql1); 
DocumentSet documents = (DocumentSet) search.fetchObjects(searchSQL,Integer.valueOf("50"),null, Boolean.valueOf(true)); 

//Without Paging Concept
Document doc;
Iterator DocIt= documents.iterator();
while (DocIt.hasNext())
{
doc = (Document)DocIt.next();
System.out.println("document="+doc.get_Name());
}

// With Paging Concept
PageIterator pageIter= documents.pageIterator();
pageIter.getPageSize();pageIter.setPageSize(5);
int pageCount= 0; 
while (pageIter.nextPage() == true) 
{
pageCount++;
int elementCount= pageIter.getElementCount();
System.out.println("Element Count = "+elementCount);
Object[] pageObjects= pageIter.getCurrentPage();
for (int index=0; index<pageObjects.length;index++)
{
Document document = (Document)pageObjects[index];
System.out.println("document= "+ document.get_Name());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}