IBM Support

HelloCMIS - HelloDocument: Creating Folders and Content

Technical Blog Post


Abstract

HelloCMIS - HelloDocument: Creating Folders and Content

Body

With the release of IBM Content Navigator on Cloud, customers are embracing the concept of cloud for managing their documents.  With IBM, they know the documents will be secured and hosted in an enterprise class data center.  
In the first article we connected to our Navigator on Cloud instance, browsed the folder structure, and executed some simple queries.  In this article, we will create a new folder and file a new document from the local file system into the new folder.  Although very simple, this example can easily be converted to handle content submitted via a web site

Apache Chemistry makes it easy
IBM continues to embrace CMIS and with the Content Navigator 2.0.3 release, the JavaScript browser binding is available as a technology preview.  The Apache Chemistry project (http://chemistry.apache.org/) leverages the CMIS standard and provides client libraries for Java, PHP, Python, and others.  As with the previous example, this example will use the OpenCMIS Java library.

image

Connecting to Navigator on Cloud
OpenCMIS makes heavy use of property Maps as a simple way to collect named value pairs of parameters for operations and this example uses a HashMap.  Create a session by adding a number of required SessionParameters into the Map and creating the session as shown below.
 

package com.ibm.ecm.usingCMIS;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;

import org.apache.chemistry.opencmis.client.api.*;
import org.apache.chemistry.opencmis.client.runtime.*;
import org.apache.chemistry.opencmis.commons.*;
import org.apache.chemistry.opencmis.commons.data.*;
import org.apache.chemistry.opencmis.commons.enums.*;

public class HelloCMIS {

    public static void main(String args[]){
        
        // default factory implementation
        SessionFactory factory = SessionFactoryImpl.newInstance();
        Map<String, String> parameter = new HashMap<String, String>();
    
        // user credentials
        parameter.put(SessionParameter.USER, "p8admin");
        parameter.put(SessionParameter.PASSWORD, "filenet");
    
        // connection settings
        parameter.put(SessionParameter.ATOMPUB_URL, "http://192.168.1.143:9080/fncmis/resources/Service&quot;);
        parameter.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());        
        parameter.put(SessionParameter.REPOSITORY_ID, "ECM");
        // create session
        Session session = factory.createSession(parameter);
 

 

Creating a new Folder
As will all objects in a Content Navigator system, the required and optional properties will be determined by the Class of the Object.  The base Folder class has a single property – Folder Name – that is required.
We will file the new folder under the root folder and must first get an instance of that root folder.  With the root folder obtained, creating a new folder is as simple as calling the createFolder method with the required properties.
 

//Get an instance of the root folder
Folder root = session.getRootFolder();

// properties
// (minimal set: name and object type id)
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:folder");
properties.put(PropertyIds.NAME, "CMISDocuments");

// create the folder
Folder newFolder = root.createFolder(properties);
 

 

File a Document in to the Folder
Creating a document is a little more work than creating a folder but follows the same pattern.  Much like creating a Folder, creating a Document requires setting the object properties by way of a HashMap.  Once the property HashMap is populated, it is necessary to create the ContentStream that contains information about the document itself as well as an InputStream of the bytes.
 

try {
    //create a document
    Map<String, Object> docProps = new HashMap<String, Object>();
    docProps.put(PropertyIds.OBJECT_TYPE_ID, "cmis:document");
    docProps.put(PropertyIds.NAME, "the document name");
            
    File f = new File("C:\\Documents\\customerContactCenter.doc");
    InputStream isFile = new FileInputStream(f);
            
    ContentStream contentStream = session.getObjectFactory().createContentStream(f.getName(), f.length(), "application/ms-word", isFile);
            
    Document d = newFolder.createDocument(docProps, contentStream, VersioningState.MAJOR);
            
    System.out.println(d.getId());
    isFile.close();
} catch (Exception ex) {
    System.out.println("Something has gone horribly wrong.");
    ex.printStackTrace();
}

 


Helpful Resources:

 

Apache Chemistry openCMIS documentation and libraries

Oasis CMIS Content Management Interoperability Services

CMIS Development Forum - ask questions, get answers.

 

[{"Business Unit":{"code":"BU053","label":"Cloud & Data Platform"},"Product":{"code":"SSCTJ4","label":"IBM Case Manager"},"Component":"","Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"","Edition":"","Line of Business":{"code":"LOB45","label":"Automation"}}]

UID

ibm11281160