Commit 2eeb4f01 authored by Piotr Gawron's avatar Piotr Gawron

collection of invalid entries is processed properly

parent 92229e5e
......@@ -352,11 +352,11 @@ public abstract class BaseDao<T> {
@Override
public void accept(Object t, Field u) {
try {
Column column = getAnnotationColumn(u);
if (u.getType() == String.class) {
u.setAccessible(true);
String newObject = (String) u.get(t);
Column column = getAnnotationColumn(u);
if (column != null) {
if (!column.nullable()) {
if (newObject == null) {
......@@ -372,12 +372,18 @@ public abstract class BaseDao<T> {
} else if (u.getType() == Double.class || u.getType() == Integer.class || u.getType() == Class.class
|| u.getType() == Color.class || u.getType() == Calendar.class || u.getType().isEnum()) {
Object newObject = u.get(t);
Column column = getAnnotationColumn(u);
if (column != null && !column.nullable()) {
if (newObject == null) {
result.add(u + " is null but shouldn't");
}
}
} else if (Collection.class.isAssignableFrom(u.getType())) {
Collection<?> newObjectSet = (Collection<?>) u.get(t);
for (Object o : newObjectSet) {
if (isProblematicProperty(o, column)) {
result.add(u + " has invalid entry: " + o);
}
}
} else {
logger.error("Don't know how to handle class: " + u.getType());
}
......@@ -444,6 +450,16 @@ public abstract class BaseDao<T> {
if (!column.nullable() && newObject == null) {
u.set(t, u.getType().getEnumConstants()[0]);
}
} else if (Collection.class.isAssignableFrom(u.getType())) {
Collection<?> newObjectSet = (Collection<?>) newObject;
Set<Object> toRemove = new HashSet<>();
for (Object o : newObjectSet) {
if (isProblematicProperty(o, column)) {
toRemove.add(o);
}
}
newObjectSet.removeAll(toRemove);
} else {
logger.error("Don't know how to handle class: " + u.getType());
}
......@@ -482,6 +498,7 @@ public abstract class BaseDao<T> {
ReflectionUtils.doWithFields(object.getClass(), new ReflectionUtils.FieldCallback() {
@Override
public void doWith(Field arg0) throws IllegalArgumentException, IllegalAccessException {
arg0.setAccessible(true);
Column column = getAnnotationColumn(arg0);
Object property = arg0.get(object);
if (skippedClasses.contains(arg0.getType())) {
......@@ -495,11 +512,14 @@ public abstract class BaseDao<T> {
processIssue.accept(object, arg0);
}
} else if (Collection.class.isAssignableFrom(arg0.getType())) {
arg0.setAccessible(true);
Collection<?> newObjectSet = (Collection<?>) property;
List<Object> problematicEntries = new ArrayList<>();
if (newObjectSet != null) {
for (Object newObject : newObjectSet) {
if (isProblematicProperty(newObject, column)) {
problematicEntries.add(newObject);
}
if (newObject != null && !skippedClasses.contains(newObject.getClass())) {
if (!processed.contains(newObject)) {
objects.add(newObject);
......@@ -507,6 +527,10 @@ public abstract class BaseDao<T> {
}
}
}
if (problematicEntries.size() > 0) {
processIssue.accept(object, arg0);
}
}
} else if (Object.class.isAssignableFrom(arg0.getType())) {
......
......@@ -28,6 +28,16 @@ public class BaseDaoTest extends PersistTestFunctions {
String val = null;
}
static class NullElementInStringList {
@Column(nullable = false)
List<String> val = new ArrayList<>(Arrays.asList("str", null));
}
static class NullElementInStringSet {
@Column(nullable = false)
Set<String> val = new HashSet<>(Arrays.asList("str", null));
}
static class NullDouble {
@Column(nullable = false)
Double val = null;
......@@ -81,6 +91,8 @@ public class BaseDaoTest extends PersistTestFunctions {
data.add(new Object[] { "Null Enum for not nullable", NullEnum.class });
data.add(new Object[] { "Null Calendar for not nullable", NullCalendar.class });
data.add(new Object[] { "Null Class for not nullable", NullClass.class });
data.add(new Object[] { "Null String in List for not nullable", NullElementInStringList.class });
data.add(new Object[] { "Null String in Set for not nullable", NullElementInStringSet.class });
return data;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment