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 javax.validation.constraints.Null; 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"; /** * Builds a ConvertType Object out of a Map on the Basis of it's Keys and Values. * * @param Map with keys and values. * @return ConvertType Object */ 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(); } /** * Combines Properties of Two ConfigType Objects; {@code primary} overrides {@code fallback}. * * @param primary * @param fallback * @return Combined ConfigType */ public ConfigType merge(@Nullable ConfigType primary, ConfigType fallback) { var builder = configTypeBuilder(fallback); if(primary != null) { 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(); } /** * Checks if all mandatory fields are set. * * @param profile * @return */ 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; } }