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.client.oraclenosql.OracleNoSQLClient.java

/**
 * On embeddable id.//w  ww  .  j a va2 s . c o m
 * 
 * @param entityMetadata
 *            the entity metadata
 * @param metaModel
 *            the meta model
 * @param schemaTable
 *            the schema table
 * @param entity
 *            the entity
 * @param row
 *            the row
 * @throws InstantiationException
 *             the instantiation exception
 * @throws IllegalAccessException
 *             the illegal access exception
 */
private void onEmbeddableId(EntityMetadata entityMetadata, MetamodelImpl metaModel, Table schemaTable,
        Object entity, Row row) throws InstantiationException, IllegalAccessException {
    FieldDef fieldMetadata;
    FieldValue value;
    EmbeddableType embeddableType = metaModel.embeddable(entityMetadata.getIdAttribute().getBindableJavaType());
    Set<Attribute> embeddedAttributes = embeddableType.getAttributes();

    Object embeddedObject = entityMetadata.getIdAttribute().getBindableJavaType().newInstance();
    for (Attribute attrib : embeddedAttributes) {

        String columnName = ((AbstractAttribute) attrib).getJPAColumnName();

        fieldMetadata = schemaTable.getField(columnName);
        value = row.get(columnName);
        NoSqlDBUtils.get(fieldMetadata, value, embeddedObject, (Field) attrib.getJavaMember());
    }

    PropertyAccessorHelper.set(entity, (Field) entityMetadata.getIdAttribute().getJavaMember(), embeddedObject);
}

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

/**
 * Delete by primary key./*from w w w . jav  a 2s.c o  m*/
 * 
 * @param entity
 *            the entity
 * @param pKey
 *            the key
 */
@Override
public void delete(Object entity, Object pKey) {
    EntityMetadata entityMetadata = KunderaMetadataManager.getEntityMetadata(kunderaMetadata,
            entity.getClass());
    MetamodelImpl metamodel = (MetamodelImpl) KunderaMetadataManager.getMetamodel(kunderaMetadata,
            entityMetadata.getPersistenceUnit());

    String idColumnName = ((AbstractAttribute) entityMetadata.getIdAttribute()).getJPAColumnName();

    Table schemaTable = tableAPI.getTable(entityMetadata.getTableName());

    PrimaryKey key = schemaTable.createPrimaryKey();

    if (metamodel.isEmbeddable(entityMetadata.getIdAttribute().getBindableJavaType())) {
        EmbeddableType embeddableId = metamodel
                .embeddable(entityMetadata.getIdAttribute().getBindableJavaType());
        Set<Attribute> embeddedAttributes = embeddableId.getAttributes();

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

            NoSqlDBUtils.add(schemaTable.getField(columnName), key, embeddedColumn, columnName);
        }
    } else {
        NoSqlDBUtils.add(schemaTable.getField(idColumnName), key, pKey, idColumnName);
    }

    tableAPI.delete(key, null, null);
    KunderaCoreUtils.printQuery("Delete data from " + entityMetadata.getTableName() + " for PK " + pKey,
            showQuery);
}

From source file:com.impetus.client.couchdb.CouchDBClient.java

/**
 * Creates the and execute query./*from   w  w  w  . j ava2 s  .  c  o m*/
 * 
 * @param interpreter
 *            the interpreter
 * @return the list
 */
List createAndExecuteQuery(CouchDBQueryInterpreter interpreter) {
    EntityMetadata m = interpreter.getMetadata();
    List results = new ArrayList();
    try {
        MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata()
                .getMetamodel(m.getPersistenceUnit());
        StringBuilder q = new StringBuilder();
        String _id = CouchDBConstants.URL_SEPARATOR + m.getSchema().toLowerCase()
                + CouchDBConstants.URL_SEPARATOR + CouchDBConstants.DESIGN + m.getTableName()
                + CouchDBConstants.VIEW;
        if ((interpreter.isIdQuery() && !interpreter.isRangeQuery() && interpreter.getOperator() == null)
                || interpreter.isQueryOnCompositeKey()) {
            Object object = null;
            if (metaModel.isEmbeddable(m.getIdAttribute().getBindableJavaType())) {
                EmbeddableType embeddableType = metaModel.embeddable(m.getIdAttribute().getBindableJavaType());
                if (KunderaCoreUtils.countNonSyntheticFields(
                        m.getIdAttribute().getBindableJavaType()) == interpreter.getKeyValues().size()) {
                    Object key = CouchDBObjectMapper.getObjectFromJson(
                            gson.toJsonTree(interpreter.getKeyValues()).getAsJsonObject(),
                            m.getIdAttribute().getBindableJavaType(), embeddableType.getAttributes());
                    object = find(m.getEntityClazz(), key);
                    if (object != null) {
                        results.add(object);
                    }
                    return results;
                } else if (m.getIdAttribute().getName().equals(interpreter.getKeyName())
                        && interpreter.getKeyValues().size() == 1) {
                    object = find(m.getEntityClazz(), interpreter.getKeyValue());
                    if (object != null) {
                        results.add(object);
                    }
                    return results;
                } else {
                    log.error("There should be each and every field of composite key.");
                    throw new QueryHandlerException("There should be each and every field of composite key.");
                }
            }
            object = find(m.getEntityClazz(), interpreter.getKeyValue());
            if (object != null) {
                results.add(object);
            }
            return results;
        }

        // creating query.
        _id = createQuery(interpreter, m, q, _id);

        if (interpreter.getLimit() > 0) {
            q.append("&limit=" + interpreter.getLimit());
        }
        if (interpreter.isDescending()) {
            q.append("&descending=" + false);
        }

        // execute query.
        executeQueryAndGetResults(q, _id, m, results, interpreter);
    } catch (Exception e) {
        log.error("Error while executing query, Caused by {}.", e);
        throw new KunderaException(e);
    }
    return results;
}

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

/**
 * Scroll and populate./*  www .  j a  v a 2s  .c o m*/
 * 
 * @param key
 *            the key
 * @param entityMetadata
 *            the entity metadata
 * @param metaModel
 *            the meta model
 * @param schemaTable
 *            the schema table
 * @param rowsIter
 *            the rows iter
 * @param relationMap
 *            the relation map
 * @param columnsToSelect
 *            the columns to select
 * @return the list
 * @throws InstantiationException
 *             the instantiation exception
 * @throws IllegalAccessException
 *             the illegal access exception
 */
private List scrollAndPopulate(Object key, EntityMetadata entityMetadata, MetamodelImpl metaModel,
        Table schemaTable, Iterator<Row> rowsIter, Map<String, Object> relationMap,
        List<String> columnsToSelect) throws InstantiationException, IllegalAccessException {
    List results = new ArrayList();
    Object entity = null;
    EntityType entityType = metaModel.entity(entityMetadata.getEntityClazz());
    // here
    while (rowsIter.hasNext()) {
        relationMap = new HashMap<String, Object>();
        entity = initializeEntity(key, entityMetadata);

        Row row = rowsIter.next();

        List<String> fields = row.getTable().getFields();
        FieldDef fieldMetadata = null;
        FieldValue value = null;
        String idColumnName = ((AbstractAttribute) entityMetadata.getIdAttribute()).getJPAColumnName();
        if (/* eligibleToFetch(columnsToSelect, idColumnName) && */!metaModel
                .isEmbeddable(entityMetadata.getIdAttribute().getBindableJavaType())) {
            populateId(entityMetadata, schemaTable, entity, row);
        } else {
            onEmbeddableId(entityMetadata, metaModel, schemaTable, entity, row);
        }

        Iterator<String> fieldIter = fields.iterator();

        Set<Attribute> attributes = entityType.getAttributes();
        for (Attribute attribute : attributes) {
            String jpaColumnName = ((AbstractAttribute) attribute).getJPAColumnName();
            if (eligibleToFetch(columnsToSelect, jpaColumnName)
                    && !attribute.getName().equals(entityMetadata.getIdAttribute().getName())) {
                if (metaModel.isEmbeddable(((AbstractAttribute) attribute).getBindableJavaType())) {
                    // readEmbeddable(value, columnsToSelect,
                    // entityMetadata, metaModel, schemaTable, value,
                    // attribute);
                    EmbeddableType embeddableId = metaModel
                            .embeddable(((AbstractAttribute) attribute).getBindableJavaType());
                    Set<Attribute> embeddedAttributes = embeddableId.getAttributes();
                    Object embeddedObject = ((AbstractAttribute) attribute).getBindableJavaType().newInstance();
                    for (Attribute embeddedAttrib : embeddedAttributes) {
                        String embeddedColumnName = ((AbstractAttribute) embeddedAttrib).getJPAColumnName();

                        fieldMetadata = schemaTable.getField(embeddedColumnName);
                        value = row.get(embeddedColumnName);
                        NoSqlDBUtils.get(fieldMetadata, value, embeddedObject,
                                (Field) embeddedAttrib.getJavaMember());
                    }
                    PropertyAccessorHelper.set(entity, (Field) attribute.getJavaMember(), embeddedObject);

                } else {
                    fieldMetadata = schemaTable.getField(jpaColumnName);
                    value = row.get(jpaColumnName);

                    if (!attribute.isAssociation() && value != null) {
                        NoSqlDBUtils.get(fieldMetadata, value, entity, (Field) attribute.getJavaMember());
                    } else if (attribute.isAssociation() && value != null) {
                        Relation relation = entityMetadata.getRelation(attribute.getName());

                        if (relation != null) {
                            EntityMetadata associationMetadata = KunderaMetadataManager
                                    .getEntityMetadata(kunderaMetadata, relation.getTargetEntity());
                            if (!relation.getType().equals(ForeignKey.MANY_TO_MANY)) {
                                relationMap.put(jpaColumnName, NoSqlDBUtils.get(fieldMetadata, value,
                                        (Field) associationMetadata.getIdAttribute().getJavaMember()));
                            }
                        }
                    }
                }
            }
        }

        if (entity != null) {
            results.add(relationMap.isEmpty() ? entity
                    : new EnhanceEntity(entity,
                            key != null ? key : PropertyAccessorHelper.getId(entity, entityMetadata),
                            relationMap));
        }
    }
    return results;
}

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

/**
 * Wraps entity attributes into byte[] and return instance of attribute
 * wrapper./*w w w  .  j  av a  2 s.c  o 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 www  . j  a  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());
    }
}