Search in sources :

Example 1 with Function

use of tldgen.Function in project acs-aem-commons by Adobe-Consulting-Services.

the class MultiFieldPanelFunctions method getMultiFieldPanelValues.

/**
 * Extract the value of a MultiFieldPanel property into a list of maps. Will never return
 * a null map, but may return an empty one. Invalid property values are logged and skipped.
 *
 * @param resource the resource
 * @param name the property name
 * @return a list of maps.
 */
@Function
public static List<Map<String, String>> getMultiFieldPanelValues(Resource resource, String name) {
    ValueMap map = resource.adaptTo(ValueMap.class);
    List<Map<String, String>> results = new ArrayList<Map<String, String>>();
    if (map != null && map.containsKey(name)) {
        String[] values = map.get(name, new String[0]);
        for (String value : values) {
            try {
                JSONObject parsed = new JSONObject(value);
                Map<String, String> columnMap = new HashMap<String, String>();
                for (Iterator<String> iter = parsed.keys(); iter.hasNext(); ) {
                    String key = iter.next();
                    String innerValue = parsed.getString(key);
                    columnMap.put(key, innerValue);
                }
                results.add(columnMap);
            } catch (JSONException e) {
                log.error(String.format("Unable to parse JSON in %s property of %s", name, resource.getPath()), e);
            }
        }
    }
    return results;
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject) HashMap(java.util.HashMap) ValueMap(org.apache.sling.api.resource.ValueMap) ArrayList(java.util.ArrayList) JSONException(org.apache.sling.commons.json.JSONException) ValueMap(org.apache.sling.api.resource.ValueMap) HashMap(java.util.HashMap) Map(java.util.Map) Function(tldgen.Function)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ValueMap (org.apache.sling.api.resource.ValueMap)1 JSONException (org.apache.sling.commons.json.JSONException)1 JSONObject (org.apache.sling.commons.json.JSONObject)1 Function (tldgen.Function)1