3

Hi I try to map following Source class in to following Destination class. I used following mapping in order to map string values in to the list string. It isn't mapping properly. I need to know how to map 2 string values into one destination string list using Dozer.

public class SourceClass {

  protected String streetName;
  protected String additionalStreetName;

}

public class Destination {

protected List<String> addressLine;

}

<mapping map-id="newId" >
<class-a>myPackage.SourceClass </class-a>
<class-b>myPackage.Destination</class-b> 

  <field>
    <a>streetName</a>
    <b>addressLine[0]</b>
  </field>
   <field>
    <a>additionalStreetName</a>
    <b>addressLine[1]</b>
  </field> 
</mapping> 
Miraj Hamid
  • 554
  • 10
  • 19

4 Answers4

3

Just specify type of objects in the destination list by Hint tag to let Dozer know what type of objects you want created in the destination list:

<field>
    <a>streetName</a>
    <b>addressLine[0]</b>
    <b-hint>java.lang.String</b-hint>
</field>
<field>
    <a>additionalStreetName</a>
    <b>addressLine[1]</b>
    <b-hint>java.lang.String</b-hint>
</field>

No custom converters are required.

A.Panzer
  • 391
  • 3
  • 15
2

In order to do this you'll need to use a custom converter.

The documentation will give you a more thorough understanding but essentially, at the moment dozer has no idea how to convert a string into a list, so you have to tell it.

Your custom converter will take a String value as source and have a List as destination and will add the string it received into the list.

Something along the lines of this:

public class TestCustomConverter extends DozerConverter {

    public NewDozerConverter() {
        super(String.class, List.class);
    }

    public List<String> convertTo(String source, List<String> destination) {
        if (source == null) {
            return new ArrayList<>();
        }
        if (destination == null) {
            destination = new ArrayList<>();
        }

        destination.add(source);

        return destination;
    }

    public String convertFrom(List<String> source, String destination {
        return null;
    }
}

Your mappings will then look something like this:

<mapping map-id="newId" >
  <class-a>myPackage.SourceClass </class-a>
  <class-b>myPackage.Destination</class-b> 

  <field custom-converter="TestCustomConverter">
    <a>streetName</a>
    <b>addressLine</b>
  </field>
  <field custom-converter="TestCustomConverter">
    <a>additionalStreetName</a>
    <b>addressLine</b>
  </field> 
</mapping> 
Kialandei
  • 394
  • 1
  • 11
  • Where should I save " TestCustomConverter" class? under myPackage folder or Under resources folder where "dozerMapper.xml" in? – Miraj Hamid Sep 17 '15 at 04:45
  • Its a class, so wherever you want to put it in your sources folder. You'll want to give the fully qualified name to the dozerMapper.xml though, so if you put it in myPackage it would be ``. – Kialandei Sep 17 '15 at 08:00
1

You'll need to create a custom converter class in Java.

Reference Dozer Custom Converters, go to the heading New Custom Converter API.

your converter class will need to extend DozerConverter like so:

SourceClassToDestinationConverter extends DozerConverter <SourceClass, Destination> implements MapperAware

Then you will have to define the converter to be used in place of the mapper in your custom-converters configuration as follows:

<configuration>
    <custom-converters>
        <converter
            type="some.package.converter.SourceClassToDestinationConverter ">
            <class-a>myPackage.SourceClass</class-a>
            <class-b>myPackage.Destination</class-b>
        </converter>
        ...
    </custom-converters>
</configuration>
Blake Yarbrough
  • 2,286
  • 1
  • 20
  • 36
1

This is the answer that I am come up with..

import java.util.ArrayList;
import java.util.List;

import org.dozer.DozerConverter;
import org.dozer.Mapper;
import org.dozer.MapperAware;

public class TestConverter extends DozerConverter<String, List>
    implements MapperAware {

public TestConverter() {
    super(String.class, List.class);
    // TODO Auto-generated constructor stub
}


@Override
public void setMapper(Mapper mapper) {
    // TODO Auto-generated method stub

}

@Override
public List convertTo(String source, List destination) {
    if (source == null) {
         return new ArrayList<String>();
    }
    if (destination == null) {
         destination = new ArrayList<String>();
    }

    //destination.getAddressLine().add(source.getAdditionalStreetName());
    destination.add(source);
    return destination;
}



@Override
public String convertFrom(List source, String destination) {
    // TODO Auto-generated method stub
    return null;
}

}

===============In dozerMapper.xml under configuration===========================

   <custom-converters>
    <converter 
        type="company.realMap.TestConverter">
        <class-a>desAdv.SourceClass</class-a>
        <class-b>abcReq.Destination</class-b>
    </converter> 

===============In dozerMapper.xml under mapping==========================

  <field custom-converter="myPackage.TestConverter">
    <a>streetName</a>  
    <b>addressLine</b>
    </field>
  <field custom-converter="myPackage.TestConverter">
    <a>additionalStreetName</a>  
    <b>addressLine</b>      
Miraj Hamid
  • 554
  • 10
  • 19