Jcr导出命令过滤器以排除木兰 cms 中的"mgnl:page"



当我使用自定义操作在节点上执行 JcrExportCommand 时,我想在 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"我只会得到该类型的节点。

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

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

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

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

我希望获得一个导出的 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;
}
}
}

我的自定义操作类是:

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();
}

最新更新