package jnpf.elnService.template.docx; import org.springframework.util.StringUtils; import java.util.Collection; import java.util.LinkedHashMap; import java.util.Map; public final class DocxControlConfigMatcher { private static final String DROPDOWN = "DROPDOWN"; private static final String DATE = "DATE"; private DocxControlConfigMatcher() { } /** * OOXML does not reliably expose field placeholders, default text, default checkbox state, * or checkbox presentation metadata. Dropdown items and date format currently cross the * field-config and DOCX evidence boundary. */ public static boolean matches(String controlType, Map fieldConfig, Map docxConfig) { if (DROPDOWN.equalsIgnoreCase(controlType)) { Map expected = fieldOptions(fieldConfig); Map observed = docxOptions(docxConfig); return expected != null && observed != null && expected.equals(observed); } if (DATE.equalsIgnoreCase(controlType)) { String expected = fieldConfig == null ? null : text(fieldConfig.get("dateFormat")); String observed = docxConfig == null ? null : text(docxConfig.get("dateFormat")); return StringUtils.hasText(expected) && expected.equals(observed); } return true; } private static Map fieldOptions(Map config) { if (config == null || !(config.get("options") instanceof Collection)) { return null; } Map result = new LinkedHashMap<>(); for (Object option : (Collection) config.get("options")) { if (!(option instanceof Map)) { return null; } Map values = (Map) option; String value = text(values.get("value")); String display = text(values.get("display")); if (!StringUtils.hasText(value) || !StringUtils.hasText(display) || result.containsKey(value)) { return null; } result.put(value, display); } return result; } private static Map docxOptions(Map config) { if (config == null) { return new LinkedHashMap<>(); } if (config.get("options") instanceof Collection) { return fieldOptions(config); } Map result = new LinkedHashMap<>(); for (Map.Entry entry : config.entrySet()) { String value = text(entry.getKey()); String display = text(entry.getValue()); if (!StringUtils.hasText(value) || !StringUtils.hasText(display) || result.containsKey(value)) { return null; } result.put(value, display); } return result; } private static String text(Object value) { return value == null ? null : String.valueOf(value).trim(); } }