Difference between revisions of "Core extensions"

From OpenKM Documentation
Jump to: navigation, search
Line 36: Line 36:
  
 
So, we want to reject a document creation when placed in a folder. Our objective is the '''preCreate''' method:
 
So, we want to reject a document creation when placed in a folder. Our objective is the '''preCreate''' method:
 
  
 
<source lang="java">
 
<source lang="java">
Line 51: Line 50:
 
}
 
}
 
</source>
 
</source>
 +
 +
The hard work has been done. Now, to declare this class as an extension you need to add the '''@PluginImplementation''' annotation. This is the class once all the step have been finished:
 +
 +
<source lang="java">
 +
@PluginImplementation
 +
public class MyDocumentExtension implements DocumentExtension {
 +
  @Override
 +
  public int getOrder() {
 +
    return 0;
 +
  }
 +
 +
  public void preCreate(Session session, Ref<Node> parentNode, Ref<File> content, Ref<Document> doc) throws
 +
      AccessDeniedException, ExtensionException {
 +
    try {
 +
      if (parentNode.get().getPath().equals("/okm:root/forbidden") && doc.get().getMimeType().equals("application/pdf")) {
 +
        throw new AccessDeniedException("Can't upload PDF documents here");
 +
      }
 +
    } catch (RepositoryException e) {
 +
      new ExtensionException(e.getMessage());
 +
    }
 +
  }
 +
 +
  @Override
 +
  public void postCreate(Session session, Ref<Node> parNode, Ref<Node> docNode, Ref<Document> doc) {
 +
  }
 +
}
 +
</cource>
 
[[Category: Extension Guide]]
 
[[Category: Extension Guide]]

Revision as of 17:20, 11 May 2011

This kind of extensions enable to add features at OpenKM Core level, implementing extension points. Actually there is a couple of method which can be extended in this way but the list of the will grow as needed.


Nota advertencia.png This kind of extensions are in development and only available in trunk. We expect to be included in a future OpenKM 5.2 release.

First of all, lets an extension sample. Suppose you want to disable the uploading of certain type of documents in a folder. You need to create a Java project with your favorite IDE (or CLI), create a class which implements an interface and create a JAR archive. This generated archive should be placed at $JBOSS_HOME/plugins directory.

Thats all! The plugin will be used next time you restart JBoss. But more interesting. You can also refresh your plugins without restarting the application server. To do so, go to Administration > Scripting and execute this command:

 com.openkm.extension.core.ExtensionManager.getInstance().reset();

Let's see how you can implement this plugin.

Plugin implementation

We will create a class called MyDocumentExtension, extending from DocumentExtension:

public class MyDocumentExtension implements DocumentExtension {
  @Override
  public int getOrder() {
    return 0;
  }

  @Override
  public void preCreate(Session session, Ref<Node> parentNode, Ref<File> content, Ref<Document> doc) {
    // To be implemented
  }

  @Override
  public void postCreate(Session session, Ref<Node> parNode, Ref<Node> docNode, Ref<Document> doc) {
    // To be implemented
  }
}

These two methods represent two extension points in the document created action. The first one (preCreate) will be executed BEFORE the document is created. The second one (postCreate) will be executed AFTER the document creation. The getOrder method actually is not important but if you have many registered DocumentExtensions, maybe useful to force an execution order.

So, we want to reject a document creation when placed in a folder. Our objective is the preCreate method:

@Override
public void preCreate(Session session, Ref<Node> parentNode, Ref<File> content, Ref<Document> doc) throws 
    AccessDeniedException, ExtensionException {
  try {
    if (parentNode.get().getPath().equals("/okm:root/forbidden")) {
      throw new AccessDeniedException("No se puede aquí");
    }
  } catch (RepositoryException e) {
    new ExtensionException(e.getMessage());
  }
}

The hard work has been done. Now, to declare this class as an extension you need to add the @PluginImplementation annotation. This is the class once all the step have been finished:

<source lang="java"> @PluginImplementation public class MyDocumentExtension implements DocumentExtension {

 @Override
 public int getOrder() {
   return 0;
 }
 public void preCreate(Session session, Ref<Node> parentNode, Ref<File> content, Ref<Document> doc) throws 
     AccessDeniedException, ExtensionException {
   try {
     if (parentNode.get().getPath().equals("/okm:root/forbidden") && doc.get().getMimeType().equals("application/pdf")) {
       throw new AccessDeniedException("Can't upload PDF documents here");
     }
   } catch (RepositoryException e) {
     new ExtensionException(e.getMessage());
   }
 }
 @Override
 public void postCreate(Session session, Ref<Node> parNode, Ref<Node> docNode, Ref<Document> doc) {
 }

} </cource>