Example usage for javax.persistence.metamodel EmbeddableType getPersistenceType

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

Introduction

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

Prototype

PersistenceType getPersistenceType();

Source Link

Document

Return the persistence type.

Usage

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

/**
 * Creates the typefor embeddables.//from   w  ww.ja v a  2s . c o 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());
    }
}