Search in sources :

Example 1 with EntityTypeMetadata

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;
}
Also used : EntityTypeMetadata(com.datastax.driver.mapping.meta.EntityTypeMetadata)

Example 2 with EntityTypeMetadata

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;
}
Also used : EntityTypeMetadata(com.datastax.driver.mapping.meta.EntityTypeMetadata) Table(javax.persistence.Table) Index(javax.persistence.Index) TableProperties(com.datastax.driver.mapping.annotation.TableProperties) Ttl(com.datastax.driver.mapping.annotation.Ttl) Annotation(java.lang.annotation.Annotation) TableProperty(com.datastax.driver.mapping.annotation.TableProperty)

Example 3 with EntityTypeMetadata

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);
}
Also used : EntityFieldMetaData(com.datastax.driver.mapping.meta.EntityFieldMetaData) EntityTypeMetadata(com.datastax.driver.mapping.meta.EntityTypeMetadata)

Example 4 with EntityTypeMetadata

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);
}
Also used : EntityFieldMetaData(com.datastax.driver.mapping.meta.EntityFieldMetaData) EntityTypeMetadata(com.datastax.driver.mapping.meta.EntityTypeMetadata)

Example 5 with EntityTypeMetadata

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);
}
Also used : EntityFieldMetaData(com.datastax.driver.mapping.meta.EntityFieldMetaData) EntityTypeMetadata(com.datastax.driver.mapping.meta.EntityTypeMetadata)

Aggregations

EntityTypeMetadata (com.datastax.driver.mapping.meta.EntityTypeMetadata)40 Test (org.junit.Test)18 EntityFieldMetaData (com.datastax.driver.mapping.meta.EntityFieldMetaData)13 TableMetadata (com.datastax.driver.core.TableMetadata)8 ColumnMetadata (com.datastax.driver.core.ColumnMetadata)6 PrimaryKeyMetadata (com.datastax.driver.mapping.meta.PrimaryKeyMetadata)4 RegularStatement (com.datastax.driver.core.RegularStatement)3 EntityWithCompositeKey (com.datastax.driver.mapping.entity.EntityWithCompositeKey)2 SimpleKey (com.datastax.driver.mapping.entity.SimpleKey)2 ExecutionException (java.util.concurrent.ExecutionException)2 BoundStatement (com.datastax.driver.core.BoundStatement)1 Cluster (com.datastax.driver.core.Cluster)1 KeyspaceMetadata (com.datastax.driver.core.KeyspaceMetadata)1 PreparedStatement (com.datastax.driver.core.PreparedStatement)1 ResultSet (com.datastax.driver.core.ResultSet)1 Row (com.datastax.driver.core.Row)1 Statement (com.datastax.driver.core.Statement)1 BuiltStatement (com.datastax.driver.core.querybuilder.BuiltStatement)1 TableProperties (com.datastax.driver.mapping.annotation.TableProperties)1 TableProperty (com.datastax.driver.mapping.annotation.TableProperty)1