cancel
Showing results for 
Search instead for 
Did you mean: 

Problem creating new revisions from workflow

llacroix
Champ in-the-making
Champ in-the-making

//////////////////////////////////////////////////////
// File Name: move_version.js
//
// Purpose : Move a file from a folder to a target
// folder (which is parent folder). If file already exists
// in target folder it uses versioning to replace file
// and increase version number.
//
// Author: Julien Bonjean (julien@bonjean.info)
//
// Creation Date: 2009-02-10
// Last Modified: 2009-02-10 16:26:10 -0500
//////////////////////////////////////////////////////

var sourceFile = document;
var sourceFolder = document.parent;
var targetFolder = space.parent;
var targetFile = targetFolder.childByNamePath(document.name);

if(targetFile == null)
{
   sourceFile.move(targetFolder);
}
else
{
   var targetFileWorkingCopy = targetFile.checkout();
   targetFileWorkingCopy.content = sourceFile.content;
   targetFile = targetFileWorkingCopy.checkin();
   sourceFile.remove();
}

Here is the code we are using.It worked well with alfresco 2.1 but with alfresco 3.0. It doesn't work anymore. I think it worked with text file. But with pdf file. The new revision is an empty page. It produce corrupted odt files..etc. I don't know what's wrong. If there is something wrong with this scripts. Or it's just alfresco doing something without telling us?

By the way, the scripts was used to move a file from folder A to folder B. We look if the a file exists with the same name in folder B then we checkout the file…modify the content…then checkin and delete the source file that is now versionned.

Our problem is documented here.
http://wiki.alfresco.com/wiki/JavaScript_API_For_Alfresco_1.4_and_2.0#Modifying_and_Creating_API

The content property can only handle safely string content. In other words, the binary content get converted to the encoding we're using or the encoding of java most probably utf8. All non-ascii chars get converted to something else having the effect of corrupting versioned files.

We looked in the source code and we can't find any function or property that would allow us to get and set binary content.
1 REPLY 1

llacroix
Champ in-the-making
Champ in-the-making
//////////////////////////////////////////////////////
// File Name: move_version.js
//
// Purpose : Move a file from a folder to a target
// folder (which is parent folder). If file already exists
// in target folder it uses versioning to replace file
// and increase version number.
//
// Author: Julien Bonjean (julien.bonjean@savoirfairelinux.com)
//
// Creation Date: 2009-02-10
// Last Modified: 2009-02-18 16:26:10 -0500
//////////////////////////////////////////////////////

var sourceFile = document;
var sourceFolder = document.parent;
var targetFolder = space.parent;
var targetFile = targetFolder.childByNamePath(document.name);

if(targetFile == null && targetFile.hasPermission('WriteContent'))
{
        sourceFile.move(targetFolder);
}
else
{
        var targetFileWorkingCopy = targetFile.checkout();
   var content = sourceFile.properties['content'];
   targetFileWorkingCopy.properties['content'].write(content);
        targetFileWorkingCopy.checkin("",false);
        sourceFile.remove();
}


This version works…for people interested. It won't edit the binary data contained inside the sourceObject so you can move any kind of file… Smiley Happy