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

No comments:

Post a Comment