Dozer copy Map to another Map

1.9k views Asked by At

I´ve been triyng to copy a Map(String,ProposalViewBean) to another Map(String,Proposal)

I created a custom converter:

public class ProposalsDozerConverter  extends DozerConverter <HashMap<String,Proposal>, HashMap<String,ProposalViewBean>> {

    @Resource(name="mapper")
    private Mapper mapper;


    public ProposalsDozerConverter(Class<HashMap<String, Proposal>> prototypeA,
                                                      Class<HashMap<String, ProposalViewBean>> prototypeB) {

        super(prototypeA, prototypeB);

    }

    @Override
    public HashMap<String, Proposal> convertFrom( HashMap<String, ProposalViewBean> proposalsViewBean, HashMap<String, Proposal> proposals) {

        System.out.println("convertFrom");

        /*if (proposals == null){
            proposals = new TreeHashMap<String, Proposal>();
        }*/

        for (Map.Entry<String, ProposalViewBean> entry : proposalsViewBean.entrySet()){
            Proposal p = mapper.map(entry.getValue(), Proposal.class);
            proposals.put(entry.getKey(), p);
        }

        return proposals;
    }

    @Override
    public HashMap<String, ProposalViewBean> convertTo(HashMap<String, Proposal> proposals, HashMap<String, ProposalViewBean> proposalsViewBean) {

        System.out.println("convertTo");

        /*if (proposalsViewBean == null){
            proposalsViewBean = new TreeMap<String, ProposalViewBean>();
        }*/

        for (Map.Entry<String, Proposal> entry : proposals.entrySet()){
            ProposalViewBean p = mapper.map(entry.getValue(), ProposalViewBean.class);
            proposalsViewBean.put(entry.getKey(), p);
        }

        return proposalsViewBean;
    }

Then I configured in XML:

<configuration>
    <custom-converters>
        <converter type="com.hsbc.hbbr.frb.converters.ProposalsDozerConverter">
            <class-a>java.util.HashMap</class-a>
            <class-b>java.util.HashMap</class-b>
        </converter>
    </custom-converters>
</configuration>

<mapping>
    <class-a>com.hsbc.hbbr.frb.viewbean.ConsumerLoanViewBean</class-a>
    <class-b>com.hsbc.hbbr.frb.model.ConsumerLoan</class-b>
    <field custom-converter="com.hsbc.hbbr.frb.converters.ProposalsDozerConverter">
        <a>proposals</a>
        <b>proposals</b>
        <a-hint>com.hsbc.hbbr.frb.viewbean.ProposalViewBean</a-hint>
        <b-hint>com.hsbc.hbbr.frb.model.Proposal</b-hint>
    </field>
</mapping>

When I tried to convert I catch this error:

Caused by: java.lang.InstantiationException: com.hsbc.hbbr.frb.converters.ProposalsDozerConverter
at java.lang.Class.newInstance(Class.java:359)

I have no idea to fix that. Has anyone faced this issue?

1

There are 1 answers

0
Raphael Milani On BEST ANSWER

I found a solution of my issue. There were some mistakes on my code and XML. First I needed to change the mapping:

<mapping>
        <class-a>com.hsbc.hbbr.frb.viewbean.ConsumerLoanViewBean</class-a>
        <class-b>com.hsbc.hbbr.frb.model.ConsumerLoan</class-b>
        <field custom-converter="com.hsbc.hbbr.frb.converters.ProposalsDozerConverter">
            <a>proposals</a>
            <b>proposals</b>
            <a-hint>java.util.HashMap</a-hint>
            <b-hint>java.util.HashMap</b-hint>
        </field>
    </mapping>

My intention was copy Map to another Map not copy the Object inside the Map.

After I checked my Converter, then I realized the spring was not sent an instance of Mapper:

@Resource(name="mapper")
private Mapper mapper;

That was the reason of the Exception. To get an instance of Mapper I needed to implement the Interface MapperAware.Implement this Interface Dozer injects an instance of Mapper. I had to change the constructor of class:

public ProposalsDozerConverter() {
        super( ((Class<HashMap<String,Proposal>>) (Class<?>) HashMap.class), (Class<HashMap<String,ProposalViewBean>>) (Class<?>) HashMap.class  );
    } 

Because the raw type will erase in run time. So Dozer does not know what kind of object is inside of Map. My final code is:

public class ProposalsDozerConverter  extends DozerConverter <HashMap<String,Proposal>, HashMap<String,ProposalViewBean>>
                                                                                                implements MapperAware {
    private Mapper mapper;

    public ProposalsDozerConverter() {
        super( ((Class<HashMap<String,Proposal>>) (Class<?>) HashMap.class), (Class<HashMap<String,ProposalViewBean>>) (Class<?>) HashMap.class  );
    }

    @Override
    public HashMap<String, Proposal> convertFrom( HashMap<String, ProposalViewBean> proposalsViewBean, HashMap<String, Proposal> proposals) {

        for (Map.Entry<String, ProposalViewBean> entry : proposalsViewBean.entrySet()){
            Proposal p = mapper.map(entry.getValue(), Proposal.class);
            proposals.put(entry.getKey(), p);
        }

        return proposals;
    }

    @Override
    public HashMap<String, ProposalViewBean> convertTo(HashMap<String, Proposal> proposals, HashMap<String, ProposalViewBean> proposalsViewBean) {

        for (Map.Entry<String, Proposal> entry : proposals.entrySet()){
            ProposalViewBean p = mapper.map(entry.getValue(), ProposalViewBean.class);
            proposalsViewBean.put(entry.getKey(), p);
        }

        return proposalsViewBean;
    }

    @Override
    public void setMapper(Mapper mapper ) {
        this.mapper = mapper;
    }
}