Expand my Community achievements bar.

SOLVED

update the tags in component when tags are moved in aem  content/cq:tags/a to  content/cq:tags/b

Avatar

Level 2

How to update the tags in component when tags are moved in aem  content/cq:tags/a to  content/cq:tags/b ,considering component has couple of tag fields and it has to be updated ?

 

These component references are not stored I checked all the ways but no luck!

 

Please share me the solution if anyone has it?

1 Accepted Solution

Avatar

Correct answer by
Level 4

Hi Jyothirmai,

I have an old fiddle file, but you need to make your changes on top of it then execute in Fiddle by following safety measures mentioned above.

Note - This is not Tested Code.

 

import com.day.cq.search.PredicateGroup;
import com.day.cq.search.Query;
import com.day.cq.search.QueryBuilder;
import com.day.cq.search.result.Hit;
import com.day.cq.search.result.SearchResult;
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;

import javax.jcr.RepositoryException;
import javax.jcr.Session;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalTime;
import java.util.*;


public class UpdateProperty1 extends SlingAllMethodsServlet {

    
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {

        // Code here
        PrintWriter out = response.getWriter();
        out.println("Start Time - " + LocalTime.now() + "<br>");
        Set<String> pathsSet = new HashSet<>();
        pathsSet.add("/content/abc");
        ResourceResolver resourceResolver = request.getResourceResolver();
        if (resourceResolver != null) {
            Session session = resourceResolver.adaptTo(Session.class);
            QueryBuilder queryBuilder = resourceResolver.adaptTo(QueryBuilder.class);
            Iterator<String> iterator = pathsSet.iterator();
            Set<String> pagesSet = new HashSet<>();
            while (iterator.hasNext()) {
                String contentPath = iterator.next();
                out.println("*******Processing for: " + contentPath + "*********<br><br>");
                if (queryBuilder != null && session != null) {
                    try {
                        fetchPages(contentPath, out, queryBuilder, session, resourceResolver, pagesSet);
                    } catch (RepositoryException e) {
                        out.println("RepositoryException - " + e.getMessage());
                    }
                } else {
                    out.println("querybuilder  or session is null");
                }
            }
            //Saving changes.
            /*resourceResolver.commit();
            resourceResolver.close();*/
        }
        out.println("End Time - " + LocalTime.now());
    }

    private void fetchPages(String rootPagePath, PrintWriter out, QueryBuilder queryBuilder, Session session, ResourceResolver
            resourceResolver, Set<String> pagesSet) throws RepositoryException {
        Map<String, String> predicateMap = new HashMap<>();
        predicateMap.put("type", "cq:Page");
        predicateMap.put("path", rootPagePath);
        predicateMap.put("p.self", "true");
        predicateMap.put("1_property", "jcr:content/cq:tags");
        predicateMap.put("1_property.value", "<old tag value>");
        predicateMap.put("1_property.operation", "contains");
        predicateMap.put("p.limit", "-1");
        Query query = queryBuilder.createQuery(PredicateGroup.create(predicateMap), session);
        SearchResult result = query.getResult();
        out.println("+++++++++Total Children: " + result.getTotalMatches() + "++++++++++<br>");
        List<Hit> hits = result.getHits();
        Iterator<Hit> hitsIterator = hits.iterator();
        int ch = 0;
        while (hitsIterator.hasNext()) {
            Hit hit = hitsIterator.next();
            String path = hit.getPath();
            out.println(++ch+". " + path + "<br>");
            addProperty(path + "/jcr:content", out, resourceResolver, "<new tag value>");
        }
    }

    private void addProperty(String pagePath, PrintWriter out, ResourceResolver resourceResolver, String newTemplateVal) {
        Resource pageJcrRes = resourceResolver.getResource(pagePath);
        ModifiableValueMap modifiableValueMap = pageJcrRes.adaptTo(ModifiableValueMap.class);
        if (modifiableValueMap != null) {
            /**
             * If the property is an array, you need to update array with new value, then set to property
             * */
            modifiableValueMap.put("cq:tags", newTemplateVal);
        }
    }
}

 

View solution in original post

5 Replies

Avatar

Level 4

 

One approach is, you shall use AEM fiddle(http://localhost:4502/etc/acs-tools/aem-fiddle.html) and run a script to update your tag references to new location. This approach has control while updating.

Steps to follow:

1. Query the node with ‘Old tag path’.

2. Iterate over the list of nodes and update the ‘cq:tags’ property.

3. Save the session.

4. You need to re-publish the updated nodes/pages (if required).

While publishing, make sure you check below things

  • Don’t Publish unpublished/never pages.
  • Don’t Publish Language masters.
  • Staging pages. (pages are modified after publish)

Note- please take backup of the respective content before you proceed.

Avatar

Level 2

Could u please share sample script ? and please do let me know if there are any other approaches too 

 

Thanks in Advance

Avatar

Correct answer by
Level 4

Hi Jyothirmai,

I have an old fiddle file, but you need to make your changes on top of it then execute in Fiddle by following safety measures mentioned above.

Note - This is not Tested Code.

 

import com.day.cq.search.PredicateGroup;
import com.day.cq.search.Query;
import com.day.cq.search.QueryBuilder;
import com.day.cq.search.result.Hit;
import com.day.cq.search.result.SearchResult;
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;

import javax.jcr.RepositoryException;
import javax.jcr.Session;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalTime;
import java.util.*;


public class UpdateProperty1 extends SlingAllMethodsServlet {

    
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {

        // Code here
        PrintWriter out = response.getWriter();
        out.println("Start Time - " + LocalTime.now() + "<br>");
        Set<String> pathsSet = new HashSet<>();
        pathsSet.add("/content/abc");
        ResourceResolver resourceResolver = request.getResourceResolver();
        if (resourceResolver != null) {
            Session session = resourceResolver.adaptTo(Session.class);
            QueryBuilder queryBuilder = resourceResolver.adaptTo(QueryBuilder.class);
            Iterator<String> iterator = pathsSet.iterator();
            Set<String> pagesSet = new HashSet<>();
            while (iterator.hasNext()) {
                String contentPath = iterator.next();
                out.println("*******Processing for: " + contentPath + "*********<br><br>");
                if (queryBuilder != null && session != null) {
                    try {
                        fetchPages(contentPath, out, queryBuilder, session, resourceResolver, pagesSet);
                    } catch (RepositoryException e) {
                        out.println("RepositoryException - " + e.getMessage());
                    }
                } else {
                    out.println("querybuilder  or session is null");
                }
            }
            //Saving changes.
            /*resourceResolver.commit();
            resourceResolver.close();*/
        }
        out.println("End Time - " + LocalTime.now());
    }

    private void fetchPages(String rootPagePath, PrintWriter out, QueryBuilder queryBuilder, Session session, ResourceResolver
            resourceResolver, Set<String> pagesSet) throws RepositoryException {
        Map<String, String> predicateMap = new HashMap<>();
        predicateMap.put("type", "cq:Page");
        predicateMap.put("path", rootPagePath);
        predicateMap.put("p.self", "true");
        predicateMap.put("1_property", "jcr:content/cq:tags");
        predicateMap.put("1_property.value", "<old tag value>");
        predicateMap.put("1_property.operation", "contains");
        predicateMap.put("p.limit", "-1");
        Query query = queryBuilder.createQuery(PredicateGroup.create(predicateMap), session);
        SearchResult result = query.getResult();
        out.println("+++++++++Total Children: " + result.getTotalMatches() + "++++++++++<br>");
        List<Hit> hits = result.getHits();
        Iterator<Hit> hitsIterator = hits.iterator();
        int ch = 0;
        while (hitsIterator.hasNext()) {
            Hit hit = hitsIterator.next();
            String path = hit.getPath();
            out.println(++ch+". " + path + "<br>");
            addProperty(path + "/jcr:content", out, resourceResolver, "<new tag value>");
        }
    }

    private void addProperty(String pagePath, PrintWriter out, ResourceResolver resourceResolver, String newTemplateVal) {
        Resource pageJcrRes = resourceResolver.getResource(pagePath);
        ModifiableValueMap modifiableValueMap = pageJcrRes.adaptTo(ModifiableValueMap.class);
        if (modifiableValueMap != null) {
            /**
             * If the property is an array, you need to update array with new value, then set to property
             * */
            modifiableValueMap.put("cq:tags", newTemplateVal);
        }
    }
}

 

Avatar

Administrator

@JyothirmaiAd Did you find the suggestions from users helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!



Kautuk Sahni