use of com.datastax.driver.mapping.meta.EntityTypeMetadata in project cassandra-driver-mapping by valchkou.
the class EntityTypeParser method getEntityMetadata.
/**
* Returns List<FieldData> - all the fields which can be persisted for the
* given Entity type the field to be persisted must have getter/setter and
* not be annotated as @Transient
*/
public static <T> EntityTypeMetadata getEntityMetadata(Class<T> clazz) {
EntityTypeMetadata edata = entityData.get(clazz);
if (edata == null) {
edata = parseEntityClass(clazz);
entityData.put(clazz, edata);
}
return edata;
}
use of com.datastax.driver.mapping.meta.EntityTypeMetadata in project cassandra-driver-mapping by valchkou.
the class EntityTypeParser method parseEntityLevelMetadata.
/**
* Parses class level annotations and initializes EntityMetadata object for
* given entity class
*
* @param clazz
* @return EntityMetadata
*/
private static <T> EntityTypeMetadata parseEntityLevelMetadata(Class<T> clazz) {
EntityTypeMetadata result = null;
Annotation annotation = clazz.getAnnotation(Table.class);
if (annotation instanceof Table) {
Table tableAntn = (Table) annotation;
String tableName = tableAntn.name();
if (tableName != null && tableName.length() > 0) {
result = new EntityTypeMetadata(clazz, tableName);
} else {
result = new EntityTypeMetadata(clazz);
}
Index[] indexes = tableAntn.indexes();
if (indexes != null && indexes.length > 0) {
for (Index index : indexes) {
result.addindex(index.name(), index.columnList());
}
}
} else {
result = new EntityTypeMetadata(clazz);
}
// parse properties
annotation = clazz.getAnnotation(TableProperties.class);
if (annotation instanceof TableProperties) {
TableProperty[] props = ((TableProperties) annotation).values();
for (TableProperty prop : props) {
result.addProperty(prop.value());
}
}
// parse ttl
annotation = clazz.getAnnotation(Ttl.class);
if (annotation instanceof Ttl) {
result.setTtl(((Ttl) annotation).value());
}
return result;
}
use of com.datastax.driver.mapping.meta.EntityTypeMetadata in project cassandra-driver-mapping by valchkou.
the class MappingBuilder method prepareReplaceAt.
public static BoundStatement prepareReplaceAt(Object id, Class<?> clazz, String propertyName, Object item, int idx, WriteOptions options, String keyspace, Session session) {
EntityTypeMetadata emeta = EntityTypeParser.getEntityMetadata(clazz);
EntityFieldMetaData fmeta = emeta.getFieldMetadata(propertyName);
Update update = QueryBuilder.update(keyspace, emeta.getTableName());
if (fmeta.getType() == List.class) {
update.with(QueryBuilder.setIdx(fmeta.getColumnName(), idx, item));
}
applyOptions(options, update, null);
return prepareUpdate(id, emeta, update, session);
}
use of com.datastax.driver.mapping.meta.EntityTypeMetadata in project cassandra-driver-mapping by valchkou.
the class MappingBuilder method preparePrependItemToList.
public static BoundStatement preparePrependItemToList(Object id, Class<?> clazz, String propertyName, Object item, WriteOptions options, String keyspace, Session session) {
EntityTypeMetadata emeta = EntityTypeParser.getEntityMetadata(clazz);
EntityFieldMetaData fmeta = emeta.getFieldMetadata(propertyName);
Update update = QueryBuilder.update(keyspace, emeta.getTableName());
if (item instanceof List<?> && fmeta.getType() == List.class) {
List<?> list = (List<?>) item;
if (list.size() == 0)
return null;
update.with(QueryBuilder.prependAll(fmeta.getColumnName(), list));
} else if (fmeta.getType() == List.class) {
update.with(QueryBuilder.prepend(fmeta.getColumnName(), item));
}
applyOptions(options, update, null);
return prepareUpdate(id, emeta, update, session);
}
use of com.datastax.driver.mapping.meta.EntityTypeMetadata in project cassandra-driver-mapping by valchkou.
the class MappingBuilder method prepareAppendItemToCollection.
public static BoundStatement prepareAppendItemToCollection(Object id, Class<?> clazz, String propertyName, Object item, WriteOptions options, String keyspace, Session session) {
EntityTypeMetadata emeta = EntityTypeParser.getEntityMetadata(clazz);
EntityFieldMetaData fmeta = emeta.getFieldMetadata(propertyName);
Update update = QueryBuilder.update(keyspace, emeta.getTableName());
if (item instanceof Set<?> && fmeta.getType() == Set.class) {
Set<?> set = (Set<?>) item;
if (set.size() == 0)
return null;
update.with(QueryBuilder.addAll(fmeta.getColumnName(), set));
} else if (item instanceof List<?> && fmeta.getType() == List.class) {
List<?> list = (List<?>) item;
if (list.size() == 0)
return null;
update.with(QueryBuilder.appendAll(fmeta.getColumnName(), list));
} else if (item instanceof Map<?, ?>) {
Map<?, ?> map = (Map<?, ?>) item;
if (map.size() == 0)
return null;
update.with(QueryBuilder.putAll(fmeta.getColumnName(), map));
} else if (fmeta.getType() == Set.class) {
update.with(QueryBuilder.add(fmeta.getColumnName(), item));
} else if (fmeta.getType() == List.class) {
update.with(QueryBuilder.append(fmeta.getColumnName(), item));
}
applyOptions(options, update, null);
return prepareUpdate(id, emeta, update, session);
}
Aggregations