Skip to content

Instantly share code, notes, and snippets.

@RashidJorvee
Created October 18, 2021 17:48
Show Gist options
  • Save RashidJorvee/11449aabf492e8a8bfc9991844e4b2b9 to your computer and use it in GitHub Desktop.
Save RashidJorvee/11449aabf492e8a8bfc9991844e4b2b9 to your computer and use it in GitHub Desktop.
Servlet to add and update AEM Node properties using ModifiableValueMap
package com.rashidjorvee.aem.servlets;
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.SlingSafeMethodsServlet;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.Servlet;
import java.util.Iterator;
@Component(immediate = true,
service = Servlet.class,
property = {
"sling.servlet.methods=GET",
"sling.servlet.paths=/bin/updatePccCodesDate",
})
public class UpdatePccCodesServlet extends SlingSafeMethodsServlet {
private static final long serialVersionUID = 1L;
private static final Logger LOG = LoggerFactory.getLogger(UpdatePccCodesServlet.class);
private static final String PCC_FORM_PATH = "/content/tools/en_us/aem/pccCodeList";
private static final String PCC_CODE = "pccCode";
private static final String EXPIRY_DATE = "expiryDate";
private static final String DEFAULT_END_DATE = "10/13/2021";
private static final String NEW_END_DATE = "11/14/2021";
private static final String DATE_NEED_TO_CHANGE = "11/12/2021";
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
ResourceResolver resourceResolver = request.getResourceResolver();
LOG.debug("/bin/updatePccCodesDate start executing.");
try {
Resource resource = resourceResolver.getResource(PCC_FORM_PATH);
if(resource != null) {
Iterator<Resource> iterator = resource.listChildren();
while (iterator.hasNext()) {
Resource pccNode = iterator.next();
ModifiableValueMap valueMap = pccNode.adaptTo(ModifiableValueMap.class);
if (valueMap != null) {
if (valueMap.containsKey(EXPIRY_DATE)) {
valueMap.replace(EXPIRY_DATE, DATE_NEED_TO_CHANGE, NEW_END_DATE);
LOG.info("Expiry date replaced in "+valueMap.get(PCC_CODE) +" : " +DATE_NEED_TO_CHANGE +" to " +NEW_END_DATE);
}
else {
valueMap.putIfAbsent(EXPIRY_DATE,DEFAULT_END_DATE);
LOG.info("Expiry date set to "+ valueMap.get(PCC_CODE) +" : "+DEFAULT_END_DATE);
}
}
}
resource.getResourceResolver().commit();
}
response.setStatus(200);
} catch (Exception e) {
LOG.error("error while reading the pcc list {}", e);
}
}
}
@RashidJorvee
Copy link
Author

RashidJorvee commented Oct 18, 2021

ModifiableValueMap has some awesome methods. I use putIfAbsent() and replace() to update an existing property value and add a property if that doesn't exist on node.

e.g.
replace(propertyName, oldValue, newValue);

@rishabhverma1998
Copy link

ModifiableValueMap has some awesome methods. I use putIfAbsent() and replace() to update an existing property value and add a property if that doesn't exist on node.

e.g. replace(propertyName, oldValue, newValue);

Thanks a lot @RashidJorvee, this was really helpful!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment