Use cases of ModifiableValueMap in AEM


The ModifiableValueMap is an extension of the ValueMap which allows to modify and persist properties. 

All changes to this map are stored in the transient layer of the resource resolver or more precisely in the transient layer of the resource provider managing this resource.

Use cases in AEM ::

Mandatory step - 
    1. Get the resource resolver.
    2. Get ModifiableValueMap interface -   
          ModifiableValueMap mvp = resolver.adaptTo(ModifiableValueMap.class);

1. Get SINGLE value property of a node:
    String value = mvp.get("multivalue", String.class); 

2. Get MULTI value property of a node:
    String[] value = mvp.get("multivalue", String[].class); 

3. Add SINGLE value property to a node:
    String val = "single";
    a. mvp.put("singlevalue", val);
    b. mvp.putIfAbsent("singlevalue", val); 


4. Add MULTI value property to a node:
    String[] val = {"test1","test2"};
    mvp.put("multivalue", val); 

5. Add MULTIPLE properties to a node:
    Map<String, String[]> map = new HashMap<>();
    map.put("prop1", val1);
    map.put("prop2", val2);
    mvp.putAll(mapp);

P.S.- Perform resolver.commit() after put operation and do resolver.close() after completing all resource resolver related operations.


Happy Learning!😀


Comments

Popular posts from this blog

A Simple Example of Sling Pipes in AEM

Run modes in AEM