From 937fbf3c18b79b7b80d09e2169a9c89342ada46e Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Fri, 1 Jul 2016 11:39:25 +0200 Subject: [PATCH] DATACASS-206 - Polishing. Reorder fields in MappingCassandraConverter. Remove code duplicates. Improve JavaDoc in CassandraOperations. Align entity class parameter names in CassandraTemplate. Enhance JavaDoc for built statement factory methods. Add guards to method arguments. Original pull request: #73. --- .../convert/MappingCassandraConverter.java | 14 +- .../cassandra/core/CassandraOperations.java | 73 ++-- .../cassandra/core/CassandraTemplate.java | 336 ++++++++++-------- 3 files changed, 221 insertions(+), 202 deletions(-) diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/convert/MappingCassandraConverter.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/convert/MappingCassandraConverter.java index 5cc8252a0..2699b3529 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/convert/MappingCassandraConverter.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/convert/MappingCassandraConverter.java @@ -82,16 +82,13 @@ import com.datastax.driver.core.querybuilder.Update; public class MappingCassandraConverter extends AbstractCassandraConverter implements CassandraConverter, ApplicationContextAware, BeanClassLoaderAware { - protected ApplicationContext applicationContext; - protected final CassandraMappingContext mappingContext; - + protected ApplicationContext applicationContext; protected ClassLoader beanClassLoader; - - protected final Logger log = LoggerFactory.getLogger(getClass()); - protected SpELContext spELContext; + private final Logger log = LoggerFactory.getLogger(getClass()); + /** * Creates a new {@link MappingCassandraConverter} with a {@link BasicCassandraMappingContext}. */ @@ -509,7 +506,6 @@ public class MappingCassandraConverter extends AbstractCassandraConverter @Override public void setBeanClassLoader(ClassLoader classLoader) { this.beanClassLoader = classLoader; - } @Override @@ -543,9 +539,7 @@ public class MappingCassandraConverter extends AbstractCassandraConverter } private Class getTargetType(CassandraPersistentProperty property) { - - return (property.isCompositePrimaryKey() ? property.getType() : CodecRegistry.DEFAULT_INSTANCE.codecFor( - mappingContext.getDataType(property)).getJavaType().getRawType()); + return (property.isCompositePrimaryKey() ? property.getType() : getCodec(property).getJavaType().getRawType()); } /** diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraOperations.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraOperations.java index 7211b2d51..7303747a7 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraOperations.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraOperations.java @@ -47,7 +47,7 @@ public interface CassandraOperations extends CqlOperations { /** * The table name used for the specified class by this template. * - * @param entityClass must not be {@literal null}. + * @param entityClass The entity type, must not be {@literal null}. * @return the {@link CqlIdentifier} */ CqlIdentifier getTableName(Class entityClass); @@ -60,130 +60,130 @@ public interface CassandraOperations extends CqlOperations { * * @param element return type. * @param query query to execute. Must not be empty or {@literal null}. - * @param type Class type of the elements in the {@link Iterator} stream. Must not be {@literal null}. + * @param entityClass Class type of the elements in the {@link Iterator} stream. Must not be {@literal null}. * @return an {@link Iterator} (stream) over the elements in the query result set. * @since 1.5 */ - Iterator stream(String query, Class type); + Iterator stream(String query, Class entityClass); /** * Execute query and convert ResultSet to the list of entities. * * @param cql must not be {@literal null}. - * @param type must not be {@literal null}, mapped entity type. + * @param entityClass The entity type, must not be {@literal null}. * @return the converted results */ - List select(String cql, Class type); + List select(String cql, Class entityClass); /** * Execute the Select Query and convert to the list of entities. * * @param select must not be {@literal null}. - * @param type must not be {@literal null}, mapped entity type. + * @param entityClass The entity type, must not be {@literal null}. * @return the converted results */ - List select(Select select, Class type); + List select(Select select, Class entityClass); /** - * Select objects for the given {@code type} and {@code ids}. + * Select objects for the given {@code entityClass} and {@code ids}. * - * @param type must not be {@literal null}, mapped entity type. + * @param entityClass The entity type, must not be {@literal null}. * @param ids must not be {@literal null}. * @return the converted results */ - List selectBySimpleIds(Class type, Iterable ids); + List selectBySimpleIds(Class entityClass, Iterable ids); /** * @deprecated Calling this method could result in {@link OutOfMemoryError}, as this is a brute force selection. - * @param type The type of entity to select. + * @param entityClass The entity type, must not be {@literal null}. * @return A list of all entities of type T. */ @Deprecated - List selectAll(Class type); + List selectAll(Class entityClass); /** - * Execute the Select by {@code id} for the given {@code type}. + * Execute the Select by {@code id} for the given {@code entityClass}. * - * @param type must not be {@literal null}. + * @param entityClass The entity type, must not be {@literal null}. * @param id must not be {@literal null}. * @return the converted object or {@literal null}. */ - T selectOneById(Class type, Object id); + T selectOneById(Class entityClass, Object id); /** * Execute CQL and convert ResultSet to the entity * * @param cql must not be {@literal null}. - * @param type must not be {@literal null}, mapped entity type. + * @param entityClass The entity type, must not be {@literal null}. * @return the converted object or {@literal null}. */ - T selectOne(String cql, Class type); + T selectOne(String cql, Class entityClass); /** * Execute Select query and convert ResultSet to the entity * * @param select must not be {@literal null}. - * @param type must not be {@literal null}, mapped entity type. + * @param entityClass The entity type, must not be {@literal null}. * @return the converted object or {@literal null}. */ - T selectOne(Select select, Class type); + T selectOne(Select select, Class entityClass); /** * Executes the {@link Select} query asynchronously. * * @param select The {@link Select} query to execute. - * @param type The type of entity to retrieve. + * @param entityClass The entity type, must not be {@literal null}. * @return A {@link Cancellable} that can be used to cancel the query. */ - Cancellable selectOneAsynchronously(Select select, Class type, QueryForObjectListener listener); + Cancellable selectOneAsynchronously(Select select, Class entityClass, QueryForObjectListener listener); /** * Executes the string CQL query asynchronously. * * @param cql The string query CQL to execute. - * @param type The type of entity to retrieve. + * @param entityClass The entity type, must not be {@literal null}. * @return A {@link Cancellable} that can be used to cancel the query. */ - Cancellable selectOneAsynchronously(String cql, Class type, QueryForObjectListener listener); + Cancellable selectOneAsynchronously(String cql, Class entityClass, QueryForObjectListener listener); /** * Executes the {@link Select} query asynchronously. * * @param select The {@link Select} query to execute. - * @param type The type of entity to retrieve. + * @param entityClass The entity type, must not be {@literal null}. * @param options The {@link QueryOptions} to use. * @return A {@link Cancellable} that can be used to cancel the query. */ - Cancellable selectOneAsynchronously(Select select, Class type, QueryForObjectListener listener, + Cancellable selectOneAsynchronously(Select select, Class entityClass, QueryForObjectListener listener, QueryOptions options); /** * Executes the string CQL query asynchronously. * * @param cql The string query CQL to execute. - * @param type The type of entity to retrieve. + * @param entityClass The entity type, must not be {@literal null}. * @param options The {@link QueryOptions} to use. * @return A {@link Cancellable} that can be used to cancel the query. */ - Cancellable selectOneAsynchronously(String cql, Class type, QueryForObjectListener listener, + Cancellable selectOneAsynchronously(String cql, Class entityClass, QueryForObjectListener listener, QueryOptions options); /** - * Determine whether the row {@code type} with the given {@code id} exists. + * Determine whether the row {@code entityClass} with the given {@code id} exists. * - * @param type must not be {@literal null}. + * @param entityClass The entity type, must not be {@literal null}. * @param id must not be {@literal null}. * @return true, if the object exists */ - boolean exists(Class type, Object id); + boolean exists(Class entityClass, Object id); /** - * Returns the number of rows for the given {@code type} by querying the table of the given entity class. + * Returns the number of rows for the given {@code entityClass} by querying the table of the given entity class. * - * @param type must not be {@literal null}. + * @param entityClass The entity type, must not be {@literal null}. * @return number of rows */ - long count(Class type); + long count(Class entityClass); /** * Insert the given entity. @@ -428,10 +428,10 @@ public interface CassandraOperations extends CqlOperations { /** * Remove the given object from the table by id. * - * @param type must not be {@literal null}. + * @param entityClass The entity type, must not be {@literal null}. * @param id must not be {@literal null}. */ - void deleteById(Class type, Object id); + void deleteById(Class entityClass, Object id); /** * Remove the given object from the table by id. @@ -465,8 +465,9 @@ public interface CassandraOperations extends CqlOperations { /** * Deletes all entities of a given class. + * @param entityClass The entity type, must not be {@literal null}. */ - void deleteAll(Class clazz); + void deleteAll(Class entityClass); /** * Remove the given object from the table by id. diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraTemplate.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraTemplate.java index c8ee30374..8fdced9fc 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraTemplate.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraTemplate.java @@ -158,12 +158,12 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation } @Override - public boolean exists(Class type, Object id) { + public boolean exists(Class entityClass, Object id) { - Assert.notNull(type, "Type must not be null"); + Assert.notNull(entityClass, "EntityClass must not be null"); Assert.notNull(id, "Id must not be null"); - CassandraPersistentEntity entity = getPersistentEntity(type); + CassandraPersistentEntity entity = getPersistentEntity(entityClass); Select select = QueryBuilder.select().countAll().from(entity.getTableName().toCql()); cassandraConverter.write(id, select.where(), entity); @@ -189,12 +189,12 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation } @Override - public void deleteById(Class type, Object id) { + public void deleteById(Class entityClass, Object id) { - Assert.notNull(type); - Assert.notNull(id); + Assert.notNull(entityClass, "EntityClass must not be null"); + Assert.notNull(id, "Id must not be null"); - CassandraPersistentEntity entity = getPersistentEntity(type); + CassandraPersistentEntity entity = getPersistentEntity(entityClass); Delete delete = QueryBuilder.delete().from(entity.getTableName().toCql()); cassandraConverter.write(id, delete.where(), entity); @@ -254,17 +254,7 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation @Override public CqlIdentifier getTableName(Class entityClass) { - - if (entityClass == null) { - throw new InvalidDataAccessApiUsageException("No class parameter provided, entity table can't be determined!"); - } - - CassandraPersistentEntity entity = mappingContext.getPersistentEntity(entityClass); - if (entity == null) { - throw new InvalidDataAccessApiUsageException( - "No Persistent Entity information found for the class " + entityClass.getName()); - } - return entity.getTableName(); + return getPersistentEntity(entityClass).getTableName(); } @Override @@ -357,56 +347,63 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation } @Override - public List selectAll(Class type) { - return select(QueryBuilder.select().all().from(getTableName(type).toCql()), type); + public List selectAll(Class entityClass) { + + Assert.notNull(entityClass, "EntityClass must not be null"); + + return select(QueryBuilder.select().all().from(getTableName(entityClass).toCql()), entityClass); } @Override - public List select(String cql, Class type) { + public List select(String cql, Class entityClass) { - Assert.hasText(cql); - Assert.notNull(type); + Assert.hasText(cql, "CQL must not be empty"); + Assert.notNull(entityClass, "EntityClass must not be null"); - return select(cql, new CassandraConverterRowCallback(cassandraConverter, type)); + return select(cql, new CassandraConverterRowCallback(cassandraConverter, entityClass)); } @Override - public List select(Select select, Class type) { + public List select(Select select, Class entityClass) { - Assert.notNull(select); + Assert.notNull(select, "Select must not be null"); + Assert.notNull(entityClass, "EntityClass must not be null"); - return select(select, new CassandraConverterRowCallback(cassandraConverter, type)); + return select(select, new CassandraConverterRowCallback(cassandraConverter, entityClass)); } @Override - public List selectBySimpleIds(Class type, Iterable ids) { + public List selectBySimpleIds(Class entityClass, Iterable ids) { - CassandraPersistentEntity entity = mappingContext.getPersistentEntity(type); + Assert.notNull(entityClass, "EntityClass must not be null"); + Assert.notNull(ids, "Ids must not be null"); - if (entity.getIdProperty().isCompositePrimaryKey()) { + CassandraPersistentEntity entity = getPersistentEntity(entityClass); + + if (entity.getIdProperty() == null || entity.getIdProperty().isCompositePrimaryKey()) { throw new IllegalArgumentException( - String.format("entity class [%s] uses a composite primary key class [%s] which this method can't support", - type.getName(), entity.getIdProperty().getCompositePrimaryKeyEntity().getType().getName())); + String.format("Entity class [%s] uses a composite primary key class [%s] which this method can't support", + entityClass.getName(), entity.getIdProperty().getCompositePrimaryKeyEntity().getType().getName())); } Select select = QueryBuilder.select().all().from(entity.getTableName().toCql()); select.where(QueryBuilder.in(entity.getIdProperty().getColumnName().toCql(), CollectionUtils.toArray(ids))); - return select(select, type); + return select(select, entityClass); } @Override - public T selectOneById(Class type, Object id) { + public T selectOneById(Class entityClass, Object id) { - Assert.notNull(type, "Type must not be null"); + Assert.notNull(entityClass, "EntityClass must not be null"); Assert.notNull(id, "Id must not be null"); - CassandraPersistentEntity entity = getPersistentEntity(type); + CassandraPersistentEntity entity = getPersistentEntity(entityClass); Select select = QueryBuilder.select().all().from(entity.getTableName().toCql()); cassandraConverter.write(id, select.where(), entity); - return selectOne(select, type); + return selectOne(select, entityClass); } protected interface ClauseCallback { @@ -491,13 +488,19 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation } @Override - public T selectOne(String cql, Class type) { - return selectOne(cql, new CassandraConverterRowCallback(cassandraConverter, type)); + public T selectOne(String cql, Class entityClass) { + + Assert.notNull(entityClass, "EntityClass must not be null"); + + return selectOne(cql, new CassandraConverterRowCallback(cassandraConverter, entityClass)); } @Override - public T selectOne(Select select, Class type) { - return selectOne(select, new CassandraConverterRowCallback(cassandraConverter, type)); + public T selectOne(Select select, Class entityClass) { + + Assert.notNull(entityClass, "EntityClass must not be null"); + + return selectOne(select, new CassandraConverterRowCallback(cassandraConverter, entityClass)); } @Override @@ -600,10 +603,10 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation /* (non-Javadoc) * @see org.springframework.data.cassandra.core.CassandraOperations#stream(java.lang.String, java.lang.Class) */ - public Iterator stream(final String query, Class type) { + public Iterator stream(final String query, Class entityClass) { Assert.hasText(query, "Query must not be empty"); - Assert.notNull(type, "Type must not be null"); + Assert.notNull(entityClass, "EntityClass must not be null"); ResultSet resultSet = doExecute(new SessionCallback() { @@ -613,7 +616,7 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation } }); - return (resultSet != null ? toIterator(resultSet, type) : Collections.emptyIterator()); + return (resultSet != null ? toIterator(resultSet, entityClass) : Collections.emptyIterator()); } /* @@ -621,10 +624,10 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation * @see org.springframework.data.cassandra.core.CassandraTemplate.ResultSetIteratorAdapter */ @SuppressWarnings("unchecked") - private Iterator toIterator(ResultSet resultSet, Class type) { + private Iterator toIterator(ResultSet resultSet, Class entityClass) { return new ResultSetIteratorAdapter(resultSet.iterator(), getExceptionTranslator(), - new CassandraConverterRowCallback(cassandraConverter, type)); + new CassandraConverterRowCallback(cassandraConverter, entityClass)); } protected List select(final Select query, CassandraConverterRowCallback readRowCallback) { @@ -910,99 +913,47 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation } /** - * Generates a Query Object for an insert + * Generates a Query Object for an insert. * - * @param tableName - * @param objectToSave - * @param entity - * @param optionsByName + * @param tableName the table name, must not be empty and not {@literal null}. + * @param objectToUpdate the object to save, must not be {@literal null}. + * @param options optional {@link WriteOptions} to apply to the {@link Insert} statement, may be {@literal null}. + * @param entityWriter the {@link EntityWriter} to write insert values. * @return The Query object to run with session.execute(); */ - public static Insert createInsertQuery(String tableName, Object objectToSave, WriteOptions options, + public static Insert createInsertQuery(String tableName, Object objectToUpdate, WriteOptions options, EntityWriter entityWriter) { + Assert.hasText(tableName, "TableName is empty"); + Assert.notNull(objectToUpdate, "The object to insert is null"); + Assert.notNull(entityWriter, "EntityWriter is null"); + Insert insert = QueryBuilder.insertInto(tableName); - entityWriter.write(objectToSave, insert); + entityWriter.write(objectToUpdate, insert); CqlTemplate.addWriteOptions(insert, options); return insert; } /** - * @deprecated Method renamed. Use {@link #createUpdateQuery(String, Object, WriteOptions, EntityWriter)} - * @see #createUpdateQuery(String, Object, WriteOptions, EntityWriter) - */ - @Deprecated - public static Update toUpdateQueryX(String tableName, Object objectToSave, WriteOptions options, - EntityWriter entityWriter) { - return createUpdateQuery(tableName, objectToSave, options, entityWriter); - } - - /** - * Generates a Query Object for an Update + * Generates a Batch Object for multiple inserts. * - * @param tableName - * @param objectToSave - * @param entity - * @param optionsByName + * @param tableName the table name, must not be empty and not {@literal null}. + * @param objectsToInsert the object to save, must not be empty and not {@literal null}. + * @param options optional {@link WriteOptions} to apply to the {@link Insert} statement, may be {@literal null}. + * @param entityWriter the {@link EntityWriter} to write insert values. * @return The Query object to run with session.execute(); */ - public static Update createUpdateQuery(String tableName, Object objectToSave, WriteOptions options, + public static Batch createInsertBatchQuery(String tableName, List objectsToInsert, WriteOptions options, EntityWriter entityWriter) { - Update update = QueryBuilder.update(tableName); - entityWriter.write(objectToSave, update); - CqlTemplate.addWriteOptions(update, options); - return update; - } - - /** - * @deprecated Method renamed. Use {@link #createUpdateBatchQuery(String, List, WriteOptions, EntityWriter)} - * @see #createUpdateBatchQuery(String, List, WriteOptions, EntityWriter) - */ - @Deprecated - public static Batch toUpdateBatchQuery(String tableName, List objectsToSave, WriteOptions options, - EntityWriter entityWriter) { - return createUpdateBatchQuery(tableName, objectsToSave, options, entityWriter); - } - - /** - * Generates a Batch Object for multiple Updates - * - * @param tableName - * @param objectsToSave - * @param entity - * @param optionsByName - * @return The Query object to run with session.execute(); - */ - public static Batch createUpdateBatchQuery(String tableName, List objectsToSave, WriteOptions options, - EntityWriter entityWriter) { - - Batch b = QueryBuilder.batch(); - - for (T objectToSave : objectsToSave) { - b.add(createUpdateQuery(tableName, objectToSave, options, entityWriter)); - } - - CqlTemplate.addQueryOptions(b, options); - - return b; - } - - /** - * Generates a Batch Object for multiple inserts - * - * @param tableName - * @param entities - * @param entity - * @param optionsByName - * @return The Query object to run with session.execute(); - */ - public static Batch createInsertBatchQuery(String tableName, List entities, WriteOptions options, - EntityWriter entityWriter) { + Assert.hasText(tableName, "TableName is empty"); + Assert.notNull(objectsToInsert, "The objects to insert are null"); + Assert.notEmpty(objectsToInsert, "The objects to insert are empty"); + Assert.notNull(entityWriter, "EntityWriter is null"); Batch batch = QueryBuilder.batch(); - for (T entity : entities) { + for (T entity : objectsToInsert) { batch.add(createInsertQuery(tableName, entity, options, entityWriter)); } @@ -1012,21 +963,98 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation } /** - * Create a Delete Query Object from an annotated POJO + * Generates a Query Object for an Update. The {@link Update} uses the identity and values from the given + * {@code objectsToUpdate}. * - * @param tableName - * @param object - * @param entity - * @param optionsByName - * @return + * @param tableName the table name, must not be empty and not {@literal null}. + * @param objectToUpdate the object to update, must not be {@literal null}. + * @param options optional {@link WriteOptions} to apply to the {@link Update} statement, may be {@literal null}. + * @param entityWriter the {@link EntityWriter} to write update assignments and where clauses. + * @return The Query object to run with session.execute(); */ - public static Delete createDeleteQuery(String tableName, Object object, QueryOptions options, + public static Update createUpdateQuery(String tableName, Object objectToUpdate, WriteOptions options, EntityWriter entityWriter) { + Assert.hasText(tableName, "TableName is empty"); + Assert.notNull(objectToUpdate, "The object to update is null"); + Assert.notNull(entityWriter, "EntityWriter is null"); + + Update update = QueryBuilder.update(tableName); + entityWriter.write(objectToUpdate, update); + CqlTemplate.addWriteOptions(update, options); + return update; + } + + /** + * Generates a Batch Object for multiple Updates. The {@link Update} uses the identity and values from the given + * {@code objectsToUpdate}. + * + * @param tableName the table name, must not be empty and not {@literal null}. + * @param objectsToUpdate the object to update, must not be empty and not {@literal null}. + * @param options optional {@link WriteOptions} to apply to the {@link Update} statement, may be {@literal null}. + * @param entityWriter the {@link EntityWriter} to write update assignments and where clauses. + * @return The Query object to run with session.execute(); + */ + public static Batch createUpdateBatchQuery(String tableName, List objectsToUpdate, WriteOptions options, + EntityWriter entityWriter) { + + Assert.hasText(tableName, "TableName is empty"); + Assert.notNull(objectsToUpdate, "The objects to update are null"); + Assert.notEmpty(objectsToUpdate, "The objects to update are empty"); + Assert.notNull(entityWriter, "EntityWriter is null"); + + Batch b = QueryBuilder.batch(); + + for (T objectToSave : objectsToUpdate) { + b.add(createUpdateQuery(tableName, objectToSave, options, entityWriter)); + } + + CqlTemplate.addQueryOptions(b, options); + + return b; + } + + /** + * @deprecated Method renamed. Use {@link #createUpdateBatchQuery(String, List, WriteOptions, EntityWriter)} + * @see #createUpdateBatchQuery(String, List, WriteOptions, EntityWriter) + */ + @Deprecated + public static Batch toUpdateBatchQuery(String tableName, List objectsToUpdate, WriteOptions options, + EntityWriter entityWriter) { + return createUpdateBatchQuery(tableName, objectsToUpdate, options, entityWriter); + } + + /** + * @deprecated Method renamed. Use {@link #createUpdateQuery(String, Object, WriteOptions, EntityWriter)} + * @see #createUpdateQuery(String, Object, WriteOptions, EntityWriter) + */ + @Deprecated + public static Update toUpdateQueryX(String tableName, Object objectToUpdate, WriteOptions options, + EntityWriter entityWriter) { + return createUpdateQuery(tableName, objectToUpdate, options, entityWriter); + } + + /** + * Create a Delete Query Object from an annotated POJO. The {@link Delete} uses the identity from the given + * {@code objectToDelete}. + * + * @param tableName the table name, must not be empty and not {@literal null}. + * @param objectToDelete the object to delete, must not be {@literal null}. + * @param options optional {@link QueryOptions} to apply to the {@link Delete} statement, may be {@literal null}. + * @param entityWriter the {@link EntityWriter} to write delete where clauses. + * @return The Query object to run with session.execute(); + */ + public static Delete createDeleteQuery(String tableName, Object objectToDelete, QueryOptions options, + EntityWriter entityWriter) { + + Assert.hasText(tableName, "TableName is empty"); + Assert.notNull(objectToDelete, "The object to delete is null"); + Assert.notNull(entityWriter, "EntityWriter is null"); + Delete.Selection ds = QueryBuilder.delete(); Delete delete = ds.from(tableName); Where w = delete.where(); - entityWriter.write(object, w); + entityWriter.write(objectToDelete, w); CqlTemplate.addQueryOptions(delete, options); return delete; } @@ -1034,21 +1062,23 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation /** * Create a Batch Query object for multiple deletes. * - * @param tableName - * @param entities - * @param entity - * @param optionsByName - * @return + * @param tableName the table name, must not be empty and not {@literal null}. + * @param objectsToDelete the object to delete, must not be empty and not {@literal null}. + * @param options optional {@link QueryOptions} to apply to the {@link Delete} statement, may be {@literal null}. + * @param entityWriter the {@link EntityWriter} to write delete where clauses. + * @return The Query object to run with session.execute(); */ - public static Batch createDeleteBatchQuery(String tableName, List entities, QueryOptions options, + public static Batch createDeleteBatchQuery(String tableName, List objectsToDelete, QueryOptions options, EntityWriter entityWriter) { - Assert.notEmpty(entities); - Assert.hasText(tableName); + Assert.hasText(tableName, "TableName is empty"); + Assert.notEmpty(objectsToDelete, "The objects to delete are empty"); + Assert.notEmpty(objectsToDelete, "The objects to delete are empty"); + Assert.notNull(entityWriter, "EntityWriter is null"); Batch batch = QueryBuilder.batch(); - for (T entity : entities) { + for (T entity : objectsToDelete) { batch.add(createDeleteQuery(tableName, entity, options, entityWriter)); } @@ -1058,13 +1088,8 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation } @Override - public void deleteAll(Class clazz) { - - if (!mappingContext.contains(clazz)) { - throw new IllegalArgumentException(String.format("unknown persistent entity class [%s]", clazz.getName())); - } - - truncate(mappingContext.getPersistentEntity(clazz).getTableName()); + public void deleteAll(Class entityClass) { + truncate(getPersistentEntity(entityClass).getTableName()); } @Override @@ -1073,28 +1098,25 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation } @Override - public Cancellable selectOneAsynchronously(String cql, Class type, QueryForObjectListener listener) { - return selectOneAsynchronously(cql, type, listener, null); + public Cancellable selectOneAsynchronously(String cql, Class entityClass, QueryForObjectListener listener) { + return selectOneAsynchronously(cql, entityClass, listener, null); } @Override - public Cancellable selectOneAsynchronously(Select select, Class type, QueryForObjectListener listener, + public Cancellable selectOneAsynchronously(Select select, Class entityClass, QueryForObjectListener listener, QueryOptions options) { - return doSelectOneAsync(select, type, listener, options); + return doSelectOneAsync(select, entityClass, listener, options); } @Override - public Cancellable selectOneAsynchronously(String cql, Class type, QueryForObjectListener listener, + public Cancellable selectOneAsynchronously(String cql, Class entityClass, QueryForObjectListener listener, QueryOptions options) { - return doSelectOneAsync(cql, type, listener, options); + return doSelectOneAsync(cql, entityClass, listener, options); } private CassandraPersistentEntity getPersistentEntity(Class entityClass) { - if (entityClass == null) { - throw new InvalidDataAccessApiUsageException( - "No class parameter provided, entity collection can't be determined!"); - } + Assert.notNull(entityClass, "EntityClass must not be null"); CassandraPersistentEntity entity = mappingContext.getPersistentEntity(entityClass); @@ -1106,9 +1128,11 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation return entity; } - protected Cancellable doSelectOneAsync(final Object query, final Class type, + protected Cancellable doSelectOneAsync(final Object query, final Class entityClass, final QueryForObjectListener listener, QueryOptions options) { + Assert.notNull(entityClass, "EntityClass must not be null"); + AsynchronousQueryListener aql = new AsynchronousQueryListener() { @Override @@ -1118,7 +1142,7 @@ public class CassandraTemplate extends CqlTemplate implements CassandraOperation Iterator iterator = rs.iterator(); if (iterator.hasNext()) { Row row = iterator.next(); - T result = new CassandraConverterRowCallback(cassandraConverter, type).doWith(row); + T result = new CassandraConverterRowCallback(cassandraConverter, entityClass).doWith(row); if (iterator.hasNext()) { throw new DuplicateKeyException("found two or more results in query " + query); }