/** * Custom implementation to offer map of embeddables available for given * entityType. * * @param clazz * entity class * @return map of holding {@link EmbeddableType} as value and attribute name * as key. */ public Map<String, EmbeddableType> getEmbeddables(Class clazz) { Map<String, EmbeddableType> embeddableAttibutes = new HashMap<String, EmbeddableType>(); if (entityTypes != null) { EntityType entity = entityTypes.get(clazz); Iterator<Attribute> iter = entity.getAttributes().iterator(); while (iter.hasNext()) { Attribute attribute = iter.next(); if (isEmbeddable(((AbstractAttribute) attribute).getBindableJavaType())) { embeddableAttibutes.put(attribute.getName(), embeddable(((AbstractAttribute) attribute).getBindableJavaType())); } } } return embeddableAttibutes; }
private void addPrimaryKeyColumnsFromEmbeddable(List<ColumnSchema> columns, EmbeddableType embeddable, Field[] fields, MetamodelImpl metaModel) { for (Field f : fields) { if (!ReflectUtils.isTransientOrStatic(f)) { if (f.getType().isAnnotationPresent(Embeddable.class)) { // nested addPrimaryKeyColumnsFromEmbeddable(columns, (EmbeddableType) metaModel.embeddable(f.getType()), f .getType().getDeclaredFields(), metaModel); } else { Attribute attribute = embeddable.getAttribute(f.getName()); columns.add(new ColumnSchema.ColumnSchemaBuilder( ((AbstractAttribute) attribute).getJPAColumnName(), KuduDBValidationClassMapper .getValidTypeForClass(f.getType())).key(true).build()); } } } }
/** * @param translatedColumns * @param metaModel * @param col */ private void onEmbeddable(List<String> translatedColumns, MetamodelImpl metaModel, Attribute col, boolean isEmbeddable) { if (isEmbeddable) { EmbeddableType embeddableType = metaModel.embeddable(col.getJavaType()); Set<Attribute> attributes = embeddableType.getAttributes(); for (Attribute attribute : attributes) { translatedColumns.add(((AbstractAttribute) attribute).getJPAColumnName()); } } else { translatedColumns.add(((AbstractAttribute) col).getJPAColumnName()); } }
EmbeddableType embeddableId = metaModel.embeddable(metadata.getIdAttribute().getBindableJavaType()); Set<Attribute> embeddedAttributes = embeddableId.getAttributes();
/** * Populate compound key. * * @param dbObj * the db obj * @param m * the m * @param metaModel * the meta model * @param id * the id */ public static void populateCompoundKey(DBObject dbObj, EntityMetadata m, MetamodelImpl metaModel, Object id) { EmbeddableType compoundKey = metaModel.embeddable(m.getIdAttribute().getBindableJavaType()); // Iterator<Attribute> iter = compoundKey.getAttributes().iterator(); BasicDBObject compoundKeyObj = new BasicDBObject(); compoundKeyObj = getCompoundKeyColumns(m, id, compoundKey, metaModel); dbObj.put("_id", compoundKeyObj); }
/** * Gets the composite id embeddables. * * @param embeddable * the embeddable * @param compositeEmbeddables * the composite embeddables * @param metaModel * the meta model * @return the composite id embeddables */ private void getCompositeIdEmbeddables(EmbeddableType embeddable, List compositeEmbeddables, MetamodelImpl metaModel) { compositeEmbeddables.add(embeddable.getJavaType().getSimpleName()); for (Object column : embeddable.getAttributes()) { Attribute columnAttribute = (Attribute) column; Field f = (Field) columnAttribute.getJavaMember(); if (columnAttribute.getJavaType().isAnnotationPresent(Embeddable.class)) { getCompositeIdEmbeddables(metaModel.embeddable(columnAttribute.getJavaType()), compositeEmbeddables, metaModel); } } }
private void prepareEmbeddedIdFields(Set<Attribute> embeddedAttributes, MetamodelImpl metaModel, Map<String, Object> embeddedIdFields, Document doc, EntityMetadata metadata) { for (Attribute attribute : embeddedAttributes) { if (!ReflectUtils.isTransientOrStatic((Field) attribute.getJavaMember())) { if (metaModel.isEmbeddable(attribute.getJavaType())) { EmbeddableType embeddable = metaModel.embeddable(attribute.getJavaType()); prepareEmbeddedIdFields(embeddable.getAttributes(), metaModel, embeddedIdFields, doc, metadata); } else { String columnName = ((AbstractAttribute) attribute).getJPAColumnName(); embeddedIdFields.put(columnName, doc.get(metadata.getEntityClazz().getSimpleName() + "." + columnName)); } } } }
if (compositeColumn.getJavaType().isAnnotationPresent(Embeddable.class)) { EmbeddableType partitionCol = metamodel.embeddable(compositeColumn.getJavaType()); Set<Attribute> cols = partitionCol.getAttributes(); for (Attribute col : cols) {
private void populateEntityFromDocument(Object entity, Object rowKey, MetamodelImpl metaModel, AbstractAttribute attrib) throws InstantiationException, IllegalAccessException { EmbeddableType embeddable = metaModel.embeddable(attrib.getBindableJavaType()); Iterator<Attribute> iter = embeddable.getAttributes().iterator(); Object compoundKey = attrib.getBindableJavaType().newInstance(); while (iter.hasNext()) { AbstractAttribute compositeAttrib = (AbstractAttribute) iter.next(); Object value = ((BasicDBObject) rowKey).get(compositeAttrib.getJPAColumnName()); if (metaModel.isEmbeddable(compositeAttrib.getBindableJavaType())) { populateEntityFromDocument(compoundKey, value, metaModel, compositeAttrib); } else { PropertyAccessorHelper.set(compoundKey, (Field) compositeAttrib.getJavaMember(), value); } } PropertyAccessorHelper.set(entity, (Field) attrib.getJavaMember(), compoundKey); }
/** * index compositekey * * @param metadata * @param id * @param document * @param metaModel */ protected void indexCompositeKey(Set<Attribute> embeddedAttributes, EntityMetadata metadata, Object id, Document document, final MetamodelImpl metaModel) { // indexing individual fields of the composite key try { for (Attribute attribute : embeddedAttributes) { if (!ReflectUtils.isTransientOrStatic((java.lang.reflect.Field) attribute.getJavaMember())) { if (metaModel.isEmbeddable(attribute.getJavaType())) { EmbeddableType embeddable = metaModel.embeddable(attribute.getJavaType()); indexCompositeKey(embeddable.getAttributes(), metadata, ((java.lang.reflect.Field) attribute.getJavaMember()).get(id), document, metaModel); } else { String columnName = ((AbstractAttribute) attribute).getJPAColumnName(); addFieldToDocument(id, document, (java.lang.reflect.Field) attribute.getJavaMember(), columnName, metadata.getEntityClazz().getSimpleName()); } } } } catch (IllegalAccessException e) { LOG.error(e.getMessage()); } }
/** * Populate embeddable object. * * @param data * the data * @param obj * the obj * @param m * the m * @param clazz * the clazz * @param prefix * @return the object */ private Object populateEmbeddableObject(HBaseDataWrapper data, Object obj, EntityMetadata m, Class clazz, String prefix) { MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata().getMetamodel( m.getPersistenceUnit()); Set<Attribute> attributes = metaModel.embeddable(clazz).getAttributes(); writeValuesToEntity(obj, data, m, metaModel, attributes, null, null, -1, prefix); return obj; }
private void updateOrCreateIndexEmbeddedIdFields(Set<Attribute> embeddedAttributes, MetamodelImpl metaModel, Document document, EntityMetadata metadata, Object rowKey) { try { for (Attribute attribute : embeddedAttributes) { if (!ReflectUtils.isTransientOrStatic((Field) attribute.getJavaMember())) { if (metaModel.isEmbeddable(attribute.getJavaType())) { EmbeddableType embeddable = metaModel.embeddable(attribute.getJavaType()); updateOrCreateIndexEmbeddedIdFields(embeddable.getAttributes(), metaModel, document, metadata, ((Field) attribute.getJavaMember()).get(rowKey)); } else { String columnName = ((AbstractAttribute) attribute).getJPAColumnName(); Object embeddedColumn = PropertyAccessorHelper.getObject(rowKey, (Field) attribute.getJavaMember()); updateDocument(embeddedColumn.toString(), document, columnName); } } } } catch (IllegalAccessException e) { log.error(e.getMessage()); } }
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); } } } }
/** * Create Delete query from a given JPA query. * * @param kunderaQuery * the kundera query * @return the string */ public String createDeleteQuery(KunderaQuery kunderaQuery) { EntityMetadata metadata = kunderaQuery.getEntityMetadata(); MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata().getMetamodel(metadata.getPersistenceUnit()); CQLTranslator translator = new CQLTranslator(); String delete_query = translator.DELETE_QUERY; String tableName = kunderaQuery.getEntityMetadata().getTableName(); delete_query = StringUtils.replace(delete_query, CQLTranslator.COLUMN_FAMILY, translator.ensureCase(new StringBuilder(), tableName, false).toString()); StringBuilder builder = new StringBuilder(delete_query); builder.append(CQLTranslator.ADD_WHERE_CLAUSE); Class compoundKeyClass = metadata.getIdAttribute().getBindableJavaType(); EmbeddableType compoundKey = null; String idColumn; if (metaModel.isEmbeddable(compoundKeyClass)) { compoundKey = metaModel.embeddable(compoundKeyClass); idColumn = ((AbstractAttribute) metadata.getIdAttribute()).getJPAColumnName(); } else { idColumn = ((AbstractAttribute) metadata.getIdAttribute()).getJPAColumnName(); } onCondition(metadata, metaModel, compoundKey, idColumn, builder, false, translator, false); return builder.toString(); }
/** * Process embeddable attribute. * * @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); } }
EmbeddableType embeddable = metaModel.embeddable(embeddedClass);
/** * recursively populate all the fields present in partition key * * @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); } } } }
/** * Adds the index properties. * * @param metadata * the metadata * @param entity * the object * @param document * the document * @param metaModel */ protected void addEntityFieldsToDocument(EntityMetadata metadata, Object entity, Document document, MetamodelImpl metaModel) { String indexName = metadata.getIndexName(); Map<String, PropertyIndex> indexProperties = metadata.getIndexProperties(); for (String columnName : indexProperties.keySet()) { PropertyIndex index = indexProperties.get(columnName); java.lang.reflect.Field property = index.getProperty(); String propertyName = index.getName(); addFieldToDocument(entity, document, property, propertyName, indexName); } if (metaModel.isEmbeddable(metadata.getIdAttribute().getBindableJavaType())) { Object id = PropertyAccessorHelper.getId(entity, metadata); EmbeddableType embeddableId = metaModel.embeddable(metadata.getIdAttribute().getBindableJavaType()); Set<Attribute> embeddedAttributes = embeddableId.getAttributes(); indexCompositeKey(embeddedAttributes, metadata, id, document, metaModel); } }
/** * Id value in byte arr. * * @return the byte[] */ private byte[] idValueInByteArr() { Object entity = results.get(results.size() - 1); Object id = PropertyAccessorHelper.getId(entity, entityMetadata); String idName = ((AbstractAttribute) entityMetadata.getIdAttribute()).getJPAColumnName(); Class idClazz = ((AbstractAttribute) entityMetadata.getIdAttribute()).getBindableJavaType(); MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata().getMetamodel( entityMetadata.getPersistenceUnit()); EmbeddableType keyObj = null; ByteBuffer bytes = null; // if the key attribute is composite if (metaModel.isEmbeddable(entityMetadata.getIdAttribute().getBindableJavaType())) { keyObj = metaModel.embeddable(entityMetadata.getIdAttribute().getBindableJavaType()); Field embeddedField = getPartitionKeyField(); Attribute partitionKey = keyObj.getAttribute(embeddedField.getName()); Object partitionKeyValue = PropertyAccessorHelper.getObject(id, (Field) partitionKey.getJavaMember()); bytes = CassandraUtilities.toBytes(partitionKeyValue, (Field) partitionKey.getJavaMember()); } else { bytes = query.getBytesValue(idName, entityMetadata, id); } return bytes.array(); }
/** * Initialize ID attribute */ @Override protected void initialize(Node created, Map<String, Object> properties) { // Set Embeddable ID attribute if (metaModel.isEmbeddable(m.getIdAttribute().getBindableJavaType())) { // Populate embedded field value in serialized format created.setProperty(idColumnName, idUniqueValue); // Populated all other attributes of embedded into this node Set<Attribute> embeddableAttributes = metaModel .embeddable(m.getIdAttribute().getBindableJavaType()).getSingularAttributes(); for (Attribute attribute : embeddableAttributes) { String columnName = ((AbstractAttribute) attribute).getJPAColumnName(); if (columnName == null) columnName = attribute.getName(); Object value = PropertyAccessorHelper.getObject(id, (Field) attribute.getJavaMember()); if (value != null) created.setProperty(columnName, value); } } else { created.setProperty(idColumnName, properties.get(idColumnName)); } } };