How to extend mapper - 4.4

Writing your own processor

Writing processor is as easy as implementing com.cognifide.slice.mapper.api.processor.FieldProcessor interface.

Below is an example of FieldProcessor that sets custom text to model field if there is no resource or the property we are mapping from is not set.

DefaultStringValueFieldProcessor.java
 public class DefaultStringValueFieldProcessor implements FieldProcessor {

   private static final String DEFAULT_STRING_VALUE = "NOT_EXISTING";

   @Override
   public boolean accepts(Resource resource, Field field) {
      return String.class.isAssignableFrom(field.getType());
   }

   @Override
   public Object mapResourceToField(Resource resource, ValueMap valueMap, Field field, String propertyName) {
      String result = DEFAULT_STRING_VALUE;
      if (valueMap != null) {
         String propertyValue = valueMap.get(propertyName, String.class);
         if (propertyValue != null) {
            result = propertyValue;
         }
      }
      return result;
   }
}

There are two methods to be implemented.

  • accepts - this method decides if the field can be mapped by this processor
  • mapResourceToField - this method returns a value which is set to specified field.

Registering your processor

 To register custom FieldProcessor insert following code in your implementation of com.google.inject.Module.

CustomProcessorModule.java
public class CustomProcessorModule implements Module {

   @Override
   protected void configure() {
      Multibinder<FieldProcessor> multibinder = Multibinder.newSetBinder(binder(), FieldProcessor.class);
      multibinder.addBinding().to(DefaultStringValueFieldProcessor.class);
   }
 
}

All registered processors are added at the beginning of the processors list. There is also a way to ensure the order of custom processors. In order to do so, use  com.cognifide.slice.mapper.api.processor.PriorityFieldProcessor interface when writing custom processor and implement getPriority method. Processors with higher priority are placed at the beginning of processors list.

All custom processors registered with Mulitbinder always take precedence over the standard ones that come with Slice.

Writing your own post-processor

Writing post-processor is essentially the same as writing processor. Instead of implementing com.cognifide.slice.mapper.api.processor.FieldProcessor you should implement com.cognifide.slice.mapper.api.processor.FieldPostProcessor.

Below you will find example of FieldPostPorcessor that reverses the text assigned to model's field.

ReverseTextFieldPostProcessor.java
public class ReverseTextFieldPostProcessor implements FieldPostProcessor {

   @Override
   public boolean accepts(Resource resource, Field field, Object value) {
      return value instanceof String;
   }

   @Override
   public Object processValue(Resource resource, Field field, Object value) {
      return StringUtils.reverse((String) value);
   }
}

Registering your post-processor

To register custom FieldPostProcessor insert following code in your implementation of com.google.inject.Module.

CustomPostProcessorModule.java
public class CustomPostProcessorModule implements Module {

   @Override
   protected void configure() {
      Multibinder<FieldPostProcessor> multibinder = Multibinder.newSetBinder(binder(), FieldPostProcessor.class);
      multibinder.addBinding().to(ReverseTextFieldProcessor.class);
   }
 
}

All registered post-processors will be added at the beginning of post-processors list. There is also a way to ensure the order of custom post-processors.  In order to do so, use  com.cognifide.slice.mapper.api.processor.PriorityFieldPostProcessor interface when writing custom post-processor and implement getPriority method.

 

Unlike PriorityFieldProcessor , the order of post-processors with regard to the ones that come with Slice differs and depends of priority value.

Custom post-processors with priority value greater or equal to 0 are placed on the list before built-in post-processors.

Custom post-processors with priority value lower than 0 are placed on the list after built-in post-processors.