package at.gv.egiz.moazs.preprocess; import at.gv.zustellung.app2mzs.xsd.ConfigType; import at.gv.zustellung.app2mzs.xsd.ServerType; import org.springframework.lang.Nullable; import org.springframework.stereotype.Component; import java.util.Map; import static at.gv.zustellung.app2mzs.xsd.ConfigType.configTypeBuilder; import static at.gv.zustellung.app2mzs.xsd.ServerType.serverTypeBuilder; @Component public class ConfigUtil { private static final String TNVZ_REQUEST_KEY = "perform-query-person-request"; private static final String MSG_URL_KEY = "msg.url"; /** * Convert a map into a Config object. * * @param map with well-defined indexes and values * @return Config */ public ConfigType convert(Map values) { var server = serverTypeBuilder() .withZUSEUrlID(values.get(MSG_URL_KEY)) .build(); Boolean performQueryPersonRequest = values.get(TNVZ_REQUEST_KEY) == null ? null : Boolean.getBoolean(values.get(TNVZ_REQUEST_KEY)); return ConfigType.configTypeBuilder() .withPerformQueryPersonRequest(performQueryPersonRequest) .withServer(server) .build(); } /** * Combine properties of two Configs; {@code primary} overrides {@code fallback}. * * @param primary * @param fallback * @return Merged Config */ public ConfigType merge(ConfigType primary, ConfigType fallback) { var builder = configTypeBuilder(fallback); if(primary.getServer() != null) { builder.withServer(merge(primary.getServer(), fallback.getServer())); } if(primary.isPerformQueryPersonRequest() != null) { builder.withPerformQueryPersonRequest(primary.isPerformQueryPersonRequest()); } return builder.build(); } private ServerType merge(ServerType primary, ServerType fallback) { if (fallback == null) { return primary; } var builder = serverTypeBuilder(fallback); if (primary.getX509() != null) builder.withX509 (primary.getX509() ); if (primary.getZUSEUrlID() != null) builder.withZUSEUrlID(primary.getZUSEUrlID()); return builder.build(); } /** * Check if all mandatory fields are set. * * @param profile * @return true if all mandatory fields are set */ public boolean isComplete(@Nullable ConfigType profile) { //TODO: add check fo x509 certificate return profile != null && profile.isPerformQueryPersonRequest() != null && profile.getServer() != null && profile.getServer().getZUSEUrlID() != null; } }