private <Y> void checkTypeForSingleAttribute( String attributeType, SingularAttribute<?,?> attribute, String name, Class<Y> javaType) { if ( attribute == null || ( javaType != null && !attribute.getBindableJavaType().equals( javaType ) ) ) { if ( isPrimitiveVariant( attribute, javaType ) ) { return; } throw new IllegalArgumentException( attributeType + " named " + name + ( javaType != null ? " and of type " + javaType.getName() : "" ) + " is not present" ); } }
@SuppressWarnings({ "SimplifiableIfStatement" }) protected <Y> boolean isPrimitiveVariant(SingularAttribute<?,?> attribute, Class<Y> javaType) { if ( attribute == null ) { return false; } Class declaredType = attribute.getBindableJavaType(); if ( declaredType.isPrimitive() ) { return ( Boolean.class.equals( javaType ) && Boolean.TYPE.equals( declaredType ) ) || ( Character.class.equals( javaType ) && Character.TYPE.equals( declaredType ) ) || ( Byte.class.equals( javaType ) && Byte.TYPE.equals( declaredType ) ) || ( Short.class.equals( javaType ) && Short.TYPE.equals( declaredType ) ) || ( Integer.class.equals( javaType ) && Integer.TYPE.equals( declaredType ) ) || ( Long.class.equals( javaType ) && Long.TYPE.equals( declaredType ) ) || ( Float.class.equals( javaType ) && Float.TYPE.equals( declaredType ) ) || ( Double.class.equals( javaType ) && Double.TYPE.equals( declaredType ) ); } if ( javaType.isPrimitive() ) { return ( Boolean.class.equals( declaredType ) && Boolean.TYPE.equals( javaType ) ) || ( Character.class.equals( declaredType ) && Character.TYPE.equals( javaType ) ) || ( Byte.class.equals( declaredType ) && Byte.TYPE.equals( javaType ) ) || ( Short.class.equals( declaredType ) && Short.TYPE.equals( javaType ) ) || ( Integer.class.equals( declaredType ) && Integer.TYPE.equals( javaType ) ) || ( Long.class.equals( declaredType ) && Long.TYPE.equals( javaType ) ) || ( Float.class.equals( declaredType ) && Float.TYPE.equals( javaType ) ) || ( Double.class.equals( declaredType ) && Double.TYPE.equals( javaType ) ); } return false; }
private <Y> JoinImplementor<X, Y> constructJoin(SingularAttribute<? super X, Y> attribute, JoinType jt) { if ( Type.PersistenceType.BASIC.equals( attribute.getType().getPersistenceType() ) ) { throw new BasicPathUsageException( "Cannot join to attribute of basic type", attribute ); } // TODO : runtime check that the attribute in fact belongs to this From's model/bindable if ( jt.equals( JoinType.RIGHT ) ) { throw new UnsupportedOperationException( "RIGHT JOIN not supported" ); } final Class<Y> attributeType = attribute.getBindableJavaType(); return new SingularAttributeJoin<X, Y>( criteriaBuilder(), attributeType, this, attribute, jt ); }
/** * Returns declared singular attribute. * * @param <Y> * the generic type * @param paramString * the param string * @param paramClass * the param class * @param checkValidity * the check validity * @return the declared singular attribute */ private <Y> SingularAttribute<X, Y> getDeclaredSingularAttribute(String paramString, Class<Y> paramClass, boolean checkValidity) { SingularAttribute<X, ?> declaredAttrib = declaredSingluarAttribs.get(paramString); if (declaredAttrib != null && declaredAttrib.getBindableJavaType().equals(paramClass)) { return (SingularAttribute<X, Y>) declaredAttrib; } if (checkValidity) { throw new IllegalArgumentException( "attribute of the given name and type is not present in the managed type, for name:" + paramString + " , type:" + paramClass); } return null; }
/** * Will return partition key part of composite id. * * @return the partition key field */ private Field getPartitionKeyField() { Field[] embeddedFields = entityMetadata.getIdAttribute().getBindableJavaType().getDeclaredFields(); Field field = null; for (Field embeddedField : embeddedFields) { if (!ReflectUtils.isTransientOrStatic(embeddedField)) { field = embeddedField; break; } } return field; }
/** * Prepares ID column value for embedded IDs by combining its attributes * * @param m * @param id * @param metaModel * @return */ private String serializeIdAttributeValue(final EntityMetadata m, MetamodelImpl metaModel, Object id) { if (!metaModel.isEmbeddable(m.getIdAttribute().getBindableJavaType())) { return null; } Class<?> embeddableClass = m.getIdAttribute().getBindableJavaType(); String idUniqueValue = ""; for (Field embeddedField : embeddableClass.getDeclaredFields()) { if (!ReflectUtils.isTransientOrStatic(embeddedField)) { Object value = PropertyAccessorHelper.getObject(id, embeddedField); if (value != null && !StringUtils.isEmpty(value.toString())) idUniqueValue = idUniqueValue + value + COMPOSITE_KEY_SEPARATOR; } } if (idUniqueValue.endsWith(COMPOSITE_KEY_SEPARATOR)) idUniqueValue = idUniqueValue.substring(0, idUniqueValue.length() - COMPOSITE_KEY_SEPARATOR.length()); return idUniqueValue; }
EmbeddableType embeddableId = metaModel.embeddable(metadata.getIdAttribute().getBindableJavaType()); Set<Attribute> embeddedAttributes = embeddableId.getAttributes();
@Override public Object[] findIdsByColumn(String schemaName, String tableName, String pKeyName, String columnName, Object columnValue, Class entityClazz) { EntityMetadata metadata = KunderaMetadataManager.getEntityMetadata(kunderaMetadata, entityClazz); return getColumnsById(schemaName, tableName, columnName, ((AbstractAttribute) metadata.getIdAttribute()).getJPAColumnName(), columnValue, metadata.getIdAttribute().getBindableJavaType()).toArray(); }
/** * 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); }
@Override public Map<String, Object> search(String query, Class<?> parentClass, EntityMetadata parentMetadata, Class<?> childClass, EntityMetadata childMetadata, Object entityId, int start, int count) { String secIndexName = getIndexTableName(childMetadata); String parentIdColumnName = ((AbstractAttribute) parentMetadata.getIdAttribute()).getJPAColumnName(); String childIdColumnName = ((AbstractAttribute) childMetadata.getIdAttribute()).getJPAColumnName(); String id = PropertyAccessorHelper.getString(entityId); List<String> majorComponents = new ArrayList<String>(); majorComponents.add(secIndexName); majorComponents.add(parentIdColumnName); majorComponents.add(id); Key majorKeyToFind = Key.createKey(majorComponents); Iterator<KeyValueVersion> iterator = kvStore.multiGetIterator(Direction.FORWARD, 0, majorKeyToFind, null, null); Map<String, Object> results = new HashMap<String, Object>(); while (iterator.hasNext()) { KeyValueVersion keyValueVersion = iterator.next(); String minorKey = keyValueVersion.getKey().getMinorPath().get(0); PropertyAccessor accessor = PropertyAccessorFactory .getPropertyAccessor(childMetadata.getIdAttribute().getBindableJavaType()); byte[] idByteArr = keyValueVersion.getValue().getValue(); Object keyObj = accessor.fromBytes(childMetadata.getIdAttribute().getBindableJavaType(), idByteArr); results.put(childIdColumnName + "|" + minorKey, keyObj); } return results; }
@Override public Object[] findIdsByColumn(String schemaName, String tableName, String pKeyName, String columnName, Object columnValue, Class entityClazz) { EntityMetadata m = KunderaMetadataManager.getEntityMetadata(kunderaMetadata, entityClazz); byte[] valueInBytes = HBaseUtils.getBytes(columnValue); Filter f = new SingleColumnValueFilter(Bytes.toBytes(tableName), Bytes.toBytes(columnName), CompareOp.EQUAL, valueInBytes); KeyOnlyFilter keyFilter = new KeyOnlyFilter(); FilterList filterList = new FilterList(f, keyFilter); try { return handler.scanRowyKeys(filterList, schemaName, tableName, columnName + "_" + columnValue, m .getIdAttribute().getBindableJavaType()); } catch (IOException e) { log.error("Error while executing findIdsByColumn(), Caused by: .", e); throw new KunderaException(e); } }
@Override public Object[] findIdsByColumn(String schemaName, String tableName, String pKeyName, String columnName, Object columnValue, Class entityClazz) { CompareOp operator = HBaseUtils.getOperator("=", false, false).getOperator(); EntityMetadata m = KunderaMetadataManager.getEntityMetadata(kunderaMetadata, entityClazz); byte[] valueInBytes = HBaseUtils.getBytes(columnValue); Filter f = new SingleColumnValueFilter(Bytes.toBytes(tableName), Bytes.toBytes(columnName), operator, valueInBytes); KeyOnlyFilter keyFilter = new KeyOnlyFilter(); FilterList filterList = new FilterList(f, keyFilter); try { return handler.scanRowyKeys(filterList, schemaName, tableName, columnName + "_" + columnValue, m .getIdAttribute().getBindableJavaType()); } catch (IOException e) { log.error("Error while executing findIdsByColumn(), Caused by: .", e); throw new KunderaException(e); } }
/** * 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); } }
@Override public void delete(Object entity, Object rowKey) { EntityMetadata m = KunderaMetadataManager.getEntityMetadata(kunderaMetadata, entity.getClass()); MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata().getMetamodel( m.getPersistenceUnit()); if (metaModel.isEmbeddable(m.getIdAttribute().getBindableJavaType())) { rowKey = KunderaCoreUtils.prepareCompositeKey(m, rowKey); } deleteByColumn(m.getSchema(), m.getTableName(), null, rowKey); }
/** * Returns true in case of, composite Id and if cql3 opted and not a embedded entity. * * @param metadata * the metadata * @return true, if is cql3 enabled */ public boolean isCql3Enabled(EntityMetadata metadata) { if (metadata != null) { MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata().getMetamodel(metadata.getPersistenceUnit()); if (metaModel.isEmbeddable(metadata.getIdAttribute().getBindableJavaType())) { return true; } // added for embeddables support on cql3 AbstractManagedType managedType = (AbstractManagedType) metaModel.entity(metadata.getEntityClazz()); if (managedType.hasEmbeddableAttribute()) { return getCqlVersion().equalsIgnoreCase(CassandraConstants.CQL_VERSION_3_0); } if (getCqlVersion().equalsIgnoreCase(CassandraConstants.CQL_VERSION_3_0) && metadata.getType().equals(Type.SUPER_COLUMN_FAMILY)) { log.warn( "Super Columns not supported by cql, Any operation on supercolumn family will be executed using thrift, returning false."); return false; } return getCqlVersion().equalsIgnoreCase(CassandraConstants.CQL_VERSION_3_0); } return getCqlVersion().equalsIgnoreCase(CassandraConstants.CQL_VERSION_3_0); }
/** * Find join column values for inverse join column. * * @param <E> * the element type * @param schemaName * the schema name * @param tableName * the table name * @param pKeyName * the key name * @param columnName * the column name * @param columnValue * the column value * @param entityClazz * the entity clazz * @return the list */ protected <E> List<E> findIdsByColumnUsingCql(String schemaName, String tableName, String pKeyName, String columnName, Object columnValue, Class entityClazz) { EntityMetadata metadata = KunderaMetadataManager.getEntityMetadata(kunderaMetadata, entityClazz); return getColumnsByIdUsingCql(schemaName, tableName, columnName, ((AbstractAttribute) metadata.getIdAttribute()).getJPAColumnName(), columnValue, metadata.getIdAttribute().getBindableJavaType()); }
/** * From bytes. * * @param m * the m * @param metaModel * the meta model * @param b * the b * @return the object */ public static Object fromBytes(EntityMetadata m, MetamodelImpl metaModel, byte[] b) { Class idFieldClass = m.getIdAttribute().getJavaType(); if (metaModel.isEmbeddable(m.getIdAttribute().getBindableJavaType())) { return fromBytes(b, String.class); } return fromBytes(b, idFieldClass); }
/** * From bytes. * * @param m * the m * @param metaModel * the meta model * @param b * the b * @return the object */ public static Object fromBytes(EntityMetadata m, MetamodelImpl metaModel, byte[] b) { Class idFieldClass = m.getIdAttribute().getJavaType(); if (metaModel.isEmbeddable(m.getIdAttribute().getBindableJavaType())) { return fromBytes(b, String.class); } return fromBytes(b, idFieldClass); }
/** * 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(); }
@Override public void delete(Object entity, Object pKey) { EntityMetadata metadata = KunderaMetadataManager.getEntityMetadata(kunderaMetadata, entity.getClass()); MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata().getMetamodel( metadata.getPersistenceUnit()); AbstractManagedType managedType = (AbstractManagedType) metaModel.entity(metadata.getEntityClazz()); List<String> secondaryTables = ((DefaultEntityAnnotationProcessor) managedType.getEntityAnnotation()) .getSecondaryTablesName(); secondaryTables.add(metadata.getTableName()); if (metaModel.isEmbeddable(metadata.getIdAttribute().getBindableJavaType())) { pKey = KunderaCoreUtils.prepareCompositeKey(metadata, pKey); } for (String colTableName : secondaryTables) { deleteByColumn(metadata.getSchema(), colTableName, ((AbstractAttribute) metadata.getIdAttribute()).getJPAColumnName(), pKey); } }