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

info.archinnov.achilles.composite.ThriftCompositeTransformer Maven / Gradle / Ivy

The newest version!
/**
 *
 * Copyright (C) 2012-2013 DuyHai DOAN
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package info.archinnov.achilles.composite;

import info.archinnov.achilles.compound.ThriftCompoundKeyMapper;
import info.archinnov.achilles.context.ThriftPersistenceContext;
import info.archinnov.achilles.entity.ThriftEntityMapper;
import info.archinnov.achilles.entity.metadata.PropertyMeta;

import java.util.List;

import me.prettyprint.hector.api.beans.AbstractComposite.Component;
import me.prettyprint.hector.api.beans.Composite;
import me.prettyprint.hector.api.beans.HColumn;
import me.prettyprint.hector.api.beans.HCounterColumn;

import com.google.common.base.Function;

public class ThriftCompositeTransformer {

	private ThriftCompoundKeyMapper compoundKeyMapper = new ThriftCompoundKeyMapper();
	private ThriftEntityMapper mapper = new ThriftEntityMapper();

	public Function, ?> buildRawValueTransformer() {
		return new Function, Object>() {
			@Override
			public Object apply(HColumn hColumn) {
				return hColumn.getValue();
			}
		};
	}

	// //////////////////// Clustered Entities

	public  Function, T> clusteredEntityTransformer(final Class entityClass,
			final ThriftPersistenceContext context) {
		return new Function, T>() {
			@Override
			public T apply(HColumn hColumn) {
				return buildClusteredEntity(entityClass, context, hColumn);
			}
		};
	}

	public  Function, T> valuelessClusteredEntityTransformer(final Class entityClass,
			final ThriftPersistenceContext context) {
		return new Function, T>() {
			@Override
			public T apply(HColumn hColumn) {
				return buildClusteredEntityWithIdOnly(entityClass, context, hColumn.getName().getComponents());
			}
		};
	}

	public  Function, T> counterClusteredEntityTransformer(final Class entityClass,
			final ThriftPersistenceContext context) {
		return new Function, T>() {
			@Override
			public T apply(HCounterColumn hColumn) {
				return buildClusteredEntityWithIdOnly(entityClass, context, hColumn.getName().getComponents());
			}
		};
	}

	public  T buildClusteredEntity(Class entityClass, ThriftPersistenceContext context,
			HColumn hColumn) {
		PropertyMeta pm = context.getFirstMeta();
		Object embeddedId = buildEmbeddedIdFromComponents(context, hColumn.getName().getComponents());
		Object value = pm.decode(hColumn.getValue());
		return mapper.createClusteredEntityWithValue(entityClass, context.getEntityMeta(), pm, embeddedId, value);
	}

	public  T buildClusteredEntityWithIdOnly(Class entityClass, ThriftPersistenceContext context,
			List> components) {
		Object embeddedId = buildEmbeddedIdFromComponents(context, components);
		return mapper.initClusteredEntity(entityClass, context.getEntityMeta(), embeddedId);
	}

	private Object buildEmbeddedIdFromComponents(ThriftPersistenceContext context, List> components) {
		Object primaryKey = context.getPrimaryKey();
		PropertyMeta idMeta = context.getIdMeta();
		return compoundKeyMapper.fromCompositeToEmbeddedId(idMeta, components, primaryKey);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy