Example usage for javax.persistence.metamodel EmbeddableType getAttribute

List of usage examples for javax.persistence.metamodel EmbeddableType getAttribute

Introduction

In this page you can find the example usage for javax.persistence.metamodel EmbeddableType getAttribute.

Prototype

Attribute<? super X, ?> getAttribute(String name);

Source Link

Document

Return the attribute of the managed type that corresponds to the specified name.

Usage

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

/**
 * Append order by clause./*from ww w .j  a v  a2  s  .  co  m*/
 * 
 * @param metaModel
 *            the meta model
 * @param m
 *            the m
 * @param keyObj
 *            the key obj
 * @param builder
 *            the builder
 * @param translator
 *            the translator
 * @return the string builder
 */
private StringBuilder appendOrderByClause(MetamodelImpl metaModel, EntityMetadata m, EmbeddableType keyObj,
        StringBuilder builder, CQLTranslator translator) {
    List<SortOrdering> orders = getKunderaQuery().getOrdering();

    if (orders != null) {
        builder.append(CQLTranslator.SPACE_STRING);
        builder.append(CQLTranslator.SORT_CLAUSE);

        for (SortOrdering order : orders) {
            String orderColumnName = order.getColumnName();

            orderColumnName = orderColumnName.substring(orderColumnName.indexOf(".") + 1,
                    orderColumnName.length());

            String orderByColumnName;

            if (StringUtils.contains(orderColumnName, '.')) {
                String propertyName = orderColumnName.substring(0, orderColumnName.indexOf("."));
                Attribute embeddableAttribute = metaModel.getEntityAttribute(m.getEntityClazz(), propertyName);
                EmbeddableType embeddableType = metaModel
                        .embeddable(((AbstractAttribute) embeddableAttribute).getBindableJavaType());
                orderColumnName = orderColumnName.substring(orderColumnName.indexOf(".") + 1);
                AbstractAttribute attribute = (AbstractAttribute) embeddableType.getAttribute(orderColumnName);
                orderByColumnName = attribute.getJPAColumnName();

            } else {
                Attribute attribute = metaModel.getEntityAttribute(m.getEntityClazz(), orderColumnName);
                orderByColumnName = ((AbstractAttribute) attribute).getJPAColumnName();
            }

            builder = translator.ensureCase(builder, orderByColumnName, false);
            builder.append(CQLTranslator.SPACE_STRING);
            builder.append(order.getOrder());
            builder.append(CQLTranslator.COMMA_STR);
        }

        if (!orders.isEmpty()) {
            builder.deleteCharAt(builder.lastIndexOf(CQLTranslator.COMMA_STR));
        }
    }
    return builder;
}

From source file:com.impetus.client.cassandra.thrift.CQLTranslator.java

/**
 * Translate composite id./*from ww  w. j a  va2 s  .c  o m*/
 * 
 * @param record
 *            the record
 * @param m
 *            the m
 * @param type
 *            the type
 * @param metaModel
 *            the meta model
 * @param builders
 *            the builders
 * @param columnBuilders
 *            the column builders
 * @param externalProperties
 *            the external properties
 * @param kunderaMetadata
 *            the kundera metadata
 * @param tableName
 *            the table name
 * @param attribute
 *            the attribute
 */
private void translateCompositeId(final Object record, final EntityMetadata m, TranslationType type,
        MetamodelImpl metaModel, Map<String, StringBuilder> builders, Map<String, StringBuilder> columnBuilders,
        Map<String, Object> externalProperties, final KunderaMetadata kunderaMetadata, String tableName,
        SingularAttribute attribute) {
    StringBuilder builder = builders.get(tableName);
    StringBuilder columnBuilder = columnBuilders.get(tableName);
    Field field = (Field) attribute.getJavaMember();
    if (metaModel.isEmbeddable(((AbstractAttribute) attribute).getBindableJavaType())) {
        // builder.
        // Means it is a compound key! As other
        // iterate for it's fields to populate it's values in
        // order!
        EmbeddableType compoundKey = metaModel.embeddable(field.getType());
        Object compoundKeyObj = PropertyAccessorHelper.getObject(record, field);
        for (Field compositeColumn : field.getType().getDeclaredFields()) {
            if (!ReflectUtils.isTransientOrStatic(compositeColumn)) {
                attribute = (SingularAttribute) compoundKey.getAttribute(compositeColumn.getName());
                if (metaModel.isEmbeddable(((AbstractAttribute) attribute).getBindableJavaType())) {
                    translateCompositeId(compoundKeyObj, m, type, metaModel, builders, columnBuilders,
                            externalProperties, kunderaMetadata, tableName, attribute);
                } else {
                    onTranslation(type, builder, columnBuilder,
                            ((AbstractAttribute) (compoundKey.getAttribute(compositeColumn.getName())))
                                    .getJPAColumnName(),
                            compoundKeyObj, compositeColumn);
                }
            }
        }
    } else if (!ReflectUtils.isTransientOrStatic(field)) {
        onTranslation(type, builder, columnBuilder,
                CassandraUtilities.getIdColumnName(kunderaMetadata, m, externalProperties, true), record,
                field);
    }
}

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

/**
 * Builds where Clause.// w w w.  j  a va 2  s  .c o  m
 * 
 * @param kunderaQuery
 *            the kundera query
 * @param metadata
 *            the metadata
 * @param metaModel
 *            the meta model
 * @param translator
 *            the translator
 * @param builder
 *            the builder
 */
private void buildWhereClause(KunderaQuery kunderaQuery, EntityMetadata metadata, MetamodelImpl metaModel,
        CQLTranslator translator, StringBuilder builder) {
    for (Object clause : kunderaQuery.getFilterClauseQueue()) {
        FilterClause filterClause = (FilterClause) clause;
        Field f = (Field) metaModel.entity(metadata.getEntityClazz())
                .getAttribute(metadata.getFieldName(filterClause.getProperty())).getJavaMember();
        String jpaColumnName = getColumnName(metadata, filterClause.getProperty());

        if (metaModel.isEmbeddable(metadata.getIdAttribute().getBindableJavaType())) {
            Field[] fields = metadata.getIdAttribute().getBindableJavaType().getDeclaredFields();
            EmbeddableType compoundKey = metaModel.embeddable(metadata.getIdAttribute().getBindableJavaType());
            for (Field field : fields) {
                if (field != null && !Modifier.isStatic(field.getModifiers())
                        && !Modifier.isTransient(field.getModifiers())
                        && !field.isAnnotationPresent(Transient.class)) {
                    Attribute attribute = compoundKey.getAttribute(field.getName());
                    String columnName = ((AbstractAttribute) attribute).getJPAColumnName();
                    Object value = PropertyAccessorHelper.getObject(filterClause.getValue().get(0), field);
                    // TODO
                    translator.buildWhereClause(builder, field.getType(), columnName, value,
                            filterClause.getCondition(), false);
                }
            }
        } else {
            translator.buildWhereClause(builder, f.getType(), jpaColumnName, filterClause.getValue().get(0),
                    filterClause.getCondition(), false);
        }
    }
    builder.delete(builder.lastIndexOf(CQLTranslator.AND_CLAUSE), builder.length());
}

From source file:com.impetus.client.cassandra.thrift.CQLTranslator.java

/**
 * Builds the embedded value.//from www  .j av  a  2 s .  c o m
 * 
 * @param record
 *            the record
 * @param metaModel
 *            the meta model
 * @param embeddedValueBuilder
 *            the embedded value builder
 * @param attribute
 *            the attribute
 */
private void buildEmbeddedValue(final Object record, MetamodelImpl metaModel,
        StringBuilder embeddedValueBuilder, SingularAttribute attribute) {
    // TODO Auto-generated method stub
    Field field = (Field) attribute.getJavaMember();

    EmbeddableType embeddableKey = metaModel.embeddable(field.getType());
    Object embeddableKeyObj = PropertyAccessorHelper.getObject(record, field);
    if (embeddableKeyObj != null) {
        StringBuilder tempBuilder = new StringBuilder();
        tempBuilder.append(Constants.OPEN_CURLY_BRACKET);

        for (Field embeddableColumn : field.getType().getDeclaredFields()) {
            if (!ReflectUtils.isTransientOrStatic(embeddableColumn)) {
                Attribute subAttribute = (SingularAttribute) embeddableKey
                        .getAttribute(embeddableColumn.getName());
                if (metaModel.isEmbeddable(((AbstractAttribute) subAttribute).getBindableJavaType())) {
                    // construct map; recursive
                    // send attribute
                    buildEmbeddedValue(embeddableKeyObj, metaModel, tempBuilder,
                            (SingularAttribute) subAttribute);
                } else {
                    // append key value
                    appendColumnName(tempBuilder,
                            ((AbstractAttribute) (embeddableKey.getAttribute(embeddableColumn.getName())))
                                    .getJPAColumnName());
                    tempBuilder.append(Constants.COLON);
                    appendColumnValue(tempBuilder, embeddableKeyObj, embeddableColumn);
                }
                tempBuilder.append(Constants.COMMA);
            }

        }
        // strip last char and append '}'
        tempBuilder.deleteCharAt(tempBuilder.length() - 1);
        tempBuilder.append(Constants.CLOSE_CURLY_BRACKET);
        appendColumnName(embeddedValueBuilder, ((AbstractAttribute) attribute).getJPAColumnName());
        embeddedValueBuilder.append(Constants.COLON);
        embeddedValueBuilder.append(tempBuilder);
    } else {
        embeddedValueBuilder.deleteCharAt(embeddedValueBuilder.length() - 1);
    }
}

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

/**
 * Gets the compound key column.//from   w  w w .j  a va  2s .c om
 * 
 * @param metamodel
 *            the metamodel
 * @param keyObj
 *            the key obj
 * @param builder
 *            the builder
 * @param isPresent
 *            the is present
 * @param translator
 *            the translator
 * @param fieldName
 *            the field name
 * @param condition
 *            the condition
 * @param value
 *            the value
 * @param useInClause
 *            the use in clause
 * @return the compound key column
 */
private boolean getCompoundKeyColumn(MetamodelImpl metamodel, EmbeddableType keyObj, StringBuilder builder,
        boolean isPresent, CQLTranslator translator, String fieldName, String condition, List<Object> value,
        boolean useInClause) {
    fieldName = fieldName.substring(fieldName.indexOf(".") + 1);

    // If partition key part age given in query, i.e. restriction on
    // id.compositekey.compositePartitionkey.partitionkeyColumn.
    if (fieldName.indexOf(".") > 0) {
        String compositePartitionkeyName = fieldName.substring(0, fieldName.indexOf("."));
        AbstractAttribute attribute = (AbstractAttribute) keyObj.getAttribute(compositePartitionkeyName);
        fieldName = fieldName.substring(fieldName.indexOf(".") + 1);

        EmbeddableType compositePartitionkey = metamodel.embeddable(attribute.getBindableJavaType());

        attribute = (AbstractAttribute) compositePartitionkey.getAttribute(fieldName);

        String columnName = attribute.getJPAColumnName();

        isPresent = buildWhereClause(builder, isPresent, translator, condition, value, useInClause, attribute,
                columnName, false);
    }
    // if composite partition key object is given in query, i.e. restriction
    // on id.compositekey.compositePartitionkey
    else if (metamodel
            .isEmbeddable(((AbstractAttribute) keyObj.getAttribute(fieldName)).getBindableJavaType())) {
        AbstractAttribute attribute = (AbstractAttribute) keyObj.getAttribute(fieldName);
        Set<Attribute> attributes = metamodel.embeddable(attribute.getBindableJavaType()).getAttributes();

        if (!useInClause) {
            // Iterating and appending each column of composite partition
            // key in query builder.
            for (Attribute nestedAttribute : attributes) {
                String columnName = ((AbstractAttribute) nestedAttribute).getJPAColumnName();
                Object valueObject = PropertyAccessorHelper.getObject(value.isEmpty() ? null : value.get(0),
                        (Field) nestedAttribute.getJavaMember());
                translator.buildWhereClause(builder, nestedAttribute.getJavaType(), columnName, valueObject,
                        condition, false);
            }
        } else {
            throw new IllegalArgumentException("In clause is not supported on first part of partition key.");
        }
        isPresent = true;
    }
    // if Not a composite partition key,
    // id.compositekey.partitionkey/clusterKey.
    else {
        AbstractAttribute attribute = (AbstractAttribute) keyObj.getAttribute(fieldName);
        String columnName = attribute.getJPAColumnName();
        isPresent = buildWhereClause(builder, isPresent, translator, condition, value, useInClause, attribute,
                columnName, false);
    }
    return isPresent;
}

From source file:com.impetus.client.cassandra.thrift.CQLTranslator.java

/**
 * On translation.//ww  w  .jav  a  2s  .c o m
 * 
 * @param record
 *            the record
 * @param m
 *            the m
 * @param type
 *            the type
 * @param metaModel
 *            the meta model
 * @param entityClazz
 *            the entity clazz
 * @param entityType
 *            the entity type
 * @param builders
 *            the builders
 * @param columnBuilders
 *            the column builders
 * @param externalProperties
 *            the external properties
 * @param kunderaMetadata
 *            the kundera metadata
 */
private void onTranslation(final Object record, final EntityMetadata m, TranslationType type,
        MetamodelImpl metaModel, Class entityClazz, EntityType entityType, Map<String, StringBuilder> builders,
        Map<String, StringBuilder> columnBuilders, Map<String, Object> externalProperties,
        final KunderaMetadata kunderaMetadata) {
    Set<Attribute> attributes = entityType.getAttributes();
    Iterator<Attribute> iterator = attributes.iterator();
    while (iterator.hasNext()) {
        Attribute attribute = iterator.next();

        // Populating table name.
        String tableName = ((AbstractAttribute) attribute).getTableName() != null
                ? ((AbstractAttribute) attribute).getTableName()
                : m.getTableName();

        StringBuilder columnBuilder = columnBuilders.get(tableName);
        if (columnBuilder == null) {
            columnBuilder = new StringBuilder();
            columnBuilders.put(tableName, columnBuilder);
        }

        StringBuilder builder = builders.get(tableName);
        if (builder == null) {
            builder = new StringBuilder();
            builders.put(tableName, builder);
        }
        Field field = (Field) attribute.getJavaMember();
        if (!attribute.equals(m.getIdAttribute()) && !((AbstractAttribute) attribute).getJPAColumnName()
                .equals(((AbstractAttribute) m.getIdAttribute()).getJPAColumnName())) {
            if (metaModel.isEmbeddable(((AbstractAttribute) attribute).getBindableJavaType())) {
                // create embedded entity persisting format
                if (field.isAnnotationPresent(ElementCollection.class)) {
                    // handle embeddable collection
                    // check list, map, set
                    // build embedded value
                    StringBuilder elementCollectionValue = buildElementCollectionValue(field, record, metaModel,
                            attribute);
                    columnBuilder.append(Constants.ESCAPE_QUOTE);
                    columnBuilder.append(((AbstractAttribute) attribute).getJPAColumnName());
                    columnBuilder.append(Constants.ESCAPE_QUOTE);
                    columnBuilder.append(Constants.COMMA);
                    builder.append(elementCollectionValue);
                    builder.append(Constants.COMMA);
                } else {
                    EmbeddableType embeddableKey = metaModel.embeddable(field.getType());
                    Object embeddableKeyObj = PropertyAccessorHelper.getObject(record, field);
                    if (embeddableKeyObj != null) {

                        StringBuilder embeddedValueBuilder = new StringBuilder(Constants.OPEN_CURLY_BRACKET);

                        for (Field embeddableColumn : field.getType().getDeclaredFields()) {
                            if (!ReflectUtils.isTransientOrStatic(embeddableColumn)) {
                                AbstractAttribute subAttribute = (AbstractAttribute) embeddableKey
                                        .getAttribute(embeddableColumn.getName());
                                if (metaModel.isEmbeddable(subAttribute.getBindableJavaType())) {
                                    // construct map; recursive
                                    // send attribute
                                    if (embeddableColumn.isAnnotationPresent(ElementCollection.class)) {
                                        // build element collection value
                                        StringBuilder elementCollectionValue = buildElementCollectionValue(
                                                embeddableColumn, embeddableKeyObj, metaModel,
                                                (Attribute) subAttribute);

                                        appendColumnName(embeddedValueBuilder,
                                                ((AbstractAttribute) (embeddableKey
                                                        .getAttribute(embeddableColumn.getName())))
                                                                .getJPAColumnName());
                                        embeddedValueBuilder.append(Constants.COLON);
                                        embeddedValueBuilder.append(elementCollectionValue);
                                    } else {
                                        buildEmbeddedValue(embeddableKeyObj, metaModel, embeddedValueBuilder,
                                                (SingularAttribute) subAttribute);
                                    }
                                } else {
                                    // append key value
                                    appendColumnName(embeddedValueBuilder,
                                            ((AbstractAttribute) (embeddableKey
                                                    .getAttribute(embeddableColumn.getName())))
                                                            .getJPAColumnName());
                                    embeddedValueBuilder.append(Constants.COLON);
                                    appendColumnValue(embeddedValueBuilder, embeddableKeyObj, embeddableColumn);
                                }
                                embeddedValueBuilder.append(Constants.COMMA);
                            }
                        }
                        // strip last char and append '}'
                        embeddedValueBuilder.deleteCharAt(embeddedValueBuilder.length() - 1);
                        embeddedValueBuilder.append(Constants.CLOSE_CURLY_BRACKET);
                        // add to columnbuilder and builder
                        columnBuilder.append(Constants.ESCAPE_QUOTE);
                        columnBuilder.append(((AbstractAttribute) attribute).getJPAColumnName());
                        columnBuilder.append(Constants.ESCAPE_QUOTE);
                        columnBuilder.append(Constants.COMMA);
                        builder.append(embeddedValueBuilder);
                        builder.append(Constants.COMMA);
                        // end if
                    }
                }
            } else if (!ReflectUtils.isTransientOrStatic(field) && !attribute.isAssociation()) {
                onTranslation(type, builder, columnBuilder, ((AbstractAttribute) attribute).getJPAColumnName(),
                        record, field);
            }
        }
    }

    for (String tableName : columnBuilders.keySet()) {
        translateCompositeId(record, m, type, metaModel, builders, columnBuilders, externalProperties,
                kunderaMetadata, tableName, m.getIdAttribute());
    }

    // on inherited columns.
    onDiscriminatorColumn(builders.get(m.getTableName()), columnBuilders.get(m.getTableName()), entityType);
}

From source file:com.impetus.client.redis.RedisClient.java

/**
 * Unwraps redis results into entity./*www.  j a v  a 2 s . c  o m*/
 * 
 * @param entityMetadata
 *            the entity metadata
 * @param results
 *            the results
 * @param key
 *            the key
 * @return the object
 * @throws InstantiationException
 *             the instantiation exception
 * @throws IllegalAccessException
 *             the illegal access exception
 */
private Object unwrap(EntityMetadata entityMetadata, Map<byte[], byte[]> results, Object key)
        throws InstantiationException, IllegalAccessException {

    MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata()
            .getMetamodel(entityMetadata.getPersistenceUnit());

    List<String> relationNames = entityMetadata.getRelationNames();
    EntityType entityType = metaModel.entity(entityMetadata.getEntityClazz());

    Map<String, Object> relations = new HashMap<String, Object>();
    Object entity = null;

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

    Set<byte[]> columnNames = results.keySet();
    for (byte[] nameInByte : columnNames) {
        if (entity == null) {
            entity = KunderaCoreUtils.createNewInstance(entityMetadata.getEntityClazz());
        }

        String columnName = PropertyAccessorFactory.STRING.fromBytes(String.class, nameInByte);

        byte[] value = results.get(nameInByte);
        String discriminatorColumn = ((AbstractManagedType) entityType).getDiscriminatorColumn();

        if (columnName != null && !columnName.equals(discriminatorColumn)) {
            String fieldName = entityMetadata.getFieldName(columnName);

            if (fieldName != null) {
                Attribute attribute = entityType.getAttribute(fieldName);

                if (relationNames != null && relationNames.contains(columnName)) {
                    Field field = (Field) attribute.getJavaMember();
                    EntityMetadata associationMetadata = KunderaMetadataManager.getEntityMetadata(
                            kunderaMetadata, ((AbstractAttribute) attribute).getBindableJavaType());
                    relations.put(columnName, PropertyAccessorHelper
                            .getObject(associationMetadata.getIdAttribute().getBindableJavaType(), value));
                } else {
                    PropertyAccessorHelper.set(entity, (Field) attribute.getJavaMember(), value);
                }
            } else {
                // means it might be an embeddable field, if not simply omit
                // this field.

                if (StringUtils.contains(columnName, ":")) {
                    StringTokenizer tokenizer = new StringTokenizer(columnName, ":");
                    while (tokenizer.hasMoreTokens()) {
                        String embeddedFieldName = tokenizer.nextToken();
                        String embeddedColumnName = tokenizer.nextToken();

                        Map<String, EmbeddableType> embeddables = metaModel
                                .getEmbeddables(entityMetadata.getEntityClazz());

                        EmbeddableType embeddableAttribute = embeddables.get(embeddedFieldName);

                        Attribute attrib = embeddableAttribute.getAttribute(embeddedColumnName);

                        Object embeddedObject = PropertyAccessorHelper.getObject(entity,
                                (Field) entityType.getAttribute(embeddedFieldName).getJavaMember());

                        if (embeddedObject == null) {
                            embeddedObject = KunderaCoreUtils.createNewInstance(
                                    ((AbstractAttribute) entityType.getAttribute(embeddedFieldName))
                                            .getBindableJavaType());

                            PropertyAccessorHelper.set(entity,
                                    (Field) entityType.getAttribute(embeddedFieldName).getJavaMember(),
                                    embeddedObject);
                        }

                        PropertyAccessorHelper.set(embeddedObject, (Field) attrib.getJavaMember(), value);
                        // PropertyAccessorHelper.

                    }
                }
                // It might be a case of embeddable attribute.

            }
        }

    }

    if (entity != null) {
        Class javaType = entityMetadata.getIdAttribute().getBindableJavaType();

        if (!metaModel.isEmbeddable(entityMetadata.getIdAttribute().getBindableJavaType())
                && key.getClass().isAssignableFrom(String.class) && !key.getClass().equals(javaType)) {
            key = PropertyAccessorFactory.getPropertyAccessor(javaType).fromString(javaType, key.toString());
        }
        //            PropertyAccessorHelper.set(entity, (Field) entityMetadata.getIdAttribute().getJavaMember(), key);
    }
    if (!relations.isEmpty()) {
        return new EnhanceEntity(entity, key, relations);
    }

    return entity;
}

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

/**
 * On where clause./*from   w ww  .  j  av  a 2 s. c  o  m*/
 * 
 * @param metadata
 *            the metadata
 * @param key
 *            the compound key object
 * @param translator
 *            the translator
 * @param queryBuilder
 *            the query builder
 * @param metaModel
 *            the meta model
 * @param attribute
 *            the attribute
 */
protected void onWhereClause(EntityMetadata metadata, Object key, CQLTranslator translator,
        StringBuilder queryBuilder, MetamodelImpl metaModel, SingularAttribute attribute) {
    // SingularAttribute idAttribute = metadata.getIdAttribute();
    if (metaModel.isEmbeddable(attribute.getBindableJavaType())) {
        Field[] fields = attribute.getBindableJavaType().getDeclaredFields();
        EmbeddableType compoundKey = metaModel.embeddable(attribute.getBindableJavaType());

        for (Field field : fields) {
            if (field != null && !Modifier.isStatic(field.getModifiers())
                    && !Modifier.isTransient(field.getModifiers())
                    && !field.isAnnotationPresent(Transient.class)) {
                attribute = (SingularAttribute) compoundKey.getAttribute(field.getName());
                Object valueObject = PropertyAccessorHelper.getObject(key, field);
                if (metaModel.isEmbeddable(((AbstractAttribute) attribute).getBindableJavaType())) {
                    onWhereClause(metadata, valueObject, translator, queryBuilder, metaModel, attribute);
                } else {
                    String columnName = ((AbstractAttribute) attribute).getJPAColumnName();
                    translator.buildWhereClause(queryBuilder, field.getType(), columnName, valueObject,
                            CQLTranslator.EQ_CLAUSE, false);
                }
            }
        }
    } else {
        translator.buildWhereClause(queryBuilder, ((AbstractAttribute) attribute).getBindableJavaType(),
                CassandraUtilities.getIdColumnName(kunderaMetadata, metadata, getExternalProperties(),
                        isCql3Enabled(metadata)),
                key, translator.EQ_CLAUSE, false);
    }
}

From source file:com.impetus.client.cassandra.thrift.CQLTranslator.java

/**
 * Builds the element collection value.//ww  w . j  a v  a 2 s  . c  om
 * 
 * @param field
 *            the field
 * @param record
 *            the record
 * @param metaModel
 *            the meta model
 * @param attribute
 *            the attribute
 * @return the string builder
 */
private StringBuilder buildElementCollectionValue(Field field, Object record, MetamodelImpl metaModel,
        Attribute attribute) {
    StringBuilder elementCollectionValueBuilder = new StringBuilder();
    EmbeddableType embeddableKey = metaModel.embeddable(((AbstractAttribute) attribute).getBindableJavaType());
    ((AbstractAttribute) attribute).getJavaMember();
    Object value = PropertyAccessorHelper.getObject(record, field);
    boolean isPresent = false;
    if (Collection.class.isAssignableFrom(field.getType())) {

        if (value instanceof Collection) {
            Collection collection = ((Collection) value);
            isPresent = true;
            if (List.class.isAssignableFrom(field.getType())) {
                elementCollectionValueBuilder.append(Constants.OPEN_SQUARE_BRACKET);
            }
            if (Set.class.isAssignableFrom(field.getType())) {
                elementCollectionValueBuilder.append(Constants.OPEN_CURLY_BRACKET);
            }
            for (Object o : collection) {
                // Allowing null values.
                // build embedded value
                if (o != null) {

                    StringBuilder embeddedValueBuilder = new StringBuilder(Constants.OPEN_CURLY_BRACKET);

                    for (Field embeddableColumn : ((AbstractAttribute) attribute).getBindableJavaType()
                            .getDeclaredFields()) {
                        if (!ReflectUtils.isTransientOrStatic(embeddableColumn)) {
                            AbstractAttribute subAttribute = (AbstractAttribute) embeddableKey
                                    .getAttribute(embeddableColumn.getName());
                            if (metaModel.isEmbeddable(subAttribute.getBindableJavaType())) {
                                // construct map; recursive
                                // send attribute
                                if (embeddableColumn.getType().isAnnotationPresent(ElementCollection.class)) {
                                    // build element collection value
                                    StringBuilder elementCollectionValue = buildElementCollectionValue(
                                            embeddableColumn, o, metaModel, (Attribute) subAttribute);
                                    appendColumnName(embeddedValueBuilder,
                                            ((AbstractAttribute) (embeddableKey
                                                    .getAttribute(embeddableColumn.getName())))
                                                            .getJPAColumnName());
                                    embeddedValueBuilder.append(Constants.COLON);
                                    embeddedValueBuilder.append(elementCollectionValue);
                                } else {
                                    buildEmbeddedValue(o, metaModel, embeddedValueBuilder,
                                            (SingularAttribute) subAttribute);
                                }
                            } else {
                                // append key value
                                appendColumnName(embeddedValueBuilder, ((AbstractAttribute) (embeddableKey
                                        .getAttribute(embeddableColumn.getName()))).getJPAColumnName());
                                embeddedValueBuilder.append(Constants.COLON);
                                appendColumnValue(embeddedValueBuilder, o, embeddableColumn);
                            }
                            embeddedValueBuilder.append(Constants.COMMA);
                        }
                    }
                    // strip last char and append '}'
                    embeddedValueBuilder.deleteCharAt(embeddedValueBuilder.length() - 1);
                    embeddedValueBuilder.append(Constants.CLOSE_CURLY_BRACKET);
                    // add to columnbuilder and builder
                    elementCollectionValueBuilder.append(embeddedValueBuilder);
                    // end if
                }

                elementCollectionValueBuilder.append(Constants.COMMA);
            }
            if (!collection.isEmpty()) {
                elementCollectionValueBuilder.deleteCharAt(elementCollectionValueBuilder.length() - 1);
            }
            if (List.class.isAssignableFrom(field.getType())) {
                elementCollectionValueBuilder.append(Constants.CLOSE_SQUARE_BRACKET);
            }
            if (Set.class.isAssignableFrom(field.getType())) {
                elementCollectionValueBuilder.append(Constants.CLOSE_CURLY_BRACKET);
            }
            return elementCollectionValueBuilder;
        }
        return null;

    }

    else if (Map.class.isAssignableFrom(field.getType())) {
        if (value instanceof Map) {
            Map map = ((Map) value);
            isPresent = true;
            elementCollectionValueBuilder.append(Constants.OPEN_CURLY_BRACKET);
            for (Object mapKey : map.keySet()) {
                Object mapValue = map.get(mapKey);
                // Allowing null keys.
                // key is basic type.. no support for embeddable keys
                appendValue(elementCollectionValueBuilder, mapKey != null ? mapKey.getClass() : null, mapKey,
                        false);
                elementCollectionValueBuilder.append(Constants.COLON);
                // Allowing null values.
                if (mapValue != null) {

                    StringBuilder embeddedValueBuilder = new StringBuilder(Constants.OPEN_CURLY_BRACKET);

                    for (Field embeddableColumn : ((AbstractAttribute) attribute).getBindableJavaType()
                            .getDeclaredFields()) {
                        if (!ReflectUtils.isTransientOrStatic(embeddableColumn)) {
                            AbstractAttribute subAttribute = (AbstractAttribute) embeddableKey
                                    .getAttribute(embeddableColumn.getName());
                            if (metaModel.isEmbeddable(subAttribute.getBindableJavaType())) {
                                // construct map; recursive
                                // send attribute
                                if (embeddableColumn.getType().isAnnotationPresent(ElementCollection.class)) {
                                    // build element collection value
                                    StringBuilder elementCollectionValue = buildElementCollectionValue(
                                            embeddableColumn, mapValue, metaModel, (Attribute) subAttribute);
                                    appendColumnName(embeddedValueBuilder,
                                            ((AbstractAttribute) (embeddableKey
                                                    .getAttribute(embeddableColumn.getName())))
                                                            .getJPAColumnName());
                                    embeddedValueBuilder.append(Constants.COLON);
                                    embeddedValueBuilder.append(elementCollectionValue);
                                } else {
                                    buildEmbeddedValue(mapValue, metaModel, embeddedValueBuilder,
                                            (SingularAttribute) subAttribute);
                                }
                            } else {
                                // append key value
                                appendColumnName(embeddedValueBuilder, ((AbstractAttribute) (embeddableKey
                                        .getAttribute(embeddableColumn.getName()))).getJPAColumnName());
                                embeddedValueBuilder.append(Constants.COLON);
                                appendColumnValue(embeddedValueBuilder, mapValue, embeddableColumn);

                            }
                            embeddedValueBuilder.append(Constants.COMMA);
                        }

                    }
                    // strip last char and append '}'
                    embeddedValueBuilder.deleteCharAt(embeddedValueBuilder.length() - 1);
                    embeddedValueBuilder.append(Constants.CLOSE_CURLY_BRACKET);
                    // add to columnbuilder and builder
                    elementCollectionValueBuilder.append(embeddedValueBuilder);
                    // end if
                }
                elementCollectionValueBuilder.append(Constants.COMMA);
            }
            if (!map.isEmpty()) {
                elementCollectionValueBuilder.deleteCharAt(elementCollectionValueBuilder.length() - 1);
            }

            elementCollectionValueBuilder.append(Constants.CLOSE_CURLY_BRACKET);
            return elementCollectionValueBuilder;
        }
        return null;
    }
    return null;
}

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

/**
 * On composite columns./*from   w  ww. ja  v a  2 s . co  m*/
 * 
 * @param translator
 *            the translator
 * @param compositeColumns
 *            the composite columns
 * @param queryBuilder
 *            the query builder
 * @param columns
 *            the columns
 * @param isCounterColumnFamily
 *            the is counter column family
 */
private void onCompositeColumns(CQLTranslator translator, List<ColumnInfo> compositeColumns,
        StringBuilder queryBuilder, List<ColumnInfo> columns, boolean isCounterColumnFamily) {
    MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata()
            .getMetamodel(puMetadata.getPersistenceUnitName());
    for (ColumnInfo colInfo : compositeColumns) {
        if (columns == null || (columns != null && !columns.contains(colInfo))) {
            String cqlType = null;
            if (isCounterColumnFamily) {
                cqlType = "counter";
                translator.appendColumnName(queryBuilder, colInfo.getColumnName(), cqlType);
                queryBuilder.append(Constants.SPACE_COMMA);
            }
            // check for composite partition keys #734
            else if (colInfo.getType().isAnnotationPresent(Embeddable.class)) {
                EmbeddableType embeddedObject = (EmbeddableType) metaModel.embeddable(colInfo.getType());
                for (Field embeddedColumn : colInfo.getType().getDeclaredFields()) {
                    if (!ReflectUtils.isTransientOrStatic(embeddedColumn)) {
                        validateAndAppendColumnName(translator, queryBuilder,
                                ((AbstractAttribute) embeddedObject.getAttribute(embeddedColumn.getName()))
                                        .getJPAColumnName(),
                                embeddedColumn.getType());
                    }
                }

            } else {
                validateAndAppendColumnName(translator, queryBuilder, colInfo.getColumnName(),
                        colInfo.getType());
            }

        }
    }
}