Tuesday, 7 February 2017

AEM Code Snippet

//Convert ValueMap to Map Object
protected Map<String,Object> convertValueMapToMap(final ValueMap){

final Map<String,Object>  map=new HashMap<String,Object>();

           for(String key=map.keySet()){
               final Object object=map.get(key);
               map.put(key,object);
           }

return map;

}

Getting Resource Object From SlingHttpServletRequest

ResourceResolver resourceResolver=slingHttpServletRequest.getResourceResolver();

Resource resource= resourceResolver.getResource("/content/geometrixx");

Adding Property To Node
 final Node node=resource.adaptTo(Node.class);

 final Session session=node.getSession();

 node.setProperty("linkUrl","http://www.google.com");

  session.save();

In java code knowing the sling run mode

@Inject
SlingSettingsService slingSettingsService

slingSettingsService.getRunModes().contains("author");

Getting Resources In Sling Models
public List<Resource> getResources(){
    ArrayList<Resource>  resources= new ArrayList<Resource>();
    Iterable<Resource>  children=request.getResource().getChiildren();  
    for(Resource child:children){
                  if(child.getName().startsWith(name)){
                               resource.add(child);
                    }

      }
return resource;
}

Checking Property In Child Nodes

public boolean isMultipleAllowedPropertyPresent(){
Iterable<Resource> children=request.getResource().getChidren();
for(Resource child:children){
ValueMap properties=child.getValueMap();
if(null !=properties && Boolean.TRUE.equal(properties.get("isMultipleAllowed",Boolean.FALSE))){
if(child.getName().startsWith(name)){
return true;
}
if(StringUtils.equals(child.getName(),name)){
return true;
}
}
return false;

}

ResouceResolver Object in Component
 @Reference
 private ResourceResolverFactory resolverFactory;

 private static final String SERVICE_MAPPING = "testService";

 final Map<String, Object> resourceResolverParamMap = new HashMap<String, Object>();
 resourceResolverParamMap.put(ResourceResolverFactory.SUBSERVICE, SERVICE_MAPPING);

 final ResourceResolver resourceResolver = resolverFactory.getServiceResourceResolver(resourceResolverParamMap);

 final Resource resource = resourceResolver.getResource("/content");

 place the below path

 place the file in /apps/geometrixx/configs/confix.author

 org.apache.sling.serviceusermapping.impl.ServiceUserMapperImpl

 <?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
    jcr:primaryType="sling:OsgiConfig"
    user.default=""
    user.mapping="[com.day.cq.cq-test-bundle:configService=test_servicer]"/>


GetProperty
private String getProperty(final String propertyName){
final InheritanceValueMap properties=new ComponentInheritanceValueMap(request.getResource());
return StringUtils.trimToNull(properties.getInherited(propertyName,String.class));
}

No comments:

Post a Comment