Example usage for javax.persistence.metamodel Attribute getJavaMember

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

Introduction

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

Prototype

java.lang.reflect.Member getJavaMember();

Source Link

Document

Return the java.lang.reflect.Member for the represented attribute.

Usage

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

/**
 * On translation./* www  .j a va2  s  .  com*/
 * 
 * @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

/**
 * Wraps entity attributes into byte[] and return instance of attribute
 * wrapper./*from   ww w . ja  va 2 s.co  m*/
 * 
 * @param entityMetadata
 * @param entity
 * @return
 */
private AttributeWrapper wrap(EntityMetadata entityMetadata, Object entity) {
    MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata()
            .getMetamodel(entityMetadata.getPersistenceUnit());

    EntityType entityType = metaModel.entity(entityMetadata.getEntityClazz());
    Set<Attribute> attributes = entityType.getAttributes();

    // attributes can be null??? i guess NO
    AttributeWrapper wrapper = new AttributeWrapper(attributes.size());

    List<String> relationNames = entityMetadata.getRelationNames();

    // PropertyAccessorHelper.get(entity,
    for (Attribute attr : attributes) {
        if (/* !entityMetadata.getIdAttribute().equals(attr) && */!attr.isAssociation()) {
            if (metaModel.isEmbeddable(((AbstractAttribute) attr).getBindableJavaType())) {
                EmbeddableType embeddableAttribute = metaModel
                        .embeddable(((AbstractAttribute) attr).getBindableJavaType());

                Object embeddedObject = PropertyAccessorHelper.getObject(entity, (Field) attr.getJavaMember());

                Set<Attribute> embeddedAttributes = embeddableAttribute.getAttributes();

                for (Attribute attrib : embeddedAttributes) {
                    addToWrapper(entityMetadata, wrapper, embeddedObject, attrib, attr);
                }

            } else {
                addToWrapper(entityMetadata, wrapper, entity, attr);
            }
        } else if (attributes.size() == 1) // means it is only a key! weird
                                           // but possible negative
                                           // scenario
        {
            byte[] value = PropertyAccessorHelper.get(entity, (Field) attr.getJavaMember());
            byte[] name;
            name = getEncodedBytes(((AbstractAttribute) attr).getJPAColumnName());

            // add column name as key and value as value
            wrapper.addColumn(name, value);

        }
    }

    return wrapper;
}

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

/**
 * Creates the typefor embeddables./*from  w w  w  . j av a 2  s. 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.schemamanager.CassandraSchemaManager.java

/**
 * Handle element collection attribute.//w ww . ja v  a  2  s  . c o  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);

}