AnsweredAssumed Answered

Problem With Javascript to Dynamically Create Spaces/Folders

Question asked by th1s_pr0gramm3r on Aug 22, 2012
Latest reply on Aug 23, 2012 by jpotts
Hi everyone, I am currently working with Alfresco 4.0.d community edition on a Oracle Linux 6 virtual machine, using Firefox as my browser.

Most recently, I had been attempting to create a script which could be executed via a rule that would dynamically create sub-folders as new items entered a space/folder.

Below is the script I created…


function main()
{
   var rootSpaceName = companyhome.childByNamePath("mainFolder");
   var childList = rootSpaceName.children;
   var count = childList.length;
   
   for(var i = 0; i <= count; i++)
   {
      var childTitle = childList[i].properties.title;
      var childAuthor = childList[i].properties.author;
      var child = childList[i];
      
      if(child.isContainer == false)
      {
         for(var j = 0; j < count; j++)
         {
            var newChildName = childList[j].properties.name;
            var newChild = childList[j];
            
            if((newChild.isContainer == true) && (childTitle.equals(newChildName)))
            {
               var newSpaceName = rootSpaceName.childByNamePath(newChildName);
               var newChildList = newSpaceName.children;
               var newCount = newChildList.length;
               
               for(var k = 0; k < newCount; k++)
               {
                  var newNewChildName = newChildList[k].properties.name;
                  var newNewChildAuthor = newChildList[k].properties.author;
                  var newNewChild = newChildList[k];
                  
                  if((newNewChild.isContainer == true) && (newNewChildAuthor.equals(childAuthor)))
                  {
                     var currentSpace = newSpaceName.childByNamePath(newNewChildName);
                     child.move(currentSpace);
                  }
               }
            }
            else
            {
               var newSpaceName = rootSpaceName.createFolder(childTitle);
               var newNewSpaceName = newSpaceName.createFolder(childAuthor);
               child.move(newNewSpaceName);
            }
         }
      }
   }
   return;
}

main();

The logic seems fine (at least in my head, but when I try to implement the script as admin, i.e. put it into the scripts folder within the data dictionary and try to create a rule to execute the script from within my "mainFolder", nothing happens. I tried using the Alfresco javascript debugger, and what I found was that the original
var rootSpaceName = companyhome.childByNamePath("mainFolder");
doesn't seem to find the correct folder, instead the value that it returns in the debugger is: A valid SecureContext was not provided in the RequestContext.

Then stepping into the script, I find that in the else condition, where it declares
var newSpaceName = rootSpaceName.createFolder(childTitle);
, I get a pop-up exception stating that
org.mozilla.javascript.WrappedExectution: Wrapped java.lang.IllegalArgumentException: Node Name is a mandatory parameter (workspace://SpacesStore/792dd872-5cfe-4a65-a509-4b9979f803d4#41)
. Which I assume is because the original root node cannot be found to begin with?

Does anyone see what the problems might be with this script, as well as what the aforementioned errors could be caused by? Additionally, I was trying to write out to the console in the Alfresco javascript debugger using
console.log("Write this out");
, but it seemed to keep resulting in an error stating the console was not specified. Does anyone know how to actually write out to the debugger console?

Outcomes