fr.mael.microrss.util.DozerConverter.java Source code

Java tutorial

Introduction

Here is the source code for fr.mael.microrss.util.DozerConverter.java

Source

/*
   Copyright  2013 Mael Le Guvel
   This work is free. You can redistribute it and/or modify it under the
   terms of the Do What The Fuck You Want To Public License, Version 2,
   as published by Sam Hocevar. See the COPYING file for more details.
*/
package fr.mael.microrss.util;

import java.util.Collections;
import java.util.Set;

import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;

public class DozerConverter implements GenericConverter, InitializingBean, BeanFactoryAware {

    /** Mapper Dozer */
    private Mapper mapper;

    private BeanFactory beanFactory;

    /**
     * Each object can be converted
     */
    public Set<ConvertiblePair> getConvertibleTypes() {
        return Collections.singleton(new ConvertiblePair(Object.class, Object.class));
    }

    /**
     * {@inheritDoc}
     */
    public Object convert(Object aSource, TypeDescriptor aSourceType, TypeDescriptor aTargetType) {
        Object o = mapper.map(aSource, aTargetType.getType());
        //      if (o instanceof WebFeed && aSource instanceof Feed) {
        //         WebFeed webFeed = (WebFeed) o;
        //         webFeed.setTitle(beanFactory.getBean(FeedService.class).getFeedTitle(webFeed.getId()));
        //      }
        return o;
    }

    /**
     */
    public void afterPropertiesSet() {
        if (mapper == null) {
            mapper = new DozerBeanMapper();
        }
    }

    public void setMapper(final Mapper aMapper) {
        mapper = aMapper;
    }

    public Mapper getMapper() {
        return mapper;
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
    }

}