Expand my Community achievements bar.

SOLVED

Need help with resourceResolver.commit()

Avatar

Level 8

Hi All

I need help with resourceResolver.commit(). When I do that I get org.apache.sling.api.resource.PersistenceException: Unable to commit changes to session.

 

This is the code that is generating the PersistanceException:

 

logger.info("\n EventZZZ component path: {}", component);
    resourceResolverService.withServiceUserResourceResolver(ServiceUser.CONTENT_READER, resourceResolver -> {
        Resource resource = resourceResolver.getResource(component);
        resetCampaign(resource);
        try {
            if (resourceResolver.hasChanges()) {
                resourceResolver.commit();
            }
        } catch (PersistenceException e) {
            logger.error(String.valueOf(e));
        }
    });

 

The resourceResolver.hasChanges() is true and I am expecting resourceResolver.commit() would persist the node change in repo.

 

private void resetCampaign(Resource res) {
Node node = res.adaptTo(Node.class);

try {

if (node.hasProperty("campaign")) {
node.setProperty("campaign", StringUtils.EMPTY);
logger.info("\n Removed property CAMPAIGN");
}
if (node.hasProperty("campaignTargetGroup")) {
node.setProperty("campaignTargetGroup", StringUtils.EMPTY);
logger.info("\n Removed property campaignTargetGroup");
}
if (node.hasProperty("campaignTopic")) {
node.setProperty("campaignTopic", StringUtils.EMPTY);
logger.info("\n Removed property campaignTopic");
}
node.setProperty("eventHandlerTask", "By "+ res.getResourceResolver().getUserID());



} catch (RepositoryException e) {
logger.error(String.valueOf(e));
}

}

 

I followed all several Q/A like this one and this one.

Can you please point me what is wrong with this approach?

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

This could be due to permission, check the modify and create permission to the campaign for the service user.



Arun Patidar

View solution in original post

6 Replies

Avatar

Community Advisor

Hi @anasustic 

 

The service user which you are using to get the resource resolver might not have "modify" access to the resource you are trying to modify. Please check the permissions for this service user.

Avatar

Community Advisor

@anasustic 

Instead of adapting the resource to node, use

 ModifiableValueMap properties = resource.adaptTo(ModifiableValueMap.class);
  if (properties.containsKey("campaign")) {
    properties.put("campaign", StringUtils.EMPTY);
  } 
 if  (properties.containsKey("campaignTargetGroup")) {
    properties.put("campaignTargetGroup", StringUtils.EMPTY);
  }  and so on.

and if you want to go with the node

Session session = resourceResolver.adapTo(Session.class)
session.save()
As directed in the Reference link :https://aem4beginner.blogspot.com/update-node-property-using-aem-bundle

Thanks

Avatar

Level 8

Thanks @ShaileshBassi for the advice.

I modifed the method accordingly.

private void resetCampaign(Resource res) {
    ModifiableValueMap properties = res.adaptTo(ModifiableValueMap.class);
    if (properties.containsKey("campaign")) {
        properties.put("campaign", StringUtils.EMPTY);
        logger.info("\n Removed property campaign");
    }
    if  (properties.containsKey("campaignTargetGroup")) {
        properties.put("campaignTargetGroup", StringUtils.EMPTY);
        logger.info("\n Removed property campaignTargetGroup");
    }
    if (properties.containsKey("campaignTopic")) {
        properties.put("campaignTopic", StringUtils.EMPTY);
        logger.info("\n Removed property campaignTopic");
    }
}

Avatar

Correct answer by
Community Advisor

This could be due to permission, check the modify and create permission to the campaign for the service user.



Arun Patidar

Avatar

Community Advisor

Hey @anasustic 

@arunpatidar is right that it seems to be a permission issue. When I see your code and noticed that you are using the CONTENT_READER service user to write the data and I don't think the CONTENT_READER service user is intended to write something in the repository so you need to validate the permission of your service user.

From my perspective, You should either use an existing service user who has permission to write in the repository or create a new system user and use that one. 

Regards,

Arpit Varshney

Avatar

Level 8

Thanks so much @ArpitVarshney @arunpatidar @ShaileshBassi @JeevanRaj for your replies.

I created a new System User system-content-writer and gave it write permissions to specific paths. 

 

I then used this new created account in my code for the commit()

resourceResolverService.withServiceUserResourceResolver(ServiceUser.CONTENT_WRITER, resourceResolver -> {
Resource resource = resourceResolver.getResource(component);
resetCampaign(resource);
try {
if (resourceResolver.hasChanges()) {
resourceResolver.commit();
}
} catch (PersistenceException e) {
logger.error(e.getMessage() + "\n" + e);

}
});