Example usage for javax.persistence.metamodel Attribute getJavaType

List of usage examples for javax.persistence.metamodel Attribute getJavaType

Introduction

In this page you can find the example usage for javax.persistence.metamodel Attribute getJavaType.

Prototype

Class<Y> getJavaType();

Source Link

Document

Return the Java type of the represented attribute.

Usage

From source file:com.impetus.client.cassandra.CassandraClientBase.java

/**
 * Return update query string for given entity intended for counter column
 * family.//from w  w w . j  a v  a  2  s .  com
 * 
 * @param entityMetadata
 *            the entity metadata
 * @param entity
 *            the entity
 * @param cassandra_client
 *            the cassandra_client
 * @param rlHolders
 *            the rl holders
 * @return the list
 */
protected List<String> createUpdateQueryForCounter(EntityMetadata entityMetadata, Object entity,
        Cassandra.Client cassandra_client, List<RelationHolder> rlHolders) {
    Map<String, String> builders = new HashMap<String, String>();

    CQLTranslator translator = new CQLTranslator();

    Object rowId = PropertyAccessorHelper.getId(entity, entityMetadata);
    MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata()
            .getMetamodel(entityMetadata.getPersistenceUnit());

    EntityType entityType = metaModel.entity(entityMetadata.getEntityClazz());

    Set<Attribute> attributes = entityType.getAttributes();

    for (Attribute attrib : attributes) {
        if (!entityMetadata.getIdAttribute().getName().equals(attrib.getName())
                && !metaModel.isEmbeddable(attrib.getJavaType()) && !attrib.isAssociation()) {
            String tableName = ((AbstractAttribute) attrib).getTableName() != null
                    ? ((AbstractAttribute) attrib).getTableName()
                    : entityMetadata.getTableName();

            String queryString = builders.get(tableName);
            StringBuilder builder;
            if (queryString == null) {
                builder = new StringBuilder();
            } else {
                builder = new StringBuilder(queryString);
            }
            translator.buildSetClauseForCounters(builder, ((AbstractAttribute) attrib).getJPAColumnName(),
                    PropertyAccessorHelper.getObject(entity, attrib.getName()));
            builders.put(tableName, builder.toString());
        }
    }
    for (RelationHolder rl : rlHolders) {
        translator.buildSetClauseForCounters(new StringBuilder(builders.get(entityMetadata.getTableName())),
                rl.getRelationName(), rl.getRelationValue());
    }

    for (String tableName : builders.keySet()) {
        StringBuilder builder = new StringBuilder(builders.get(tableName));

        String update_Query = translator.UPDATE_QUERY;

        update_Query = StringUtils.replace(update_Query, CQLTranslator.COLUMN_FAMILY,
                translator.ensureCase(new StringBuilder(), tableName, false).toString());

        // strip last "," clause.
        builder.delete(builder.lastIndexOf(CQLTranslator.COMMA_STR), builder.length());

        builder.append(CQLTranslator.ADD_WHERE_CLAUSE);
        onWhereClause(entityMetadata, rowId, translator, builder, metaModel, entityMetadata.getIdAttribute());

        // strip last "AND" clause.
        builder.delete(builder.lastIndexOf(CQLTranslator.AND_CLAUSE), builder.length());

        StringBuilder queryBuilder = new StringBuilder(update_Query);
        queryBuilder.append(CQLTranslator.ADD_SET_CLAUSE);
        queryBuilder.append(builder);

        if (log.isDebugEnabled()) {
            log.debug("Returning update query {}.", queryBuilder.toString());
        }

        builders.put(tableName, queryBuilder.toString());
    }
    return new ArrayList(builders.values());
}

From source file:com.impetus.client.cassandra.schemamanager.CassandraSchemaManager.java

/**
 * Handle element collection attribute.//from  w  ww.jav  a  2s .  co  m
 * 
 * @param translator
 *            the translator
 * @param attribute
 *            the attribute
 * @param typeQueryBuilder
 *            the type query builder
 */
private void handleElementCollectionAttribute(CQLTranslator translator, Attribute attribute,
        StringBuilder typeQueryBuilder) {
    String dataType = CassandraValidationClassMapper.getValidationClass(attribute.getJavaType(), true);

    // CQL Type of collection column
    String collectionCqlType = translator.getCQLType(dataType);

    // Collection Column Name
    String collectionColumnName = new String(((AbstractAttribute) attribute).getJPAColumnName());

    // Generic Type list
    StringBuilder genericTypesBuilder = null;
    List<Class<?>> genericClasses = PropertyAccessorHelper.getGenericClasses((Field) attribute.getJavaMember());
    if (!genericClasses.isEmpty()) {
        genericTypesBuilder = new StringBuilder();
        if (MapType.class.getSimpleName().equals(dataType) && genericClasses.size() == 2) {
            genericTypesBuilder.append(Constants.STR_LT);
            if (genericClasses.get(0).getAnnotation(Embeddable.class) != null) {
                String frozenKey = CQLTranslator.FROZEN + Constants.STR_LT + Constants.ESCAPE_QUOTE
                        + genericClasses.get(0).getSimpleName() + Constants.ESCAPE_QUOTE + Constants.STR_GT;
                genericTypesBuilder.append(frozenKey);
            } else {
                String keyDataType = CassandraValidationClassMapper.getValidationClass(genericClasses.get(0),
                        true);
                genericTypesBuilder.append(translator.getCQLType(keyDataType));
            }
            genericTypesBuilder.append(Constants.SPACE_COMMA);
            if (genericClasses.get(1).getAnnotation(Embeddable.class) != null) {
                String frozenKey = CQLTranslator.FROZEN + Constants.STR_LT + Constants.ESCAPE_QUOTE
                        + genericClasses.get(1).getSimpleName() + Constants.ESCAPE_QUOTE + Constants.STR_GT;
                genericTypesBuilder.append(frozenKey);
            } else {
                String keyDataType = CassandraValidationClassMapper.getValidationClass(genericClasses.get(1),
                        true);
                genericTypesBuilder.append(translator.getCQLType(keyDataType));
            }
            genericTypesBuilder.append(Constants.STR_GT);
        } else if ((ListType.class.getSimpleName().equals(dataType)
                || SetType.class.getSimpleName().equals(dataType)) && genericClasses.size() == 1) {
            genericTypesBuilder.append(Constants.STR_LT);
            if (genericClasses.get(0).getAnnotation(Embeddable.class) != null) {
                String frozenKey = CQLTranslator.FROZEN + Constants.STR_LT + Constants.ESCAPE_QUOTE
                        + genericClasses.get(0).getSimpleName() + Constants.ESCAPE_QUOTE + Constants.STR_GT;
                genericTypesBuilder.append(frozenKey);
            } else {
                String keyDataType = CassandraValidationClassMapper.getValidationClass(genericClasses.get(0),
                        true);
                genericTypesBuilder.append(translator.getCQLType(keyDataType));
            }
            genericTypesBuilder.append(Constants.STR_GT);
        } else {
            throw new SchemaGenerationException("Incorrect collection field definition for "
                    + ((AbstractAttribute) attribute).getJPAColumnName()
                    + ". Generic Types must be defined correctly.");
        }
    }

    if (genericTypesBuilder != null) {
        collectionCqlType += genericTypesBuilder.toString();
    }

    translator.appendColumnName(typeQueryBuilder, collectionColumnName, collectionCqlType);
    typeQueryBuilder.append(Constants.SPACE_COMMA);

}

From source file:com.impetus.client.cassandra.schemamanager.CassandraSchemaManager.java

/**
 * Creates the typefor embeddables.//  w  ww  .  j a  va 2s .co  m
 * 
 */
private void createTypeforEmbeddables() {
    if (!createdPuEmbeddables.contains(puMetadata.getPersistenceUnitName())) {
        CQLTranslator translator = new CQLTranslator();

        Map<String, String> embNametoUDTQuery = new HashMap<String, String>();
        Map<String, List<String>> embNametoDependentList = new HashMap<String, List<String>>();

        MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata()
                .getMetamodel(puMetadata.getPersistenceUnitName());

        Iterator iter = metaModel.getEmbeddables().iterator();
        while (iter.hasNext()) {
            List<String> childEmb = new ArrayList<String>();

            String typeQuery = CQLTranslator.CREATE_TYPE;
            EmbeddableType embeddedColumn = (EmbeddableType) iter.next();
            if (!embeddedColumn.getPersistenceType().equals(PersistenceType.EMBEDDABLE)) {
                continue;
            }

            typeQuery = StringUtils.replace(typeQuery, CQLTranslator.TYPE, translator
                    .ensureCase(new StringBuilder(), embeddedColumn.getJavaType().getSimpleName(), false)
                    .toString());

            StringBuilder typeQueryBuilder = new StringBuilder();

            for (Object column : embeddedColumn.getAttributes()) {

                Attribute columnAttribute = (Attribute) column;
                Field f = (Field) columnAttribute.getJavaMember();

                if (columnAttribute.getJavaType().isAnnotationPresent(Embeddable.class)) {
                    // handle embeddable
                    String cqlType = CQLTranslator.FROZEN + Constants.STR_LT + Constants.ESCAPE_QUOTE
                            + columnAttribute.getJavaType().getSimpleName() + Constants.ESCAPE_QUOTE
                            + Constants.STR_GT;
                    translator.appendColumnName(typeQueryBuilder, columnAttribute.getName(), cqlType);
                    typeQueryBuilder.append(Constants.SPACE_COMMA);
                    childEmb.add(columnAttribute.getJavaType().getSimpleName());
                } else if (columnAttribute.isCollection()) {
                    // handle element collection with embeddables
                    handleElementCollectionAttribute(translator, columnAttribute, typeQueryBuilder);
                    if (!MetadataUtils.isBasicElementCollectionField((Field) columnAttribute.getJavaMember())) {
                        childEmb.add(
                                ((AbstractAttribute) columnAttribute).getBindableJavaType().getSimpleName());
                    }

                } else {
                    String cqlType = null;
                    String dataType = CassandraValidationClassMapper.getValidationClass(f.getType(), true);
                    cqlType = translator.getCQLType(dataType);
                    // check for JPA names
                    translator.appendColumnName(typeQueryBuilder,
                            ((AbstractAttribute) columnAttribute).getJPAColumnName(), cqlType);
                    typeQueryBuilder.append(Constants.SPACE_COMMA);

                }

            }
            typeQueryBuilder = replaceColumnsAndStripLastChar(typeQuery, typeQueryBuilder);
            typeQueryBuilder.append(CQLTranslator.CLOSE_BRACKET);
            embNametoUDTQuery.put(embeddedColumn.getJavaType().getSimpleName(), typeQueryBuilder.toString());
            embNametoDependentList.put(embeddedColumn.getJavaType().getSimpleName(), childEmb);
            // run query final

        }
        postProcessEmbedded(embNametoUDTQuery, embNametoDependentList);
        createdPuEmbeddables.add(puMetadata.getPersistenceUnitName());
    }
}

From source file:com.impetus.client.cassandra.query.CassQuery.java

/**
 * Extract composite key.//ww w . j  a v  a 2  s.c  o m
 * 
 * @param metaModel
 *            the meta model
 * @param keyObj
 *            the key obj
 * @param builder
 *            the builder
 * @param translator
 *            the translator
 * @param value
 *            the value
 * @param useInClause
 *            the use in clause
 * @param columnValues
 *            the column values
 * @param field
 *            the field
 * @return true, if successful
 */
private boolean extractCompositeKey(MetamodelImpl metaModel, EmbeddableType keyObj, StringBuilder builder,
        CQLTranslator translator, List<Object> value, boolean useInClause,
        Map<Attribute, List<Object>> columnValues, Field field) {
    Attribute compositeColumn = keyObj.getAttribute(field.getName());
    String jpaColumnName = ((AbstractAttribute) compositeColumn).getJPAColumnName();

    if (useInClause) {
        for (Object embeddedObject : value) {

            Object valueObject = PropertyAccessorHelper.getObject(embeddedObject, field);
            // Checking for composite partition key.
            if (metaModel.isEmbeddable(((AbstractAttribute) compositeColumn).getBindableJavaType())) {
                Set<Attribute> attributes = metaModel
                        .embeddable(((AbstractAttribute) compositeColumn).getBindableJavaType())
                        .getAttributes();

                // Iterating over composite partition key columns.
                for (Attribute nestedAttribute : attributes) {
                    List<Object> valueList = columnValues.get(compositeColumn);

                    if (valueList == null) {
                        valueList = new ArrayList<Object>();
                    }

                    Object obj = PropertyAccessorHelper.getObject(valueObject,
                            (Field) nestedAttribute.getJavaMember());
                    valueList.add(obj);
                    columnValues.put(nestedAttribute, valueList);
                }
            } else {
                List<Object> valueList = columnValues.get(compositeColumn);

                if (valueList == null) {
                    valueList = new ArrayList<Object>();
                }
                valueList.add(valueObject);
                columnValues.put(compositeColumn, valueList);
            }
        }
    } else {
        Object valueObject = PropertyAccessorHelper.getObject(value.isEmpty() ? null : value.get(0), field);

        // Checking for composite partition key.
        if (metaModel.isEmbeddable(((AbstractAttribute) compositeColumn).getBindableJavaType())) {
            Set<Attribute> attributes = metaModel
                    .embeddable(((AbstractAttribute) compositeColumn).getBindableJavaType()).getAttributes();

            // Iterating over composite partition key columns.
            for (Attribute nestedAttribute : attributes) {
                String columnName = ((AbstractAttribute) nestedAttribute).getJPAColumnName();
                Object obj = PropertyAccessorHelper.getObject(valueObject,
                        (Field) nestedAttribute.getJavaMember());
                translator.buildWhereClause(builder, nestedAttribute.getJavaType(), columnName, obj,
                        CQLTranslator.EQ_CLAUSE, false);
            }
            // returning true because builder has AND clause at end.
            return true;
        } else {
            translator.buildWhereClause(builder, field.getType(), jpaColumnName, valueObject,
                    CQLTranslator.EQ_CLAUSE, false);
            // returning true because builder has AND clause at end.
            return true;
        }
    }
    // returning false because builder does not have AND clause at end.
    return false;
}