Expand my Community achievements bar.

SOLVED

Getting error while accessing current resource

Avatar

Level 4

Hi Everyone,

 

I am trying to create a multifield dialog using sling model but while accessing the values, my code is failing to get current resource. My code is failing inside try block , i am getting null value of current resource. Please help me to understand and resolve this issue.

 

package com.site.myproj.core.models.impl;

import com.site.myproj.core.models.MultifieldAccordionFAQModel;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Default;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Model(
adaptables = SlingHttpServletRequest.class,
adapters = MultifieldAccordionFAQModel.class,
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL
)

public class MultifieldAccordionFAQModelImpl implements MultifieldAccordionFAQModel {

private static final Logger LOG = LoggerFactory.getLogger(MultifieldAccordionFAQModelImpl.class);

@Inject
Resource componentResource;

@ValueMapValue
@Default( values = "FAQ's Default")
private String accordionHeading;

@Override
public String getAccordionHeading() {
return accordionHeading;
}


@Override
public List<Map<String, String>> getAccordionDetails() {

List<Map<String,String>> accordionDetailsMap = new ArrayList<>();
try{
LOG.debug("Component Resource>> {}", componentResource);
Resource accordionParentNode = componentResource.getChild("accordionDetails");
LOG.debug("accordionParentNode >> {}", accordionParentNode);
if(accordionParentNode != null){
for( Resource accordion : accordionParentNode.getChildren()){

Map<String, String> accordionsMap = new HashMap<>();

accordionsMap.put("accordionTitle",accordion.getValueMap().get("accordionTitle", String.class));
accordionsMap.put("accordionDescription",accordion.getValueMap().get("accordionDescription", String.class));

accordionDetailsMap.add(accordionsMap);
}
}
}catch ( Exception e){
LOG.debug("<< Error occured during MultifieldAccordionModelImpl Implementation >> {}", e.getMessage());
}

return accordionDetailsMap;
}
}

 

1 Accepted Solution

Avatar

Correct answer by
Level 6

Hi @monish_gavali ,

You can get the resource like below in a sling model,

@SlingObject
private Resource resource;

Hope it helps. 

View solution in original post

6 Replies

Avatar

Correct answer by
Level 6

Hi @monish_gavali ,

You can get the resource like below in a sling model,

@SlingObject
private Resource resource;

Hope it helps. 

Avatar

Level 7

 

@inject 
private Resource resource;
public List<Map<String, String>> getAccordionDetails() {
        List<Map<String,String>> accordionDetailsMap = new ArrayList<>();
        try {
            Resource accordionParentNode = resource.getChild("accordionDetails");
            LOG.debug("Accordion Parent Node: {}", accordionParentNode);

            if (accordionParentNode != null) {
                for (Resource accordion : accordionParentNode.getChildren()) {
                    Map<String, String> accordionsMap = new HashMap<>();
                    accordionsMap.put("accordionTitle", accordion.getValueMap().get("accordionTitle", String.class));
                    accordionsMap.put("accordionDescription", accordion.getValueMap().get("accordionDescription", String.class));
                    accordionDetailsMap.add(accordionsMap);
                }
            }
        } catch (Exception e) {
            LOG.error("Error occurred while fetching accordion details: {}", e.getMessage());
        }

        return accordionDetailsMap;
    }

i have injected the resource using the @inject annotation.
The resource field now holds the reference to the injected resource.
The method attempts to retrieve the child resource named “accordionDetails” from the injected resource.
If the “accordionDetails” resource exists, it proceeds to iterate over its children (individual accordion items).

Hope this helps!

 

Avatar

Level 4

Thank you so much @arunpatidar for the solution, I am able to do this using @Self annontation. Before that i tried with 

@SlingObject

 so it is also working.