Skip to content

Commit

Permalink
Implemented CustomFieldValidator to inject a point of prevention befo…
Browse files Browse the repository at this point in the history
…re a source field value is fetched during mapping

A custom field validator is usually in conjunction with a
CustomFieldMapper when implementing the CustomFieldMapperAndValidator
interface.
Use this when you want logic to prevent source field values being
fetched.
e.g. when checking lazy loaded relationships in an ORM

If a custom field validator is specified, Dozer will invoke this class
when performing all field mappings. If false is returned from the call
the source field value will never be fetched, and subsequently the field
will never be mapped.
  • Loading branch information
gilbertg authored and Gilbert committed Jun 23, 2017
1 parent 1d5ddeb commit e535798
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 15 deletions.
@@ -0,0 +1,9 @@
package org.dozer;

/**
* Public custom field validator and mapper interface.
* Implement this interface on your custom field mapper if you want to implement both CustomFieldMapper & CustomFieldValidator functions
*
* @author Gilbert Grant
*/
public interface CustomFieldMapperAndValidator extends CustomFieldMapper, CustomFieldValidator {}
20 changes: 20 additions & 0 deletions core/src/main/java/org/dozer/CustomFieldValidator.java
@@ -0,0 +1,20 @@
package org.dozer;

import org.dozer.classmap.ClassMap;
import org.dozer.fieldmap.FieldMap;

/**
* Public custom field validator interface. A custom field validator is usually in conjunction with a CustomFieldMapper
* when implementing the CustomFieldMapperAndValidator interface.
* Use this when you want logic to prevent source field values being fetched
* e.g. when checking lazy loaded relationships in an ORM
*
* <p>
* If a custom field validator is specified, Dozer will invoke this class when performing all field mappings. If false is
* returned from the call the source field value will never be fetched, and subsequently the field will never be mapped.
*
* @author Gilbert Grant
*/
public interface CustomFieldValidator {
boolean canMapField(Object source, Object destination, ClassMap classMap, FieldMap fieldMapping);
}
39 changes: 24 additions & 15 deletions core/src/main/java/org/dozer/MappingProcessor.java
Expand Up @@ -336,22 +336,31 @@ private void mapField(FieldMap fieldMapping, Object srcObj, Object destObj) {
// custom field mapper returns false(indicating the
// field was not actually mapped by the custom field mapper), proceed as
// normal(use Dozer to map the field)
srcFieldValue = fieldMapping.getSrcFieldValue(srcObj);
boolean fieldMapped = false;
if (customFieldMapper != null) {
fieldMapped = customFieldMapper.mapField(srcObj, destObj, srcFieldValue, fieldMapping.getClassMap(), fieldMapping);
}

if (!fieldMapped) {
if (fieldMapping.getDestFieldType() != null && ITERATE.equals(fieldMapping.getDestFieldType())) {
// special logic for iterate feature
mapFromIterateMethodFieldMap(srcObj, destObj, srcFieldValue, fieldMapping);
} else {
// either deep field map or generic map. The is the most likely
// scenario
mapFromFieldMap(srcObj, destObj, srcFieldValue, fieldMapping);
}
}
boolean canMapField = true;
if (customFieldMapper != null && customFieldMapper instanceof CustomFieldMapperAndValidator) {
canMapField = ((CustomFieldMapperAndValidator)customFieldMapper).canMapField(srcObj, destObj, fieldMapping.getClassMap(), fieldMapping);
}

if (canMapField) {
boolean fieldMapped = false;

srcFieldValue = fieldMapping.getSrcFieldValue(srcObj);
if (customFieldMapper != null) {
fieldMapped = customFieldMapper.mapField(srcObj, destObj, srcFieldValue, fieldMapping.getClassMap(), fieldMapping);
}

if (!fieldMapped) {
if (fieldMapping.getDestFieldType() != null && ITERATE.equals(fieldMapping.getDestFieldType())) {
// special logic for iterate feature
mapFromIterateMethodFieldMap(srcObj, destObj, srcFieldValue, fieldMapping);
} else {
// either deep field map or generic map. The is the most likely
// scenario
mapFromFieldMap(srcObj, destObj, srcFieldValue, fieldMapping);
}
}
}

statsMgr.increment(StatisticType.FIELD_MAPPING_SUCCESS_COUNT);

Expand Down

0 comments on commit e535798

Please sign in to comment.