Expand my Community achievements bar.

SOLVED

AEM 6.1 Touch UI | Populating Drop Down | Fetching resourceType of the actual component in datasource

Avatar

Level 3

Hi,

Can anyone please let me know how we can read the calling component ResourceType in the datasource.jsp.

Requirement is to populate the dropdown in Touch UI mode . For which I created a datasource and invoking my custom osgi service. Till this it works and service is getting invoked. But when I do "resource.getResourceType()" as highlighted below. It returns me "/apps/mywebsite/components/datasource" path. But I want the resourceType for the calling component which is "/apps/mywebsite2/components/herotext2".

Please let me know how we I can read the resourceType for herotext2 in datasource.jsp.

//INVOKE A BACKEND OSGI Service com.sapient.platform.iea.aem.core.accessors.api.HelloService mySource  = sling.getService(com.sapient.platform.iea.aem.core.accessors.api.HelloService.class) ; // String osgiValue = mySource.getData1(); Map<String, String> optionMap = new HashMap<String, String>(); optionMap = mySource.getData(resource.getResourceType()); String Text=""; for (Map.Entry<String, String> entry : optionMap.entrySet()) { String value= entry.getKey(); String text= entry.getValue(); vm = new ValueMapDecorator(new HashMap<String, Object>()); vm.put("value",value); vm.put("text",text); fakeResourceList.add(new ValueMapResource(resolver, new ResourceMetadata(), "nt:unstructured", vm)); }
1 Accepted Solution

Avatar

Correct answer by
Level 10

""But when I do "resource.getResourceType()

I do not think this is necessary at all. You can invoke a OSGi service and populate a DataSource object without using getReourceType. Then the TouchUI dropdown will be populated with data. 

For example - assume you want to read a specific resource in the JCR (a node somewhere) and create a map using its node props (rest of this answer addresses this use case)  

If you want to populate a Map with JCR data - write a method in the OSGi bundle that performs these tasks:

1-  gets node data (from a specific node in the JCR)

2 - populates a map using the data

3 - returns the map.

For example - OSGi service that returns a map:

public HashMap getMapData(String url)
  {
    try
    {
      this.resourcePath = url;
      ResourceResolver resourceResolver = this.resolverFactory
        .getAdministrativeResourceResolver(null);
      Resource res = resourceResolver.getResource(this.resourcePath);
      ValueMap readMap = res.getValueMap();
      HashMap<String, String> myMap= new HashMap();

//populate the map

return myMap

In your JSP - you can call this method

optionMap = mySource.getMapData(pathFieldValue);

If you need a JCR Path to point to the node - then use a PathField in the dialog of the component. Get the value of the pathfield in the JSP and pass that to getMapData (apthFieldValue argument).

For example:

Then the Java code can read that and populate the map with the JCR node prop name and values. (shown above).

THen the drop-down in the TOuch UI will then display node props names and prop values. 

View solution in original post

6 Replies

Avatar

Level 10

According to your code snapshot,

'resource' contains the current resource which is datasource and thats the expected behavior !

check the below thread which may be of a help

http://help-forums.adobe.com/content/adobeforums/en/experience-manager-forum/adobe-experience-manage...

Avatar

Level 3

Thanks for the quick response but I need a little more guidance on it.

Is their any way by which I can get the resourceType ("/apps/mywebsite2/components/herotext2)  of the actual component(herotext2)  which is using this datasource as given in this example https://helpx.adobe.com/experience-manager/using/creating-granite-datasource.html. Based on the component I need to perform some manipulation in my service before returning the data.

Thank you.

Regards,

Shikha

Avatar

Correct answer by
Level 10

""But when I do "resource.getResourceType()

I do not think this is necessary at all. You can invoke a OSGi service and populate a DataSource object without using getReourceType. Then the TouchUI dropdown will be populated with data. 

For example - assume you want to read a specific resource in the JCR (a node somewhere) and create a map using its node props (rest of this answer addresses this use case)  

If you want to populate a Map with JCR data - write a method in the OSGi bundle that performs these tasks:

1-  gets node data (from a specific node in the JCR)

2 - populates a map using the data

3 - returns the map.

For example - OSGi service that returns a map:

public HashMap getMapData(String url)
  {
    try
    {
      this.resourcePath = url;
      ResourceResolver resourceResolver = this.resolverFactory
        .getAdministrativeResourceResolver(null);
      Resource res = resourceResolver.getResource(this.resourcePath);
      ValueMap readMap = res.getValueMap();
      HashMap<String, String> myMap= new HashMap();

//populate the map

return myMap

In your JSP - you can call this method

optionMap = mySource.getMapData(pathFieldValue);

If you need a JCR Path to point to the node - then use a PathField in the dialog of the component. Get the value of the pathfield in the JSP and pass that to getMapData (apthFieldValue argument).

For example:

Then the Java code can read that and populate the map with the JCR node prop name and values. (shown above).

THen the drop-down in the TOuch UI will then display node props names and prop values. 

Avatar

Level 3

Thanks for the response. I will try the solution.

Avatar

Level 10

If you want to see an example of a pathfield in a dialog and then OSGi reading that - converting to a resource and populating a Java object -- see this article - there is a lot of good AEM code techniques here: 

Developing a custom Adobe Experience Manager Quiz Component

Avatar

Level 10

I agree with @scott, you dont need to do resource.getResourceType for herotext2. Example which he mentioned should help