From 4501468f1ffe1a8141e0c8711cd3cf78260df0c9 Mon Sep 17 00:00:00 2001 From: Christof Rabensteiner Date: Thu, 16 May 2019 13:31:53 +0200 Subject: Refactor: Merge Converter, Validator, and Merger into Util - Reason: All three classes opertate with the same data type, have the same clients, and have the same reasons for change. - Makes code in client more readable as it reduces number of dependencies. --- .../at/gv/egiz/moazs/preprocess/ConfigUtil.java | 95 ++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 src/main/java/at/gv/egiz/moazs/preprocess/ConfigUtil.java (limited to 'src/main/java/at/gv/egiz/moazs/preprocess/ConfigUtil.java') diff --git a/src/main/java/at/gv/egiz/moazs/preprocess/ConfigUtil.java b/src/main/java/at/gv/egiz/moazs/preprocess/ConfigUtil.java new file mode 100644 index 0000000..736fb0a --- /dev/null +++ b/src/main/java/at/gv/egiz/moazs/preprocess/ConfigUtil.java @@ -0,0 +1,95 @@ +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(); + } + + /** + * + * + * @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; + } + +} -- cgit v1.2.3