3
votes

I am trying to programmatically extract data from a json string, converts into a string array and adding it as cq:tags property and corresponding values into a node, however when I do so, though cq:tags property is added but with blank values.

My node is something like this: /content/<my project node>/ContentPage/jcr:content

        ResourceResolver resolver = CommonUtils.getResourceResolver(resourceResolverFactory);

  String[] strValue = tagList.stream().toArray(String[]::new); // tagList has String values in form of array.
            Resource resource = resolver.getResource(CONTENT_DATA_NODE);
            if (resource != null) {
                Node node = resource.adaptTo(Node.class);
                if (node != null) {
                    NodeIterator nodeIterator = node.getNodes();
                    while (nodeIterator.hasNext()) {
                        innerNode = nodeIterator.nextNode();
                        innerNode.setProperty(CQ_TAGS, strValue);
                        innerNode.getSession().save();
                    }
                }
            }

and my sling user mapper service is mybundle.core:datawrite=userdatawriter , also if my resource resolverfactory is null, I get resolver from request directly.

Initially, I thought it could be an access issue, so I programmatically tried with any random property and value: property: xyz , values: aa,bb,cc,dd Which is written by my code without any issues, it is only when programmatically adding cq:tags is when the problem arises. Though I can add cq:tags with any long list of values manually without any issues, either from page properties or in the crxde node itself.

What am I missing here and doing wrong in the code which can not only add cq:tags but also overwrite if cq:tags exists.

P.S: my AEM version is AEM 6.5 SP2

1

1 Answers

2
votes

I can see the same thing happen in AEM 6.4.3. Immediately upon saving the property, the value can be read as expected. Here's a few quick examples I ran in the AEM Groovy console.

def node = getNode('/content/screens/we-retail/apps/virtual-showroom/en/jcr:content')
String[] arr = ['a', 'b', 'c']; 
String[] tagArr  = ['we-retail:equipment', 'we-retail:activity/biking']
node.setProperty('foo', arr)
println node.getProperty('foo').values // prints the a, b,c tags
node.setProperty('cq:tags', tagArr)
session.save()
println node.getProperty('cq:tags').values // prints the a, b,c tags
println node.getProperty('foo').values // prints the a, b,c tags

However, upon inspecting the page in CRXDE, I can see that the property is empty. This does not happen when the values you use match existing tags in AEM. For example:

def node = getNode('/content/screens/we-retail/apps/virtual-showroom/en/jcr:content')
String[] arr = ['a', 'b', 'c'];
String[] tagArr  = ['we-retail:equipment', 'we-retail:activity/biking']
node.setProperty('foo', arr)
println node.getProperty('foo').values
node.setProperty('cq:tags', tagArr)
println node.getProperty('cq:tags').values // prints the we-retail tags
session.save()
println node.getProperty('foo').values
println node.getProperty('cq:tags').values // prints the we-retail tags

and the same values are visible in CRXDE.

This behaviour, I believe, is controlled by the Day CQ Tagging Service (com.day.cq.tagging.impl.JcrTagManagerFactoryImpl)

enter image description here

Unchecking the box will disable validation and allow you to persist those values. However, tagging a page with tags that don't exist will cause its own share of problems. Instead, I would suggest making sure to create those tags before using them.