【问题标题】:JcrExportCommand filter to exclude "mgnl:page" in magnolia cmsJcrExportCommand 过滤器以排除 magnolia cms 中的“mgnl:page”
【发布时间】:2019-12-02 13:11:27
【问题描述】:

当我在具有自定义操作的节点上执行它时,我想过滤掉 magnolia 中 JcrExportCommand 的“mgnl:page”节点。

我在下面的代码中编写的过滤器不起作用。它仍然给我导出文件中的 mgnl:page 子节点。

//set filter to only export mgnl:area subnodes
    DefaultFilter filter = new JcrExportCommand.DefaultFilter();
    NodeFilteringPredicate nodePredicate = new NodeFilteringPredicate();
    nodePredicate.setNodeTypes(Lists.newArrayList("mgnl:area"));
    filter.setNodePredicate(nodePredicate);

如何设置正确的过滤器来导出除“mgnl:page”子节点之外的所有内容?我相信将 NodeFilteringPredicate 设置为 "mgnl:area" 我只会得到该类型的节点。

【问题讨论】:

    标签: magnolia


    【解决方案1】:

    您必须在JcrExportCommand 上设置过滤器才能使其生效:

    DefaultFilter filter = new DefaultFilter();
    filter.getNodePredicate().getNodeTypes().add("mgnl:page");
    jcrExport.setFilter(Collections.singletonMap("website", filter));
    

    【讨论】:

    • 谢谢@michid 我测试了它并且它工作但由于顶级节点的类型为 mgnl:page 它会从导出的文件中删除它以及它下面的所有内容:) 我想创建一个过滤器将保留顶级页面节点并删除导出文件中所有 mgnl:page 类型的子节点。我可以通过过滤器来实现吗?
    • 在这种情况下,您可能必须实现自己的谓词来决定要保留哪些节点。从AbstractPredicate 继承并在那里实现您的逻辑,并通过调用JcrExportCommand.DefaultFilter#setNodePredicate() 在默认过滤器中设置您的谓词。
    • 我一无所知。我建议您首先从NodeFilteringPredicate 派生谓词并覆盖evaluate() 方法。在这里,您必须确定传递的节点是否为页面以及是否为顶级节点。你可以通过查看它的父母来做到这一点。
    【解决方案2】:

    * 这不是我问题的答案,而是评论的答案,因为评论中的代码格式不正确 *

    正如@michid 建议的那样,我创建了一个自定义预测器并使用 JcrExportCommand.DefaultFilter#setNodePredicate() 来应用它。

    我希望根据 Predicate 得到一个带有过滤节点的导出 YAML,但我仍然得到所有节点(包括 mgnl:page 类型的子节点)。

    我的自定义谓词类是:

    public class MyPredicate extends NodeFilteringPredicate {
    
        public boolean evaluate(Node node) throws AccessDeniedException, ItemNotFoundException, RepositoryException {
                //only nodes that are not of type mgnl:page 
                if((node.getParent().getPrimaryNodeType().getName().contains("mgnl:page"))&&(node.getPrimaryNodeType().getName().contains("mgnl:page"))) {
                    return false;
                }else{
                    return true;
                }
        }
    }
    

    我的自定义 Action 类是:

    public class MyAction extends AbstractMultiItemAction<UzhVersioning>  {
    
    
    private AbstractPredicate<Node> MyPredicate;
    
    
    
    public MyAction(xxxVersioning definition, JcrItemAdapter item, UiContext uiContext) {
        super(definition, item, uiContext);
        // TODO Auto-generated constructor stub
    }
    
    
    
    
    @Override
    public void execute() {
    
        //export nodes from a JCR workspace
        JcrExportCommand exporter = new JcrExportCommand();
        //sets export format to yaml
        exporter.setFormat("yaml");
        exporter.setRepository("website");
    
    
        //set filter to only export top mgnl:page and its mgnl:area nodes
        DefaultFilter filter = new JcrExportCommand.DefaultFilter();
        AbstractPredicate<Node> predicate = new MyPredicate();
        filter.setNodePredicate(predicate);
        exporter.setFilters(Collections.singletonMap("website", filter));
    
    
    
        //setup the root directory for exports
        File rootDir = new File("/Users/asusti/Downloads/yamlExport");
        // clean up first
        rootDir.delete();
        rootDir.mkdirs();
    
        //get root node
        Node node = (Node) getItems().get(0).getJcrItem();
    
    
        try {
            //set export path
            exporter.setPath(node.getPath());
            File file = new File(rootDir, node.getName()+".yaml");
            FileOutputStream out = new FileOutputStream(file);
            exporter.setOutputStream(out);
            exporter.execute(MgnlContext.getInstance());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-14
      • 1970-01-01
      • 2013-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多