adding child properties to the parent node via java code
i need to add email address from all child nodes that are having, to the parent node but I'm unable to replicate my changes . my changes are not reflcting in the aem node . this is the code I'm using . ive installed the folder node from the package manager.
package com.cni.cq.dam.ui.contactsheet.service;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.cni.cq.dam.core.assetbinary.AssetBinaryService;
import com.cni.cq.dam.core.publications.PublicationsService;
import java.util.HashMap;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.ValueFactory;
@8220494(immediate = true, service = UpdateEmailsService.class)
public class UpdateEmailsService {
private static final Logger log = LoggerFactory.getLogger(UpdateEmailsService.class);
@3214626
ResourceResolverFactory resolverFactory;
@3214626
private XMLHandler xmlHandler;
@3214626
PublicationsService publicationService;
@3214626
AssetBinaryService assetBinaryService;
@3214626
SlingRepository slingRepository;
static String serviceUser = "shivakumar";
public static ResourceResolver newResolver(ResourceResolverFactory resourceResolverFactory) throws LoginException {
final Map<String, Object> paramMap = new HashMap<>();
paramMap.put(ResourceResolverFactory.SUBSERVICE, serviceUser);
// fetches the admin service resolver using service user.
ResourceResolver resolver = resourceResolverFactory.getServiceResourceResolver(paramMap);
return resolver;
}
public void updateEmails() throws LoginException {
ResourceResolver resolver = null;
try {
// Assuming you have a service user with sufficient privileges
resolver=newResolver(resolverFactory);
log.debug("Obtained Resource Resolver");
Session session = resolver.adaptTo(Session.class);
// Parent node path
String parentNodePath = "/content/contracted-creator/g/ethan_james_green";
Node parentNode = session.getNode(parentNodePath);
log.debug("Got parent node: {}", parentNodePath);
// Create or retrieve the "emails" property in the parent node
ValueFactory valueFactory = session.getValueFactory();
Value[] existingEmailsValue = parentNode.hasProperty("emailId")
? parentNode.getProperty("emailId").getValues()
: new Value[0];
log.debug("Existing emails count: {}", existingEmailsValue.length);
// Create an array to store the updated email values
Value[] updatedEmailsValue = new Value[existingEmailsValue.length];
// Iterate through child nodes
NodeIterator childNodes = parentNode.getNodes();
int i = 0;
while (childNodes.hasNext()) {
Node childNode = childNodes.nextNode();
if (childNode.hasProperty("emailId")) {
Value emailValue = childNode.getProperty("emailId").getValue();
// Add the email value to the array
updatedEmailsValue[i++] = emailValue;
}
}
// Update the "emails" property in the parent node
parentNode.setProperty("emailId", updatedEmailsValue);
// Save changes
session.save();
} catch (RepositoryException e) {
log.error("Error updating emailId", e);
} finally {
if (resolver != null && resolver.isLive()) {
resolver.close();
}
}
}
}