Skip to content

Commit

Permalink
refactor(logging): improve ddi mapper debug log
Browse files Browse the repository at this point in the history
  • Loading branch information
nsenave committed Jul 18, 2024
1 parent e952748 commit d71fd20
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
3 changes: 3 additions & 0 deletions eno-core/src/main/java/fr/insee/eno/core/annotations/DDI.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@
* The mapper will not throw an exception if the property is set to true. */
boolean allowNullList() default true;

/** To activate or not debug logging for this annotation. */
boolean debug() default false;

}
46 changes: 25 additions & 21 deletions eno-core/src/main/java/fr/insee/eno/core/mappers/DDIMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,6 @@ public void mapDDIObject(AbstractIdentifiableType ddiObject, EnoObject enoObject

private void recursiveMapping(Object ddiObject, EnoObject enoObject) {

log.debug("Start mapping for "+ DDIUtils.ddiToString(ddiObject)
+" with model context type '"+enoObject.getClass().getSimpleName()+"'");

// Use Spring BeanWrapper to iterate on property descriptors of the model object
BeanWrapper beanWrapper = new BeanWrapperImpl(enoObject);
for (Iterator<PropertyDescriptor> iterator = propertyDescriptorIterator(beanWrapper); iterator.hasNext();) {
Expand Down Expand Up @@ -123,24 +120,26 @@ public void propertyMapping(Object ddiObject, EnoObject enoObject, BeanWrapper b
DDI ddiAnnotation = typeDescriptor.getAnnotation(DDI.class);
if (ddiAnnotation != null) {

log.debug("Processing property '"+ propertyName +"' of class '"+ modelContextType.getSimpleName()+"' ");
boolean debug = ddiAnnotation.debug();
if (debug)
log.debug("Processing property '"+ propertyName +"' of class '"+ modelContextType.getSimpleName()+"' ");

// Instantiate a Spring expression with the annotation content
Expression expression = new SpelExpressionParser().parseExpression(ddiAnnotation.value());

// Simple types
if (isSimpleType(classType)) {
simpleTypeMapping(ddiObject, modelContextType, beanWrapper, propertyName, expression);
simpleTypeMapping(ddiObject, modelContextType, beanWrapper, propertyName, expression, debug);
}

// Complex types
else if (EnoObject.class.isAssignableFrom(classType)) {
complexTypeMapping(ddiObject, modelContextType, beanWrapper, propertyName, expression, classType);
complexTypeMapping(ddiObject, modelContextType, beanWrapper, propertyName, expression, classType, debug);
}

// Collections
else if (Collection.class.isAssignableFrom(classType)) {
collectionMapping(ddiObject, enoObject, propertyDescriptor, typeDescriptor, ddiAnnotation, expression);
collectionMapping(ddiObject, enoObject, propertyDescriptor, typeDescriptor, ddiAnnotation, expression, debug);
}

else {
Expand All @@ -150,40 +149,43 @@ else if (Collection.class.isAssignableFrom(classType)) {
}
}

private void simpleTypeMapping(Object ddiObject, Class<?> modelContextType, BeanWrapper beanWrapper, String propertyName, Expression expression) {
private void simpleTypeMapping(Object ddiObject, Class<?> modelContextType, BeanWrapper beanWrapper, String propertyName, Expression expression, boolean debug) {
// Read DDI value by evaluating mapping expression
Object ddiValue = spelEngine.evaluate(expression, ddiObject, modelContextType, propertyName);
// It is allowed to have null values (a DDI property can be present or not depending on the case)
if (ddiValue == null) {
if (ddiValue == null && debug) {
log.debug("null value got from evaluating DDI annotation expression "
+ propertyDescription(propertyName, modelContextType.getSimpleName()));
}
// Simply set the value in the field
beanWrapper.setPropertyValue(propertyName, ddiValue);
log.debug("Value '"+ beanWrapper.getPropertyValue(propertyName)+"' set "
+ propertyDescription(propertyName, modelContextType.getSimpleName()));
if (debug)
log.debug("Value '"+ beanWrapper.getPropertyValue(propertyName)+"' set "
+ propertyDescription(propertyName, modelContextType.getSimpleName()));
}

private void complexTypeMapping(Object ddiObject, Class<?> modelContextType, BeanWrapper beanWrapper, String propertyName, Expression expression, Class<?> classType) {
private void complexTypeMapping(Object ddiObject, Class<?> modelContextType, BeanWrapper beanWrapper, String propertyName, Expression expression, Class<?> classType, boolean debug) {
// Get the DDI object from annotation expression
Object ddiObject2 = spelEngine.evaluate(expression, ddiObject, modelContextType, propertyName);
// It is now allowed to have a null DDI object on complex type properties
if (ddiObject2 == null) {
log.debug("DDI object mapped by the annotation is null "
+ propertyDescription(propertyName, modelContextType.getName()));
if (debug)
log.debug("DDI object mapped by the annotation is null "
+ propertyDescription(propertyName, modelContextType.getName()));
return;
}
// Instantiate the model target object
EnoObject enoObject2 = convert(ddiObject2, classType);
// Attach it to the current object
beanWrapper.setPropertyValue(propertyName, enoObject2);
log.debug("New instance of '"+enoObject2.getClass().getSimpleName()+"' set "
+ propertyDescription(propertyName, modelContextType.getSimpleName()));
if (debug)
log.debug("New instance of '"+enoObject2.getClass().getSimpleName()+"' set "
+ propertyDescription(propertyName, modelContextType.getSimpleName()));
// Recursive call of the mapper to dive into this object
recursiveMapping(ddiObject2, enoObject2);
}

private void collectionMapping(Object ddiObject, EnoObject enoObject, PropertyDescriptor propertyDescriptor, TypeDescriptor typeDescriptor, DDI ddiAnnotation, Expression expression) {
private void collectionMapping(Object ddiObject, EnoObject enoObject, PropertyDescriptor propertyDescriptor, TypeDescriptor typeDescriptor, DDI ddiAnnotation, Expression expression, boolean debug) {
// Local variables used for logging purposes
Class<?> modelContextType = enoObject.getClass();
String propertyName = propertyDescriptor.getName();
Expand All @@ -210,15 +212,17 @@ private void collectionMapping(Object ddiObject, EnoObject enoObject, PropertyDe
// Collection of simple types
if (isSimpleType(modelTargetType)) {
modelCollection.addAll(ddiCollection);
log.debug(collectionSize+" values set "
+ propertyDescription(propertyName, modelContextType.getSimpleName()));
if (debug)
log.debug(collectionSize+" values set "
+ propertyDescription(propertyName, modelContextType.getSimpleName()));
}
// Collection of complex types
else if (EnoObject.class.isAssignableFrom(modelTargetType)) {
// Iterate on the DDI collection
for (Object ddiObject2 : ddiCollection) {
log.debug("Iterating on "+collectionSize+" DDI objects "
+ propertyDescription(propertyName, modelContextType.getSimpleName()));
if (debug)
log.debug("Iterating on "+collectionSize+" DDI objects "
+ propertyDescription(propertyName, modelContextType.getSimpleName()));
// Instantiate a model object per DDI object and add it in the model collection
EnoObject enoObject2 = convert(ddiObject2, modelTargetType);
// Add the created instance in the model collection
Expand Down

0 comments on commit d71fd20

Please sign in to comment.