From 725c3509659913089e174b7ccb2493a1b1d1be43 Mon Sep 17 00:00:00 2001 From: Sergey Nuyanzin Date: Mon, 20 Nov 2023 12:57:43 +0100 Subject: [PATCH] Add DefaultAttributeConverter Signed-off-by: Sergey Nuyanzin --- .../klaw/helpers/AivenAclIdConverter.java | 30 +++------ .../helpers/DefaultAttributeConverter.java | 67 +++++++++++++++++++ .../klaw/helpers/EnvParamsConverter.java | 32 +-------- .../aiven/klaw/helpers/EnvTagConverter.java | 32 +-------- .../helpers/ServiceAccountsConverter.java | 32 +-------- .../klaw/helpers/SwitchTeamsConverter.java | 31 +++------ 6 files changed, 98 insertions(+), 126 deletions(-) create mode 100644 core/src/main/java/io/aiven/klaw/helpers/DefaultAttributeConverter.java diff --git a/core/src/main/java/io/aiven/klaw/helpers/AivenAclIdConverter.java b/core/src/main/java/io/aiven/klaw/helpers/AivenAclIdConverter.java index 531ab8d1d6..8ebf0066ef 100644 --- a/core/src/main/java/io/aiven/klaw/helpers/AivenAclIdConverter.java +++ b/core/src/main/java/io/aiven/klaw/helpers/AivenAclIdConverter.java @@ -1,35 +1,27 @@ package io.aiven.klaw.helpers; -import static io.aiven.klaw.service.UsersTeamsControllerService.OBJECT_MAPPER; - -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; -import jakarta.persistence.AttributeConverter; import jakarta.persistence.Converter; -import java.util.HashMap; +import java.util.Collections; import java.util.Map; @Converter -public class AivenAclIdConverter implements AttributeConverter, String> { +public class AivenAclIdConverter implements DefaultAttributeConverter> { + + private static final TypeReference> TYPE_REFERENCE = new TypeReference<>() {}; @Override public String convertToDatabaseColumn(Map aclIdMap) { - try { - if (aclIdMap != null) return OBJECT_MAPPER.writer().writeValueAsString(aclIdMap); - else return ""; - } catch (JsonProcessingException e) { - return ""; - } + return convertToDatabaseColumn(aclIdMap, ""); } @Override public Map convertToEntityAttribute(String aclIdMapJson) { - try { - if (aclIdMapJson != null) - return OBJECT_MAPPER.readValue(aclIdMapJson, new TypeReference<>() {}); - else return new HashMap<>(); - } catch (JsonProcessingException e) { - return new HashMap<>(); - } + return convertToEntityAttribute(aclIdMapJson, Collections.emptyMap()); + } + + @Override + public TypeReference> getTypeReference() { + return TYPE_REFERENCE; } } diff --git a/core/src/main/java/io/aiven/klaw/helpers/DefaultAttributeConverter.java b/core/src/main/java/io/aiven/klaw/helpers/DefaultAttributeConverter.java new file mode 100644 index 0000000000..c764c86ad4 --- /dev/null +++ b/core/src/main/java/io/aiven/klaw/helpers/DefaultAttributeConverter.java @@ -0,0 +1,67 @@ +package io.aiven.klaw.helpers; + +import static io.aiven.klaw.service.UsersTeamsControllerService.OBJECT_MAPPER; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import jakarta.persistence.AttributeConverter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public interface DefaultAttributeConverter extends AttributeConverter { + + Logger LOG = LoggerFactory.getLogger(DefaultAttributeConverter.class); + + @Override + default String convertToDatabaseColumn(X x) { + return convertToDatabaseColumn(x, null); + } + + default String convertToDatabaseColumn(X x, String defaultValue) { + String res = defaultValue; + try { + if (x != null) { + res = OBJECT_MAPPER.writeValueAsString(x); + } + } catch (JsonProcessingException e) { + LOG.error("Exception converting object to json: {}", e.getMessage()); + } + return res; + } + + @Override + default X convertToEntityAttribute(String value) { + return convertToEntityAttribute(value, null); + } + + default X convertToEntityAttribute(String value, X defaultValue) { + X res = defaultValue; + try { + if (value != null) { + Class classOfEntityAttribute = getClassOfEntityAttribute(); + if (classOfEntityAttribute != null) { + res = OBJECT_MAPPER.readValue(value, classOfEntityAttribute); + } else { + TypeReference typeReference = getTypeReference(); + if (typeReference != null) { + res = OBJECT_MAPPER.readValue(value, typeReference); + } else { + throw new RuntimeException( + "Either getClassOfEntityAttribute or getTypeReference should provide non null value"); + } + } + } + } catch (JsonProcessingException e) { + LOG.error("Exception converting json to object: {}", e.getMessage()); + } + return res; + } + + default Class getClassOfEntityAttribute() { + return null; + } + + default TypeReference getTypeReference() { + return null; + } +} diff --git a/core/src/main/java/io/aiven/klaw/helpers/EnvParamsConverter.java b/core/src/main/java/io/aiven/klaw/helpers/EnvParamsConverter.java index be79c9a3e8..df4fd645d4 100644 --- a/core/src/main/java/io/aiven/klaw/helpers/EnvParamsConverter.java +++ b/core/src/main/java/io/aiven/klaw/helpers/EnvParamsConverter.java @@ -1,41 +1,15 @@ package io.aiven.klaw.helpers; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; import io.aiven.klaw.model.response.EnvParams; -import jakarta.persistence.AttributeConverter; import jakarta.persistence.Converter; import lombok.extern.slf4j.Slf4j; @Slf4j @Converter -public class EnvParamsConverter implements AttributeConverter { - - ObjectMapper mapper = new ObjectMapper(); - - @Override - public String convertToDatabaseColumn(EnvParams envParams) { - String envParamsStr = null; - try { - if (envParams != null) { - envParamsStr = mapper.writeValueAsString(envParams); - } - } catch (JsonProcessingException e) { - log.error("Exception converting object: {} to json: {}", envParams, e.getMessage()); - } - return envParamsStr; - } +public class EnvParamsConverter implements DefaultAttributeConverter { @Override - public EnvParams convertToEntityAttribute(String envParamsStr) { - EnvParams params = null; - try { - if (envParamsStr != null) { - params = mapper.readValue(envParamsStr, EnvParams.class); - } - } catch (JsonProcessingException e) { - log.error("Exception converting json: {} to object: {}", envParamsStr, e.getMessage()); - } - return params; + public Class getClassOfEntityAttribute() { + return EnvParams.class; } } diff --git a/core/src/main/java/io/aiven/klaw/helpers/EnvTagConverter.java b/core/src/main/java/io/aiven/klaw/helpers/EnvTagConverter.java index 5b1ef2b1f3..451b2162a8 100644 --- a/core/src/main/java/io/aiven/klaw/helpers/EnvTagConverter.java +++ b/core/src/main/java/io/aiven/klaw/helpers/EnvTagConverter.java @@ -1,41 +1,15 @@ package io.aiven.klaw.helpers; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; import io.aiven.klaw.dao.EnvTag; -import jakarta.persistence.AttributeConverter; import jakarta.persistence.Converter; import lombok.extern.slf4j.Slf4j; @Slf4j @Converter -public class EnvTagConverter implements AttributeConverter { - - ObjectMapper mapper = new ObjectMapper(); - - @Override - public String convertToDatabaseColumn(EnvTag envTag) { - String envTagStr = null; - try { - if (envTag != null) { - envTagStr = mapper.writeValueAsString(envTag); - } - } catch (JsonProcessingException e) { - log.error("Exception converting object to json: {}", e.getMessage()); - } - return envTagStr; - } +public class EnvTagConverter implements DefaultAttributeConverter { @Override - public EnvTag convertToEntityAttribute(String envTagStr) { - EnvTag tag = null; - try { - if (envTagStr != null) { - tag = mapper.readValue(envTagStr, EnvTag.class); - } - } catch (JsonProcessingException e) { - log.error("Exception converting json to object: {}", e.getMessage()); - } - return tag; + public Class getClassOfEntityAttribute() { + return EnvTag.class; } } diff --git a/core/src/main/java/io/aiven/klaw/helpers/ServiceAccountsConverter.java b/core/src/main/java/io/aiven/klaw/helpers/ServiceAccountsConverter.java index a570c2a2e2..b05680c82e 100644 --- a/core/src/main/java/io/aiven/klaw/helpers/ServiceAccountsConverter.java +++ b/core/src/main/java/io/aiven/klaw/helpers/ServiceAccountsConverter.java @@ -1,41 +1,15 @@ package io.aiven.klaw.helpers; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; import io.aiven.klaw.dao.ServiceAccounts; -import jakarta.persistence.AttributeConverter; import jakarta.persistence.Converter; import lombok.extern.slf4j.Slf4j; @Slf4j @Converter -public class ServiceAccountsConverter implements AttributeConverter { - - ObjectMapper mapper = new ObjectMapper(); - - @Override - public String convertToDatabaseColumn(ServiceAccounts serviceAccounts) { - String serviceAccountsStr = null; - try { - if (serviceAccounts != null) { - serviceAccountsStr = mapper.writeValueAsString(serviceAccounts); - } - } catch (JsonProcessingException e) { - log.error("Exception converting object to json: {}", e.getMessage()); - } - return serviceAccountsStr; - } +public class ServiceAccountsConverter implements DefaultAttributeConverter { @Override - public ServiceAccounts convertToEntityAttribute(String serviceAccountsStr) { - ServiceAccounts serviceAccounts = null; - try { - if (serviceAccountsStr != null) { - serviceAccounts = mapper.readValue(serviceAccountsStr, ServiceAccounts.class); - } - } catch (JsonProcessingException e) { - log.error("Exception converting json to object: {}", e.getMessage()); - } - return serviceAccounts; + public Class getClassOfEntityAttribute() { + return ServiceAccounts.class; } } diff --git a/core/src/main/java/io/aiven/klaw/helpers/SwitchTeamsConverter.java b/core/src/main/java/io/aiven/klaw/helpers/SwitchTeamsConverter.java index 404c780e35..082f335800 100644 --- a/core/src/main/java/io/aiven/klaw/helpers/SwitchTeamsConverter.java +++ b/core/src/main/java/io/aiven/klaw/helpers/SwitchTeamsConverter.java @@ -1,36 +1,27 @@ package io.aiven.klaw.helpers; -import static io.aiven.klaw.service.UsersTeamsControllerService.OBJECT_MAPPER; - -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; -import jakarta.persistence.AttributeConverter; import jakarta.persistence.Converter; -import java.util.HashSet; +import java.util.Collections; import java.util.Set; @Converter -public class SwitchTeamsConverter implements AttributeConverter, String> { +public class SwitchTeamsConverter implements DefaultAttributeConverter> { + + private static final TypeReference> TYPE_REFERENCE = new TypeReference<>() {}; @Override public String convertToDatabaseColumn(Set switchTeamsList) { - try { - if (switchTeamsList != null) - return OBJECT_MAPPER.writer().writeValueAsString(switchTeamsList); - else return ""; - } catch (JsonProcessingException e) { - return ""; - } + return convertToDatabaseColumn(switchTeamsList, ""); } @Override public Set convertToEntityAttribute(String switchTeamsJson) { - try { - if (switchTeamsJson != null) - return OBJECT_MAPPER.readValue(switchTeamsJson, new TypeReference<>() {}); - else return new HashSet<>(); - } catch (JsonProcessingException e) { - return new HashSet<>(); - } + return convertToEntityAttribute(switchTeamsJson, Collections.emptySet()); + } + + @Override + public TypeReference> getTypeReference() { + return TYPE_REFERENCE; } }