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);
}
}
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);
}
Aggregations