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

info.archinnov.achilles.entity.ThriftEntityMapper Maven / Gradle / Ivy

package info.archinnov.achilles.entity;

import static info.archinnov.achilles.serializer.ThriftSerializerUtils.STRING_SRZ;
import info.archinnov.achilles.entity.metadata.EntityMeta;
import info.archinnov.achilles.entity.metadata.PropertyMeta;
import info.archinnov.achilles.helper.EntityMapper;
import org.apache.cassandra.utils.Pair;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import me.prettyprint.hector.api.beans.Composite;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * ThriftEntityMapper
 * 
 * @author DuyHai DOAN
 * 
 */
public class ThriftEntityMapper extends EntityMapper
{
	private static final Logger log = LoggerFactory.getLogger(ThriftEntityMapper.class);

	public void setEagerPropertiesToEntity(Object key, List> columns,
			EntityMeta entityMeta,
			Object entity)
	{
		log.trace("Set eager properties to entity {} ", entityMeta.getClassName());

		Map> listProperties = new HashMap>();
		Map> setProperties = new HashMap>();
		Map> mapProperties = new HashMap>();

		setIdToEntity(key, entityMeta.getIdMeta(), entity);

		Map> propertyMetas = entityMeta.getPropertyMetas();

		for (Pair pair : columns)
		{
			String propertyName = pair.left.get(1, STRING_SRZ);

			PropertyMeta propertyMeta = propertyMetas.get(propertyName);

			if (propertyMeta != null)
			{

				switch (propertyMeta.type())
				{
					case SIMPLE:
						setSimplePropertyToEntity(pair.right, propertyMeta, entity);
						break;
					case LIST:
						addToList(listProperties, propertyMeta,
								propertyMeta.getValueFromString(pair.right));
						break;
					case SET:
						addToSet(setProperties, propertyMeta,
								propertyMeta.getValueFromString(pair.right));
						break;
					case MAP:
						addToMap(mapProperties, propertyMeta,
								propertyMeta.getKeyValueFromString(pair.right));
						break;
					default:
						log.debug("Property {} is lazy or of proxy type, do not set to entity now");
						break;
				}
			}
			else
			{
				log.warn("No field mapping for property {}", propertyName);
			}
		}

		setMultiValuesProperties(entity, listProperties, setProperties, mapProperties,
				propertyMetas);
	}

	public  T initClusteredEntity(Class entityClass, PropertyMeta idMeta,
			Object embeddedId)
	{
		T clusteredEntity = null;
		clusteredEntity = invoker.instanciate(entityClass);
		invoker.setValueToField(clusteredEntity, idMeta.getSetter(), embeddedId);
		return clusteredEntity;
	}

	public  T createClusteredEntityWithValue(Class entityClass, PropertyMeta idMeta,
			PropertyMeta pm,
			Object embeddedId, Object clusteredValue)
	{
		T clusteredEntity = null;
		clusteredEntity = invoker.instanciate(entityClass);
		invoker.setValueToField(clusteredEntity, idMeta.getSetter(), embeddedId);
		invoker.setValueToField(clusteredEntity, pm.getSetter(), clusteredValue);

		return clusteredEntity;
	}

	private void setMultiValuesProperties(Object entity, Map> listProperties,
			Map> setProperties, Map> mapProperties,
			Map> propertyMetas)
	{
		for (Entry> entry : listProperties.entrySet())
		{
			setListPropertyToEntity(entry.getValue(), propertyMetas.get(entry.getKey()), entity);
		}

		for (Entry> entry : setProperties.entrySet())
		{
			setSetPropertyToEntity(entry.getValue(), propertyMetas.get(entry.getKey()), entity);
		}

		for (Entry> entry : mapProperties.entrySet())
		{
			setMapPropertyToEntity(entry.getValue(), propertyMetas.get(entry.getKey()), entity);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy