如何使用查询或脚本批量替换sling:resourceType值

时间:2016-05-09 16:32:40

标签: cq5 aem sling

如何使用Query and Scipt批量替换sling:resourceType值。

例如,我想更改sling:resourceType值 从app/component/linkButtonapp/component/content/linkbutton1

该组件正在20页上使用,我希望使用查询而不是每页手动更改它。

5 个答案:

答案 0 :(得分:5)

目的的最佳选择是groovy console

完成工作的Bellow脚本:

import javax.jcr.Node

getNode('/content/').recurse { resourceNode ->
    if (resourceNode.hasProperty('sling:resourceType')) {
        final def resourceType = resourceNode.getProperty('sling:resourceType').string
        if (resourceType.equals('OLD_RESOURCE_TYPE')) {
            println "changing " + resourceNode.path
            resourceNode.setProperty('sling:resourceType', 'NEW_RESOURCE_TYPE')
            resourceNode.save();
        }
    }
}

答案 1 :(得分:1)

您可以使用包含ACS AEM ToolsAEM Fiddle开源项目。 AEM Fiddle允许您直接在AEM实例上运行脚本,而无需构建。

如果您使用AEM小提琴,请导航至http://localhost:4502/miscadmin#/etc/acs-tools/aem-fiddle,点击右上角的加号,然后选择.java。插入此代码并运行。确保更新查询的路径。

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;

import javax.jcr.query.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;

public class fiddle extends SlingAllMethodsServlet {

    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        response.setStatus(HttpServletResponse.SC_OK);
        PrintWriter out = response.getWriter();
        ResourceResolver resolver = null;

        out.println("starting...");

        try {
            resolver = request.getResourceResolver();

            if (resolver != null) {
                Iterator<Resource> resources = resolver.findResources("/jcr:root/content/mysite//*[@sling:resourceType='app/component/linkButton']", Query.XPATH);

                while (resources.hasNext()) {
                    Resource resource = resources.next();
                    ModifiableValueMap properties = resource.adaptTo(ModifiableValueMap.class);
                    properties.put("sling:resourceType", "app/component/linkButton1");
                    resolver.commit();
                    out.println(resource.getPath());
                }
            }
        } catch(Exception e) {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            e.printStackTrace(out);
        } finally {
            if (resolver != null && resolver.isLive()) {
                resolver.close();
                resolver = null;
            }
        }

        out.println("...finished");
    }
}

如果您更喜欢使用JSP,那么代码是相同的:

<%@include file="/libs/foundation/global.jsp"%><%
%><%@page session="false" contentType="text/html; charset=utf-8" 
pageEncoding="UTF-8"
import="org.apache.sling.api.resource.*,
java.util.*,
javax.jcr.*,
com.day.cq.search.*,
com.day.cq.wcm.api.*,
com.day.cq.dam.api.*,
javax.jcr.query.Query,
org.apache.sling.api.resource.ModifiableValueMap"%><%

    Iterator<Resource> resources = resourceResolver.findResources("/jcr:root/content/mysite//*[@sling:resourceType='app/component/linkButton']", Query.XPATH);

    while (resources.hasNext()) {
        Resource current = resources.next();
        ModifiableValueMap props = current.adaptTo(ModifiableValueMap.class);
        props.put("sling:resourceType", "app/component/linkButton1");
        resourceResolver.commit();
%>
    <%=current.getPath()%>
<%
    }
%>

答案 2 :(得分:0)

另一种肮脏的方法,但对我有用。 :)

  1. 打包路径并下载zip文件。
  2. 提取到文件夹。
  3. 根据您的操作系统,
    • 如果使用Windows,请使用Notepad ++在目录下的所有文件中使用搜索模式查找替换。
    • 如果是linux,请使用find或sed命令替换导演中的所有实例

答案 3 :(得分:0)

AEM ACS TOOLS怎么样?

它是用于sling:resourceTypecq:Template的批量更新工具。

Click here for the article on Getting Started

Click here for the Github Repo

祝你好运...

答案 4 :(得分:0)

您也可以看看吊索。

https://sling.apache.org/documentation/bundles/sling-pipes.html

这是解决您问题的完美解决方案