com.adobe.granite.ui.components.ds.ValueMapResource Java Examples

The following examples show how to use com.adobe.granite.ui.components.ds.ValueMapResource. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RelationTypesDataSourceServlet.java    From aem-core-cif-components with Apache License 2.0 7 votes vote down vote up
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {

    ResourceBundle resourceBundle = request.getResourceBundle(null);
    List<Resource> values = new ArrayList<>();

    for (RelationType relationType : RelationType.values()) {
        ValueMap vm = new ValueMapDecorator(new HashMap<String, Object>());
        vm.put("value", relationType);
        vm.put("text", toText(resourceBundle, relationType.getText()));
        values.add(new ValueMapResource(request.getResourceResolver(), new ResourceMetadata(), JcrConstants.NT_UNSTRUCTURED, vm));
    }

    DataSource ds = new SimpleDataSource(values.iterator());
    request.setAttribute(DataSource.class.getName(), ds);
}
 
Example #2
Source File: RelationTypesDataSourceServletTest.java    From aem-core-cif-components with Apache License 2.0 6 votes vote down vote up
@Test
public void testDataSource() throws ServletException, IOException {
    servlet.doGet(context.request(), context.response());

    // Verify data source
    DataSource dataSource = (DataSource) context.request().getAttribute(DataSource.class.getName());
    Assert.assertNotNull(dataSource);

    AtomicInteger size = new AtomicInteger(0);
    dataSource.iterator().forEachRemaining(resource -> {
        ValueMapResource vmr = (ValueMapResource) resource;
        String value = vmr.getValueMap().get("value", String.class);
        String text = vmr.getValueMap().get("text", String.class);

        // Check that the value/text is a valid RelationType
        RelationType relationType = RelationType.valueOf(value);
        Assert.assertNotNull(relationType);
        Assert.assertEquals(relationType.toString(), value);
        Assert.assertEquals(relationType.getText(), text);

        size.incrementAndGet();
    });
    Assert.assertEquals(RelationType.values().length, size.get()); // Check that all RelationType enums have been collected
}
 
Example #3
Source File: CatalogIdentifierDatasource.java    From commerce-cif-connector with Apache License 2.0 5 votes vote down vote up
protected List<Resource> generateResources(ResourceResolver resolver, List<String> properties) {
    return properties.stream().map(property -> {
        Map<String, Object> resourceProperties = new HashMap<String, Object>() {
            {
                put("text", property);
                put("value", property);
            }
        };
        ValueMapResource syntheticResource = new ValueMapResource(resolver, new ResourceMetadata(), "", new ValueMapDecorator(
            resourceProperties));
        return syntheticResource;
    }).collect(Collectors.toList());
}
 
Example #4
Source File: SimpleDataSourceBuilder.java    From APM with Apache License 2.0 5 votes vote down vote up
private Resource createDataSourceItem(ResourceResolver resolver, String name, String value) {
  Map<String, Object> valueMap = ImmutableMap.of(
      CONFIGURATION_NAME_PROP, name,
      CONFIGURATION_PATH_PROP, value
  );
  ValueMapDecorator result = new ValueMapDecorator(valueMap);
  return new ValueMapResource(resolver, new ResourceMetadata(), JcrConstants.NT_RESOURCE, result);
}
 
Example #5
Source File: MagentoGraphqlClient.java    From aem-core-cif-components with Apache License 2.0 4 votes vote down vote up
private MagentoGraphqlClient(Resource resource, Page page) {

        Resource configurationResource = page != null ? page.adaptTo(Resource.class) : resource;

        LOGGER.debug("Try to get a graphql client from the resource at {}", configurationResource.getPath());

        ComponentsConfiguration configuration = configurationResource.adaptTo(ComponentsConfiguration.class);
        if (configuration.size() == 0) {
            LOGGER.warn("Context configuration not found, attempt to read the configuration from the page");
            graphqlClient = configurationResource.adaptTo(GraphqlClient.class);
        } else {
            LOGGER.debug("Crafting a configuration resource and attempting to get a GraphQL client from it...");
            // The Context-Aware Configuration API does return a ValueMap with all the collected properties from /conf and /libs,
            // but if you ask it for a resource via ConfigurationResourceResolver#getConfigurationResource() you get the resource that
            // resolves first (e.g. /conf/.../settings/cloudonfigs/commerce). This resource might not contain the properties
            // we need to adapt it to a graphql client so we just craft our own resource using the value map provided above.
            Resource configResource = new ValueMapResource(configurationResource.getResourceResolver(),
                configurationResource.getPath(),
                configurationResource.getResourceType(),
                configuration.getValueMap());

            graphqlClient = configResource.adaptTo(GraphqlClient.class);
        }
        if (graphqlClient == null) {
            throw new RuntimeException("GraphQL client not available for resource " + configurationResource.getPath());
        }
        requestOptions = new RequestOptions().withGson(QueryDeserializer.getGson());

        CachingStrategy cachingStrategy = new CachingStrategy()
            .withCacheName(resource.getResourceType())
            .withDataFetchingPolicy(DataFetchingPolicy.CACHE_FIRST);
        requestOptions.withCachingStrategy(cachingStrategy);

        String storeCode;

        if (configuration.size() > 0) {
            storeCode = configuration.get(STORE_CODE_PROPERTY, String.class);
            if (storeCode == null) {
                storeCode = readFallBackConfiguration(configurationResource, STORE_CODE_PROPERTY);
            }
        } else {
            storeCode = readFallBackConfiguration(configurationResource, STORE_CODE_PROPERTY);
        }
        if (StringUtils.isNotEmpty(storeCode)) {
            Header storeHeader = new BasicHeader("Store", storeCode);
            requestOptions.withHeaders(Collections.singletonList(storeHeader));
        }
    }