Search in sources :

Example 1 with DataSourceOption

use of com.adobe.acs.commons.wcm.datasources.DataSourceOption in project acs-aem-commons by Adobe-Consulting-Services.

the class DynamicSelectDataSource method doGet.

@Override
protected void doGet(@Nonnull SlingHttpServletRequest request, @Nonnull SlingHttpServletResponse response) throws ServletException, IOException {
    final ResourceResolver resolver = request.getResourceResolver();
    final ValueMap properties = request.getResource().getValueMap();
    final List<DataSourceOption> options = new ArrayList<>();
    try {
        // The query language property
        final String queryLanguage = properties.get(PN_DROP_DOWN_QUERY_LANGUAGE, JCR_SQL2);
        // The query statement (this must match the queryLanguage else the query will fail)
        final String queryStatement = properties.get(PN_DROP_DOWN_QUERY, String.class);
        // The property names to extract; these are specified as a String[] property
        final String[] allowedPropertyNames = properties.get(PN_ALLOW_PROPERTY_NAMES, new String[0]);
        if (StringUtils.isNotBlank(queryStatement)) {
            // perform the query
            final List<Resource> results = queryHelper.findResources(resolver, queryLanguage, queryStatement, StringUtils.EMPTY);
            final List<String> distinctOptionValues = new ArrayList<>();
            for (final Resource resource : results) {
                // For each result...
                // - ensure the property value is a String
                // - ensure either no properties have been specified (which means ALL properties are eligible) OR the property is in the list of enumerated propertyNames
                // - ensure this property value has not already been processed
                // -- if the above criteria is satisfied, add to the options
                resource.getValueMap().entrySet().stream().filter(entry -> entry.getValue() instanceof String).filter(entry -> ArrayUtils.isEmpty(allowedPropertyNames) || ArrayUtils.contains(allowedPropertyNames, entry.getKey())).filter(entry -> !distinctOptionValues.contains(entry.getValue().toString())).forEach(entry -> {
                    String value = entry.getValue().toString();
                    distinctOptionValues.add(value);
                    options.add(new DataSourceOption(value, value));
                });
            }
        }
        // Create a datasource from the collected options, even if there are 0 options.
        dataSourceBuilder.addDataSource(request, options);
    } catch (Exception e) {
        log.error("Unable to collect the information to populate the ACS Commons Report Builder dynamic-select drop-down.", e);
        response.sendError(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Logger(org.slf4j.Logger) ServletException(javax.servlet.ServletException) JCR_SQL2(javax.jcr.query.Query.JCR_SQL2) Servlet(javax.servlet.Servlet) LoggerFactory(org.slf4j.LoggerFactory) Resource(org.apache.sling.api.resource.Resource) HttpConstants(org.apache.sling.api.servlets.HttpConstants) ArrayUtils(org.apache.commons.lang3.ArrayUtils) IOException(java.io.IOException) SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) StringUtils(org.apache.commons.lang3.StringUtils) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) ArrayList(java.util.ArrayList) Component(org.osgi.service.component.annotations.Component) List(java.util.List) DataSourceBuilder(com.adobe.acs.commons.wcm.datasources.DataSourceBuilder) SlingSafeMethodsServlet(org.apache.sling.api.servlets.SlingSafeMethodsServlet) QueryHelper(com.adobe.acs.commons.util.QueryHelper) DataSourceOption(com.adobe.acs.commons.wcm.datasources.DataSourceOption) Reference(org.osgi.service.component.annotations.Reference) Nonnull(javax.annotation.Nonnull) DataSourceOption(com.adobe.acs.commons.wcm.datasources.DataSourceOption) ValueMap(org.apache.sling.api.resource.ValueMap) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ArrayList(java.util.ArrayList) Resource(org.apache.sling.api.resource.Resource) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 2 with DataSourceOption

use of com.adobe.acs.commons.wcm.datasources.DataSourceOption in project acs-aem-commons by Adobe-Consulting-Services.

the class DataSourceBuilderImpl method addDataSource.

@Override
public void addDataSource(final SlingHttpServletRequest slingRequest, final List<DataSourceOption> options) {
    final ArrayList<Resource> resourceList = new ArrayList<Resource>();
    DataSource dataSource = null;
    for (final DataSourceOption option : options) {
        final Map map = new HashMap();
        map.put(TEXT, option.getText());
        map.put(VALUE, option.getValue());
        resourceList.add(new ValueMapResource(slingRequest.getResourceResolver(), new ResourceMetadata(), "", new ValueMapDecorator(map)));
    }
    if (resourceList.size() > 0) {
        dataSource = new SimpleDataSource(resourceList.iterator());
    } else {
        dataSource = EmptyDataSource.instance();
    }
    slingRequest.setAttribute(DataSource.class.getName(), dataSource);
}
Also used : HashMap(java.util.HashMap) DataSourceOption(com.adobe.acs.commons.wcm.datasources.DataSourceOption) SimpleDataSource(com.adobe.granite.ui.components.ds.SimpleDataSource) ValueMapResource(com.adobe.granite.ui.components.ds.ValueMapResource) Resource(org.apache.sling.api.resource.Resource) ArrayList(java.util.ArrayList) ValueMapResource(com.adobe.granite.ui.components.ds.ValueMapResource) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) ResourceMetadata(org.apache.sling.api.resource.ResourceMetadata) ValueMap(org.apache.sling.api.resource.ValueMap) HashMap(java.util.HashMap) Map(java.util.Map) EmptyDataSource(com.adobe.granite.ui.components.ds.EmptyDataSource) SimpleDataSource(com.adobe.granite.ui.components.ds.SimpleDataSource) DataSource(com.adobe.granite.ui.components.ds.DataSource)

Aggregations

DataSourceOption (com.adobe.acs.commons.wcm.datasources.DataSourceOption)2 ArrayList (java.util.ArrayList)2 Resource (org.apache.sling.api.resource.Resource)2 ValueMap (org.apache.sling.api.resource.ValueMap)2 QueryHelper (com.adobe.acs.commons.util.QueryHelper)1 DataSourceBuilder (com.adobe.acs.commons.wcm.datasources.DataSourceBuilder)1 DataSource (com.adobe.granite.ui.components.ds.DataSource)1 EmptyDataSource (com.adobe.granite.ui.components.ds.EmptyDataSource)1 SimpleDataSource (com.adobe.granite.ui.components.ds.SimpleDataSource)1 ValueMapResource (com.adobe.granite.ui.components.ds.ValueMapResource)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Nonnull (javax.annotation.Nonnull)1 JCR_SQL2 (javax.jcr.query.Query.JCR_SQL2)1 Servlet (javax.servlet.Servlet)1 ServletException (javax.servlet.ServletException)1 ArrayUtils (org.apache.commons.lang3.ArrayUtils)1 StringUtils (org.apache.commons.lang3.StringUtils)1