3

我目前正在 Oracle Linux 6 64 位虚拟机上使用 Alfresco 4.0.d 社区版(我也在 Alfresco 4.0.c 社区版上尝试过),使用 Firefox 作为我的浏览器。

最近,我一直在尝试创建一个脚本,该脚本可以通过在新项目进入空间/文件夹时动态创建子文件夹的规则来执行。

本质上,假设您有一个名为fileOne的新创建的内容。该文件将位于名为mainFolder的空间中,当在文件上运行规则以执行脚本时,脚本将根据文件的标题作者值查看是否存在子文件夹。如果没有,它将根据该文件的标题值--> 子文件夹和作者值--> 子子文件夹在该mainFolder中创建子文件夹。然后我希望将该内容/文件移到现有或新创建的子文件夹中。

最终我想要的东西看起来像:

Space: mainFolder
|---> Content: fileOne --> Properties: Name, Title, Author
|---> Space: Title
      |---> Space: Author
            |---> Content: fileOne --> Properties: Name, Title, Author 

下面是脚本...

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 < 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 < count; j++)
                    {
                        var newChildName = childList[j].properties.name;
                        logger.log("New child name: " + newChildName);
                        var newChild = childList[j];

                        if((newChild.isContainer == true) && (childTitle == 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 == 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();

所以为了测试这个,我一步一步地做:

  1. 我点击公司主页
  2. 我点击CreateCreate Space
  3. 然后,我创建了一个名为mainFolder的空间(位于 Company Home 下)。
  4. 然后我单击新创建的文件夹,然后单击More ActionsManage Content Rules
  5. 然后我点击Create Rule
    5a。它告诉我选择一个条件,我选择All Items,然后单击Add to List,然后单击Next

    5b。现在它告诉我选择一个动作,我选择Execute Script,然后单击Set Values 和 Add,这允许我选择名为new-create-folders.js的脚本,我之前已将其添加到Data中的Scripts文件夹中字典。然后我单击确定,然后单击下一步

    5c。在这里它要求我输入类型,我选择项目已更新。然后我输入一个标题,如创建文件夹规则。最后我在后台检查运行规则,然后点击下一步

    5d。现在我单击完成以完成我的规则。
  6. 现在创建规则后,我单击CreateCreate Content

    6a。我给我的内容起一个名字,比如name001,然后我点击Next,然后我输入一个标题,比如title001和一个作者,比如author001。然后我点击OK

现在发生的事情让我感到困惑;它像我想要的那样创建了一个名为title001的子文件夹,并在该子文件夹中创建了另一个名为author001的文件夹,并且在该文件夹中名为name001的文件已被移动。这就是我希望它工作的方式,但仍然存在问题。在mainFolder下,还有一个名为New Title 1346102873.393 New Title 1346102873.393的新子文件夹,其中有一个名为New Author 1346102873.393的文件夹,但该文件夹内没有文件。而且不知道为什么只在mainFolder中添加了一条内容就创建了这个文件夹? 此外,既然已经创建了文件夹,该规则看起来也已从mainFolder中删除,即不再有任何规则应用于该文件夹。当我返回在mainFolder上再次创建规则时,我收到错误:Failed to create Rule due to error: null. 以下是我在日志文件中遇到的错误...

Alfresco 日志的输出/错误

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)

有谁知道为什么要创建该附加文件夹,以及为什么在对新创建的内容执行该规则后要删除该规则,以及为什么在第一次删除原始规则后我无法向该文件夹添加新规则?

4

5 回答 5

3

听起来您的规则运行了多次并且自己绊倒了。会不会是脚本本身正在执行的某些操作导致它再次运行?

检查您的规则是否未应用于子文件夹,而不是在向导的条件部分中选择“所有项目”,而是尝试更具体的内容,例如所有内容项目。

于 2012-08-29T11:47:29.097 回答
3

我在 Alfresco 论坛的同一张海报上发布了这个确切问题的答案。答案是代码尝试使用与文档相同的名称创建文件夹。节点必须在文件夹中唯一命名,因此这是不允许的。

于 2012-08-22T23:51:36.050 回答
2

实际上我认为的问题应该是规则被多次触发,条件规则“所有项目”正在更新。所以每个节点都会在存储库中收到更新,在带有规则的空间内(也许还有子文件夹,我会检查一下),它会触发并可能创建一个循环。您是否尝试过使用带有“创建节点时”触发器的规则?

更新:

因此,我尝试在 Alfresco 3.4.7 Enteprise 和 4.0 Community x64(Linux 和 Windows)上对此进行调试。

  • 我在 Alfresco 内的 Scripts 目录中创建了一个名为 /Company Home/Data Dictionary 的 js 文件。
  • 然后我在 Company Home 空间内创建了一个文件夹 mainFolder。
  • 我在文件夹 mainFolder 中上传了一个新的 PDF 文件。
  • 然后我对 PDF 文件执行 fs 文件。因此,进入文档的详细视图,然后单击“运行操作”-“执行脚本”。
  • 在那里,一切正常!

所以最后,问题出在规则执行中,它在“所有项目”上触发,因此,当脚本创建具有标题名称的新文件夹时,它会递归触发。我尝试使用“仅 PDF 类型”节点执行规则,它就像一个魅力!

希望这可以帮助...

于 2012-08-31T11:03:41.057 回答
1

可能您的要求是 mainFolder 中存在的文件具有标题和作者字段。您想将文件移动到标题->作者文件夹中。

就像那个 mainFolder -> FileName: file1, Title: title1, Author: author1

mainFolder -> title1 ->author1 ->file1

我尝试用我自己的代码编辑你的代码,并在 alfresco 端的 alfresco community 4.0c 中运行,并在 Windows 的共享端使用 java 脚本控制台。

为 mainFolder 创建规则时

1) 选择条件

1.1类型或子类型的内容作为选择条件

1.2内容作为设置条件值,不选中摘要下面的复选框 是项目是“内容”的子类型

2)选择与您相同的动作执行脚本

3) 创建或插入类型项目

我选择这些是因为动态运行我们的代码会创建文件夹。所以规则只适用于内容。除了取消选中后台执行规则外,其他与您相同。如果创建的项目没有标题名称,则 alfresco 默认假定项目名称为标题。

最后更新的代码:

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;

        if(count == 0)
        {
            logger.log("Company Home/mainFolder does not have child, nothing to do.");
            return;
        }
        else
        {
            for(var i = 0; i < count; i++)
            {   
                var child = childList[i];
                logger.log("This child is: " + child);

                // If each of child is file
                if(child.isDocument)
                {
                    // Get title of file and
                    // If childTitle is null, we set DEFAULT value
                    var childTitle = childList[i].properties.title;
                    if(childTitle == null)
                    {
                        childTitle = "New Title";
                    }
                    else if(childTitle == child.properties.name)
                    {
                        childTitle = "Title " + childTitle;
                    }

                    // Get author of file.
                    // If childAuthor is null, we set DEFAULT value
                    var childAuthor = childList[i].properties.author;
                    if(childAuthor == null)
                    {
                        childAuthor = "New Author";
                    }
                    logger.log("Original Title :" + child.properties.title + "\tAuthor :" + child.properties.author);
                    logger.log("Setting Title :" + childTitle + "\tAuthor :" + childAuthor);

                    // If title folder does not exist, create title folder
                    var newSpaceTitle = companyhome.childByNamePath("mainFolder/" + childTitle);

                    if(newSpaceTitle == null)
                    {
                        newSpaceTitle = rootSpaceName.createFolder(childTitle);
                        logger.log("mainFolder/" + childTitle + " is created.");
                    }

                    // If author folder does not exist, create author folder
                    var newSpaceAuthor = companyhome.childByNamePath("mainFolder/" + childTitle + "/" + childAuthor);
                    if(newSpaceAuthor == null)
                    {
                        newSpaceAuthor = newSpaceTitle.createFolder(childAuthor);
                        logger.log("mainFolder/" + childTitle + "/" + childAuthor + " is created.");
                    }

                    child.move(newSpaceAuthor);
                    logger.log("Moving file " + child.properties.name);
                }
            }
        }
    }
    return;
}

main();
于 2012-08-23T13:16:58.363 回答
1

该行:

 var rootSpaceName = companyhome.childByNamePath("mainFolder");

将尝试定位相对于companyhome存储库根目录的文件夹。该文件夹是否存在于您的存储库中?

如果您正在尝试编写一个对进入空间的文件进行操作的脚本,那么您是否不需要使用相对于该文件或空间的路径(使用documentorspace对象,而不是companyhome)?

如果没有,也许您可​​以多解释一下您要达到的目标?

如果您还没有使用它,那么 Alfresco JavaScript 控制台是一个非常有用的附加组件,用于尝试 JavaScript 代码,您可以使用logger.log().

于 2012-08-22T23:17:53.550 回答