Search in sources :

Example 6 with OptionItem

use of com.adobe.cq.wcm.core.components.models.form.OptionItem 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

OptionItem (com.adobe.cq.wcm.core.components.models.form.OptionItem)6 RequestDispatcherOptions (org.apache.sling.api.request.RequestDispatcherOptions)6 Resource (org.apache.sling.api.resource.Resource)6 Options (com.adobe.cq.wcm.core.components.models.form.Options)5 ValueMap (org.apache.sling.api.resource.ValueMap)5 Test (org.junit.Test)5 SimpleDataSource (com.adobe.granite.ui.components.ds.SimpleDataSource)1 RequestDispatcher (javax.servlet.RequestDispatcher)1 ChildResource (org.apache.sling.models.annotations.injectorspecific.ChildResource)1