Showing posts with label FileNet API. Show all posts
Showing posts with label FileNet API. Show all posts

Tuesday, 24 April 2018

Java Code to get Security and DIS (Default Instance Security ) on all Classes and sub Classes (FileNet API)

//Java Code to get Security and DIS (Default Instance Security ) on all Classes and sub Classes (FileNet API)

import java.io.IOException;
import java.util.Iterator;
import javax.security.auth.Subject;
import com.filenet.api.admin.ClassDefinition;
import com.filenet.api.collection.AccessPermissionList;
import com.filenet.api.collection.ClassDefinitionSet;
import com.filenet.api.core.Connection;
import com.filenet.api.core.Domain;
import com.filenet.api.core.Factory;
import com.filenet.api.core.ObjectStore;
import com.filenet.api.security.AccessPermission;
import com.filenet.api.util.UserContext;

public class GetClassSecurity {
    private static Connection conn = null;
    static String className=null;
   
    static String disGranteeName =null;
    static String disGranteeType =null;
    static String disPermissionSource =null;
    static String disaccessType =null;
    static String disInheritableDepth =null;
    static String disAccessMask=null;
   
    static String GranteeName =null;
    static String GranteeType =null;
    static String PermissionSource =null;
    static String accessType =null;
    static String InheritableDepth =null;
    static String AccessMask=null;
    public static Connection getCEConn()
    {
        try {
            String ceURI =    "http://localHost:9080/wsi/FNCEWS40MTOM/";
            String userName ="userName";
            String password ="Password";
            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 getClassPermissions(String osName) throws IOException{
       
        try{
           
           
            Connection conn = getCEConn();
            Domain domain = Factory.Domain.fetchInstance(conn,null, null);
            ObjectStore objStore = Factory.ObjectStore.fetchInstance(domain, osName,null);
           
            System.out.println("ObjectStoreDisplayName :: "+objStore.get_DisplayName());
            System.out.println("Class Name");
            System.out.println("Grantee Name");
            System.out.println("Grantee Type");
            System.out.println("Permission Source");
            System.out.println("Access Type");
            System.out.println("Inheritabled Depth");
            System.out.println("Access Mask(Object Access Rights)");
          
           
            ClassDefinitionSet cds= objStore.get_RootClassDefinitions();
            ClassDefinition cd;
            Iterator it = cds.iterator();
            while (it.hasNext())
            {
                      System.out.println("-----------------------------");
                cd = (ClassDefinition)it.next();
                className=cd.get_DisplayName();
                System.out.println("Class name = " + className);
                System.out.println("ClassDisplayName :: "+cd.get_DisplayName());
                System.out.println("ClassName :: "+cd.get_Name());
                System.out.println("ClassSymbolicName :: "+cd.get_SymbolicName());
                System.out.println("ClassCreator :: "+cd.get_Creator());
                System.out.println("ClassDefaultInstanceOwner :: "+cd.get_DefaultInstanceOwner());
                System.out.println("ClassLastModifier :: "+cd.get_LastModifier());
                System.out.println("ClassOwner :: "+cd.get_Owner());
                System.out.println("ClassDateCreated :: "+cd.get_DateCreated());
                System.out.println("ClassID :: "+cd.get_Id());
                System.out.println("ClassDateLastModified :: "+cd.get_DateLastModified());
                System.out.println("ClassIsHidden :: "+cd.get_IsHidden());
                System.out.println("ClassIsCurrent :: "+cd.isCurrent());
                System.out.println("ClassIsPersistent :: "+cd.get_IsPersistent());
                System.out.println("ClassIsSystemOwned :: "+cd.get_IsSystemOwned());
                System.out.println("ClassIsCBREnabled :: "+cd.get_IsCBREnabled());

               
               
                System.out.println("------Default Instance Security------");

                AccessPermissionList dis = cd.get_DefaultInstancePermissions();
           
                Iterator itdis = dis.iterator();
                while (itdis.hasNext())
                {
                AccessPermission permission = (AccessPermission)itdis.next();
               
                disGranteeName=permission.get_GranteeName();
                disGranteeType=permission.get_GranteeType().toString();
                disPermissionSource=permission.get_PermissionSource().toString();
                disaccessType=permission.get_AccessType().toString();
                disInheritableDepth=permission.get_InheritableDepth().toString();       
                disAccessMask=permission.get_AccessMask().toString();
               
                System.out.println("GranteeName="+ disGranteeName);
                System.out.println("GranteeType=" + disGranteeType);
                System.out.println("PermissionSource=" +disPermissionSource);
               
                System.out.println("Accesstype =" +disaccessType);
                System.out.println("Inheritabledepth=" +disInheritableDepth);
                System.out.println(className);
                 System.out.println(disGranteeName);
                 System.out.println(disGranteeType);
                 System.out.println(disPermissionSource);
                 System.out.println(disaccessType);
                 System.out.println(disInheritableDepth);
                 System.out.println(disAccessMask);
               
               
                }
               
               
                System.out.println("------Direct Security------");
               
                 AccessPermissionList s = cd.get_Permissions();
               
                Iterator itdis1 = s.iterator();
                while (itdis1.hasNext())
                {
                AccessPermission permission1 = (AccessPermission)itdis1.next();
               
                GranteeName=permission1.get_GranteeName();
                GranteeType=permission1.get_GranteeType().toString();
                PermissionSource=permission1.get_PermissionSource().toString();
                accessType=permission1.get_AccessType().toString();
                InheritableDepth=permission1.get_InheritableDepth().toString();       
                AccessMask=permission1.get_AccessMask().toString();
                System.out.println("GranteeName="+ GranteeName);
                System.out.println("GranteeType=" + GranteeType);
                System.out.println("PermissionSource=" +PermissionSource);
               
                System.out.println("Accesstype =" +accessType);
                System.out.println("Inheritabledepth=" +InheritableDepth);
                System.out.println(className);
                 System.out.println(GranteeName);
                 System.out.println(GranteeType);
                 System.out.println(PermissionSource);
                 System.out.println(accessType);
                 System.out.println(InheritableDepth);
                 System.out.println(AccessMask);
               
               
                }
               
                ClassDefinitionSet cds1= cd.get_ImmediateSubclassDefinitions();
                  if(!cds1.isEmpty())
                  {
                      System.out.println("--------Children Class of "+className +"----------");
                       
                      childClass(cds1);
                      System.out.println("Not Empty");
                  }
            }
           
            System.out.println("-------------End Object Store--------------------------");
           
            System.out.println("Done");
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
    private static void childClass(ClassDefinitionSet cds1) throws IOException {
        // TODO Auto-generated method stub
        ClassDefinition cd;
        Iterator it = cds1.iterator();
        while (it.hasNext())
        {
                  System.out.println("-----------------------------");
               
               
            cd = (ClassDefinition)it.next();
           
            System.out.println("Class name = " + cd.get_DisplayName());
            System.out.println("ClassDisplayName :: "+cd.get_DisplayName());       
            System.out.println("ClassName :: "+cd.get_Name());
            System.out.println("ClassSymbolicName :: "+cd.get_SymbolicName());
            System.out.println("ClassCreator :: "+cd.get_Creator());
            System.out.println("ClassDefaultInstanceOwner :: "+cd.get_DefaultInstanceOwner());
            System.out.println("ClassLastModifier :: "+cd.get_LastModifier());
            System.out.println("ClassOwner :: "+cd.get_Owner());
            System.out.println("ClassDateCreated :: "+cd.get_DateCreated());
            System.out.println("ClassID :: "+cd.get_Id());
            System.out.println("ClassDateLastModified :: "+cd.get_DateLastModified());
            System.out.println("ClassIsHidden :: "+cd.get_IsHidden());
            System.out.println("ClassIsCurrent :: "+cd.isCurrent());
            System.out.println("ClassIsPersistent :: "+cd.get_IsPersistent());
            System.out.println("ClassIsSystemOwned :: "+cd.get_IsSystemOwned());
            System.out.println("ClassIsCBREnabled :: "+cd.get_IsCBREnabled());
           
            System.out.println("------Default Instance Security------");
           
            AccessPermissionList dis = cd.get_DefaultInstancePermissions();
            Iterator itdis = dis.iterator();
            while (itdis.hasNext())
            {
            AccessPermission permission = (AccessPermission)itdis.next();
           
            disGranteeName=permission.get_GranteeName();
            disGranteeType=permission.get_GranteeType().toString();
            disPermissionSource=permission.get_PermissionSource().toString();
            disaccessType=permission.get_AccessType().toString();
            disInheritableDepth=permission.get_InheritableDepth().toString();       
            disAccessMask=permission.get_AccessMask().toString();
            System.out.println("GranteeName="+ disGranteeName);
            System.out.println("GranteeType=" + disGranteeType);
            System.out.println("PermissionSource=" +disPermissionSource);

            System.out.println("Accesstype =" +disaccessType);
            System.out.println("Inheritabledepth=" +disInheritableDepth);
           
           
           
            }
           
           
            System.out.println("------Direct Security------");
           
             AccessPermissionList s = cd.get_Permissions();
            System.out.println("------------");
             System.out.println("------------");
             System.out.println("------------");
             System.out.println("------------");
             System.out.println("------------");
             System.out.println("------------");
           
            Iterator itdis1 = s.iterator();
            while (itdis1.hasNext())
            {
            AccessPermission permission1 = (AccessPermission)itdis1.next();
           
            GranteeName=permission1.get_GranteeName();
            GranteeType=permission1.get_GranteeType().toString();
            PermissionSource=permission1.get_PermissionSource().toString();
            accessType=permission1.get_AccessType().toString();
            InheritableDepth=permission1.get_InheritableDepth().toString();       
            AccessMask=permission1.get_AccessMask().toString();
           
            System.out.println(className);
             System.out.println(GranteeName);
             System.out.println(GranteeType);
             System.out.println(PermissionSource);
             System.out.println(accessType);
             System.out.println(InheritableDepth);
             System.out.println(AccessMask);
           
           
            }
           
            ClassDefinitionSet cds2= cd.get_ImmediateSubclassDefinitions();
              if(!cds2.isEmpty())
              {
                  System.out.println("--------Children Class of "+className +"----------");
                 
                  childClass(cds2);
                  System.out.println("Not Empty");
              }
        }

    }
    public static void main(String[] args) throws IOException {
   
            getClassPermissions("ObjectStore");
        }
       
       
        //    FetchOSList();
/*
         InheritableDepth
         0 - No inheritance (this object only).
        1 - This object and immediate children only.
        -1 - This object and all children (infinite levels deep).
        -2 - All children (infinite levels deep) but not this object.
        -3 - Immediate children only; not this object.
 
 */
   
}

Java Code to get security on Object Stores

//Java Code to get security on Object Stores

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.security.auth.Subject;
import com.filenet.api.collection.AccessPermissionList;
import com.filenet.api.collection.ObjectStoreSet;
import com.filenet.api.core.Connection;
import com.filenet.api.core.Domain;
import com.filenet.api.core.Factory;
import com.filenet.api.core.ObjectStore;
import com.filenet.api.security.AccessPermission;
import com.filenet.api.util.UserContext;

public class GetObjectStoreSecurity {
    private static Connection conn = null;
   
    static String GranteeName =null;
    static String GranteeType =null;
    static String PermissionSource =null;
    static String accessType =null;
    static String InheritableDepth =null;
    static String AccessMask=null;
    public static Connection getCEConn()
    {
        try {
            String ceURI =    "http://localhost:9080/wsi/FNCEWS40MTOM/";
            String userName ="user Name";
            String password ="password";
            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 ArrayList FetchOSList() {
        ArrayList<String> list = new ArrayList();
        try {
           
            //String[] oslist= new String[100];
            Connection conn= getCEConn();
            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();
            list.add(store.get_Name());
            System.out.println(store.get_Name());
            }
           
        } catch (Exception e) {
            e.printStackTrace();
        }
       
        return list;
    }
    public static void getObjectStorePermissions(String osName) throws IOException{
       
        try{
                   
            Connection conn = getCEConn();
            Domain domain = Factory.Domain.fetchInstance(conn,null, null);
            ObjectStore objStore = Factory.ObjectStore.fetchInstance(domain, osName,null);
           
            System.out.println("ObjectStoreDisplayName :: "+objStore.get_DisplayName());
            System.out.println("ObjectStoreSymbolicName :: "+objStore.get_SymbolicName());
            System.out.println("ObjectStoreName :: "+objStore.get_Name());           
            System.out.println("ObjectStoreID :: "+objStore.get_Id());           
            System.out.println("ObjectStoreCreator :: "+objStore.get_Creator());
            System.out.println("ObjectStoreLastModifier :: "+objStore.get_LocaleName());           
            System.out.println("ObjectStoreDatabaseSchemaName :: "+objStore.get_DatabaseSchemaName());           
            System.out.println("ObjectStoreJNDIDataSource :: "+objStore.get_JNDIDataSource());           
            System.out.println("ObjectStore.get_JNDIXADataSource() :: "+objStore.get_JNDIXADataSource());           
            System.out.println("ObjectStoreDescriptiveText :: "+objStore.get_DescriptiveText());           
            System.out.println("ObjectStoreSchemaVersion :: "+objStore.get_SchemaVersion());           
            System.out.println("ObjectStoreTextSearchIndexingLanguage :: "+objStore.get_TextSearchIndexingLanguage());           
            System.out.println("ObjectStoreVerityPartitionProperty :: "+objStore.get_VerityPartitionProperty());           
            System.out.println("ObjectStoreClassName :: "+objStore.getClassName());
                       
             AccessPermissionList apos = objStore.get_Permissions();
             Iterator it = apos.iterator();
             while (it.hasNext())
             {
                 AccessPermission permission = (AccessPermission)it.next();
                   
                    GranteeName=permission.get_GranteeName();
                    GranteeType=permission.get_GranteeType().toString();
                    PermissionSource=permission.get_PermissionSource().toString();
                    accessType=permission.get_AccessType().toString();
                    InheritableDepth=permission.get_InheritableDepth().toString();       
                    AccessMask=permission.get_AccessMask().toString();
                    System.out.println(objStore.get_DisplayName());
                    System.out.println("GranteeName="+ GranteeName);
                    System.out.println("GranteeType=" + GranteeType);
                    System.out.println("PermissionSource=" +PermissionSource);       
                    System.out.println("Accesstype =" +accessType);
                    System.out.println("Inheritabledepth=" +InheritableDepth);
             }
           
            System.out.println("-------------End Object Store--------------------------");
            System.out.println("Done");
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
   
    public static void main(String[] args) throws IOException {
        //getObjectStorePermissions("DMS");
   
        List objectStoreList = FetchOSList();
       
        String objectStore;
       
        Iterator it = objectStoreList.iterator();
       
        while(it.hasNext())
        {
            objectStore=(String) it.next();
            System.out.println("Object Store :: "+objectStore);
            getObjectStorePermissions(objectStore);
        }
       
        //    FetchOSList();
/*
         InheritableDepth
         0 - No inheritance (this object only).
        1 - This object and immediate children only.
        -1 - This object and all children (infinite levels deep).
        -2 - All children (infinite levels deep) but not this object.
        -3 - Immediate children only; not this object.
 
 */
    }
}

Java Code to get security on FileNet Domain

//Java Code to get security on FileNet Domain


import java.io.IOException;
import java.util.Iterator;
import javax.security.auth.Subject;
import com.filenet.api.collection.AccessPermissionList;
import com.filenet.api.core.Connection;
import com.filenet.api.core.Domain;
import com.filenet.api.core.Factory;
import com.filenet.api.security.AccessPermission;
import com.filenet.api.util.UserContext;

public class GetDomainSecurity {
private static Connection conn = null;

static String GranteeName =null;
static String GranteeType =null;
static String PermissionSource =null;
static String accessType =null;
static String InheritableDepth =null;
static String AccessMask=null;

public static Connection getCEConn()
{
try {
String ceURI="localhost:9080/wsi/FNCEWS40MTOM/";
String userName ="userName";
String password ="password";
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 getObjectStorePermissions() throws IOException{

try{
  conn=null;
      conn = getCEConn();
Domain domain = Factory.Domain.fetchInstance(conn,null, null);
AccessPermissionList apld = domain.get_Permissions();
Iterator itd = apld.iterator();

while (itd.hasNext())
{
AccessPermission permission = (AccessPermission)itd.next();

GranteeName=permission.get_GranteeName();
GranteeType=permission.get_GranteeType().toString();
PermissionSource=permission.get_PermissionSource().toString();
accessType=permission.get_AccessType().toString();
InheritableDepth=permission.get_InheritableDepth().toString();
AccessMask=permission.get_AccessMask().toString();
System.out.println(domain.get_Name());
System.out.println("GranteeName="+ GranteeName);
System.out.println("GranteeType=" + GranteeType);
System.out.println("PermissionSource=" +PermissionSource);
System.out.println("Accesstype =" +accessType);
System.out.println("Inheritabledepth=" +InheritableDepth);

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

public static void main(String[] args) throws IOException {


getObjectStorePermissions();
        
       
        }
}

Friday, 26 May 2017

Java code for Event Subscription (FileNet Content Engine API)

//Java code for Event Subscription (FileNet Content Engine API)

import java.util.Iterator;
import com.filenet.api.engine.EventActionHandler;
import com.filenet.api.events.ObjectChangeEvent;
import com.filenet.api.util.Id;
import com.filenet.api.property.*;
import com.filenet.api.core.*;
import com.filenet.api.constants.*;

public class MyCustomeEventHandler implements EventActionHandler
 {
public void onEvent(ObjectChangeEvent event, Id subId)
  {
try
     {
ObjectStore os = event.getObjectStore();
        Id id = event.get_SourceObjectId();
        System.out.println("Event Action Call Started");
        System.out.println("guid="+id);
        PropertyFilter pf = new PropertyFilter();
        pf.addIncludeType(0, null, Boolean.TRUE, FilteredPropertyType.ANY, null);
        Document doc = Factory.Document.fetchInstance(os,id,pf);
        doc.fetchProperties(pf);
        com.filenet.api.property.Properties props = doc.getProperties();      
        Iterator iter = props.iterator();
        System.out.println("Property" +"\t" + "Value");
 
        while (iter.hasNext() )
        {
            Property prop = (Property)iter.next();
            
            if (prop.getPropertyName().equals("DocumentTitle"))
            {
            String docTitle = prop.getStringValue();
                System.out.println("Doc title" + "\t" + docTitle );
            }

            if (prop.getPropertyName().equals("Creator"))
            {
            String createdBy = prop.getStringValue();
                System.out.println("Creator" + "\t" +createdBy );
            }
            if (prop.getPropertyName().equals("DateCreated"))
            {
            String createdOn = prop.getDateTimeValue().toString();
                System.out.println("Date Created" + "\t" +createdOn );
                
            }
        }
       
        doc.getProperties().putObjectValue("DocumentTitle", "changedtoEvent");
doc.save(RefreshMode.NO_REFRESH);
     
     }
     catch (Exception e)
     {
        throw new RuntimeException(e);
     }
  }

}

Java code for Downloading Document Content (Attachments) (FileNet Content Engine API)

//Java code for Downloading Document Content (Attachments) (FileNet Content Engine API)

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
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.RefreshMode;
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.property.Properties;
import com.filenet.api.query.SearchSQL;
import com.filenet.api.query.SearchScope;
import com.filenet.api.util.UserContext;

public class DownloadContent {

private static Connection conn = null;
public static Connection getCEConn()
{

try {

String ceURI = "http://localhost:9080/wsi/FNCEWS40MTOM/";
String userName ="username";
String password ="password";
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 Conn :: "+conn);
return conn;
}
public static void downloadDoc(String osName) throws IOException{

try{
//get the CE document

Connection conn = getCEConn();
Domain domain = Factory.Domain.fetchInstance(conn,null, null);
ObjectStore objStore = Factory.ObjectStore.fetchInstance(domain, osName,null);
SearchScope searchScope = new SearchScope(objStore);

int count=1;
String sqlStr = "Select * FROM DocClass where Title='abcd'";
SearchSQL searchSQL = new SearchSQL(sqlStr);
System.out.println("Query ::"+sqlStr);
IndependentObjectSet independentObjectSet = searchScope.fetchObjects(searchSQL, new Integer(10), null, new Boolean(true)); 
String docTitle=null;
if(!(independentObjectSet.isEmpty())){
Iterator it=independentObjectSet.iterator();
while(it.hasNext()) {
Document doc=(Document)it.next();
Properties documentProperties = doc.getProperties();
docTitle=documentProperties.getStringValue("DocumentTitle");
System.out.println("Doc Title :: "+ docTitle);
ContentElementList docContentList = doc.get_ContentElements();
Iterator iter = docContentList.iterator();
String filepath = "D:\\";
FileOutputStream fos = new FileOutputStream(filepath+docTitle);
count++; 
while (iter.hasNext() )
{
   ContentTransfer ct = (ContentTransfer) iter.next();
   // Print element sequence number and content type of the element.
   System.out.println("\nElement Sequence number: " + ct.get_ElementSequenceNumber().intValue() + "\n" +"Content type: " + ct.get_ContentType() + "\n");
   InputStream stream = ct.accessContentStream();
   byte[] buffer = new byte[4096000];
   int bytesRead = 0;
   while ((bytesRead = stream.read(buffer)) != -1) {
    System.out.print(".");
    fos.write(buffer,0,bytesRead);
   }
   System.out.println("done!");
       fos.close();
   stream.close();
    }
}
System.out.println("Count:::;"+count);
System.out.println("Done");
}
}
catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
downloadDoc("ObjectStore");
}


}

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

}