Wednesday, 3 April 2019

openCMIS: Deleting a document


In my previous post, I explained how to create and update documents. In this post, I am going to explain how to delete a document from the repository.

Document interface provides following methods to delete an object.

void delete()
Deletes this object. If this object is a document, the whole version series is deleted.

void delete(boolean allVersions)
Deletes this object. If allVersions is false, then this version of the document is deleted, else all the versions of the document are deleted.

void deleteAllVersions()
Deletes this document and all its versions.

Following step-by-step procedure explains how to delete a document.

Step 1: Get the document by using document id (or) the document path.
Document document = (Document) session.getObjectByPath("/Hello.txt");

Step 2: Use one of the above delete() methods to delete the document.
document.delete();

Find the following 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.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.Action;
import org.apache.chemistry.opencmis.commons.enums.BindingType;
import org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException;

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 {
  Session session = getSession();

  Document document = (Document) session.getObjectByPath("/Hello.txt");

  System.out.println("Document Name : " + document.getName());

  /*Checking for permissions, whether user has permission to delete or not. */
  if (!document.getAllowableActions().getAllowableActions().contains(Action.CAN_DELETE_OBJECT)) {
   System.out.println("User don't have permission to delete the object");
  }

  document.delete();

  try {
   document.refresh();
  } catch (CmisObjectNotFoundException e) {
   System.out.println("Document is deleted");
   System.out.println(e.getMessage());
  }

 }
}

Output
Document Name : Hello.txt
Document is deleted
Unknown object id: 136


Previous                                                 Next                                                 Home

No comments:

Post a Comment