From 0e93567de0cb20f7dcaf4c1a51b5d6f2c1959f56 Mon Sep 17 00:00:00 2001 From: Chris Cleveland Date: Mon, 9 Dec 2019 14:50:31 -0600 Subject: [PATCH] added support for AttributeConverter --- .../converter/IntArrayToListConverter.java | 7 +-- .../converter/StringToIntListConverter.java | 53 +++++++++++++++++++ .../norm/sqlmakers/StandardPojoInfo.java | 7 ++- 3 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/dieselpoint/norm/converter/StringToIntListConverter.java diff --git a/src/main/java/com/dieselpoint/norm/converter/IntArrayToListConverter.java b/src/main/java/com/dieselpoint/norm/converter/IntArrayToListConverter.java index de6430b..649345a 100644 --- a/src/main/java/com/dieselpoint/norm/converter/IntArrayToListConverter.java +++ b/src/main/java/com/dieselpoint/norm/converter/IntArrayToListConverter.java @@ -25,10 +25,11 @@ public List convertToEntityAttribute(Array dbData) { if (dbData.getBaseType() != java.sql.Types.INTEGER) { throw new DbException("Database is not returning an integer array"); } - int[] arr = (int[]) dbData.getArray(); + + Integer [] arr = (Integer[]) dbData.getArray(); List out = new ArrayList<>(); - for (int i = 0; i < arr.length; i++) { - out.add(arr[i]); + for (Integer i: arr) { + out.add(i); } return out; diff --git a/src/main/java/com/dieselpoint/norm/converter/StringToIntListConverter.java b/src/main/java/com/dieselpoint/norm/converter/StringToIntListConverter.java new file mode 100644 index 0000000..7386854 --- /dev/null +++ b/src/main/java/com/dieselpoint/norm/converter/StringToIntListConverter.java @@ -0,0 +1,53 @@ +package com.dieselpoint.norm.converter; + +import java.util.ArrayList; +import java.util.List; + +import javax.persistence.AttributeConverter; +import javax.persistence.Converter; + +@Converter +public class StringToIntListConverter implements AttributeConverter, String> { + + @Override + public String convertToDatabaseColumn(List attribute) { + if (attribute == null) { + return null; + } + StringBuilder sb = new StringBuilder(); + int len = attribute.size(); + + for (int i = 0; i < len; i++) { + if (i > 0) { + sb.append(','); + } + sb.append(attribute.get(i).intValue()); + + } + return sb.toString(); + } + + @Override + public List convertToEntityAttribute(String in) { + // deserialize string in the form "123,456" no spaces allowed + List list = new ArrayList<>(); + if (in == null || in.length() == 0) { + return list; + } + + int value = 0; + for (int i = 0; i < in.length(); i++) { + int digit = in.charAt(i) - '0'; + if (digit >= 0 && digit <= 9) { + value = (value * 10) + digit; + } else { + // hit a comma + list.add(value); + value = 0; + } + } + list.add(value); + return list; + } + +} diff --git a/src/main/java/com/dieselpoint/norm/sqlmakers/StandardPojoInfo.java b/src/main/java/com/dieselpoint/norm/sqlmakers/StandardPojoInfo.java index 5471bde..6fd8dc5 100644 --- a/src/main/java/com/dieselpoint/norm/sqlmakers/StandardPojoInfo.java +++ b/src/main/java/com/dieselpoint/norm/sqlmakers/StandardPojoInfo.java @@ -234,9 +234,9 @@ public Object getValue(Object pojo, String name) { if (value != null) { if (prop.serializer != null) { value = prop.serializer.serialize(value); - - // TODO put prop.converter here + } else if (prop.converter != null) { + value = prop.converter.convertToDatabaseColumn(value); } else if (prop.isEnumField) { // handle enums according to selected enum type @@ -274,6 +274,9 @@ public void putValue(Object pojo, String name, Object value, boolean ignoreIfMis if (value != null) { if (prop.serializer != null) { value = prop.serializer.deserialize((String) value, prop.dataType); + + } else if (prop.converter != null) { + value = prop.converter.convertToEntityAttribute(value); } else if (prop.isEnumField) { value = getEnumConst(prop.enumClass, prop.enumType, value);