Tuesday 2 April 2019

openCMIS: renaming a document


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

Name of a cmis object is stored inside the property ‘cmis:name’, by updating this property you can change the name of a document.

Following step-by-step procedure explains how to rename a document ‘a.txt’ located inside the folder f1 to ‘b.txt’.

Step 1: Get the document that you want to rename by using document absolute path (or) by id. I am using path to get the document reference.
Document document = (Document) session.getObjectByPath("/f1/a.txt");

Step 2: Set the property ‘cmis:name’ to ‘b.txt’
Map<String, String> properties = new HashMap<>();
properties.put("cmis:name", "b.txt");

Step 3: Call the updateProperties method to rename the document.
document = (Document) document.updateProperties(properties);

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

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

  System.out.println("Before renaming : ");
  System.out.println("name : " + document.getName());
  System.out.println("path : " + document.getPaths().get(0));

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

  document = (Document) document.updateProperties(properties);

  System.out.println("After renaming : ");
  System.out.println("name : " + document.getName());
  System.out.println("path : " + document.getPaths().get(0));

 }
}


Output
Before renaming : 
name : a.txt
path : /f1/a.txt
After renaming : 
name : b.txt
path : /f1/b.txt


Previous                                                 Next                                                 Home

No comments:

Post a Comment