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