cancel
Showing results for 
Search instead for 
Did you mean: 

Control version storage bloat with Javascript?

sarnold
Champ in-the-making
Champ in-the-making
Hi All,

We are currently trailing Alfresco Community 4.0a with a view to using it in-house for document storage and management.

Firstly, none of this would be needed except that so far as I can see there is no way in Alfresco to limit the number of document versions that can be created.
So if I've missed something obvious please correct my understanding.

So I thought that perhaps we could put the "brakes" on an ever increasing file-store size by using a Javascript rule. Something along the lines of (in pseudo code):-

1. On document update check if document has the "versionable aspect":-
2. If it does then check the number if versions currently stored:-
3. If the number of versions is greater than say 10 then:-
4. Create a new document and fill it with lets say the most recent three versions of the document in question.
(we have to do it this way I guess because the javascript API appears to have NO mechanism for deleting a specific document version - true?)
5. Now delete the original
6. Then rename the new to match the original name.

So that's what I'm trying to do. If there is an easier way then please point me in the right direction.

My problem is I don't understand how to use document.versionHistory; and document.getVersion(); properly.

document.versionHistory returns an array like this:-

org.alfresco.repo.jscript.ScriptVersion@240f767d
org.alfresco.repo.jscript.ScriptVersion@732c1b09
org.alfresco.repo.jscript.ScriptVersion@54a326f1
org.alfresco.repo.jscript.ScriptVersion@2351c3a0
org.alfresco.repo.jscript.ScriptVersion@4d024f7d
org.alfresco.repo.jscript.ScriptVersion@52b0bf0a
org.alfresco.repo.jscript.ScriptVersion@51ce53ef
org.alfresco.repo.jscript.ScriptVersion@40e899de
org.alfresco.repo.jscript.ScriptVersion@66cddf1c
org.alfresco.repo.jscript.ScriptVersion@55f2dbfe
org.alfresco.repo.jscript.ScriptVersion@3b492cc7
org.alfresco.repo.jscript.ScriptVersion@411adb39
org.alfresco.repo.jscript.ScriptVersion@4a2e9c93
org.alfresco.repo.jscript.ScriptVersion@3a299cf1

(14 lines above one for each document version saved)

Disconcertingly every time the script is run the @hexadecimal numbers appear to all be totally different.

And if I try to retrieve a version of the document using say:-

document.getVersion("org.alfresco.repo.jscript.ScriptVersion@240f767d");

It just generates an error in the server side Javascript debugger saying that "org.alfresco.repo.jscript.ScriptVersion@240f767d" does not exist in version store "Workspace://SpaceStore/great-long-string-of-hex-digits"

Can someone give me just a few pointers on how I'm supposed to correctly use the Javascript 4.0 API to access and control document revisions?

Thanks!

Cheers Steve.

Update 1:

Using the Share Node browser I was able to verify that the store workspace being referenced by document.getVersion() was the node of the actual script and not the document.

I found that:-

space.getVersion("org.alfresco.repo.jscript.ScriptVersion@240f767d");

will execute without error. But:-

oldDoc = space.getVersion("org.alfresco.repo.jscript.ScriptVersion@240f767d");

Just assigns Null to oldDoc.

How does one use the getVersion API call?
3 REPLIES 3

gyro_gearless
Champ in-the-making
Champ in-the-making
Hi,

just a few thought on this:

  • Not sure if this can be done in Javascript, but for sure using a Behaviour (in Java).

  • The identifiers you get are for sure internal Javascript references, you would perhaps use something like "document.getVersion("1.42")"

  • Creating a new document would surely work, but is rather unelegant: It would break for example all other associations to that document. Maybe you dont use that now, buts its an rather elegant Alfresco feature….
HTH
Gyro

sarnold
Champ in-the-making
Champ in-the-making
Hi Gyro,

Thanks for taking the time to reply!

Creating a new document would surely work, but is rather unelegant: It would break for example all other associations to that document. Maybe you dont use that now, buts its an rather elegant Alfresco feature….

I hear you! The elegant solution would be to delete older versions. But the Javascript API has no call for removal of a document version. At least not yet. There is a request for such a feature in JIRA https://issues.alfresco.com/jira/browse/ALFCOM-2839. But it looks to have been "archived" back in 2009 and so I'm not sure if this is still on anyones to-do list.

I also understand that it can be done in Java. Sadly I've not done any programming in Java before so this will be a bit of a learning curve.

I did try document.getVersion("1.3") in my script and that worked. At least the code line executes without error. But I'm at a complete loss as to how to use it. I thought it would be something like:

var oldver = document.getVersion("1.3");
oldver.copy(space); // <– This does not work!

To get an old copy of the document and make a copy of it into a new document in the current document space. But it doesn't work. The correct syntax eludes me.

What I have managed to cobble together is a small script that sends out a "Nag" e-mail to the person who updates a document which has many revisions and directs the same message to the IT support department. Just in case anyone is following along I'll include it below.

Thanks again for your input.

Cheers Steve.

// Alfresco Server Side Javascript
// Script to send "Nag" emails if a document gains an excessive number of revisions
// One e-mail to the user who updated the document and a copy to the IT Dept
// Script set to run on document updates

var textBuffer = ""

// Test to see if the document in question is versioned. If not then just bail out

if (document.isVersioned) {

   // Grab the total number of revisions held

   NbrOfRevs = document.versionHistory.length;
   
   // Test below triggers on only 3 revisions for testing purposes
   // Set this number to something sensible for your environment

   if (NbrOfRevs > 3) {

      // create mail action
      var mail = actions.create("mail");
      mail.parameters.to = person.properties.email;
      mail.parameters.subject = "Large number of document revisions detected.";
      mail.parameters.from = "alfresco@fake.email.local";
      
      // Uncomment line below if you want to build and use your own
      // template document for the e-mail. I have not played with this yet.
      // With no template e-mail it uses the mail.parameters.text
      // for the message body.

      //mail.parameters.template = root.childByNamePath("Company Home/Data Dictionary/Email Templates/excessive_revisions_email.ftl");

      // Get the document name and the name of the submitting user
      var DocuName = document.properties.name;
      var PersonName = person.properties.firstName;

      // Build a text buffer containing the email body message.
      // Then assign this buffer to mail.parameters.text
      // I did try adding text directly to mail.parameters.text with a series of
      // mail.parameters.text += but got very unpredictable results.

      textBuffer = "Dear " + PersonName + "\n\n";
      textBuffer += "Warning! A document you just updated called:- \n\n";
      textBuffer += DocuName + "\n\n";
      textBuffer += "has more than 3 revisions currently stored in the Alfresco system.\n\n";
      textBuffer += "Could you please contact the IT Department for some instructions\n";
      textBuffer += "on how to remove any non essential older revisions of this document.\n\n";
      textBuffer += "Our file storage space is not unlimited.\n\n";
      textBuffer += "Thankyou.\n";

      mail.parameters.text = textBuffer;

      // Execute action against a document
      // This sends an e-mail to the person who did the document update
      mail.execute(document);
      
      // There seems to be no support for Cc: or Bcc: e-mails
      // Nor could I work out how to specifiy multiple To: addresses on the one line.
      // So to send the same message to the IT dept as well
      // we reset the mail.parameters.to and then mail.execute again
   
      mail.parameters.to = "itadmin@fake.email.address"; // Change e-mail address to the address of your support group
      mail.execute(document);

   }

}

sarnold
Champ in-the-making
Champ in-the-making
Not specifically related to Javascript but thought I'd mention that I have solved my problem of limiting the number of file revisions using an AMP add-on module written by Jared Ottley. You can get it here:-

http://jared.ottleys.net/alfresco/alfresco-max-version-policy

And "yes" it works correctly with Community 4.0.a