Search in sources :

Example 6 with SimpleDataSource

use of com.adobe.granite.ui.components.ds.SimpleDataSource in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class OptionsImpl method populateOptionItemsFromDatasource.

@SuppressWarnings("unchecked")
private void populateOptionItemsFromDatasource() {
    if (StringUtils.isBlank(datasourceRT)) {
        return;
    }
    // build the options by running the datasource code (the list is set as a request attribute)
    RequestDispatcherOptions opts = new RequestDispatcherOptions();
    opts.setForceResourceType(datasourceRT);
    RequestDispatcher dispatcher = request.getRequestDispatcher(resource, opts);
    try {
        if (dispatcher != null) {
            dispatcher.include(request, response);
        } else {
            LOGGER.error("Failed to include the datasource at " + datasourceRT);
        }
    } catch (Exception e) {
        LOGGER.error("Failed to include the datasource at " + datasourceRT, e);
    }
    // retrieve the datasource from the request and adapt it to form options
    SimpleDataSource dataSource = (SimpleDataSource) request.getAttribute(DataSource.class.getName());
    if (dataSource != null) {
        Iterator<Resource> itemIterator = dataSource.iterator();
        if (itemIterator != null) {
            while (itemIterator.hasNext()) {
                Resource item = itemIterator.next();
                OptionItem optionItem = item.adaptTo(OptionItem.class);
                if (optionItem != null && (optionItem.isDisabled() || StringUtils.isNotBlank(optionItem.getValue()))) {
                    optionItems.add(optionItem);
                }
            }
        }
    }
}
Also used : RequestDispatcherOptions(org.apache.sling.api.request.RequestDispatcherOptions) OptionItem(com.adobe.cq.wcm.core.components.models.form.OptionItem) SimpleDataSource(com.adobe.granite.ui.components.ds.SimpleDataSource) Resource(org.apache.sling.api.resource.Resource) ChildResource(org.apache.sling.models.annotations.injectorspecific.ChildResource) RequestDispatcher(javax.servlet.RequestDispatcher)

Aggregations

SimpleDataSource (com.adobe.granite.ui.components.ds.SimpleDataSource)6 DataSource (com.adobe.granite.ui.components.ds.DataSource)5 Resource (org.apache.sling.api.resource.Resource)3 OptionItem (com.adobe.cq.wcm.core.components.models.form.OptionItem)1 WorkflowException (com.adobe.granite.workflow.WorkflowException)1 WorkflowSession (com.adobe.granite.workflow.WorkflowSession)1 WorkflowModel (com.adobe.granite.workflow.model.WorkflowModel)1 ArrayList (java.util.ArrayList)1 RequestDispatcher (javax.servlet.RequestDispatcher)1 ServletException (javax.servlet.ServletException)1 RequestDispatcherOptions (org.apache.sling.api.request.RequestDispatcherOptions)1 ChildResource (org.apache.sling.models.annotations.injectorspecific.ChildResource)1