DATACASS-11 added Delete.Where to the entityWriter in CassandraConverter
This commit is contained in:
@@ -127,9 +127,7 @@ public abstract class AbstractCassandraConfiguration implements BeanClassLoaderA
|
||||
*/
|
||||
@Bean
|
||||
public CassandraOperations cassandraTemplate() throws Exception {
|
||||
CassandraTemplate template = new CassandraTemplate(keyspace());
|
||||
template.setBeanClassLoader(beanClassLoader);
|
||||
return template;
|
||||
return new CassandraTemplate(keyspace());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -140,9 +138,7 @@ public abstract class AbstractCassandraConfiguration implements BeanClassLoaderA
|
||||
*/
|
||||
@Bean
|
||||
public CassandraAdminOperations cassandraAdminTemplate() throws Exception {
|
||||
CassandraAdminTemplate adminTemplate = new CassandraAdminTemplate(keyspace());
|
||||
adminTemplate.setBeanClassLoader(beanClassLoader);
|
||||
return adminTemplate;
|
||||
return new CassandraAdminTemplate(keyspace());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -157,14 +153,16 @@ public abstract class AbstractCassandraConfiguration implements BeanClassLoaderA
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the {@link CassandraConverter} instance to convert Rows to Objects.
|
||||
* Return the {@link CassandraConverter} instance to convert Rows to Objects, Objects to BuiltStatements
|
||||
*
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
@Bean
|
||||
public CassandraConverter converter() {
|
||||
return new MappingCassandraConverter(mappingContext());
|
||||
MappingCassandraConverter converter = new MappingCassandraConverter(mappingContext());
|
||||
converter.setBeanClassLoader(beanClassLoader);
|
||||
return converter;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -19,14 +19,12 @@ import org.springframework.data.cassandra.mapping.CassandraPersistentEntity;
|
||||
import org.springframework.data.cassandra.mapping.CassandraPersistentProperty;
|
||||
import org.springframework.data.convert.EntityConverter;
|
||||
|
||||
import com.datastax.driver.core.Row;
|
||||
|
||||
/**
|
||||
* Central Cassandra specific converter interface from Object to Row.
|
||||
*
|
||||
* @author Alex Shvid
|
||||
*/
|
||||
public interface CassandraConverter extends
|
||||
EntityConverter<CassandraPersistentEntity<?>, CassandraPersistentProperty, Object, Row> {
|
||||
EntityConverter<CassandraPersistentEntity<?>, CassandraPersistentProperty, Object, Object> {
|
||||
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.data.cassandra.convert;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanClassLoaderAware;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
@@ -34,8 +35,13 @@ import org.springframework.data.mapping.model.PropertyValueProvider;
|
||||
import org.springframework.data.mapping.model.SpELContext;
|
||||
import org.springframework.data.util.ClassTypeInformation;
|
||||
import org.springframework.data.util.TypeInformation;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
import com.datastax.driver.core.Row;
|
||||
import com.datastax.driver.core.querybuilder.Delete.Where;
|
||||
import com.datastax.driver.core.querybuilder.Insert;
|
||||
import com.datastax.driver.core.querybuilder.QueryBuilder;
|
||||
import com.datastax.driver.core.querybuilder.Update;
|
||||
|
||||
/**
|
||||
* {@link CassandraConverter} that uses a {@link MappingContext} to do sophisticated mapping of domain objects to
|
||||
@@ -43,7 +49,8 @@ import com.datastax.driver.core.Row;
|
||||
*
|
||||
* @author Alex Shvid
|
||||
*/
|
||||
public class MappingCassandraConverter extends AbstractCassandraConverter implements ApplicationContextAware {
|
||||
public class MappingCassandraConverter extends AbstractCassandraConverter implements ApplicationContextAware,
|
||||
BeanClassLoaderAware {
|
||||
|
||||
protected static final Logger log = LoggerFactory.getLogger(MappingCassandraConverter.class);
|
||||
|
||||
@@ -52,6 +59,8 @@ public class MappingCassandraConverter extends AbstractCassandraConverter implem
|
||||
private SpELContext spELContext;
|
||||
private boolean useFieldAccessOnly = true;
|
||||
|
||||
private ClassLoader beanClassLoader;
|
||||
|
||||
/**
|
||||
* Creates a new {@link MappingCassandraConverter} given the new {@link MappingContext}.
|
||||
*
|
||||
@@ -65,9 +74,11 @@ public class MappingCassandraConverter extends AbstractCassandraConverter implem
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <R> R read(Class<R> clazz, Row row) {
|
||||
public <R> R readRow(Class<R> clazz, Row row) {
|
||||
|
||||
TypeInformation<? extends R> type = ClassTypeInformation.from(clazz);
|
||||
Class<R> beanClassLoaderClass = transformClassToBeanClassLoaderClass(clazz);
|
||||
|
||||
TypeInformation<? extends R> type = ClassTypeInformation.from(beanClassLoaderClass);
|
||||
// TypeInformation<? extends R> typeToUse = typeMapper.readType(row, type);
|
||||
TypeInformation<? extends R> typeToUse = type;
|
||||
Class<? extends R> rawType = typeToUse.getType();
|
||||
@@ -82,7 +93,7 @@ public class MappingCassandraConverter extends AbstractCassandraConverter implem
|
||||
throw new MappingException("No mapping metadata found for " + rawType.getName());
|
||||
}
|
||||
|
||||
return read(persistentEntity, row);
|
||||
return readRowInternal(persistentEntity, row);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -102,7 +113,7 @@ public class MappingCassandraConverter extends AbstractCassandraConverter implem
|
||||
this.spELContext = new SpELContext(this.spELContext, applicationContext);
|
||||
}
|
||||
|
||||
private <S extends Object> S read(final CassandraPersistentEntity<S> entity, final Row row) {
|
||||
private <S extends Object> S readRowInternal(final CassandraPersistentEntity<S> entity, final Row row) {
|
||||
|
||||
final DefaultSpELExpressionEvaluator evaluator = new DefaultSpELExpressionEvaluator(row, spELContext);
|
||||
|
||||
@@ -144,14 +155,122 @@ public class MappingCassandraConverter extends AbstractCassandraConverter implem
|
||||
* @see org.springframework.data.convert.EntityWriter#write(java.lang.Object, java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public void write(Object source, Row sink) {
|
||||
public <R> R read(Class<R> type, Object row) {
|
||||
if (row instanceof Row) {
|
||||
return readRow(type, (Row) row);
|
||||
}
|
||||
throw new MappingException("Unknown row object " + row.getClass().getName());
|
||||
}
|
||||
|
||||
/*
|
||||
* There is no concept of passing a Row into Cassandra for Writing.
|
||||
* This must be done with Query
|
||||
*
|
||||
* See the CQLUtils.
|
||||
*/
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.convert.EntityWriter#write(java.lang.Object, java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public void write(Object obj, Object builtStatement) {
|
||||
|
||||
if (obj == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Class<?> beanClassLoaderClass = transformClassToBeanClassLoaderClass(obj.getClass());
|
||||
CassandraPersistentEntity<?> entity = mappingContext.getPersistentEntity(beanClassLoaderClass);
|
||||
|
||||
if (entity == null) {
|
||||
throw new MappingException("No mapping metadata found for " + obj.getClass());
|
||||
}
|
||||
|
||||
if (builtStatement instanceof Insert) {
|
||||
writeInsertInternal(obj, (Insert) builtStatement, entity);
|
||||
} else if (builtStatement instanceof Update) {
|
||||
writeUpdateInternal(obj, (Update) builtStatement, entity);
|
||||
} else if (builtStatement instanceof Where) {
|
||||
writeDeleteWhereInternal(obj, (Where) builtStatement, entity);
|
||||
} else {
|
||||
throw new MappingException("Unknown buildStatement " + builtStatement.getClass().getName());
|
||||
}
|
||||
}
|
||||
|
||||
private void writeInsertInternal(final Object objectToSave, final Insert insert, CassandraPersistentEntity<?> entity) {
|
||||
|
||||
final BeanWrapper<CassandraPersistentEntity<Object>, Object> wrapper = BeanWrapper.create(objectToSave,
|
||||
conversionService);
|
||||
|
||||
// Write the properties
|
||||
entity.doWithProperties(new PropertyHandler<CassandraPersistentProperty>() {
|
||||
public void doWithPersistentProperty(CassandraPersistentProperty prop) {
|
||||
|
||||
Object propertyObj = wrapper.getProperty(prop, prop.getType(), useFieldAccessOnly);
|
||||
|
||||
if (propertyObj != null) {
|
||||
insert.value(prop.getColumnName(), propertyObj);
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private void writeUpdateInternal(final Object objectToSave, final Update update, CassandraPersistentEntity<?> entity) {
|
||||
|
||||
final BeanWrapper<CassandraPersistentEntity<Object>, Object> wrapper = BeanWrapper.create(objectToSave,
|
||||
conversionService);
|
||||
|
||||
// Write the properties
|
||||
entity.doWithProperties(new PropertyHandler<CassandraPersistentProperty>() {
|
||||
public void doWithPersistentProperty(CassandraPersistentProperty prop) {
|
||||
|
||||
Object propertyObj = wrapper.getProperty(prop, prop.getType(), useFieldAccessOnly);
|
||||
|
||||
if (propertyObj != null) {
|
||||
if (prop.isIdProperty()) {
|
||||
update.where(QueryBuilder.eq(prop.getColumnName(), propertyObj));
|
||||
} else {
|
||||
update.with(QueryBuilder.set(prop.getColumnName(), propertyObj));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private void writeDeleteWhereInternal(final Object objectToSave, final Where whereId, CassandraPersistentEntity<?> entity) {
|
||||
|
||||
final BeanWrapper<CassandraPersistentEntity<Object>, Object> wrapper = BeanWrapper.create(objectToSave,
|
||||
conversionService);
|
||||
|
||||
// Write the properties
|
||||
entity.doWithProperties(new PropertyHandler<CassandraPersistentProperty>() {
|
||||
public void doWithPersistentProperty(CassandraPersistentProperty prop) {
|
||||
|
||||
if (prop.isIdProperty()) {
|
||||
|
||||
Object propertyObj = wrapper.getProperty(prop, prop.getType(), useFieldAccessOnly);
|
||||
|
||||
if (propertyObj != null) {
|
||||
whereId.and(QueryBuilder.eq(prop.getColumnName(), propertyObj));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> Class<T> transformClassToBeanClassLoaderClass(Class<T> entity) {
|
||||
try {
|
||||
return (Class<T>) ClassUtils.forName(entity.getName(), beanClassLoader);
|
||||
} catch (ClassNotFoundException e) {
|
||||
return entity;
|
||||
} catch (LinkageError e) {
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBeanClassLoader(ClassLoader classLoader) {
|
||||
this.beanClassLoader = classLoader;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@ import java.util.Map;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.BeanClassLoaderAware;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
import org.springframework.dao.support.PersistenceExceptionTranslator;
|
||||
@@ -24,7 +23,7 @@ import com.datastax.driver.core.TableMetadata;
|
||||
/**
|
||||
* Default implementation of {@link CassandraAdminOperations}.
|
||||
*/
|
||||
public class CassandraAdminTemplate implements CassandraAdminOperations, BeanClassLoaderAware {
|
||||
public class CassandraAdminTemplate implements CassandraAdminOperations {
|
||||
|
||||
private static Logger log = LoggerFactory.getLogger(CassandraAdminTemplate.class);
|
||||
|
||||
@@ -35,8 +34,6 @@ public class CassandraAdminTemplate implements CassandraAdminOperations, BeanCla
|
||||
|
||||
private final PersistenceExceptionTranslator exceptionTranslator = new CassandraExceptionTranslator();
|
||||
|
||||
private ClassLoader beanClassLoader;
|
||||
|
||||
/**
|
||||
* Constructor used for a basic template configuration
|
||||
*
|
||||
@@ -241,8 +238,4 @@ public class CassandraAdminTemplate implements CassandraAdminOperations, BeanCla
|
||||
return entity.getTable();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBeanClassLoader(ClassLoader classLoader) {
|
||||
this.beanClassLoader = classLoader;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,6 @@ import java.util.Set;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.BeanClassLoaderAware;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
@@ -41,7 +40,6 @@ import org.springframework.data.cassandra.util.CqlUtils;
|
||||
import org.springframework.data.convert.EntityReader;
|
||||
import org.springframework.data.mapping.context.MappingContext;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
import com.datastax.driver.core.Host;
|
||||
import com.datastax.driver.core.Metadata;
|
||||
@@ -59,7 +57,7 @@ import com.datastax.driver.core.querybuilder.Batch;
|
||||
* @author Alex Shvid
|
||||
* @author David Webb
|
||||
*/
|
||||
public class CassandraTemplate implements CassandraOperations, BeanClassLoaderAware {
|
||||
public class CassandraTemplate implements CassandraOperations {
|
||||
|
||||
/**
|
||||
* Simple {@link RowCallback} that will transform {@link Row} into the given target type using the given
|
||||
@@ -69,10 +67,10 @@ public class CassandraTemplate implements CassandraOperations, BeanClassLoaderAw
|
||||
*/
|
||||
private static class ReadRowCallback<T> implements RowCallback<T> {
|
||||
|
||||
private final EntityReader<? super T, Row> reader;
|
||||
private final EntityReader<? super T, Object> reader;
|
||||
private final Class<T> type;
|
||||
|
||||
public ReadRowCallback(EntityReader<? super T, Row> reader, Class<T> type) {
|
||||
public ReadRowCallback(EntityReader<? super T, Object> reader, Class<T> type) {
|
||||
Assert.notNull(reader);
|
||||
Assert.notNull(type);
|
||||
this.reader = reader;
|
||||
@@ -105,8 +103,6 @@ public class CassandraTemplate implements CassandraOperations, BeanClassLoaderAw
|
||||
private final MappingContext<? extends CassandraPersistentEntity<?>, CassandraPersistentProperty> mappingContext;
|
||||
private final PersistenceExceptionTranslator exceptionTranslator = new CassandraExceptionTranslator();
|
||||
|
||||
private ClassLoader beanClassLoader;
|
||||
|
||||
/**
|
||||
* Constructor used for a basic template configuration
|
||||
*
|
||||
@@ -720,13 +716,6 @@ public class CassandraTemplate implements CassandraOperations, BeanClassLoaderAw
|
||||
return selectOneInternal(query, new ReadRowCallback<T>(cassandraConverter, selectClass));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param classLoader
|
||||
*/
|
||||
public void setBeanClassLoader(ClassLoader classLoader) {
|
||||
this.beanClassLoader = classLoader;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.cassandra.core.CassandraOperations#update(java.util.List)
|
||||
*/
|
||||
@@ -985,13 +974,10 @@ public class CassandraTemplate implements CassandraOperations, BeanClassLoaderAw
|
||||
|
||||
Assert.notEmpty(entities);
|
||||
|
||||
CassandraPersistentEntity<?> CPEntity = getEntity(entities.get(0));
|
||||
|
||||
Assert.notNull(CPEntity);
|
||||
|
||||
try {
|
||||
|
||||
final Batch b = CqlUtils.toDeleteBatchQuery(keyspace.getKeyspace(), tableName, entities, CPEntity, optionsByName);
|
||||
final Batch b = CqlUtils.toDeleteBatchQuery(keyspace.getKeyspace(), tableName, entities, optionsByName,
|
||||
cassandraConverter);
|
||||
log.info(b.toString());
|
||||
|
||||
execute(new SessionCallback<Object>() {
|
||||
@@ -1030,13 +1016,10 @@ public class CassandraTemplate implements CassandraOperations, BeanClassLoaderAw
|
||||
|
||||
Assert.notEmpty(entities);
|
||||
|
||||
CassandraPersistentEntity<?> CPEntity = getEntity(entities.get(0));
|
||||
|
||||
Assert.notNull(CPEntity);
|
||||
|
||||
try {
|
||||
|
||||
final Batch b = CqlUtils.toInsertBatchQuery(keyspace.getKeyspace(), tableName, entities, CPEntity, optionsByName);
|
||||
final Batch b = CqlUtils.toInsertBatchQuery(keyspace.getKeyspace(), tableName, entities, optionsByName,
|
||||
cassandraConverter);
|
||||
log.info(b.getQueryString());
|
||||
|
||||
return execute(new SessionCallback<List<T>>() {
|
||||
@@ -1075,13 +1058,10 @@ public class CassandraTemplate implements CassandraOperations, BeanClassLoaderAw
|
||||
|
||||
Assert.notEmpty(entities);
|
||||
|
||||
CassandraPersistentEntity<?> CPEntity = getEntity(entities.get(0));
|
||||
|
||||
Assert.notNull(CPEntity);
|
||||
|
||||
try {
|
||||
|
||||
final Batch b = CqlUtils.toUpdateBatchQuery(keyspace.getKeyspace(), tableName, entities, CPEntity, optionsByName);
|
||||
final Batch b = CqlUtils.toUpdateBatchQuery(keyspace.getKeyspace(), tableName, entities, optionsByName,
|
||||
cassandraConverter);
|
||||
log.info(b.toString());
|
||||
|
||||
return execute(new SessionCallback<List<T>>() {
|
||||
@@ -1115,13 +1095,10 @@ public class CassandraTemplate implements CassandraOperations, BeanClassLoaderAw
|
||||
protected <T> void doDelete(final String tableName, final T objectToRemove, Map<String, Object> optionsByName,
|
||||
final boolean deleteAsynchronously) {
|
||||
|
||||
CassandraPersistentEntity<?> entity = getEntity(objectToRemove);
|
||||
|
||||
Assert.notNull(entity);
|
||||
|
||||
try {
|
||||
|
||||
final Query q = CqlUtils.toDeleteQuery(keyspace.getKeyspace(), tableName, objectToRemove, entity, optionsByName);
|
||||
final Query q = CqlUtils.toDeleteQuery(keyspace.getKeyspace(), tableName, objectToRemove, optionsByName,
|
||||
cassandraConverter);
|
||||
log.info(q.toString());
|
||||
|
||||
execute(new SessionCallback<Object>() {
|
||||
@@ -1155,13 +1132,10 @@ public class CassandraTemplate implements CassandraOperations, BeanClassLoaderAw
|
||||
protected <T> T doInsert(final String tableName, final T entity, final Map<String, Object> optionsByName,
|
||||
final boolean insertAsychronously) {
|
||||
|
||||
CassandraPersistentEntity<?> CPEntity = getEntity(entity);
|
||||
|
||||
Assert.notNull(CPEntity);
|
||||
|
||||
try {
|
||||
|
||||
final Query q = CqlUtils.toInsertQuery(keyspace.getKeyspace(), tableName, entity, CPEntity, optionsByName);
|
||||
final Query q = CqlUtils.toInsertQuery(keyspace.getKeyspace(), tableName, entity, optionsByName,
|
||||
cassandraConverter);
|
||||
log.info(q.toString());
|
||||
if (q.getConsistencyLevel() != null) {
|
||||
log.info(q.getConsistencyLevel().name());
|
||||
@@ -1205,13 +1179,10 @@ public class CassandraTemplate implements CassandraOperations, BeanClassLoaderAw
|
||||
protected <T> T doUpdate(final String tableName, final T entity, final Map<String, Object> optionsByName,
|
||||
final boolean updateAsychronously) {
|
||||
|
||||
CassandraPersistentEntity<?> CPEntity = getEntity(entity);
|
||||
|
||||
Assert.notNull(CPEntity);
|
||||
|
||||
try {
|
||||
|
||||
final Query q = CqlUtils.toUpdateQuery(keyspace.getKeyspace(), tableName, entity, CPEntity, optionsByName);
|
||||
final Query q = CqlUtils.toUpdateQuery(keyspace.getKeyspace(), tableName, entity, optionsByName,
|
||||
cassandraConverter);
|
||||
log.info(q.toString());
|
||||
|
||||
return execute(new SessionCallback<T>() {
|
||||
@@ -1269,30 +1240,6 @@ public class CassandraTemplate implements CassandraOperations, BeanClassLoaderAw
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines the PersistentEntityType for a given Object
|
||||
*
|
||||
* @param o
|
||||
* @return
|
||||
*/
|
||||
protected CassandraPersistentEntity<?> getEntity(Object o) {
|
||||
|
||||
CassandraPersistentEntity<?> entity = null;
|
||||
try {
|
||||
String entityClassName = o.getClass().getName();
|
||||
Class<?> entityClass = ClassUtils.forName(entityClassName, beanClassLoader);
|
||||
entity = mappingContext.getPersistentEntity(entityClass);
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (LinkageError e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
}
|
||||
|
||||
return entity;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param query
|
||||
* @param readRowCallback
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package org.springframework.data.cassandra.util;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -16,6 +15,7 @@ import org.springframework.data.cassandra.core.RetryPolicyResolver;
|
||||
import org.springframework.data.cassandra.exception.EntityWriterException;
|
||||
import org.springframework.data.cassandra.mapping.CassandraPersistentEntity;
|
||||
import org.springframework.data.cassandra.mapping.CassandraPersistentProperty;
|
||||
import org.springframework.data.convert.EntityWriter;
|
||||
import org.springframework.data.mapping.PropertyHandler;
|
||||
|
||||
import com.datastax.driver.core.ColumnMetadata;
|
||||
@@ -214,40 +214,14 @@ public abstract class CqlUtils {
|
||||
* @throws EntityWriterException
|
||||
*/
|
||||
public static Query toInsertQuery(String keyspaceName, String tableName, final Object objectToSave,
|
||||
CassandraPersistentEntity<?> entity, Map<String, Object> optionsByName) throws EntityWriterException {
|
||||
Map<String, Object> optionsByName, EntityWriter<Object, Object> entityWriter) throws EntityWriterException {
|
||||
|
||||
final Insert q = QueryBuilder.insertInto(keyspaceName, tableName);
|
||||
final Exception innerException = new Exception();
|
||||
|
||||
entity.doWithProperties(new PropertyHandler<CassandraPersistentProperty>() {
|
||||
public void doWithPersistentProperty(CassandraPersistentProperty prop) {
|
||||
|
||||
/*
|
||||
* See if the object has a value for that column, and if so, add it to the Query
|
||||
*/
|
||||
try {
|
||||
|
||||
Object o = prop.getGetter().invoke(objectToSave, new Object[0]);
|
||||
|
||||
log.info("Getter Invoke [" + prop.getColumnName() + " => " + o);
|
||||
|
||||
if (o != null) {
|
||||
q.value(prop.getColumnName(), o);
|
||||
}
|
||||
|
||||
} catch (IllegalAccessException e) {
|
||||
innerException.initCause(e);
|
||||
} catch (IllegalArgumentException e) {
|
||||
innerException.initCause(e);
|
||||
} catch (InvocationTargetException e) {
|
||||
innerException.initCause(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (innerException.getCause() != null) {
|
||||
throw new EntityWriterException("Failed to convert Persistent Entity to CQL/Query", innerException.getCause());
|
||||
}
|
||||
/*
|
||||
* Write properties
|
||||
*/
|
||||
entityWriter.write(objectToSave, q);
|
||||
|
||||
/*
|
||||
* Add Query Options
|
||||
@@ -280,44 +254,14 @@ public abstract class CqlUtils {
|
||||
* @throws EntityWriterException
|
||||
*/
|
||||
public static Query toUpdateQuery(String keyspaceName, String tableName, final Object objectToSave,
|
||||
CassandraPersistentEntity<?> entity, Map<String, Object> optionsByName) throws EntityWriterException {
|
||||
Map<String, Object> optionsByName, EntityWriter<Object, Object> entityWriter) throws EntityWriterException {
|
||||
|
||||
final Update q = QueryBuilder.update(keyspaceName, tableName);
|
||||
final Exception innerException = new Exception();
|
||||
|
||||
entity.doWithProperties(new PropertyHandler<CassandraPersistentProperty>() {
|
||||
public void doWithPersistentProperty(CassandraPersistentProperty prop) {
|
||||
|
||||
/*
|
||||
* See if the object has a value for that column, and if so, add it to the Query
|
||||
*/
|
||||
try {
|
||||
|
||||
Object o = prop.getGetter().invoke(objectToSave, new Object[0]);
|
||||
|
||||
log.info("Getter Invoke [" + prop.getColumnName() + " => " + o);
|
||||
|
||||
if (o != null) {
|
||||
if (prop.isIdProperty()) {
|
||||
q.where(QueryBuilder.eq(prop.getColumnName(), o));
|
||||
} else {
|
||||
q.with(QueryBuilder.set(prop.getColumnName(), o));
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IllegalAccessException e) {
|
||||
innerException.initCause(e);
|
||||
} catch (IllegalArgumentException e) {
|
||||
innerException.initCause(e);
|
||||
} catch (InvocationTargetException e) {
|
||||
innerException.initCause(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (innerException.getCause() != null) {
|
||||
throw new EntityWriterException("Failed to convert Persistent Entity to CQL/Query", innerException.getCause());
|
||||
}
|
||||
/*
|
||||
* Write properties
|
||||
*/
|
||||
entityWriter.write(objectToSave, q);
|
||||
|
||||
/*
|
||||
* Add Query Options
|
||||
@@ -349,7 +293,7 @@ public abstract class CqlUtils {
|
||||
* @throws EntityWriterException
|
||||
*/
|
||||
public static <T> Batch toUpdateBatchQuery(final String keyspaceName, final String tableName,
|
||||
final List<T> objectsToSave, CassandraPersistentEntity<?> entity, Map<String, Object> optionsByName)
|
||||
final List<T> objectsToSave, Map<String, Object> optionsByName, EntityWriter<Object, Object> entityWriter)
|
||||
throws EntityWriterException {
|
||||
|
||||
/*
|
||||
@@ -359,7 +303,7 @@ public abstract class CqlUtils {
|
||||
|
||||
for (final T objectToSave : objectsToSave) {
|
||||
|
||||
b.add((Statement) toUpdateQuery(keyspaceName, tableName, objectToSave, entity, optionsByName));
|
||||
b.add((Statement) toUpdateQuery(keyspaceName, tableName, objectToSave, optionsByName, entityWriter));
|
||||
|
||||
}
|
||||
|
||||
@@ -383,7 +327,7 @@ public abstract class CqlUtils {
|
||||
* @throws EntityWriterException
|
||||
*/
|
||||
public static <T> Batch toInsertBatchQuery(final String keyspaceName, final String tableName,
|
||||
final List<T> objectsToSave, CassandraPersistentEntity<?> entity, Map<String, Object> optionsByName)
|
||||
final List<T> objectsToSave, Map<String, Object> optionsByName, EntityWriter<Object, Object> entityWriter)
|
||||
throws EntityWriterException {
|
||||
|
||||
/*
|
||||
@@ -393,7 +337,7 @@ public abstract class CqlUtils {
|
||||
|
||||
for (final T objectToSave : objectsToSave) {
|
||||
|
||||
b.add((Statement) toInsertQuery(keyspaceName, tableName, objectToSave, entity, optionsByName));
|
||||
b.add((Statement) toInsertQuery(keyspaceName, tableName, objectToSave, optionsByName, entityWriter));
|
||||
|
||||
}
|
||||
|
||||
@@ -412,45 +356,16 @@ public abstract class CqlUtils {
|
||||
* @throws EntityWriterException
|
||||
*/
|
||||
public static Query toDeleteQuery(String keyspace, String tableName, final Object objectToRemove,
|
||||
CassandraPersistentEntity<?> entity, Map<String, Object> optionsByName) throws EntityWriterException {
|
||||
Map<String, Object> optionsByName, EntityWriter<Object, Object> entityWriter) throws EntityWriterException {
|
||||
|
||||
final Delete.Selection ds = QueryBuilder.delete();
|
||||
final Delete q = ds.from(keyspace, tableName);
|
||||
final Where w = q.where();
|
||||
|
||||
final Exception innerException = new Exception();
|
||||
|
||||
entity.doWithProperties(new PropertyHandler<CassandraPersistentProperty>() {
|
||||
public void doWithPersistentProperty(CassandraPersistentProperty prop) {
|
||||
|
||||
/*
|
||||
* See if the object has a value for that column, and if so, add it to the Query
|
||||
*/
|
||||
try {
|
||||
|
||||
if (prop.isIdProperty()) {
|
||||
Object o = (String) prop.getGetter().invoke(objectToRemove, new Object[0]);
|
||||
|
||||
log.info("Getter Invoke [" + prop.getColumnName() + " => " + o);
|
||||
|
||||
if (o != null) {
|
||||
w.and(QueryBuilder.eq(prop.getColumnName(), o));
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IllegalAccessException e) {
|
||||
innerException.initCause(e);
|
||||
} catch (IllegalArgumentException e) {
|
||||
innerException.initCause(e);
|
||||
} catch (InvocationTargetException e) {
|
||||
innerException.initCause(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (innerException.getCause() != null) {
|
||||
throw new EntityWriterException("Failed to convert Persistent Entity to CQL/Query", innerException.getCause());
|
||||
}
|
||||
/*
|
||||
* Write where condition to find by Id
|
||||
*/
|
||||
entityWriter.write(objectToRemove, w);
|
||||
|
||||
addQueryOptions(q, optionsByName);
|
||||
|
||||
@@ -504,7 +419,7 @@ public abstract class CqlUtils {
|
||||
* @throws EntityWriterException
|
||||
*/
|
||||
public static <T> Batch toDeleteBatchQuery(String keyspaceName, String tableName, List<T> entities,
|
||||
CassandraPersistentEntity<?> entity, Map<String, Object> optionsByName) throws EntityWriterException {
|
||||
Map<String, Object> optionsByName, EntityWriter<Object, Object> entityWriter) throws EntityWriterException {
|
||||
|
||||
/*
|
||||
* Return variable is a Batch statement
|
||||
@@ -513,7 +428,7 @@ public abstract class CqlUtils {
|
||||
|
||||
for (final T objectToSave : entities) {
|
||||
|
||||
b.add((Statement) toDeleteQuery(keyspaceName, tableName, objectToSave, entity, optionsByName));
|
||||
b.add((Statement) toDeleteQuery(keyspaceName, tableName, objectToSave, optionsByName, entityWriter));
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user