title: Javascript to Dynamically Create Spaces/Folders in Alfresco Doesn't Work Correctly
body: <p>I am currently working with Alfresco 4.0.d community edition (I also tried this on Alfresco 4.0.c community edition) on a Oracle Linux 6 64bit virtual machine, using Firefox as my browser.</p>
  
  <p>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.</p>
  
  <p>Essentially, say you have a newly created piece of content called <strong>fileOne</strong>. This file will sit in a space called <strong>mainFolder</strong>, and when a rule is run on the file to execute a script, the script will look to see if sub-folders exist based on the <strong>title</strong> and <strong>author</strong> values of the file. If not, it will create sub-folders within that <strong>mainFolder</strong> based on the <strong>title value</strong> --> sub-folder, and <strong>author value</strong> --> sub-sub-folder of that file. And then I want that content/file to be moved over to that either that existing or newly created sub-sub folder.</p>
  
  <p>Ultimately I want something that looks like:</p>
  
  <pre><code>Space: mainFolder
  |---&gt; Content: fileOne --&gt; Properties: Name, Title, Author
  |---&gt; Space: Title
        |---&gt; Space: Author
              |---&gt; Content: fileOne --&gt; Properties: Name, Title, Author 
  </code></pre>
  
  <p>Below is the script...</p>
  
  <pre><code>function main()
  {
      //mainFolder space/folder MUST exist under companyhome.
      var rootSpaceName = companyhome.childByNamePath("mainFolder");
  
      if(rootSpaceName == null)
      { 
          logger.log("Company Home/mainFolder does not exist, so we have nothing to do.");
          return;
      }
      else
      {
          logger.log("Company Home/mainFolder exists, so carry on our process.");
  
          var childList = rootSpaceName.children;
          var count = childList.length;
          var seconds = new Date().getTime() / 1000;
  
          if(count == 0)
          {
              logger.log("Company Home/mainFolder does not have child, nothing to do.");
              return;
          }
          else
          {
              for(var i = 0; i &lt; count; i++)
              {
                  // File Title MUST exist.
                  var childTitle = childList[i].properties.title;
  
                  if(childTitle == null)
                  {
                      childTitle = "New Title " + seconds;
                  }
  
                  // File Author MUST exist.
                  var childAuthor = childList[i].properties.author;
  
                  if(childAuthor == null)
                  {
                      childAuthor = "New Author " + seconds;
                  }
  
                  var child = childList[i];
                  logger.log("This child is: " + child);
  
                  if(child.isContainer == false)
                  {
                      for(var j = 0; j &lt; count; j++)
                      {
                          var newChildName = childList[j].properties.name;
                          logger.log("New child name: " + newChildName);
                          var newChild = childList[j];
  
                          if((newChild.isContainer == true) &amp;&amp; (childTitle == newChildName))
                          {
                              var newSpaceName = rootSpaceName.childByNamePath(newChildName);
                              var newChildList = newSpaceName.children;
                              var newCount = newChildList.length;
  
                              for(var k = 0; k &lt; newCount; k++)
                              {
                                  var newNewChildName = newChildList[k].properties.name;
                                  var newNewChildAuthor = newChildList[k].properties.author;
                                  var newNewChild = newChildList[k];
  
                                  if((newNewChild.isContainer == true) &amp;&amp; (newNewChildAuthor == childAuthor))
                                  {
                                      var currentSpace = newSpaceName.childByNamePath(newNewChildName);
                                      child.move(currentSpace);
                                  }
                              }
                          }
                          else
                          {
                              // If title folder is already created, not need to create again.
                              var newSpaceName = companyhome.childByNamePath("mainFolder/" + childTitle);
  
                              if(newSpaceName == null)
                              {
                                  newSpaceName = rootSpaceName.createFolder(childTitle);
                                  logger.log("mainFolder/" + childTitle + " is created.");
                              }
  
                              // If author folder is already created, not need to create again.
                              var newNewSpaceName = companyhome.childByNamePath("mainFolder/" + childTitle + "/" + childAuthor);
                              if(newNewSpaceName == null)
                              {
                                  newNewSpaceName = newSpaceName.createFolder(childAuthor);
                                  logger.log("mainFolder/" + childTitle + "/" + childAuthor + " is created.");
                              }
  
                              child.move(newNewSpaceName);
                              logger.log("Moving file " + child.properties.name);
                          }
  
                      }
  
                  }
  
              }
          }
      }
      return;
  }
  
  main();
  </code></pre>
  
  <p>So to test this, step-by-step here is what I do:</p>
  
  <ol>
  <li>I click on <strong>Company Home</strong>.</li>
  <li>I click on <strong>Create</strong>, <strong>Create Space</strong>.</li>
  <li>I then create a space called <strong>mainFolder</strong> (this is under Company Home).</li>
  <li>I then click on the newly created folder, then click on <strong>More Actions</strong>, <strong>Manage Content Rules</strong>.</li>
  <li>Then I click on <strong>Create Rule</strong>.
  <br>5a. Where it tells me to select a condition, I choose <strong>All Items</strong>, then I click <strong>Add to List</strong>, then I hit <strong>Next</strong>.
  <br><br>5b. Where it now tells me to select an action, I choose <strong>Execute Script</strong>, then I click <strong>Set Values and Add</strong>, which allows me to choose the script called <strong>new-create-folders.js</strong> which I had previously added to the <strong>Scripts</strong> folder within the <strong>Data Dictionary</strong>. Then I click <strong>OK</strong>, and then <strong>Next</strong>.
  <br><br>5c. Here where it asks me for the <strong>type</strong>, I choose <strong>Items are updated</strong>. I then enter a title like <strong>create folders rule</strong>. And finally I check <strong>Run rule in the background</strong>, then I hit <strong>Next</strong>.
  <br><br>5d. Now I click <strong>Finish</strong> to finalize my rule.</li>
  <li>Now with the rule created, I click <strong>Create</strong>, <strong>Create Content</strong>.
  <br><br>6a. I give my content a name like <strong>name001</strong>, then I hit <strong>Next</strong>, then I enter a title like <strong>title001</strong> and an author like <strong>author001</strong>. Then I hit <strong>OK</strong>.</li>
  </ol>
  
  <p>What happens now has got me confused; it creates one sub folder called <strong>title001</strong> like I wanted it to, and within that sub-folder it creates another folder called <strong>author001</strong>, and within that folder the file called <strong>name001</strong> has been moved. And that is sort of how I want it to work, but there are still problems. Under the <strong>mainFolder</strong>, there is also a new sub-folder called <strong>New Title 1346102873.393  New Title 1346102873.393</strong> with a folder inside of that called <strong>New Author 1346102873.393</strong>, but there is no file inside of that folder. And I don't know why this folder is created when I only added one piece of content to the <strong>mainFolder</strong>? Also, now that the folders have been created, the rule also looks to have been deleted from the <strong>mainFolder</strong>, i.e. there are no longer any rules applied to the folder. And when I go back to create the rule again on the <strong>mainFolder</strong>, I get the error: <code>Failed to create Rule due to error: null</code>. And below are the errors I get in the log files...</p>
  
  <p><strong>Output/Errors from Alfresco Log</strong></p>
  
  <pre><code>17:27:51,373 DEBUG [org.alfresco.repo.jscript.RhinoScriptProcessor] Imports resolved, adding resource '_root
  17:27:53,389 DEBUG [org.alfresco.repo.jscript.ScriptLogger] Company Home/mainFolder exists, so carry on our process.
  17:27:53,400 DEBUG [org.alfresco.repo.jscript.ScriptLogger] This child is: Node Type: {http://www.alfresco.org/model/content/1.0}systemfolder, Node Aspects: [{http://www.alfresco.org/model/content/1.0}auditable, {http://www.alfresco.org/model/system/1.0}referenceable, {http://www.alfresco.org/model/system/1.0}localized]
  17:27:53,400 DEBUG [org.alfresco.repo.jscript.ScriptLogger] New child name: b8ea2cb0-6fb6-481e-bb7b-d5364d9c509e
  17:27:53,422 DEBUG [org.alfresco.repo.jscript.ScriptLogger] mainFolder/New Title 1346102873.393 is created.
  17:27:53,439 DEBUG [org.alfresco.repo.jscript.ScriptLogger] mainFolder/New Title 1346102873.393/New Author 1346102873.393 is created.
  17:27:53,498 DEBUG [org.alfresco.repo.jscript.ScriptLogger] Moving file b8ea2cb0-6fb6-481e-bb7b-d5364d9c509e
  17:27:53,499 DEBUG [org.alfresco.repo.jscript.ScriptLogger] New child name: name001
  17:27:53,523 DEBUG [org.alfresco.repo.jscript.ScriptLogger] Moving file b8ea2cb0-6fb6-481e-bb7b-d5364d9c509e
  17:27:53,524 DEBUG [org.alfresco.repo.jscript.ScriptLogger] This child is: Node Type: {http://www.alfresco.org/model/content/1.0}content, Node Aspects: [{http://www.alfresco.org/model/content/1.0}auditable, {http://www.alfresco.org/model/system/1.0}referenceable, {http://www.alfresco.org/model/content/1.0}titled, {http://www.alfresco.org/model/content/1.0}author, {http://www.alfresco.org/model/system/1.0}localized, {http://www.alfresco.org/model/application/1.0}inlineeditable]
  17:27:53,524 DEBUG [org.alfresco.repo.jscript.ScriptLogger] New child name: b8ea2cb0-6fb6-481e-bb7b-d5364d9c509e
  17:27:53,534 DEBUG [org.alfresco.repo.jscript.ScriptLogger] mainFolder/title001 is created.
  17:27:53,546 DEBUG [org.alfresco.repo.jscript.ScriptLogger] mainFolder/title001/author001 is created.
  17:27:53,561 DEBUG [org.alfresco.repo.jscript.ScriptLogger] Moving file name001
  17:27:53,561 DEBUG [org.alfresco.repo.jscript.ScriptLogger] New child name: name001
  17:27:53,577 DEBUG [org.alfresco.repo.jscript.ScriptLogger] Moving file name001
  17:27:53,578 DEBUG [org.alfresco.repo.jscript.RhinoScriptProcessor] Time to execute script: 1976.9988ms
  17:31:13,385 ERROR [org.alfresco.web.ui.common.Utils] Failed to create Rule due to error: null
  java.lang.NullPointerException
      at org.alfresco.repo.rule.RuleServiceImpl.getLinkedToRuleNode(RuleServiceImpl.java:1516)
      at org.alfresco.repo.rule.RuleServiceImpl.isLinkedToRuleNode(RuleServiceImpl.java:1504)
      at org.alfresco.repo.rule.RuleServiceImpl.checkForLinkedRules(RuleServiceImpl.java:947)
      at org.alfresco.repo.rule.RuleServiceImpl.saveRule(RuleServiceImpl.java:740)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
      at org.alfresco.repo.security.permissions.impl.AlwaysProceedMethodInterceptor.invoke(AlwaysProceedMethodInterceptor.java:34)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      at org.alfresco.repo.security.permissions.impl.ExceptionTranslatorMethodInterceptor.invoke(ExceptionTranslatorMethodInterceptor.java:46)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      at org.alfresco.repo.audit.AuditMethodInterceptor.invoke(AuditMethodInterceptor.java:147)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
      at $Proxy53.saveRule(Unknown Source)
      at org.alfresco.web.bean.rules.CreateRuleWizard.finishImpl(CreateRuleWizard.java:148)
      at org.alfresco.web.bean.dialog.BaseDialogBean$1.execute(BaseDialogBean.java:123)
      at org.alfresco.web.bean.dialog.BaseDialogBean$1.execute(BaseDialogBean.java:119)
      at org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:388)
      at org.alfresco.web.bean.dialog.BaseDialogBean.finish(BaseDialogBean.java:129)
      at org.alfresco.web.bean.wizard.WizardManager.finish(WizardManager.java:593)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.apache.myfaces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:132)
      at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:61)
      at javax.faces.component.UICommand.broadcast(UICommand.java:151)
      at javax.faces.component.UIViewRoot._broadcastForPhase(UIViewRoot.java:115)
      at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:191)
      at org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:32)
      at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:105)
      at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:80)
      at javax.faces.webapp.FacesServlet.service(FacesServlet.java:143)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.alfresco.repo.web.filter.beans.SessionSynchronizedFilter.doFilter(SessionSynchronizedFilter.java:67)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.alfresco.web.app.servlet.AuthenticationFilter.doFilter(AuthenticationFilter.java:104)
      at sun.reflect.GeneratedMethodAccessor443.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.alfresco.repo.management.subsystems.ChainingSubsystemProxyFactory$1.invoke(ChainingSubsystemProxyFactory.java:116)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
      at $Proxy238.doFilter(Unknown Source)
      at org.alfresco.repo.web.filter.beans.BeanProxyFilter.doFilter(BeanProxyFilter.java:82)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.alfresco.repo.web.filter.beans.NullFilter.doFilter(NullFilter.java:68)
      at sun.reflect.GeneratedMethodAccessor443.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.alfresco.repo.management.subsystems.ChainingSubsystemProxyFactory$1.invoke(ChainingSubsystemProxyFactory.java:116)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
      at $Proxy238.doFilter(Unknown Source)
      at org.alfresco.repo.web.filter.beans.BeanProxyFilter.doFilter(BeanProxyFilter.java:82)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.alfresco.web.app.servlet.GlobalLocalizationFilter.doFilter(GlobalLocalizationFilter.java:58)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:470)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
      at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
      at java.lang.Thread.run(Thread.java:662)
  </code></pre>
  
  <p>Does anyone know why that additional folder is being created, as well as why the rule is being deleted after it executes on the newly created piece of content, and why I cannot add a new rule to the folder after it first deletes the original rule?</p>