All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.foreach.across.modules.entity.converters.EntityConverter Maven / Gradle / Ivy

There is a newer version: 4.5.1
Show newest version
package com.foreach.across.modules.entity.converters;

import com.foreach.across.modules.entity.registry.EntityConfiguration;
import com.foreach.across.modules.entity.registry.EntityModel;
import com.foreach.across.modules.entity.registry.EntityRegistry;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
import org.springframework.core.convert.converter.ConverterRegistry;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.Collections;
import java.util.Set;

/**
 * {@link org.springframework.core.convert.converter.Converter} to convert arbitrary input to an entity
 * that is fully registered with the {@link com.foreach.across.modules.entity.registry.EntityRegistry}.
 */
public class EntityConverter implements
		ConditionalGenericConverter
{
	private final T conversionService;
	private final EntityRegistry entityRegistry;

	public EntityConverter( T conversionService, EntityRegistry entityRegistry ) {
		this.conversionService = conversionService;
		this.entityRegistry = entityRegistry;
	}

	/**
	 * (non-Javadoc)
	 *
	 * @see org.springframework.core.convert.converter.GenericConverter#getConvertibleTypes()
	 */
	@Override
	public Set getConvertibleTypes() {
		return Collections.singleton( new ConvertiblePair( Object.class, Object.class ) );
	}

	/*
	 * (non-Javadoc)
	 * @see org.springframework.core.convert.converter.GenericConverter#convert(java.lang.Object, org.springframework.core.convert.TypeDescriptor, org.springframework.core.convert.TypeDescriptor)
	 */
	@SuppressWarnings("unchecked")
	public Object convert( Object source, TypeDescriptor sourceType, TypeDescriptor targetType ) {
		if ( source == null || !StringUtils.hasText( source.toString() ) ) {
			return null;
		}

		if ( sourceType.equals( targetType ) ) {
			return source;
		}

		EntityModel entityModel = findEntityModel( targetType.getType() );

		if ( entityModel != null ) {
			return entityModel.findOne( conversionService.convert( source, entityModel.getIdType() ) );
		}

		return null;
	}

	/*
	 * (non-Javadoc)
	 * @see org.springframework.core.convert.converter.ConditionalGenericConverter#matches(org.springframework.core.convert.TypeDescriptor, org.springframework.core.convert.TypeDescriptor)
	 */
	public boolean matches( TypeDescriptor sourceType, TypeDescriptor targetType ) {
		if ( sourceType.isAssignableTo( targetType ) ) {
			return false;
		}

		EntityModel entityModel = findEntityModel( targetType.getType() );

		if ( entityModel == null ) {
			return false;
		}

		return sourceType.equals( TypeDescriptor.valueOf( entityModel.getIdType() ) )
				|| conversionService.canConvert( sourceType.getType(), entityModel.getIdType() );
	}

	@SuppressWarnings("unchecked")
	private EntityModel findEntityModel( Class entityType ) {
		EntityConfiguration entityConfiguration = entityRegistry.getEntityConfiguration( entityType );

		if ( entityConfiguration != null ) {
			return (EntityModel) entityConfiguration.getEntityModel();
		}

		return null;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy