Wednesday 2 March 2022

Dozer: Specify custom type converter using xml file

In this post, I am going to explain how to specify custom converter in an xml file.

 

Step 1: Define custom converter by implementing CustomConverter interface.

public class AppLogToTransactionLogConverter implements CustomConverter {

	@Override
	public Object convert(Object destination, Object source, Class<?> destinationClass, Class<?> sourceClass) {

		if (source instanceof AppLog) {
			AppLog appLog = (AppLog) source;

			return new TransactionLog(appLog.getLogMessage(),
					Date.from(Instant.ofEpochMilli(appLog.getCreatedTimeInMillis())));

		}

		else if (source instanceof TransactionLog) {
			TransactionLog transactionLog = (TransactionLog) source;
			return new AppLog(transactionLog.getMessage(), transactionLog.getCreatedDate().getTime());
		}
		
		throw new IllegalArgumentException("Invalid types");
	}

}

 

Step 2: Specify the converter in xml file.

<configuration>
	<custom-converters>
		<converter
			type="com.sample.app.converter.AppLogToTransactionLogConverter">
			<class-a>com.sample.app.model.TransactionLog</class-a>
			<class-b>com.sample.app.model.AppLog</class-b>
		</converter>
	</custom-converters>
</configuration>

  In the above snippet, I am defining AppLogToTransactionLogConverter can be used to convert TransactionLog to AppLog instances and vice versa.

 


Find the below working application.

 

Step 1: Define AppLog and TransactionLog classes.

 

AppLog.java

package com.sample.app.model;

public class AppLog {

	private String logMessage;
	private long createdTimeInMillis;

	public AppLog() {
	}

	public AppLog(String logMessage, long createdTimeInMillis) {
		this.logMessage = logMessage;
		this.createdTimeInMillis = createdTimeInMillis;
	}

	public String getLogMessage() {
		return logMessage;
	}

	public void setLogMessage(String logMessage) {
		this.logMessage = logMessage;
	}

	public long getCreatedTimeInMillis() {
		return createdTimeInMillis;
	}

	public void setCreatedTimeInMillis(long createdTimeInMillis) {
		this.createdTimeInMillis = createdTimeInMillis;
	}

	@Override
	public String toString() {
		return "AppLog [logMessage=" + logMessage + ", createdTimeInMillis=" + createdTimeInMillis + "]";
	}

}

 

TransactionLog.java

package com.sample.app.model;

import java.util.Date;

public class TransactionLog {
	private String message;
	private Date createdDate;

	public TransactionLog() {
	}

	public TransactionLog(String message, Date createdDate) {
		this.message = message;
		this.createdDate = createdDate;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Date getCreatedDate() {
		return createdDate;
	}

	public void setCreatedDate(Date createdDate) {
		this.createdDate = createdDate;
	}

	@Override
	public String toString() {
		return "TransactionLog [message=" + message + ", createdDate=" + createdDate + "]";
	}

}

 

Step 2: Define custom converter.

 

AppLogToTransactionLogConverter.java

 

package com.sample.app.converter;

import java.time.Instant;
import java.util.Date;

import org.dozer.CustomConverter;

import com.sample.app.model.AppLog;
import com.sample.app.model.TransactionLog;

public class AppLogToTransactionLogConverter implements CustomConverter {

	@Override
	public Object convert(Object destination, Object source, Class<?> destinationClass, Class<?> sourceClass) {

		if (source instanceof AppLog) {
			AppLog appLog = (AppLog) source;

			return new TransactionLog(appLog.getLogMessage(),
					Date.from(Instant.ofEpochMilli(appLog.getCreatedTimeInMillis())));

		}

		else if (source instanceof TransactionLog) {
			TransactionLog transactionLog = (TransactionLog) source;
			return new AppLog(transactionLog.getMessage(), transactionLog.getCreatedDate().getTime());
		}
		
		throw new IllegalArgumentException("Invalid types");
	}

}

 

Step 3: Specify converter details in the configuration file.

 

appLog-to-transactionLog-mapping.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">

	<configuration>
		<custom-converters>
			<converter
				type="com.sample.app.converter.AppLogToTransactionLogConverter">
				<class-a>com.sample.app.model.TransactionLog</class-a>
				<class-b>com.sample.app.model.AppLog</class-b>
			</converter>
		</custom-converters>
	</configuration>

</mappings>

 

Step 4: Define TypeConverterDemo class.

 

TypeConverterDemo.java

package com.sample.app;

import java.io.IOException;
import java.util.Arrays;
import java.util.Date;

import org.dozer.DozerBeanMapper;

import com.sample.app.model.AppLog;
import com.sample.app.model.TransactionLog;

public class TypeConverterDemo {

	public static void main(String[] args) throws IOException {
		DozerBeanMapper mapper = new DozerBeanMapper();
		mapper.setMappingFiles(Arrays.asList("appLog-to-transactionLog-mapping.xml"));
		
		TransactionLog transactionLog1 = new TransactionLog("Unable to connect to database", new Date());
		AppLog appLog = mapper.map(transactionLog1, AppLog.class);
		TransactionLog transactionLog2 = mapper.map(appLog, TransactionLog.class);

		System.out.println(transactionLog1);
		System.out.println(appLog);
		System.out.println(transactionLog2);
	}
}

 

Output

TransactionLog [message=Unable to connect to database, createdDate=Sun Feb 06 12:30:40 IST 2022]
AppLog [logMessage=Unable to connect to database, createdTimeInMillis=1644130840006]
TransactionLog [message=Unable to connect to database, createdDate=Sun Feb 06 12:30:40 IST 2022]

 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment