{"id":2474,"date":"2016-07-29T19:19:00","date_gmt":"2016-07-29T19:19:00","guid":{"rendered":"https:\/\/site.axamit.com\/?p=370"},"modified":"2024-01-09T06:40:26","modified_gmt":"2024-01-09T06:40:26","slug":"groovyconsole","status":"publish","type":"post","link":"https:\/\/axamit.com\/en\/blog\/adobe-experience-manager\/groovyconsole\/","title":{"rendered":"Bath Processing Content in AEM with Groovy Console"},"content":{"rendered":"<div class=\"custom-toc\"><ul><\/ul><\/div>\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<h2 class=\"wp-block-heading\">Tasks<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">One of the tasks that <a href=\"https:\/\/axamit.com\/en\/adobe-experience-cloud\/adobe-experience-manager\/hire-aem-developers\/\">AEM developers<\/a> may meet is JCR content processing. Sometimes you need to update\/modify\/package\/ analyze an immense amount of data, and doing this in a live environment makes it twice as difficult.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here are some typical cases:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Changing the content structure (for example, news\/feb\/event -&gt; news\/2015-2016\/feb\/event);<\/li>\n\n\n\n<li>Modifying\/fixing content (for example, need to change \u2018colour\u2019 on \u2018color\u2019 on every page);<\/li>\n\n\n\n<li>Analyzing content (on MSM, editors use assets from another website, need to find all wrong usages);<\/li>\n\n\n\n<li>Preparing and packaging content (e.g., during migration from CQ5.x to AEM6.x, you need to analyze content first and create a valid package for the migration).<\/li>\n<\/ul>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<h2 class=\"wp-block-heading\">Solutions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">There are several solutions and workarounds for each case:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Servlets\/JSP\/Scriptlets<\/li>\n\n\n\n<li>AEM Util packagesDiscover how you can simplify batch content processing in Adobe Experience Manager using Groovy Console, making content updates, analysis, and migration a breeze in a live environment.<\/li>\n\n\n\n<li>External tools<\/li>\n\n\n\n<li>Groovy Console<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s focus on the Groove Console. It\u2019s a silver bullet when it comes to solving issues like those mentioned above.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here are a few advantages of Groovy Console:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Does not mess up project code;<\/li>\n\n\n\n<li>Short and clear scripts;<\/li>\n\n\n\n<li>Predefined services\/methods.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<h2 class=\"wp-block-heading\">Getting Started<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The AEM Groovy Console is hosted on&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/github.com\/Citytechinc\/cq-groovy-console\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/Citytechinc\/cq-groovy-console<\/a>&nbsp;and is available for versions AEM\/CQ5 starting from CQ5.4. Once the Groovy Console is installed on a local AEM\/CQ5 instance, go to&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"http:\/\/\/etc\/groovyconsole.html\">http:\/\/:\/etc\/groovyconsole.html<\/a><\/p>\n\n\n\n<figure data-wp-context=\"{&quot;imageId&quot;:&quot;6a334dc828d3c&quot;}\" data-wp-interactive=\"core\/image\" data-wp-key=\"6a334dc828d3c\" class=\"wp-block-image size-full wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"1193\" height=\"843\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on--click=\"actions.showLightbox\" data-wp-on--load=\"callbacks.setButtonStyles\" data-wp-on--pointerdown=\"actions.preloadImage\" data-wp-on--pointerenter=\"actions.preloadImageWithDelay\" data-wp-on--pointerleave=\"actions.cancelPreload\" data-wp-on-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/d11cw4yu2zh4tp.cloudfront.net\/wp-content\/uploads\/2016\/07\/09064022\/groovyconsole_2.webp\" alt=\"\" class=\"wp-image-6639\" srcset=\"https:\/\/d11cw4yu2zh4tp.cloudfront.net\/wp-content\/uploads\/2016\/07\/09064022\/groovyconsole_2.webp 1193w, https:\/\/d11cw4yu2zh4tp.cloudfront.net\/wp-content\/uploads\/2016\/07\/09064022\/groovyconsole_2-300x212.webp 300w, https:\/\/d11cw4yu2zh4tp.cloudfront.net\/wp-content\/uploads\/2016\/07\/09064022\/groovyconsole_2-1024x724.webp 1024w, https:\/\/d11cw4yu2zh4tp.cloudfront.net\/wp-content\/uploads\/2016\/07\/09064022\/groovyconsole_2-768x543.webp 768w\" sizes=\"auto, (max-width: 1193px) 100vw, 1193px\" \/><button\n\t\t\tclass=\"lightbox-trigger\"\n\t\t\ttype=\"button\"\n\t\t\taria-haspopup=\"dialog\"\n\t\t\tdata-wp-bind--aria-label=\"state.thisImage.triggerButtonAriaLabel\"\n\t\t\tdata-wp-init=\"callbacks.initTriggerButton\"\n\t\t\tdata-wp-on--click=\"actions.showLightbox\"\n\t\t\tdata-wp-style--right=\"state.thisImage.buttonRight\"\n\t\t\tdata-wp-style--top=\"state.thisImage.buttonTop\"\n\t\t>\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewBox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\" \/>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The following predefined variables are immediately available for any groovy script:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>session &#8211; javax.jcr.Session<\/li>\n\n\n\n<li>pageManager &#8211; com.day.cq.wcm.api.PageManager-resourceResolver-org.apache.sling.api.resource.ResourceResolver<\/li>\n\n\n\n<li>slingRequest &#8211; org.apache.sling.api.SlingHttpServletRequest-queryBuilder-com.day.cq.search.QueryBuilder-bundleContext-org.osgi.framework.BundleContext-log-org.slf4j.Logger<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">methods (some of them):<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>getPage(String path) &#8211; Get the Page for the given path, or null if it does not exist.<\/li>\n\n\n\n<li>getNode(String path) &#8211; Get the Node for the given path. Throws javax.jcr.RepositoryException if it does not exist.<\/li>\n\n\n\n<li>activate(String path) &#8211; Activate the node at the given path.<\/li>\n\n\n\n<li>deactivate(String path) &#8211; Deactivate the node at the given path.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">And imports:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>com.day.cq.search<\/li>\n\n\n\n<li>com.day.cq.tagging<\/li>\n\n\n\n<li>com.day.cq.wcm.api<\/li>\n\n\n\n<li>com.day.cq.replication<\/li>\n\n\n\n<li>javax.jcr<\/li>\n\n\n\n<li>org.apache.sling.api<\/li>\n\n\n\n<li>org.apache.sling.api.resource<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">You can also use history and script archives. These features allow you to solve these issues in an easy and elegant way.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s say we need to find pages with the \u201cbaking\u201d word in the title and replace it with \u201cbanking.\u201d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import com.day.cq.commons.jcr.JcrConstants\ndef search =  \"Baking\"\ndef replace = \"Banking\"\ndef path = \"\/content\/geometrixx\"\ndef property = JcrConstants.JCR_TITLE;\ndef query = createSQL2Query(path,  search , property)\ndef result = query.execute()\nresult.nodes.each{node -&gt; \n    def title = node.get(property)Learn what <a href=\"https:\/\/axamit.com\/en\/blog\/adobe-experience-manager\/aem-dispatcher\/\">AEM Dispatcher<\/a> is and how it combines the capabilities of the Static Web Server and Content Management Server for better caching, load balancing, security, and request processing management.\n    node.set(JcrConstants.JCR_TITLE, title.replaceAll(search ,replace))\n    println node.path\n}\nsave()  \ndef createSQL2Query(path, term, property) {\n    def queryManager = session.workspace.queryManager\n    def statement = \"SELECT * FROM &#91;cq:PageContent] AS s WHERE ISDESCENDANTNODE(&#91;${path}]) and s.&#91;${property}] like '%${term}%'\"\n    def query = queryManager.createQuery(statement, \"JCR-SQL2\")\n    query\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">As you can see, the Groovy console solution is straightforward and logical.<\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Tasks One of the tasks that AEM developers may meet is JCR content processing. Sometimes you need to [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":5015,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[22],"tags":[],"class_list":["post-2474","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-adobe-experience-manager"],"acf":{"cta":"Give Us a Call","phone":"+1 (438) 383-6878","meta-description":"Discover how you can simplify batch content processing in Adobe Experience Manager using Groovy Console, making content updates, analysis, and migration a breeze in a live environment."},"_links":{"self":[{"href":"https:\/\/axamit.com\/en\/wp-json\/wp\/v2\/posts\/2474","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/axamit.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/axamit.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/axamit.com\/en\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/axamit.com\/en\/wp-json\/wp\/v2\/comments?post=2474"}],"version-history":[{"count":0,"href":"https:\/\/axamit.com\/en\/wp-json\/wp\/v2\/posts\/2474\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/axamit.com\/en\/wp-json\/wp\/v2\/media\/5015"}],"wp:attachment":[{"href":"https:\/\/axamit.com\/en\/wp-json\/wp\/v2\/media?parent=2474"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/axamit.com\/en\/wp-json\/wp\/v2\/categories?post=2474"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/axamit.com\/en\/wp-json\/wp\/v2\/tags?post=2474"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}