Tuesday 2 April 2019

openCMIS: Create a document


In my previous post, I explained how to create a folder, in this post, I am going to explain how to create a document.

Create an empty document inside root folder
Step 1: Get the parent folder reference, where you want to create new document. Suppose if you want to create a folder inside root folder, get the root folder reference.
Folder rootFolder = session.getRootFolder();
                 
Step 2: Create a map, and populate the properties like document name and type of the cmis object.
Map<String, String> properties = new HashMap<>();
properties.put("cmis:objectTypeId", "cmis:document");
properties.put("cmis:name", "emptyDocument.txt");

Step 3: Call the createDocument method to create document.
Document document = rootFolder.createDocument(properties, null, null);

Following is the complete working application.

TestCmis.java
package com.sample.util;

import java.util.HashMap;
import java.util.Map;

import org.apache.chemistry.opencmis.client.api.Document;
import org.apache.chemistry.opencmis.client.api.Folder;
import org.apache.chemistry.opencmis.client.api.Session;
import org.apache.chemistry.opencmis.client.api.SessionFactory;
import org.apache.chemistry.opencmis.client.runtime.SessionFactoryImpl;
import org.apache.chemistry.opencmis.commons.SessionParameter;
import org.apache.chemistry.opencmis.commons.enums.BindingType;

public class TestCmis {

 private static String serverURL = "http://localhost:8080/chemistry-opencmis-server-inmemory-1.1.0/browser";
 private static String repositoryId = "A1";

 public static Session getSession() {
  Map<String, String> parameters = new HashMap<>();
  parameters.put(SessionParameter.BINDING_TYPE, BindingType.BROWSER.value());

  parameters.put(SessionParameter.USER, "");
  parameters.put(SessionParameter.PASSWORD, "");

  parameters.put(SessionParameter.REPOSITORY_ID, repositoryId);
  parameters.put(SessionParameter.BROWSER_URL, serverURL);

  SessionFactory sessionFactory = SessionFactoryImpl.newInstance();
  return sessionFactory.createSession(parameters);
 }

 public static void main(String args[]) {
  Session session = getSession();

  Folder rootFolder = session.getRootFolder();

  Map<String, String> properties = new HashMap<>();
  properties.put("cmis:objectTypeId", "cmis:document");
  properties.put("cmis:name", "emptyDocument.txt");

  Document document = rootFolder.createDocument(properties, null, null);

  System.out.println("Name Of the Document " + document.getName());
  System.out.println("Path Of the Document " + document.getPaths().get(0));
 }
}

Output
Name Of the Document emptyDocument.txt
Path Of the Document /emptyDocument.txt

Create document with content
Most of the times, you would like to upload a document from your local file system to the repository. Following step-by-step procedure explain you, how to upload a document with content.

Step 1: Create content stream to the input file.
InputStream inputStream = new FileInputStream(inputFile);
ContentStream contentStream = session.getObjectFactory().createContentStream(inputFile.getName(),inputFile.length(), mimeType, inputStream);

Step 2: Populate the properties like document name and type of the cmis object
Map<String, String> properties = new HashMap<>();
properties.put("cmis:objectTypeId", "cmis:document");
properties.put("cmis:name", inputFile.getName());

Step 3: Call the ‘createDocument’ method by passing the content stream and properties in step 2 as arguments.

Folder rootFolder = session.getRootFolder();
Document document = rootFolder.createDocument(properties, contentStream, null);

Following is the complete working application.


TestCmis.java
package com.sample.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;

import org.apache.chemistry.opencmis.client.api.Document;
import org.apache.chemistry.opencmis.client.api.Folder;
import org.apache.chemistry.opencmis.client.api.Session;
import org.apache.chemistry.opencmis.client.api.SessionFactory;
import org.apache.chemistry.opencmis.client.runtime.SessionFactoryImpl;
import org.apache.chemistry.opencmis.commons.SessionParameter;
import org.apache.chemistry.opencmis.commons.data.ContentStream;
import org.apache.chemistry.opencmis.commons.enums.BindingType;

public class TestCmis {

 private static String serverURL = "http://localhost:8080/chemistry-opencmis-server-inmemory-1.1.0/browser";
 private static String repositoryId = "A1";

 public static Session getSession() {
  Map<String, String> parameters = new HashMap<>();
  parameters.put(SessionParameter.BINDING_TYPE, BindingType.BROWSER.value());

  parameters.put(SessionParameter.USER, "");
  parameters.put(SessionParameter.PASSWORD, "");

  parameters.put(SessionParameter.REPOSITORY_ID, repositoryId);
  parameters.put(SessionParameter.BROWSER_URL, serverURL);

  SessionFactory sessionFactory = SessionFactoryImpl.newInstance();
  return sessionFactory.createSession(parameters);
 }

 public static void main(String args[]) throws IOException {
  File inputFile = new File("C:\\Users\\Krishna\\Documents\\deltaCalc\\How to use the Eclipse Project.docx");

  String mimeType = Files.probeContentType(inputFile.toPath());

  Session session = getSession();
  InputStream inputStream = new FileInputStream(inputFile);
  ContentStream contentStream = session.getObjectFactory().createContentStream(inputFile.getName(),
    inputFile.length(), mimeType, inputStream);

  Map<String, String> properties = new HashMap<>();
  properties.put("cmis:objectTypeId", "cmis:document");
  properties.put("cmis:name", inputFile.getName());

  Folder rootFolder = session.getRootFolder();
  Document document = rootFolder.createDocument(properties, contentStream, null);

  System.out.println("Name Of the Document " + document.getName());
  System.out.println("Path Of the Document " + document.getPaths().get(0));

 }
}

Output
Name Of the Document How to use the Eclipse Project.docx

Path Of the Document /How to use the Eclipse Project.docx



Previous                                                 Next                                                 Home

No comments:

Post a Comment