cancel
Showing results for 
Search instead for 
Did you mean: 

How to transform just the content?

opoplawski
Champ in-the-making
Champ in-the-making
I'd like to be able to transform just the content of a document without automatically creating a new document (which causes problems with versioning….).  The idea would be something like:

  var pdfWorkingCopy = pdffile.checkout();
  pdfWorkingCopy.content = transformContent("application/pdf",document.content);
  pdfWorkingCopy.checkin("",false);
Is this possible?
3 REPLIES 3

uzi
Champ in-the-making
Champ in-the-making
The JavaScript API appears to require a new ScriptNode to be created which contains the transformed data.

One option would be to copy the data from the new file back into the old (and then discard the new file).  This is a bit wasteful, perhaps, but I think it also presents a problem for you due to versioning.  Not entirely sure what that means but let's assume this won't work.

A better option is one that leverages the extensibility of Alfresco's platform.  With Alfresco, you can write any custom repository action that you want and then invoke it from JavaScript.  This means that things are flexible but it also means that it is a bit more involved.

To do so, you can write a Spring Bean which implements a repository Action.  This is then available in JavaScript via the Actions API.  Suppose you wrote an action called "mytransform" - you could then do something like the following in JavaScript:

var transformer = actions.create("mytransform");
transformer.parameters.targetMimetype = "pdf";
transformer.execute(doc);

You could have your action take the content stream from the current document, transform it to the specified mimetype and then plug it back into the object.  You could adjust input parameters to suit whatever your need.

To build a custom action, you would have get comfortable with the ActionExecuter pattern in the Java code.  Since Alfresco is open source, you can take a look at how the product looks and how it presently does transformations.  Specifically, take a look at: 

org.alfresco.repo.action.executer.TransformActionExecuter

This is an implementation of a transformer action that you could base your custom action on.

There may be a much easier way but this is certainly an option.  It gives you the maximum amount of control, allows you to consolidate your code and gives you a nice reusable bit of logic that you can plug into your Alfresco repository framework.

Michael

opoplawski
Champ in-the-making
Champ in-the-making
It seems like what I need is already in the underlying Java API.  TransformActionExecutor.doTransform() takes a sourceNodeRef and contentReader and a destinationNodeRef and contentWriter.

What I can't figure out (as a complete noob to JavaScript->Java interaction) is what connects the "documentTransform()" javascript call to a java call (presumably TransformActionExecutor.executeImpl())?

Thanks for the help.

opoplawski
Champ in-the-making
Champ in-the-making
Actually, I think I'm figuring it out, off to try some code…