Wednesday, 27 March 2019

openCMIS: Delete folder


Folder class following delete methods to delete a folder.

void delete()
Deletes a folder. It delete the folder, if it is empty, else it throws an exception.

void delete(boolean allVersions)
Deletes a folder. It delete the folder, if it is empty, else it throws an exception.

List<String> deleteTree(boolean allversions, UnfileObject unfile, boolean continueOnFailure);
Deletes this folder and all subfolders. Use this method to delete the folder, if the folder is not empty. It returns a list of object IDs which failed to be deleted.

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

Step 1: Get the folder reference by using the folder id (or) folder path.
Folder folder = (Folder) session.getObjectByPath("/My_Folder-0-0");

Step 2: Use any one of above delete methods to delete the folder.

Find the following working application.

TestCmis.java
package com.sample.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

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

  Folder folder = (Folder) session.getObjectByPath("/My_Folder-0-0");

  System.out.println("Name Of the Folder : " + folder.getName());
  System.out.println("Deleting the folder : " + folder.getName());

  folder.deleteTree(true, null, true);

  try {
   folder.refresh();
  } catch (CmisObjectNotFoundException e) {
   System.out.println("Folder is deleted : " + folder.getName());
   System.out.println(e.getMessage());
  }

 }
}


Output

Name Of the Folder : My_Folder-0-0
Deleting the folder : My_Folder-0-0
Folder is deleted : My_Folder-0-0
Unknown object id: 101




Previous                                                 Next                                                 Home

No comments:

Post a Comment