How to add a document in IPM using Imaging Services

Here is how you can add a document programmatically using Oracle Imaging APIs — Document and Document Content Services.

The example shown is a simple java client that you can use to test the document creation and check-in. Make sure your project has the imaging-client.jar, from IPM lib, included in its class path.

Create a project and a class to test the Document Services. Following 2 lines will help you authenticate, so that you can start using the Document Services.

	try{
            	String IMAGING_WS_URL = "http:///imaging/ws";  		//Imaging Services URL
		UserToken credential = new BasicUserToken("yourUserId", "password"); 	//UserToken API from imaging
            	ServicesFactory servicesFactory = ServicesFactory.login(credential, Locale.US, IMAGING_WS_URL);
	}  catch (Exception ex) {
            	ex.printStackTrace();
      	}

As a best practice, wrap any code after login with try{}finally{} just to make sure you log out of the Imaging framework when the work is done — regardless of exceptions — else you will end up having lots of open sessions in Imaging.

        ServicesFactory servicesFactory = ServicesFactory.login(credential, Locale.US, IMAGING_WS_URL);
	try{
                ...
	} finally{
                if (servicesFactory != null){
                    servicesFactory.logout(); //handle logout gracefully to eliminate unnecessary open sessions
                }
        }

From the service factory you can now tap into Document and Document Content Services. With the Document services handy you can now query your application defined in IPM.

         DocumentService documentService = servicesFactory.getDocumentService();
         DocumentContentService documentContentService = servicesFactory.getDocumentContentService();
         NameId appNameId = null;
         List appList = documentService.listTargetApplications(Document.Ability.CREATEDOCUMENT);
         for(NameId nameId : appList){
                if(nameId.getName().equals("app_name")){
                     appNameId = nameId;
                }
         }

Once you get hold of your application in IPM, creating document is simple. Prepare a file to be added, populate list of required metadata for it, then upload and create the document in IPM, like so.

         String fileNameWithPath="/test/doc/testdoc.txt";
         FileDataSource fileDataSource = new FileDataSource(fileNameWithPath);
         DataHandler dataHandler = new DataHandler(fileDataSource);
         String uploadToken = documentContentService.uploadDocument(dataHandler,"testdoc.txt");
 //                 add all required field values
         List fieldValues = new ArrayList();
         fieldValues.add(new Document.FieldValue("RequiredDateField", new Date()));
         fieldValues.add(new Document.FieldValue("RequiredTextField ", "/test/doc"));
         fieldValues.add(new Document.FieldValue("RequiredNumberField", 1));
         String result = documentService.createDocument(uploadToken, appNameId, fieldValues, true);

Here is the complete method code.

      try{
         UserToken credential = new BasicUserToken("uid", "pwd");
         ServicesFactory servicesFactory = ServicesFactory.login(credential, Locale.US, "http:///imaging/ws");
            try{
                DocumentService documentService = servicesFactory.getDocumentService();
                DocumentContentService documentContentService = servicesFactory.getDocumentContentService();
                NameId appNameId = null;
                List appList = documentService.listTargetApplications(Document.Ability.CREATEDOCUMENT);
                for(NameId nameId : appList){
                    if(nameId.getName().equals("app_name")){
                       appNameId = nameId;
                    }
                }
                if(appNameId == null){
                    System.out.println("app not found.");
                    return;
                }
                String fileNameWithPath="/test/doc/testdoc.txt";
                FileDataSource fileDataSource = new FileDataSource(fileNameWithPath);
                DataHandler dataHandler = new DataHandler(fileDataSource);
                System.out.println("ntCalling service to send a file to app...");
                String uploadToken = documentContentService.uploadDocument(dataHandler,"testdoc.txt");
                System.out.println("nt IPM ID from Service: "+uploadToken);
//                 add all required field values
                List fieldValues = new ArrayList();
                fieldValues.add(new Document.FieldValue("RequiredDateField", new Date()));
                fieldValues.add(new Document.FieldValue("RequiredTextField ", "/test/doc"));
                fieldValues.add(new Document.FieldValue("RequiredNumberField", 1));
                String result = documentService.createDocument(uploadToken, appNameId, fieldValues, true);
                System.out.println("Document ID from create: "+result);
                System.out.println("ntend client");
            }finally{
                if (servicesFactory != null){
                    servicesFactory.logout();
                }
            }
      } catch (Exception ex) {
            ex.printStackTrace();
      }
Sanjib