Example usage for javax.persistence.metamodel EmbeddableType getAttributes

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

Introduction

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

Prototype

Set<Attribute<? super X, ?>> getAttributes();

Source Link

Document

Return the attributes of the managed type.

Usage

From source file:com.impetus.kundera.utils.KunderaCoreUtils.java

/**
 * recursively populate all the fields present in partition key
 * //from   w w  w .j  a  v a  2 s .  com
 * @param embeddedAttributes
 * @param metaModel
 * @param embeddedIdFields
 */
private static void populateEmbeddedIdFields(Set<Attribute> embeddedAttributes, MetamodelImpl metaModel,
        Set<String> embeddedIdFields) {
    for (Attribute attribute : embeddedAttributes) {
        if (!ReflectUtils.isTransientOrStatic((Field) attribute.getJavaMember())) {
            if (metaModel.isEmbeddable(attribute.getJavaType())) {
                EmbeddableType embeddable = metaModel.embeddable(attribute.getJavaType());
                populateEmbeddedIdFieldsUtil(embeddable.getAttributes(), metaModel, embeddedIdFields);
            }
        }
    }
}

From source file:com.impetus.kundera.utils.KunderaCoreUtils.java

private static void populateEmbeddedIdFieldsUtil(Set<Attribute> embeddedAttributes, MetamodelImpl metaModel,
        Set<String> embeddedIdFields) {
    for (Attribute attribute : embeddedAttributes) {
        if (!ReflectUtils.isTransientOrStatic((Field) attribute.getJavaMember())) {
            if (metaModel.isEmbeddable(attribute.getJavaType())) {
                EmbeddableType embeddable = metaModel.embeddable(attribute.getJavaType());
                populateEmbeddedIdFieldsUtil(embeddable.getAttributes(), metaModel, embeddedIdFields);
            } else {
                String columnName = ((AbstractAttribute) attribute).getJPAColumnName();
                embeddedIdFields.add(columnName);
            }/*from   www .  j a v a2  s  . c om*/
        }
    }
}

From source file:com.impetus.kundera.metadata.MetadataUtils.java

/**
 * Creates the columns field map.//from www .j  a va  2s  .c  o  m
 * 
 * @param m
 *            the m
 * @param superColumn
 *            the super column
 * @return the map
 */
public static Map<String, Field> createColumnsFieldMap(EntityMetadata m, EmbeddableType superColumn) {
    Map<String, Field> columnNameToFieldMap = new HashMap<String, Field>();

    Set<Attribute> attributes = superColumn.getAttributes();
    for (Attribute column : attributes) {
        columnNameToFieldMap.put(((AbstractAttribute) column).getJPAColumnName(),
                (Field) column.getJavaMember());
    }
    return columnNameToFieldMap;
}

From source file:com.impetus.kundera.metadata.MetadataUtils.java

private static void getAttributeOfEmbedddable(Map<String, Field> columnNameToFieldMap, Metamodel metaModel,
        Attribute attribute) {//from w  ww . j  ava2 s  .  c  o  m
    EmbeddableType embeddable = metaModel.embeddable(((AbstractAttribute) attribute).getBindableJavaType());

    Iterator<Attribute> embeddableIter = embeddable.getAttributes().iterator();
    while (embeddableIter.hasNext()) {
        Attribute embedAttrib = embeddableIter.next();

        // Reason is to avoid in case embeddable attribute within
        // embeddable.
        if (!((MetamodelImpl) metaModel).isEmbeddable(embedAttrib.getJavaType())) {
            columnNameToFieldMap.put(((AbstractAttribute) embedAttrib).getJPAColumnName(),
                    (Field) embedAttrib.getJavaMember());
        } else {
            getAttributeOfEmbedddable(columnNameToFieldMap, metaModel, embedAttrib);
        }
    }
}

From source file:com.impetus.kundera.metadata.MetadataUtils.java

/**
 * Gets the enclosing document name./* ww  w . j a va 2  s.com*/
 * 
 * @param m
 *            the m
 * @param criteria
 *            Input criteria
 * @param viaColumnName
 *            true if <code>criteria</code> is column Name, false if
 *            <code>criteria</code> is column field name
 * @return the enclosing document name
 */
public static String getEnclosingEmbeddedFieldName(EntityMetadata m, String criteria, boolean viaColumnName,
        final KunderaMetadata kunderaMetadata) {
    String enclosingEmbeddedFieldName = null;

    StringTokenizer strToken = new StringTokenizer(criteria, ".");
    String embeddableAttributeName = null;
    String embeddedFieldName = null;
    String nestedEmbeddedFieldName = null;

    if (strToken.countTokens() > 0) {
        embeddableAttributeName = strToken.nextToken();
    }
    if (strToken.countTokens() > 0) {
        embeddedFieldName = strToken.nextToken();
    }
    if (strToken.countTokens() > 0) {
        nestedEmbeddedFieldName = strToken.nextToken();
    }

    Metamodel metaModel = kunderaMetadata.getApplicationMetadata().getMetamodel(m.getPersistenceUnit());
    EntityType entity = metaModel.entity(m.getEntityClazz());

    try {
        Attribute attribute = entity.getAttribute(embeddableAttributeName);

        if (((MetamodelImpl) metaModel).isEmbeddable(((AbstractAttribute) attribute).getBindableJavaType())) {
            EmbeddableType embeddable = metaModel
                    .embeddable(((AbstractAttribute) attribute).getBindableJavaType());
            Iterator<Attribute> attributeIter = embeddable.getAttributes().iterator();
            while (attributeIter.hasNext()) {
                AbstractAttribute attrib = (AbstractAttribute) attributeIter.next();

                if (viaColumnName && attrib.getName().equals(embeddedFieldName)) {
                    if (nestedEmbeddedFieldName != null && ((MetamodelImpl) metaModel)
                            .isEmbeddable(((AbstractAttribute) attrib).getBindableJavaType())) {
                        EmbeddableType nestedEmbeddable = metaModel
                                .embeddable(((AbstractAttribute) attrib).getBindableJavaType());
                        Iterator<Attribute> iter = embeddable.getAttributes().iterator();
                        while (iter.hasNext()) {
                            AbstractAttribute nestedAttribute = (AbstractAttribute) iter.next();

                            if (viaColumnName && nestedAttribute.getName().equals(embeddedFieldName)) {
                                return nestedAttribute.getName();
                            }

                            if (!viaColumnName
                                    && nestedAttribute.getJPAColumnName().equals(embeddedFieldName)) {
                                return nestedAttribute.getName();
                            }
                        }
                    } else if (nestedEmbeddedFieldName != null && !((MetamodelImpl) metaModel)
                            .isEmbeddable(((AbstractAttribute) attrib).getBindableJavaType())) {
                        return null;
                    } else {
                        return attribute.getName();
                    }
                }

                if (!viaColumnName && attrib.getJPAColumnName().equals(embeddedFieldName)) {
                    return attribute.getName();
                }
            }
        }

    } catch (IllegalArgumentException iax) {
        return null;
    }
    return enclosingEmbeddedFieldName;
}

From source file:com.impetus.client.rdbms.query.RDBMSEntityReader.java

/**
 * Gets the sql query from jpa./*from  w  w w .  ja  v a  2 s  .c om*/
 * 
 * @param entityMetadata
 *            the entity metadata
 * @param relations
 *            the relations
 * @param primaryKeys
 *            the primary keys
 * @return the sql query from jpa
 */
public String getSqlQueryFromJPA(EntityMetadata entityMetadata, List<String> relations,
        Set<String> primaryKeys) {
    ApplicationMetadata appMetadata = kunderaMetadata.getApplicationMetadata();
    Metamodel metaModel = appMetadata.getMetamodel(entityMetadata.getPersistenceUnit());

    if (jpaQuery != null) {
        String query = appMetadata.getQuery(jpaQuery);
        boolean isNative = kunderaQuery != null ? kunderaQuery.isNative() : false;

        if (isNative) {
            return query != null ? query : jpaQuery;
        }
    }

    // Suffixing the UNDERSCORE instead of prefix as Oracle 11g complains
    // about invalid characters error while executing the request.
    String aliasName = entityMetadata.getTableName() + "_";

    StringBuilder queryBuilder = new StringBuilder("Select ");

    EntityType entityType = metaModel.entity(entityMetadata.getEntityClazz());
    Set<Attribute> attributes = entityType.getAttributes();
    for (Attribute field : attributes) {
        if (!field.isAssociation() && !field.isCollection()
                && !((Field) field.getJavaMember()).isAnnotationPresent(ManyToMany.class)
                && !((Field) field.getJavaMember()).isAnnotationPresent(Transient.class)
                && !((MetamodelImpl) metaModel)
                        .isEmbeddable(((AbstractAttribute) field).getBindableJavaType())) {
            queryBuilder.append(aliasName);
            queryBuilder.append(".");
            queryBuilder.append(((AbstractAttribute) field).getJPAColumnName());
            queryBuilder.append(", ");
        }
    }

    // Handle embedded columns, add them to list.
    Map<String, EmbeddableType> embeddedColumns = ((MetamodelImpl) metaModel)
            .getEmbeddables(entityMetadata.getEntityClazz());
    for (EmbeddableType embeddedCol : embeddedColumns.values()) {
        Set<Attribute> embeddedAttributes = embeddedCol.getAttributes();
        for (Attribute column : embeddedAttributes) {
            queryBuilder.append(aliasName);
            queryBuilder.append(".");
            queryBuilder.append(((AbstractAttribute) column).getJPAColumnName());
            queryBuilder.append(", ");
        }
    }

    if (relations != null) {
        for (String relation : relations) {
            Relation rel = entityMetadata.getRelation(entityMetadata.getFieldName(relation));
            String r = MetadataUtils.getMappedName(entityMetadata, rel, kunderaMetadata);
            if (!((AbstractAttribute) entityMetadata.getIdAttribute()).getJPAColumnName()
                    .equalsIgnoreCase(r != null ? r : relation) && rel != null
                    && !rel.getProperty().isAnnotationPresent(ManyToMany.class)
                    && !rel.getProperty().isAnnotationPresent(OneToMany.class)
                    && (rel.getProperty().isAnnotationPresent(OneToOne.class)
                            && StringUtils.isBlank(rel.getMappedBy())
                            || rel.getProperty().isAnnotationPresent(ManyToOne.class))) {
                queryBuilder.append(aliasName);
                queryBuilder.append(".");
                queryBuilder.append(r != null ? r : relation);
                queryBuilder.append(", ");
            }
        }
    }

    // Remove last ","
    queryBuilder.deleteCharAt(queryBuilder.lastIndexOf(","));

    queryBuilder.append(" From ");
    if (entityMetadata.getSchema() != null && !entityMetadata.getSchema().isEmpty()) {
        queryBuilder.append(entityMetadata.getSchema() + ".");
    }
    queryBuilder.append(entityMetadata.getTableName());
    queryBuilder.append(" ");
    queryBuilder.append(aliasName);
    // add conditions
    if (filter != null) {
        queryBuilder.append(" Where ");
    }

    // Append conditions
    onCondition(entityMetadata, (MetamodelImpl) metaModel, primaryKeys, aliasName, queryBuilder, entityType);

    return queryBuilder.toString();
}

From source file:com.impetus.kundera.client.cassandra.dsdriver.DSClient.java

/**
 * Populate composite id./*from  w  w  w.  ja va 2  s  .c om*/
 * 
 * @param metadata
 *            the metadata
 * @param entity
 *            the entity
 * @param columnName
 *            the column name
 * @param row
 *            the row
 * @param metaModel
 *            the meta model
 * @param attribute
 *            the attribute
 * @param entityClazz
 *            the entity clazz
 * @param dataType
 *            the data type
 * @return the object
 */
private Object populateCompositeId(EntityMetadata metadata, Object entity, String columnName, Row row,
        MetamodelImpl metaModel, Attribute attribute, Class<?> entityClazz, DataType dataType) {
    Class javaType = ((AbstractAttribute) attribute).getBindableJavaType();

    if (metaModel.isEmbeddable(javaType)) {
        EmbeddableType compoundKey = metaModel.embeddable(javaType);
        Object compoundKeyObject = null;
        try {
            Set<Attribute> attributes = compoundKey.getAttributes();
            entity = KunderaCoreUtils.initialize(entityClazz, entity);

            for (Attribute compoundAttribute : attributes) {
                compoundKeyObject = compoundKeyObject == null ? getCompoundKey(attribute, entity)
                        : compoundKeyObject;

                if (metaModel.isEmbeddable(((AbstractAttribute) compoundAttribute).getBindableJavaType())) {
                    Object compoundObject = populateCompositeId(metadata, compoundKeyObject, columnName, row,
                            metaModel, compoundAttribute, javaType, dataType);
                    PropertyAccessorHelper.set(entity, (Field) attribute.getJavaMember(), compoundObject);
                } else if (((AbstractAttribute) compoundAttribute).getJPAColumnName().equals(columnName)) {
                    DSClientUtilities.assign(row, compoundKeyObject, null, dataType.getName(), null, columnName,
                            (Field) compoundAttribute.getJavaMember(), metaModel);
                    PropertyAccessorHelper.set(entity, (Field) attribute.getJavaMember(), compoundKeyObject);
                    break;
                }
            }
        } catch (IllegalArgumentException iaex) {
            // ignore as it might not represented within entity.
            // No need for any logger message
        } catch (Exception e) {
            log.error("Error while retrieving data, Caused by: .", e);
            throw new PersistenceException(e);
        }
    }
    return entity;
}

From source file:com.impetus.client.rdbms.query.RDBMSEntityReader.java

/**
 * //from  w  ww . j  av  a2  s. com
 * @param entityMetadata
 * @param primaryKeys
 * @param aliasName
 * @param queryBuilder
 * @param entityType
 */
private void onCondition(EntityMetadata entityMetadata, MetamodelImpl metamodel, Set<String> primaryKeys,
        String aliasName, StringBuilder queryBuilder, EntityType entityType) {
    if (primaryKeys == null) {
        for (Object o : conditions) {
            if (o instanceof FilterClause) {
                FilterClause clause = ((FilterClause) o);
                Object value = clause.getValue().get(0);
                String propertyName = clause.getProperty();
                String condition = clause.getCondition();

                if (StringUtils.contains(propertyName, '.')) {
                    int indexOf = propertyName.indexOf(".");
                    String jpaColumnName = propertyName.substring(0, indexOf);
                    String embeddedColumnName = propertyName.substring(indexOf + 1, propertyName.length());
                    String fieldName = entityMetadata.getFieldName(jpaColumnName);
                    Attribute attribute = entityType.getAttribute(fieldName);
                    EmbeddableType embeddedType = metamodel
                            .embeddable(((AbstractAttribute) attribute).getBindableJavaType());
                    Attribute embeddedAttribute = embeddedType.getAttribute(embeddedColumnName);

                    addClause(entityMetadata, aliasName, queryBuilder, entityType, value, condition, fieldName,
                            embeddedAttribute);
                } else {
                    String fieldName = entityMetadata.getFieldName(propertyName);
                    Attribute attribute = entityType.getAttribute(fieldName);
                    if (metamodel.isEmbeddable(((AbstractAttribute) attribute).getBindableJavaType())) {
                        EmbeddableType embeddedType = metamodel
                                .embeddable(((AbstractAttribute) attribute).getBindableJavaType());
                        Set<Attribute> attributes = embeddedType.getAttributes();
                        for (Attribute embeddedAttribute : attributes) {
                            Object embeddedAttributevalue = PropertyAccessorHelper.getObject(value,
                                    (Field) embeddedAttribute.getJavaMember());
                            addClause(entityMetadata, aliasName, queryBuilder, entityType,
                                    embeddedAttributevalue, condition, propertyName, embeddedAttribute);
                            queryBuilder.append(" and ");
                        }

                        queryBuilder.delete(queryBuilder.lastIndexOf("and"),
                                queryBuilder.lastIndexOf("and") + 3);
                    } else if (((AbstractAttribute) entityMetadata.getIdAttribute()).getJPAColumnName()
                            .equals(propertyName)) {
                        addClause(entityMetadata, aliasName, queryBuilder, entityType, value, condition,
                                propertyName, entityMetadata.getIdAttribute());
                    } else {
                        addClause(entityMetadata, aliasName, queryBuilder, entityType, value, condition,
                                propertyName, attribute);
                    }
                }
            } else {
                queryBuilder.append(" ");
                queryBuilder.append(o);
                queryBuilder.append(" ");
            }
        }
    } else {
        queryBuilder.append(aliasName);
        queryBuilder.append(".");
        queryBuilder.append(((AbstractAttribute) entityMetadata.getIdAttribute()).getJPAColumnName());
        queryBuilder.append(" ");
        queryBuilder.append("IN(");
        int count = 0;
        Attribute col = entityMetadata.getIdAttribute();
        boolean isString = isStringProperty(entityType, col);
        for (String key : primaryKeys) {
            appendStringPrefix(queryBuilder, isString);
            queryBuilder.append(key);
            appendStringPrefix(queryBuilder, isString);
            if (++count != primaryKeys.size()) {
                queryBuilder.append(",");
            } else {
                queryBuilder.append(")");
            }
        }
    }
}

From source file:com.impetus.client.oraclenosql.OracleNoSQLClient.java

/**
 * Process embeddable attribute.//  w  w w  .  j  a  v  a2s. com
 * 
 * @param entity
 *            the entity
 * @param metamodel
 *            the metamodel
 * @param row
 *            the row
 * @param schemaTable
 *            the schema table
 * @param metadata
 *            the metadata
 * @param attribute
 *            the attribute
 */
private void processEmbeddableAttribute(Object entity, MetamodelImpl metamodel, Row row, Table schemaTable,
        EntityMetadata metadata, Attribute attribute) {
    // process on embeddables.
    EmbeddableType embeddable = metamodel.embeddable(((AbstractAttribute) attribute).getBindableJavaType());
    Set<Attribute> embeddedAttributes = embeddable.getAttributes();
    Object embeddedObject = PropertyAccessorHelper.getObject(entity, (Field) attribute.getJavaMember());

    for (Attribute embeddedAttrib : embeddedAttributes) {
        setField(row, schemaTable, embeddedObject, embeddedAttrib);
    }
}

From source file:com.impetus.client.oraclenosql.OracleNoSQLClient.java

/**
 * Read embeddable./* www.  j a va  2 s  . c o m*/
 * 
 * @param key
 *            the key
 * @param columnsToSelect
 *            the columns to select
 * @param entityMetadata
 *            the entity metadata
 * @param metamodel
 *            the metamodel
 * @param schemaTable
 *            the schema table
 * @param value
 *            the value
 * @param attribute
 *            the attribute
 */
private void readEmbeddable(Object key, List<String> columnsToSelect, EntityMetadata entityMetadata,
        MetamodelImpl metamodel, Table schemaTable, RecordValue value, Attribute attribute) {
    EmbeddableType embeddableId = metamodel.embeddable(((AbstractAttribute) attribute).getBindableJavaType());
    Set<Attribute> embeddedAttributes = embeddableId.getAttributes();

    for (Attribute embeddedAttrib : embeddedAttributes) {
        String columnName = ((AbstractAttribute) embeddedAttrib).getJPAColumnName();
        Object embeddedColumn = PropertyAccessorHelper.getObject(key, (Field) embeddedAttrib.getJavaMember());

        // either null or empty or contains that column
        if (eligibleToFetch(columnsToSelect, columnName)) {
            NoSqlDBUtils.add(schemaTable.getField(columnName), value, embeddedColumn, columnName);
        }
    }
}