写点什么

创建基于查询参数的包

  • 2011-08-03
  • 本文字数:1751 字

    阅读完需:约 6 分钟

目录

要求

用户级别

入门级

示例文件

在使用内容管理系统时,我们往往需要根据某些参数提取内容。一种极其常见的用例就是依据最后一次同步的日期或者生产系统中内容的最后修改日期,更新生产中的存储或者开发系统。我们可以通过编写一个简单的组件来轻松解决这个问题,首先查询存储库,并使用搜索结果来在服务器上创建包。

查询存储库

Day OOTB 提供了搜索存储库的组件,例如 http://localhost:4502/libs/cq/search/content/querydebug.hml http://localhost:4502/crx/ui/search.jsp

我们可以根据查询参数进行查询,也可在一次查询中指定 XPath/SQL(例如,搜索在 2010 年 4 月 1 日之后更改的所有内容)。

基于参数的搜索

Onhttp://localhost:4502/libs/cq/search/content/querydebug.html, type the params in the text area below:

http://localhost:4502/libs/cq/search/content/querydebug.html 中,在文本区域内输入以下参数:

复制代码
type=cq:Page
path=/content
daterange.property=jcr:content/cq:lastModified
daterange.lowerBound=2010-04-01
orderby=@jcr:content/cq:lastModified
orderby.index=true
orderby.sort=asc

基于 XPath 的查询

http://localhost:5502/crx/ui/search.jsp 中使用 XPath,如下所示:

复制代码
/jcr:root/content//element(*, cq:Page)
[
(@jcr:content/cq:lastModified > xs:dateTime('2010-04-01T00:00:00.000+05:30'))
]
order by jcr:content/@cq:lastModified

我们还可以创建自己的组件来进行搜素,方法是创建一个基于所传入的属性(参数)的 PredicateGroup,随后即可利用一个生成器对象(使用 predicateGroup 和可用 jcr 会话创建)来创建查询,如下所示:

复制代码
QueryBuilder builder = resource.getResourceResolver().adaptTo(QueryBuilder.class);
Session session = resource.getResourceResolver().adaptTo(Session.class);
String queryParam = request.getParameter(“query”);
Properties props = new Properties();
props.load(new ByteArrayInputStream(queryParam.getBytes(“ISO-8859-1″)));
PredicateGroup root = PredicateConverter.createPredicates(props);
// avoid slow //* queries
if (!root.isEmpty())
{
query = builder.createQuery(root, session);
query.setHitsPerPage(0);
}
<p>SearchResult result = query.getResult();</p>

在 SearchResult 对象中,我们可以提取 Hits (result.getHits),并遍历列表来显示结果。我们可以使用相同的列表来创建包,如下一节所述。

通过节点列表创建包

如果拥有节点列表,就可以通过创建过滤器并使用相同的过滤器来进行 Jcr 包定义,从而轻松创建包:

复制代码
DefaultWorkspaceFilter filter = new DefaultWorkspaceFilter();
for (Hit hit: hits)
{
PathFilterSet pathFilterSet = new PathFilterSet(hit.getPath());
filter.add(pathFilterSet);
}
JcrPackage jcrPackage = packMgr.create(“testGroup”,”testPackage”);
JcrPackageDefinition jcrPackageDefinition = jcrPackage.getDefinition();
jcrPackageDefinition.setFilter(filter, false);
PrintWriter pkgout = new PrintWriter(System.out);
packMgr.assemble(jcrPackage, new DefaultProgressListener(pkgout));

在执行由组名称为“testGroup”的一个包及其中的一个包构成的组合时,将创建一个“testPackage”包,并可通过包管理器访问。

我已经创建了一个示例组件,方法是扩展创建组名称为 testGroup 的包的默认搜索组件。如果已经存在具有相同名称的包,则将忽略请求,不会覆盖现有包。您可以在这里获得示例组件。

访问 http://servername:port/apps/packages/createPkgWithQuery/createPkg.html ,安装并使用包。您可以使用类似于下面这样的参数:

复制代码
type=cq:Page
path=/content
daterange.property=jcr:content/cq:lastModified
daterange.lowerBound=2010-04-01
orderby=@jcr:content/cq:lastModified
orderby.index=true
orderby.sort=asc

clip_image004

本文遵守知识共享—署名- 非商业性使用- 相同方式共享3.0 Unported License (Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License) 许可条件。

查看原文: Creating packages based on query parameters

2011-08-03 05:15982

评论

发布
暂无评论
发现更多内容
创建基于查询参数的包_Java_Yogesh Bahuguna_InfoQ精选文章