Commit 3e196ab2 authored by Piotr Gawron's avatar Piotr Gawron

not null constraints on Points are handled by validator

parent fa1b7f5a
package lcsb.mapviewer.persist;
import java.awt.Color;
import java.awt.geom.Point2D;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.*;
......@@ -49,13 +50,13 @@ public class ObjectValidator {
if (column != null) {
if (!column.nullable()) {
if (newObject == null) {
result.add(new Pair<>(objectUnderInvestigation, u + " is null but shouldn't"));
result.add(new Pair<>(objectUnderInvestigation, u.getName() + " is null but shouldn't"));
}
}
if (newObject != null && column.length() <= newObject.length()
&& !column.columnDefinition().equals("TEXT")) {
result.add(new Pair<>(objectUnderInvestigation,
u + " is too long (limit: " + column.length() + "; found: " + newObject.length() + ")"
u.getName() + " is too long (limit: " + column.length() + "; found: " + newObject.length() + ")"
+ newObject));
}
}
......@@ -64,20 +65,20 @@ public class ObjectValidator {
Object newObject = u.get(t);
if (column != null && !column.nullable()) {
if (newObject == null) {
result.add(new Pair<>(objectUnderInvestigation, u + " is null but shouldn't"));
result.add(new Pair<>(objectUnderInvestigation, u.getName() + " 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(new Pair<>(objectUnderInvestigation, u + " has invalid entry: " + o));
if (isProblematicProperty(o, u)) {
result.add(new Pair<>(objectUnderInvestigation, u.getName() + " has invalid entry: " + o));
}
}
} else {
Object newObject = u.get(t);
if (isProblematicProperty(newObject, getOneToOne(u))) {
result.add(new Pair<>(objectUnderInvestigation, u + " is invalid: " + newObject));
if (isProblematicProperty(newObject, u)) {
result.add(new Pair<>(objectUnderInvestigation, u.getName() + " is invalid: " + newObject));
}
}
} catch (IllegalArgumentException | IllegalAccessException e) {
......@@ -152,12 +153,16 @@ public class ObjectValidator {
if (!column.nullable() && newObject == null) {
u.set(t, u.getType().getEnumConstants()[0]);
}
} else if (u.getType() == Point2D.class) {
if (!column.nullable() && newObject == null) {
u.set(t, new Point2D.Double());
}
} else if (Collection.class.isAssignableFrom(u.getType())) {
Collection<?> newObjectSet = (Collection<?>) newObject;
Set<Object> toRemove = new HashSet<>();
for (Object o : newObjectSet) {
if (isProblematicProperty(o, column)) {
if (isProblematicProperty(o, u)) {
toRemove.add(o);
}
}
......@@ -190,7 +195,6 @@ public class ObjectValidator {
@Override
public void doWith(Field arg0) throws IllegalArgumentException, IllegalAccessException {
arg0.setAccessible(true);
Column column = getAnnotationColumn(arg0);
Object property = arg0.get(object);
if (SKIPPED_CLASSES.contains(arg0.getType())) {
// skip
......@@ -199,7 +203,7 @@ public class ObjectValidator {
} else if (arg0.getType() == String.class || arg0.getType() == Double.class || arg0.getType() == Integer.class
|| arg0.getType() == Color.class || arg0.getType() == Calendar.class || arg0.getType() == Class.class
|| arg0.getType() == Calendar.class || arg0.getType().isEnum()) {
if (isProblematicProperty(property, column)) {
if (isProblematicProperty(property, arg0)) {
processIssue.accept(object, arg0, pair.getRight());
}
} else if (Collection.class.isAssignableFrom(arg0.getType())) {
......@@ -208,7 +212,7 @@ public class ObjectValidator {
List<Object> problematicEntries = new ArrayList<>();
if (newObjectSet != null) {
for (Object newObject : newObjectSet) {
if (isProblematicProperty(newObject, column)) {
if (isProblematicProperty(newObject, arg0)) {
problematicEntries.add(newObject);
}
addToQueue(newObject, pair.getRight());
......@@ -222,7 +226,7 @@ public class ObjectValidator {
} else if (Object.class.isAssignableFrom(arg0.getType())) {
arg0.setAccessible(true);
if (isProblematicProperty(property, getOneToOne(arg0))) {
if (isProblematicProperty(property, arg0)) {
processIssue.accept(object, arg0, pair.getRight());
}
......@@ -257,8 +261,9 @@ public class ObjectValidator {
objects = new LinkedList<>();
}
private boolean isProblematicProperty(Object property, Column column)
private boolean isProblematicProperty(Object property, Field field)
throws IllegalArgumentException, IllegalAccessException {
Column column = getAnnotationColumn(field);
if (column != null) {
if (property instanceof String) {
String stringProperty = (String) property;
......@@ -273,16 +278,10 @@ public class ObjectValidator {
}
}
}
return false;
}
private boolean isProblematicProperty(Object property, OneToOne column)
throws IllegalArgumentException, IllegalAccessException {
if (column != null) {
if (!column.optional()) {
if (property == null) {
return true;
}
OneToOne oneToOne = getOneToOne(field);
if (oneToOne != null && !oneToOne.optional()) {
if (property == null) {
return true;
}
}
return false;
......
......@@ -3,6 +3,7 @@ package lcsb.mapviewer.persist;
import static org.junit.Assert.assertEquals;
import java.awt.Color;
import java.awt.geom.Point2D;
import java.io.IOException;
import java.util.*;
......@@ -65,6 +66,11 @@ public class ObjectValidatorGenericTest extends PersistTestFunctions {
Class<?> val = null;
}
static class NullPoint {
@Column(nullable = false)
Point2D val = null;
}
static class ToLongString {
@Column(length = 3)
String val = "xxxx";
......@@ -88,6 +94,7 @@ public class ObjectValidatorGenericTest 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 Point for not nullable", NullPoint.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;
......
......@@ -269,5 +269,4 @@ public class ProjectDaoTest extends PersistTestFunctions {
long id2 = projectDao.getNextId();
assertNotEquals(id, id2);
}
}
......@@ -49,10 +49,8 @@ public class ReferenceGenomeDaoTest extends PersistTestFunctions {
long count = referenceGenomeDao.getByType(ReferenceGenomeType.UCSC).size();
referenceGenomeDao.add(genome);
long count2 = referenceGenomeDao.getByType(ReferenceGenomeType.UCSC).size();
;
referenceGenomeDao.delete(genome);
long count3 = referenceGenomeDao.getByType(ReferenceGenomeType.UCSC).size();
;
assertEquals(count + 1, count2);
assertEquals(count, count3);
......
......@@ -49,5 +49,4 @@ public class ReferenceGenomeGeneMappingDaoTest extends PersistTestFunctions {
assertEquals(count + 1, count2);
assertEquals(count, count3);
}
}
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