Commit d8fe55f7 authored by Piotr Gawron's avatar Piotr Gawron

validation of object fetched by hibernate should ignore hibernate specific properties

parent e6763a2c
......@@ -16,6 +16,7 @@ import org.springframework.util.ReflectionUtils;
import lcsb.mapviewer.common.Pair;
import lcsb.mapviewer.model.map.BioEntity;
import lcsb.mapviewer.model.map.model.ModelData;
import lcsb.mapviewer.model.map.model.ModelFullIndexed;
import lcsb.mapviewer.model.user.User;
public class ObjectValidator {
......@@ -30,7 +31,8 @@ public class ObjectValidator {
char.class,
short.class,
byte.class,
User.class));
User.class,
ModelFullIndexed.class));
private interface ProcessIssue {
void accept(Object object, Field field, Object subjectUnderInvestigation);
......@@ -193,46 +195,56 @@ public class ObjectValidator {
ReflectionUtils.doWithFields(object.getClass(), new ReflectionUtils.FieldCallback() {
@Override
public void doWith(Field arg0) throws IllegalArgumentException, IllegalAccessException {
arg0.setAccessible(true);
Object property = arg0.get(object);
if (SKIPPED_CLASSES.contains(arg0.getType())) {
// skip
} else if (java.lang.reflect.Modifier.isStatic(arg0.getModifiers())) {
// skip static
} 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, arg0)) {
processIssue.accept(object, arg0, pair.getRight());
}
} else if (Collection.class.isAssignableFrom(arg0.getType())) {
Collection<?> newObjectSet = (Collection<?>) property;
List<Object> problematicEntries = new ArrayList<>();
if (newObjectSet != null) {
for (Object newObject : newObjectSet) {
if (isProblematicProperty(newObject, arg0)) {
problematicEntries.add(newObject);
public void doWith(Field arg0) {
try {
arg0.setAccessible(true);
Object property = arg0.get(object);
if (isIgnoredClass(arg0.getType())) {
// skip
} else if (java.lang.reflect.Modifier.isStatic(arg0.getModifiers())) {
// skip static
} else if (property == null ||
property instanceof String ||
property instanceof Double ||
property instanceof Integer ||
property instanceof Color ||
property instanceof Calendar ||
property instanceof Class ||
property instanceof Calendar ||
arg0.getType().isEnum()) {
if (isProblematicProperty(property, arg0)) {
processIssue.accept(object, arg0, pair.getRight());
}
} else if (property instanceof Collection) {
Collection<?> newObjectSet = (Collection<?>) property;
List<Object> problematicEntries = new ArrayList<>();
if (newObjectSet != null) {
for (Object newObject : newObjectSet) {
if (isProblematicProperty(newObject, arg0)) {
problematicEntries.add(newObject);
}
addToQueue(newObject, pair.getRight());
}
if (problematicEntries.size() > 0) {
processIssue.accept(object, arg0, pair.getRight());
}
addToQueue(newObject, pair.getRight());
}
if (problematicEntries.size() > 0) {
} else if (Object.class.isAssignableFrom(arg0.getType())) {
arg0.setAccessible(true);
if (isProblematicProperty(property, arg0)) {
processIssue.accept(object, arg0, pair.getRight());
}
}
} else if (Object.class.isAssignableFrom(arg0.getType())) {
arg0.setAccessible(true);
if (isProblematicProperty(property, arg0)) {
processIssue.accept(object, arg0, pair.getRight());
addToQueue(property, pair.getRight());
} else {
logger.error("Don't know how to process: " + arg0.getType());
}
addToQueue(property, pair.getRight());
} else {
logger.error("Don't know how to process: " + arg0.getType());
} catch (Exception e) {
logger.error("Problem with chcecking property: " + arg0, e);
}
}
......@@ -242,7 +254,7 @@ public class ObjectValidator {
}
private void addToQueue(Object object, Object objectUnderInvestigation) {
if (object != null && !SKIPPED_CLASSES.contains(object.getClass())) {
if (object != null && !isIgnoredClass(object.getClass())) {
if (!processed.contains(object)) {
if (object instanceof BioEntity) {
objectUnderInvestigation = object;
......@@ -287,4 +299,16 @@ public class ObjectValidator {
return false;
}
private boolean isIgnoredClass(Class<?> type) {
for (Class<?> clazz : SKIPPED_CLASSES) {
if (clazz.isAssignableFrom(type)) {
return true;
}
}
if (type.getPackage() != null && type.getPackage().getName().contains("hibernate")) {
return true;
}
return false;
}
}
......@@ -14,6 +14,7 @@ import lcsb.mapviewer.model.map.layout.*;
import lcsb.mapviewer.model.map.model.*;
import lcsb.mapviewer.model.map.species.Complex;
import lcsb.mapviewer.model.map.species.Species;
import lcsb.mapviewer.persist.ObjectValidator;
import lcsb.mapviewer.persist.PersistTestFunctions;
public class ProjectDaoTest extends PersistTestFunctions {
......@@ -269,4 +270,11 @@ public class ProjectDaoTest extends PersistTestFunctions {
long id2 = projectDao.getNextId();
assertNotEquals(id, id2);
}
@Test
public void validateHibernateObject() {
Project project = projectDao.getProjectByProjectId("empty");
assertEquals(0, new ObjectValidator().getValidationIssues(project).size());
}
}
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