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<String, Object> fieldConfig,
|
Map<String, Object> docxConfig) {
|
if (DROPDOWN.equalsIgnoreCase(controlType)) {
|
Map<String, String> expected = fieldOptions(fieldConfig);
|
Map<String, String> 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<String, String> fieldOptions(Map<String, Object> config) {
|
if (config == null || !(config.get("options") instanceof Collection)) {
|
return null;
|
}
|
Map<String, String> 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<String, String> docxOptions(Map<String, Object> config) {
|
if (config == null) {
|
return new LinkedHashMap<>();
|
}
|
if (config.get("options") instanceof Collection) {
|
return fieldOptions(config);
|
}
|
Map<String, String> result = new LinkedHashMap<>();
|
for (Map.Entry<String, Object> 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();
|
}
|
}
|