Dozer : String-To-Date Field Level Mapping for a List
Asked Answered
C

1

0

I want to map a DTO (all are of String Data Types) to VO (contains String,int,boolean,Date)

StudentDTO

private StudentDetailDTO student;

StudentDetailDTO :

private String sid;
private String name;
private String createDt;
private String studentInd;
private List<FeeReceiptDTO> feeDetails;

FeeReceiptDTO:

private String semisterNum;
private String feeAmount;
private String paidOn;

StudentDetailVO :

private int sid;
private String name;
private Date createDt;
private boolean studentInd;
private List<FeeReceiptVO> feeDetails;

FeeReceiptVO:

private int semisterNum;
private Double feeAmount;
private Date paidOn;

I am using DOZZER to map my DTO to VO String-to-Date-DozzerMapping

DozzerMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://dozer.sourceforge.net http://dozer.sourceforge.net/schema/beanmapping.xsd">

    <mapping date-format="MM/dd/yyyy HH:mm" map-null="true" map-empty-string="true" wildcard="true" type="one-way" >
        <class-a>com.college.student.dto.StudentDTO</class-a>
        <class-b>com.college.student.vo.StudentVO</class-b>
        <field>
            <a>student.sid</a>
            <b>sid</b>
        </field>
        <field>
            <a>student.name</a>
            <b>name</b>
        </field>
        <field>
            <a>student.createDt</a>
            <b>createDt</b>
        </field>
        <field>
            <a>student.studentInd</a>
            <b>studentInd</b>
        </field>
        <field>
            <a date-format="MM/dd/yyyy HH:mm">student.feeDetails</a>
            <b>feeDetails</b>
        </field>

    </mapping>
</mappings>

But, the Date in the feeDetails i.e., paidOn is unable to format from String to Date.

Am i doing something wrong?

Do i need to write a CustomStringToDateConvertor just for this paidOn field?

Error Log :

ERROR [org.dozer.MappingProcessor] (http-localhost-127.0.0.1-9090-1) Field mapping error -->
  MapId: null
  Type: null
  Source parent class: com.college.student.dto.StudentDTO
  Source field name: paidOn
  Source field type: class java.lang.String
  Source field value: 01/01/2015 01:01
  Dest parent class: com.college.student.vo.StudentVO
  Dest field name: paidOn
  Dest field type: java.util.Date: org.dozer.converters.ConversionException: Unable to determine time in millis of source object
    at org.dozer.converters.DateConverter.convert(DateConverter.java:81) [dozer-5.4.0.jar:]
    at org.dozer.converters.PrimitiveOrWrapperConverter.convert(PrimitiveOrWrapperConverter.java:70) [dozer-5.4.0.jar:]

I tried to Debug and i found that dateFormat for this date is coming as null in the Date convertor of Dozzer org.dozer.converters.DateConvertor

I defined the date-format at field-level but it is not effected for List of Objects.

Is there any other way to do this?

Casiano answered 9/1, 2015 at 16:9 Comment(1)
Is this field level declaration correct <field> <a date-format="MM/dd/yyyy HH:mm">student.feeDetails</a> <b>feeDetails</b> </field> - will dozer take this date-format, both of them are util.ListCasiano
C
1

As,Dozer is not able to convert String-To-Date - At Field Level Mapping for a List.

I had defined a new mapping for the List i.e., considering it as a Object

I Changed the DozerMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://dozer.sourceforge.net http://dozer.sourceforge.net/schema/beanmapping.xsd">

<mapping date-format="MM/dd/yyyy HH:mm" map-null="true" map-empty-string="true" wildcard="true" type="one-way" >
        <class-a>com.college.student.dto.FeeReceiptDTO</class-a>
        <class-b>com.college.student.vo.FeeReceiptVO</class-b>
</mapping>

    <mapping date-format="MM/dd/yyyy HH:mm" map-null="true" map-empty-string="true" wildcard="true" type="one-way" >
        <class-a>com.college.student.dto.StudentDTO</class-a>
        <class-b>com.college.student.vo.StudentVO</class-b>
        <field>
            <a>student.sid</a>
            <b>sid</b>
        </field>
        <field>
            <a>student.name</a>
            <b>name</b>
        </field>
        <field>
            <a>student.createDt</a>
            <b>createDt</b>
        </field>
        <field>
            <a>student.studentInd</a>
            <b>studentInd</b>
        </field>
        <field>
            <a>student.feeDetails</a>
            <b>feeDetails</b>
        </field>

    </mapping>
</mappings>
Casiano answered 14/1, 2015 at 11:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.