Tuesday, 8 November 2016

Java code to demote a version for a document (FileNet Content Engine API)

//Java code to demote a version for a document (FileNet Content Engine API)

import java.io.IOException;
import java.util.Iterator;
import javax.security.auth.Subject;
import com.filenet.api.collection.IndependentObjectSet;
import com.filenet.api.constants.RefreshMode;
import com.filenet.api.constants.VersionStatus;
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 {
private static Connection conn = null;

public static Connection getCEConnection()
{
try {
String ceURI = "http://localhost:9080/wsi/FNCEWS40MTOM/";
String userName ="username";
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 demoteVersionDocument(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='doc123.pdf'";
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 document=(Document)it.next();
if (document.get_IsCurrentVersion().booleanValue()== false) 
document = (Document)document.get_CurrentVersion();
if ((document.get_VersionStatus().getValue() == VersionStatus.RELEASED_AS_INT) && (document.get_IsReserved().booleanValue() == false))
{
document.demoteVersion();
document.save(RefreshMode.REFRESH);
}
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 {
demoteVersionDocument("OSNAME");
}
}

Java code to promote a version for a document (FileNet Content Engine API)

//Java code to promote a version for a document (FileNet Content Engine API)

import java.io.IOException;
import java.util.Iterator;
import javax.security.auth.Subject;
import com.filenet.api.collection.IndependentObjectSet;
import com.filenet.api.constants.RefreshMode;
import com.filenet.api.constants.VersionStatus;
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 {
private static Connection conn = null;

public static Connection getCEConnection()
{
try {
String ceURI = "http://localhost:9080/wsi/FNCEWS40MTOM/";
String userName ="username";
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 promoteVersionDocument(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='doc123.pdf'";
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 document=(Document)it.next();
if (document.get_IsCurrentVersion().booleanValue() == false)
document=(Document)document.get_CurrentVersion();
if ((document.get_VersionStatus().getValue() == VersionStatus.IN_PROCESS_AS_INT) &&(document.get_IsReserved().booleanValue() == false))
{
document.promoteVersion();
document.save(RefreshMode.NO_REFRESH);
}

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 {
promoteVersionDocument("OSNAME");
}
}

Java code to Check IN a CE document (FileNet Content Engine API)

//Java code to Check IN a CE document (FileNet Content Engine API)

import java.io.FileInputStream;
import java.io.IOException;
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.AutoClassify;
import com.filenet.api.constants.CheckinType;
import com.filenet.api.constants.RefreshMode;
import com.filenet.api.constants.VersionStatus;
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.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 ="username";
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 checkInDocument(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='doc123.pdf'";
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();

//Check the version
if(doc.get_IsCurrentVersion().booleanValue()== false)
{
doc= (Document) doc.get_CurrentVersion();
}
if (doc.get_IsReserved().booleanValue()== true)
{
if (doc.get_VersionStatus().getValue() !=VersionStatus.RESERVATION_AS_INT)
{
Document reservationDoc= (Document) doc.get_Reservation();
}

//Check in Document
ContentElementList contentList= Factory.ContentElement.createList();
ContentTransfer content1 = Factory.ContentTransfer.createInstance();
content1.setCaptureSource(new FileInputStream("C:/Users/Public/Pictures/Sample Pictures/Desert.jpg")); 
content1.set_ContentType("image/jpeg");
contentList.add(content1);
doc.set_ContentElements(contentList);
doc.checkin(AutoClassify.DO_NOT_AUTO_CLASSIFY, CheckinType.MINOR_VERSION); 
doc.save(RefreshMode.REFRESH);
}

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 {
checkInDocument("OSNAME");
}
}


Java code to Cancel Check-out a CE document (FileNet Content Engine API)

//Java code to Cancel check-out a CE document (FileNet Content Engine API)

import java.io.IOException;
import java.util.Iterator;
import javax.security.auth.Subject;
import com.filenet.api.collection.IndependentObjectSet;
import com.filenet.api.constants.RefreshMode;
import com.filenet.api.constants.ReservationType;
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 {
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 cancelCheckOutDocument(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='doc123.pdf4'";
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();

if(doc.get_IsCurrentVersion().booleanValue()== false)
{
doc=(Document)doc.get_CurrentVersion();
}
if (doc.get_IsReserved().booleanValue()== true)
{
Document reservationdoc= (Document)doc.cancelCheckout();
reservationdoc.save(RefreshMode.REFRESH);
}
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 {
cancelCheckOutDocument("OSNAME"); //Object Store
}
}


Sample code to check out a document (Filenet Content Engine API)

//Sample code to check out a document (Filenet Content Engine API)

import java.io.IOException;
import java.util.Iterator;
import javax.security.auth.Subject;
import com.filenet.api.collection.IndependentObjectSet;
import com.filenet.api.constants.RefreshMode;
import com.filenet.api.constants.ReservationType;
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 {
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 checkOut(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 docTitle where DocumentTitle='doc123.pdf'";
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();
//Check out the document
if (doc.get_IsCurrentVersion().booleanValue()== false) 
doc =(Document)doc.get_CurrentVersion();
if (doc.get_IsReserved().booleanValue() == false) 
{
doc.checkout(ReservationType.EXCLUSIVE,null, null, null);
doc.save(RefreshMode.REFRESH);
}
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 {
checkOut("OSNAME"); //Object store
}
}

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 
}
}