Thursday 25 April 2019

CMIS: Change log Change Events


CMIS model the changes as 'ChangeEvent'. Change Event represents an action that occurred to an object in the repository.

Session interface provides 'getContentChanges' method to get the change events from given point of time. 'getContentChanges' is defined in following overloaded forms.

ItemIterable<ChangeEvent> getContentChanges(String changeLogToken, boolean includeProperties)
ChangeEvents getContentChanges(String changeLogToken, boolean includeProperties, long maxNumItems)
ChangeEvents getContentChanges(String changeLogToken, boolean includeProperties, long maxNumItems,OperationContext context)
ItemIterable<ChangeEvent> getContentChanges(final String changeLogToken, final boolean includeProperties, OperationContext context)

Example
                  ItemIterable<ChangeEvent> changeEvents = session.getContentChanges(previousChangeLogToken, false);

                  for (ChangeEvent changeEvent : changeEvents) {
                           System.out.println(changeEvent.getObjectId());
                  }

A repository that supports the change log capability MUST expose at least the following information for each change object

Id ObjectId The object id of the object to which the change occurred.

Enum ChangeType An enumeration that indicates the type of the change. Valid values are:
created The object was created.
updated The object was updated.
deleted The object was deleted.
security The access control or security policy for the object were changed.

<Properties> properties Additionally, for events of changeType "updated", the repository MAY optionally include the new values of properties on the object (if any).

Following application connects to the inmemory repository and expects an input from the user, to get the changes from given change log token.

Follow the below steps while running the application.
         a. Run the application 'TestCmis.java'.
         b. From workbench create/delete/update some documents. For example, I deleted one file.
         c. Give some input to the application 'TestCmis.java'

TestCmis.java
package com.sample.util;

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

import org.apache.chemistry.opencmis.client.api.ChangeEvent;
import org.apache.chemistry.opencmis.client.api.ChangeEvents;
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.enums.CapabilityChanges;

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

  CapabilityChanges capabilityChanges = session.getRepositoryInfo().getCapabilities().getChangesCapability();

  if (capabilityChanges == null) {
   System.out.println("Repository is not providing any value");
   return;
  }

  if (CapabilityChanges.NONE == capabilityChanges) {
   System.out.println("Repository is not supporitng change log");
   return;
  }

  System.out.println("capabilityChanges : " + capabilityChanges);
  ChangeEvents oldChangeEvents = session.getContentChanges(null, false, 1000);
  String previousChangeLogToken = oldChangeEvents.getLatestChangeLogToken();
  System.out.println("Change log token " + previousChangeLogToken);

  System.out.println("Press Enter to get latest change events");
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  br.readLine();

  ChangeEvents changeEvents = session.getContentChanges(previousChangeLogToken, false, 1000);

  String latestChangeLogToken = changeEvents.getLatestChangeLogToken();
  System.out.println("Change log token " + latestChangeLogToken);

  for (ChangeEvent changeEvent : changeEvents.getChangeEvents()) {
   System.out.println(changeEvent.getObjectId() + ", " + changeEvent.getChangeType());
  }

 }

}




Previous                                                 Next                                                 Home

No comments:

Post a Comment