Search in sources :

Example 1 with JCR_SQL2

use of javax.jcr.query.Query.JCR_SQL2 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)

Aggregations

QueryHelper (com.adobe.acs.commons.util.QueryHelper)1 DataSourceBuilder (com.adobe.acs.commons.wcm.datasources.DataSourceBuilder)1 DataSourceOption (com.adobe.acs.commons.wcm.datasources.DataSourceOption)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)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 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)1 SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)1 Resource (org.apache.sling.api.resource.Resource)1 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 ValueMap (org.apache.sling.api.resource.ValueMap)1 HttpConstants (org.apache.sling.api.servlets.HttpConstants)1 SlingSafeMethodsServlet (org.apache.sling.api.servlets.SlingSafeMethodsServlet)1 Component (org.osgi.service.component.annotations.Component)1