Java Code Examples for org.apache.sling.api.resource.ValueMap#put()

The following examples show how to use org.apache.sling.api.resource.ValueMap#put() . 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: ChildrenDataSourceServletTest.java    From commerce-cif-connector with Apache License 2.0 5 votes vote down vote up
static Resource mockFolderResource(String name) {
    Resource resource = mock(Resource.class);
    ValueMap properties = new ModifiableMappedValueMapDecorator(new HashMap<>());
    properties.put("sling:resourceType", "sling:Folder");
    when(resource.getValueMap()).thenReturn(properties);
    when(resource.isResourceType("sling:Folder")).thenReturn(true);
    when(resource.getResourceType()).thenReturn("sling:Folder");
    when(resource.listChildren()).thenReturn(Collections.EMPTY_LIST.iterator());
    when(resource.getName()).thenReturn(name);

    return resource;
}
 
Example 3
Source File: ChildrenDataSourceServletTest.java    From commerce-cif-connector with Apache License 2.0 5 votes vote down vote up
private Resource mockProductResource(boolean mockChildren) {
    Resource resource = mock(Resource.class);
    ValueMap properties = new ModifiableMappedValueMapDecorator(new HashMap<>());
    properties.put("sling:resourceType", "commerce/components/product");
    properties.put("cq:commerceType", "product");
    when(resource.getValueMap()).thenReturn(properties);
    when(resource.isResourceType("commerce/components/product")).thenReturn(true);
    when(resource.getResourceType()).thenReturn("commerce/components/product");
    if (mockChildren) {
        when(resource.listChildren()).thenReturn(Collections.EMPTY_LIST.iterator());
    }
    when(resource.getName()).thenReturn("product" + (int) (100 * Math.random()));

    return resource;
}
 
Example 4
Source File: ChildrenDataSourceServletTest.java    From commerce-cif-connector with Apache License 2.0 5 votes vote down vote up
private Resource mockVariantResource(String name) {
    Resource resource = mock(Resource.class);
    ValueMap properties = new ModifiableMappedValueMapDecorator(new HashMap<>());
    properties.put("sling:resourceType", "commerce/components/product");
    properties.put("cq:commerceType", "variant");
    when(resource.getValueMap()).thenReturn(properties);
    when(resource.isResourceType("commerce/components/product")).thenReturn(true);
    when(resource.getResourceType()).thenReturn("commerce/components/product");
    when(resource.listChildren()).thenReturn(Collections.EMPTY_LIST.iterator());
    when(resource.getName()).thenReturn(name);

    return resource;
}
 
Example 5
Source File: ChildrenDataSourceServletTest.java    From commerce-cif-connector with Apache License 2.0 5 votes vote down vote up
private Resource mockSomeResource() {
    Resource resource = mock(Resource.class);
    ValueMap properties = new ModifiableMappedValueMapDecorator(new HashMap<>());
    properties.put("sling:resourceType", "some/type");
    when(resource.getValueMap()).thenReturn(properties);
    when(resource.getResourceType()).thenReturn("some/type");
    when(resource.listChildren()).thenReturn(Collections.EMPTY_LIST.iterator());

    return resource;
}
 
Example 6
Source File: Initializer.java    From commerce-cif-connector with Apache License 2.0 4 votes vote down vote up
@Override
public void activate() {
    final ValueMap properties = getProperties();
    final I18n i18n = new I18n(getRequest());
    final ExpressionHelper ex = new ExpressionHelper(getSlingScriptHelper().getService(ExpressionResolver.class), getRequest());
    final CommerceBasePathsService cbps = getSlingScriptHelper().getService(CommerceBasePathsService.class);

    // configure default properties for cifcategoryfield
    String defaultRootPath = new CatalogSearchSupport(getResourceResolver()).findCatalogPathForPicker(getRequest());
    if (StringUtils.isBlank(defaultRootPath)) {
        defaultRootPath = cbps.getProductsBasePath();
    }
    final String rootPath = ex.getString(properties.get("rootPath", defaultRootPath));
    final String filter = properties.get("filter", "folderOrCategory");
    final boolean multiple = properties.get("multiple", DEFAULT_SELECTION_MULTIPLE);
    final String selectionId = properties.get("selectionId", DEFAULT_SELECTION_TYPE);
    final String defaultEmptyText = "path".equals(selectionId) ? "Category path" : "Category ID";
    final String emptyText = i18n.getVar(properties.get("emptyText", i18n.get(defaultEmptyText)));

    final String selectionCount = multiple ? "multiple" : "single";
    String pickerSrc = DEFAULT_PICKER_SRC + "?root=" + Text.escape(rootPath) + "&filter=" + Text.escape(filter) + "&selectionCount="
        + selectionCount + "&selectionId=" + Text.escape(selectionId);
    pickerSrc = properties.get("pickerSrc", pickerSrc);

    // suggestions disabled

    ValueMapResourceWrapper wrapper = new ValueMapResourceWrapper(getResource(), FIELD_SUPER_TYPE);
    ValueMap wrapperProperties = wrapper.adaptTo(ValueMap.class);
    wrapperProperties.putAll(properties);
    wrapperProperties.put("rootPath", rootPath);
    wrapperProperties.put("filter", filter);
    wrapperProperties.put("multiple", multiple);
    wrapperProperties.put("pickerSrc", pickerSrc);
    // needed to disable the default suggestions of pathfield
    wrapperProperties.put("suggestionSrc", "");
    wrapperProperties.put("emptyText", emptyText);

    wrapperProperties.put("forceselection", true);

    getSlingScriptHelper().include(wrapper);
}
 
Example 7
Source File: Initializer.java    From commerce-cif-connector with Apache License 2.0 4 votes vote down vote up
@Override
public void activate() {
    final ValueMap properties = getProperties();
    final I18n i18n = new I18n(getRequest());
    final ExpressionHelper ex = new ExpressionHelper(getSlingScriptHelper().getService(ExpressionResolver.class), getRequest());
    final CommerceBasePathsService cbps = getSlingScriptHelper().getService(CommerceBasePathsService.class);

    // configure default properties for productfield
    String defaultRootPath = new CatalogSearchSupport(getResourceResolver()).findCatalogPathForPicker(getRequest());
    if (StringUtils.isBlank(defaultRootPath)) {
        defaultRootPath = cbps.getProductsBasePath();
    }
    final String rootPath = ex.getString(properties.get("rootPath", defaultRootPath));
    final String filter = properties.get("filter", DEFAULT_FILTER);
    final boolean multiple = properties.get("multiple", DEFAULT_SELECTION_MULTIPLE);
    final String selectionId = properties.get("selectionId", DEFAULT_SELECTION_TYPE);
    final String defaultEmptyText;
    if ("path".equals(selectionId)) {
        defaultEmptyText = "Product path";
    } else if ("sku".equals(selectionId)) {
        defaultEmptyText = "Product SKU";
    } else if ("slug".equals(selectionId)) {
        defaultEmptyText = "Product slug";
    } else if ("combinedSku".equals(selectionId)) {
        defaultEmptyText = "Product SKU(s) separated by # character";
    } else {
        defaultEmptyText = "Product ID";
    }
    final String emptyText = i18n.getVar(properties.get("emptyText", i18n.get(defaultEmptyText)));

    final String selectionCount = multiple ? "multiple" : "single";
    String pickerSrc = DEFAULT_PICKER_SRC + "?root=" + Text.escape(rootPath) + "&filter=" + Text.escape(filter) + "&selectionCount="
        + selectionCount + "&selectionId=" + Text.escape(selectionId);
    String suggestionSrc = DEFAULT_SUGGESTION_SRC + "?root=" + Text.escape(rootPath) + "&filter=product{&query}";
    pickerSrc = properties.get("pickerSrc", pickerSrc);
    suggestionSrc = properties.get("suggestionSrc", suggestionSrc);

    // suggestions disabled
    suggestionSrc = "";

    ValueMapResourceWrapper wrapper = new ValueMapResourceWrapper(getResource(), FIELD_SUPER_TYPE);
    ValueMap wrapperProperties = wrapper.adaptTo(ValueMap.class);
    wrapperProperties.putAll(properties);
    wrapperProperties.put("rootPath", rootPath);
    wrapperProperties.put("filter", filter);
    wrapperProperties.put("multiple", multiple);
    wrapperProperties.put("pickerSrc", pickerSrc);
    wrapperProperties.put("suggestionSrc", suggestionSrc);
    wrapperProperties.put("emptyText", emptyText);
    wrapperProperties.put("forceselection", true);

    getSlingScriptHelper().include(wrapper);
}