Expand my Community achievements bar.

SOLVED

AEM 6.1 Touch UI : Prepopulate dropdown with OSGI config properties in AEM touch UI Dialog

Avatar

Level 3

Hi All,

We have a requirement to pre populate the component dialog with osgi configuration values. We have to make the component compatible with Classic as well as Touch UI. We referred the below link for setting values in touch ui dropdown by using datasource.

link :  https://helpx.adobe.com/experience-manager/using/creating-granite-datasource.html

 

Problem  -- In order to fetch the values we created a datasource.jsp and a corresponding bean. In the bean class we are not able to get the jspContext because we need to call the osgi config service.  we are not able to do so as null is coming for getSling()

 

Could anyone please suggest how can we get the service reference to populate the datasource?

 

Thanks,

Radhika

 
1 Accepted Solution

Avatar

Correct answer by
Level 10

I got this to work - here is the screen shot

Here is the drop-down with the first value being returned from an AEM Service:

Here is the updated Script datasource.jsp from the artilce:

 

<%@page session="false" import="
                  org.apache.sling.api.resource.Resource,
                  org.apache.sling.api.resource.ResourceUtil,
                  org.apache.sling.api.resource.ValueMap,
                  org.apache.sling.api.resource.ResourceResolver,
                  org.apache.sling.api.resource.ResourceMetadata,
                  org.apache.sling.api.wrappers.ValueMapDecorator,
                  java.util.List,
                  java.util.ArrayList,
                  java.util.HashMap,
                  java.util.Locale,
                  com.adobe.granite.ui.components.ds.DataSource,
                  com.adobe.granite.ui.components.ds.EmptyDataSource,
                  com.adobe.granite.ui.components.ds.SimpleDataSource,
                  com.adobe.granite.ui.components.ds.ValueMapResource,
                  com.day.cq.wcm.api.Page,
                  com.day.cq.wcm.api.PageManager"%><%
%><%@taglib prefix="cq" uri="http://www.day.com/taglibs/cq/1.0" %><%
%><cq:defineObjects/><%
  
// set fallback
request.setAttribute(DataSource.class.getName(), EmptyDataSource.instance());
  
ResourceResolver resolver = resource.getResourceResolver();
 
//Create an ArrayList to hold data
List<Resource> fakeResourceList = new ArrayList<Resource>();
 
ValueMap vm = null; 


//INVOKE A BACKEND OSGI Service
 com.community.aem.datasource.HelloService mySource  = sling.getService(com.community.aem.datasource.HelloService.class) ; 
 String osgiValue = mySource.getData(); 

String Text=""; 
 
//Add 5 values to drop down! 
for (int i=0; i<5; i++)
{
 
    //allocate memory to the Map instance
 vm = new ValueMapDecorator(new HashMap<String, Object>());   
 
 
 // Specify the value and text values
 String Value = "value"+i ;

 if (i==0)
   Text = osgiValue;
 else    
   Text = "text"+i ; 
 
    //populate the map
 vm.put("value",Value);
 vm.put("text",Text);
 
 fakeResourceList.add(new ValueMapResource(resolver, new ResourceMetadata(), "nt:unstructured", vm));

 System.out.print("*** THE VALUE OF i IS " +i)   ;
}
 
 
//Create a DataSource that is used to populate the drop-down control
DataSource ds = new SimpleDataSource(fakeResourceList.iterator());
request.setAttribute(DataSource.class.getName(), ds);
 
%>

I will add this to the article. The OSGi service is very simple and exposes this method: 

package com.community.aem.datasource.impl;

 

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.jcr.api.SlingRepository;

import com.community.aem.datasource.HelloService;


@Service
@Component(metatype = false)
public class HelloServiceImpl implements HelloService {
    
    public String getData()
    {
        return "returned from an OSGi Service" ; 
    }
}

 

In this example - we hard coded the return value. However - if you want to use OSGi config values, you can read them in your service and return them. TO learn how to read OSGi config values from an OSGi service - read this community document. 

Reading AEM OSGi Configuration Values

View solution in original post

6 Replies

Avatar

Level 10

In the JSP - you should be able to invoke a backend OSGi service that returns data and use that data to populate the dropdown. Assume you create an OSGi service named com.adobe.cq.foo. This has a method named getData(). In the JSP - you can do: 

com.adobe.cq.foo myFoo = sling.getService(com.adobe.cq.foo.class);

myFoo.getData();

Avatar

Level 10

I will look into this and see if I get the same results as you.

Avatar

Level 3

Thanks for your time.

Do you want me to share the component xml for reference?

/RK

Avatar

Correct answer by
Level 10

I got this to work - here is the screen shot

Here is the drop-down with the first value being returned from an AEM Service:

Here is the updated Script datasource.jsp from the artilce:

 

<%@page session="false" import="
                  org.apache.sling.api.resource.Resource,
                  org.apache.sling.api.resource.ResourceUtil,
                  org.apache.sling.api.resource.ValueMap,
                  org.apache.sling.api.resource.ResourceResolver,
                  org.apache.sling.api.resource.ResourceMetadata,
                  org.apache.sling.api.wrappers.ValueMapDecorator,
                  java.util.List,
                  java.util.ArrayList,
                  java.util.HashMap,
                  java.util.Locale,
                  com.adobe.granite.ui.components.ds.DataSource,
                  com.adobe.granite.ui.components.ds.EmptyDataSource,
                  com.adobe.granite.ui.components.ds.SimpleDataSource,
                  com.adobe.granite.ui.components.ds.ValueMapResource,
                  com.day.cq.wcm.api.Page,
                  com.day.cq.wcm.api.PageManager"%><%
%><%@taglib prefix="cq" uri="http://www.day.com/taglibs/cq/1.0" %><%
%><cq:defineObjects/><%
  
// set fallback
request.setAttribute(DataSource.class.getName(), EmptyDataSource.instance());
  
ResourceResolver resolver = resource.getResourceResolver();
 
//Create an ArrayList to hold data
List<Resource> fakeResourceList = new ArrayList<Resource>();
 
ValueMap vm = null; 


//INVOKE A BACKEND OSGI Service
 com.community.aem.datasource.HelloService mySource  = sling.getService(com.community.aem.datasource.HelloService.class) ; 
 String osgiValue = mySource.getData(); 

String Text=""; 
 
//Add 5 values to drop down! 
for (int i=0; i<5; i++)
{
 
    //allocate memory to the Map instance
 vm = new ValueMapDecorator(new HashMap<String, Object>());   
 
 
 // Specify the value and text values
 String Value = "value"+i ;

 if (i==0)
   Text = osgiValue;
 else    
   Text = "text"+i ; 
 
    //populate the map
 vm.put("value",Value);
 vm.put("text",Text);
 
 fakeResourceList.add(new ValueMapResource(resolver, new ResourceMetadata(), "nt:unstructured", vm));

 System.out.print("*** THE VALUE OF i IS " +i)   ;
}
 
 
//Create a DataSource that is used to populate the drop-down control
DataSource ds = new SimpleDataSource(fakeResourceList.iterator());
request.setAttribute(DataSource.class.getName(), ds);
 
%>

I will add this to the article. The OSGi service is very simple and exposes this method: 

package com.community.aem.datasource.impl;

 

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.jcr.api.SlingRepository;

import com.community.aem.datasource.HelloService;


@Service
@Component(metatype = false)
public class HelloServiceImpl implements HelloService {
    
    public String getData()
    {
        return "returned from an OSGi Service" ; 
    }
}

 

In this example - we hard coded the return value. However - if you want to use OSGi config values, you can read them in your service and return them. TO learn how to read OSGi config values from an OSGi service - read this community document. 

Reading AEM OSGi Configuration Values

Avatar

Level 3

Hi Scott,

Thanks for your help in resolving the issue. Your time and efforts are much appreciated.

Regards,

Radhika Kush

Avatar

Level 3

Hi Scott,

Using the above example we created the datasource and the service which will provide the data for populating the drop down.Now to retrieve the data we want the request object in the service.

So the ask is do I need to explicitly pass the request to the service or it will be available to it. If neither of the case then how I can pass this to the service.

 

Regards,

Shikha