Friday, 26 May 2017

Sample Java code for Compound Document (Adding child documents to Parent document) (FileNet Content Engine api)

//Sample Java code for Compound Document (Adding child documents to Parent document)


import javax.security.auth.Subject;
import com.filenet.api.constants.ComponentRelationshipType;
import com.filenet.api.constants.CompoundDocumentState;
import com.filenet.api.constants.FilteredPropertyType;
import com.filenet.api.constants.RefreshMode;
import com.filenet.api.constants.VersionBindType;
import com.filenet.api.core.ComponentRelationship;
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.PropertyFilter;
import com.filenet.api.util.Id;
import com.filenet.api.util.UserContext;


public class CompoudDoc {
public static void main(String[] args)
{
String[] childDocs = new String[10];
childDocs[0]="{GUID}";
childDocs[1]="{GUID}";
childDocs[2]="{GUID}";
String parentDocument = "{GUID}";
String jassStanzaName = "FileNetP8";
String password = "UserName";
String username = "password";
String objectStoreName = "ObjectStore";

String bootstrap_ceuri = "http://localhost:9080/wsi/FNCEWS40MTOM/";
Id parentDocumentId = null;
Id supportDocumentID=null;
String attachment = null;
com.filenet.api.core.Connection ceConnection = null;
String uri  = null;
String versionSeriesID = null;
String returnValue="start";
boolean att_found=false;
Document parentDoc=null;
Document ChildDoc = null;
PropertyFilter pf = new PropertyFilter();
try
{
if(bootstrap_ceuri!=null && !bootstrap_ceuri.equals(""))
uri=bootstrap_ceuri;
else
uri = System.getProperty("filenet.pe.bootstrap.ceuri");
returnValue += "pe.bootstrap.ceuri= : " + uri;

ceConnection = Factory.Connection.getConnection(uri); 
Subject subject = UserContext.createSubject( ceConnection,username,password,jassStanzaName); 
UserContext uc = UserContext.get(); 
uc.pushSubject(subject); 
Domain domain = Factory.Domain.fetchInstance(ceConnection, null,null); 
returnValue += "In domain : " + domain.get_Name();
ObjectStore os = Factory.ObjectStore.fetchInstance(domain, objectStoreName, null); 
returnValue += "In Object Store : " + os.get_Name();
pf.addIncludeType(0, null, Boolean.TRUE, FilteredPropertyType.ANY, null);

for (int count=0;count<childDocs.length;count++) {
attachment = childDocs[count];
System.out.println("attachment :: "+attachment);

if(attachment!=null && attachment!=null && !attachment.equals(""))
{

if(!att_found)
{
Document parent_doc = Factory.Document.fetchInstance(os,parentDocument,pf);
        parent_doc.fetchProperties(pf);
        parentDocumentId= parent_doc.get_VersionSeries().get_Id();
System.out.println("Vs ID :: "+parentDocumentId);
returnValue+="Src Id : "+parentDocumentId+ "\n";
att_found=true;
versionSeriesID = parentDocumentId.toString(); 
com.filenet.api.core.VersionSeries parentVersionSeries = (com.filenet.api.core.VersionSeries)com.filenet.api.core.Factory.VersionSeries.fetchInstance(os,new Id(versionSeriesID),null); 
parentDoc = (com.filenet.api.core.Document) parentVersionSeries.get_CurrentVersion(); 
parentDoc.set_CompoundDocumentState(CompoundDocumentState.COMPOUND_DOCUMENT);
parentDoc.save(RefreshMode.REFRESH);
}

        Document child_doc = Factory.Document.fetchInstance(os,attachment,null);
        child_doc.fetchProperties(pf);
        supportDocumentID= child_doc.get_VersionSeries().get_Id();
System.out.println("Vs ID :: "+supportDocumentID);
returnValue+="Src Id : "+supportDocumentID+ "\n";
attachment=supportDocumentID.toString();
returnValue+="Child ID:"+attachment+ "\n";
com.filenet.api.core.VersionSeries childVersionSeries =(com.filenet.api.core.VersionSeries)com.filenet.api.core.Factory.VersionSeries.fetchInstance(os,new Id(attachment),null); 
ChildDoc = (com.filenet.api.core.Document) childVersionSeries.get_CurrentVersion();
ComponentRelationship cr = Factory.ComponentRelationship.createInstance(os, null);
cr.set_ParentComponent(parentDoc);
cr.set_ChildComponent(ChildDoc);
cr.set_ComponentSortOrder(new Integer(count));
cr.set_ComponentRelationshipType(ComponentRelationshipType.DYNAMIC_CR);            
cr.set_VersionBindType(VersionBindType.LATEST_VERSION);
cr.save(RefreshMode.NO_REFRESH);
}
}
System.out.println("Return Value :: "+returnValue);
}

catch(Exception exp){
returnValue += "Exception:"+ exp.getMessage();
System.out.println("Exception :: "+returnValue);
}
}

}

No comments:

Post a Comment