Wednesday, March 6, 2019

How to get the currentPage URL in cq:dialog dropdown datasources?

How to get the currentPage URL in cq:dialog dropdown datasources?

In Touch UI dialog's, the dynamic dropdownlist are loaded through data sources, sometime we may need to get the current page URL from where the dialog is opened to load the dropdownlist data.

String path= request.getParameter("item");

Returns the content path with out jcr:content, e.g /content/test/en/home

String path = (String) request.getAttribute(com.adobe.granite.ui.components.Value.CONTENTPATH_ATTRIBUTE);

Returns the path with jcr:content, e.g /content/test/en/home/jcr:content


import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.LinkedHashMap;

import javax.servlet.Servlet;
import javax.servlet.ServletException;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.felix.scr.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.felix.scr.annotations.Service;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;


import org.apache.sling.api.wrappers.ValueMapDecorator;
import com.adobe.granite.ui.components.ds.DataSource;
import com.adobe.granite.ui.components.ds.SimpleDataSource;
import com.adobe.granite.ui.components.ds.ValueMapResource;


import com.adobe.granite.ui.components.Value;
import org.apache.sling.api.resource.ResourceMetadata;;

@Service(value = Servlet.class)
@Component(metatype = true)
@Properties({
@Property(name = "sling.servlet.resourceTypes", value = "/services/availableCountryDataSource1"),
@Property(name = "service.description", value = "Get country list"),
@Property(name = "label", value = "countryList") })

public class AvailableCountryDataSource extends SlingSafeMethodsServlet {

@Reference
private ResourceResolverFactory resolverFactory;

private static final long serialVersionUID = 1180258251365536303L;

private static final Logger log = LoggerFactory.getLogger(AvailableCountryDataSource.class);
protected void doGet(SlingHttpServletRequest request,SlingHttpServletResponse response) throws ServletException,IOException {

final Map<String, String> countriesdata = new LinkedHashMap<String, String>();
Map<String, Object> param = new HashMap<String, Object>();
    param.put(ResourceResolverFactory.SUBSERVICE, "Albin");
    ResourceResolver resourceResolver=null;
try{

ArrayList<Resource> countryList = new ArrayList<Resource>();
ValueMap valueMap = new ValueMapDecorator(new HashMap<String, Object>());
ValueMap properties =null;

String resourcePath =(String)request.getAttribute(Value.CONTENTPATH_ATTRIBUTE);
resourceResolver = resolverFactory.getServiceResourceResolver(param);
Resource resource = resourceResolver.getResource(resourcePath);
properties=resource.adaptTo(ValueMap.class);

String data = properties.get("countryList", (String) null);

    if(data != null ){
log.error("INSIDE IF");
ValueMap vm =null;
for(String country : data.split(",")){

String[] tokens = country.split(";");
if(tokens == null || tokens.length != 2){
continue;
}
vm = new ValueMapDecorator(new HashMap<String, Object>());
vm.put("value", tokens[0] );
vm.put("text",  tokens[1]);

countryList.add(new ValueMapResource(request.getResourceResolver(),new ResourceMetadata(), "nt:unstructured", vm));
}

}
DataSource dataSource = new SimpleDataSource(countryList.iterator());
log.debug("dataSource...");
request.setAttribute(DataSource.class.getName(), dataSource);

}catch(Exception e){
log.error("Error while retrieving countries.",e);
}
finally{
if(resourceResolver!=null){
resourceResolver.close();
  }
}

    }
}
cq-dialog-dropdown-datasource

No comments:

Post a Comment