Example usage for org.apache.cassandra.utils ByteBufferUtil readBytes

List of usage examples for org.apache.cassandra.utils ByteBufferUtil readBytes

Introduction

In this page you can find the example usage for org.apache.cassandra.utils ByteBufferUtil readBytes.

Prototype

public static ByteBuffer readBytes(ByteBuffer bb, int length) 

Source Link

Usage

From source file:com.impetus.client.cassandra.datahandler.CassandraDataHandlerBase.java

License:Apache License

/**
 * Populate embedded recursive./* w w  w.j av  a  2  s  . c  o  m*/
 * 
 * @param value
 *            the value
 * @param types
 *            the types
 * @param fieldNames
 *            the field names
 * @param entity
 *            the entity
 * @param metaModel
 *            the meta model
 * @return the object
 */
public Object populateEmbeddedRecursive(ByteBuffer value, List<AbstractType<?>> types,
        List<ByteBuffer> fieldNames, Object entity, MetamodelImpl metaModel) {
    ByteBuffer input = value.duplicate();
    EmbeddableType emb = metaModel.embeddable(entity.getClass());

    for (int i = 0; i < types.size(); i++) {

        if (!input.hasRemaining())
            return entity;

        AbstractType<?> type = types.get(i);
        String name = new String(fieldNames.get(i).array()); // JPA name,
                                                             // convert to
                                                             // column name

        Field fieldToSet = null;
        AbstractAttribute attribute = null;
        // change this if possible
        for (Object attr : emb.getAttributes()) {
            if (((AbstractAttribute) attr).getJPAColumnName().equals(name)) {
                attribute = (AbstractAttribute) attr;
                break;
            }
        }
        fieldToSet = (Field) attribute.getJavaMember();
        Class embeddedClass = attribute.getBindableJavaType();

        int size = input.getInt();
        if (size < 0) {
            continue;
        }

        ByteBuffer field = ByteBufferUtil.readBytes(input, size);

        if (type.getClass().getSimpleName().equals("UserType")) {
            List<ByteBuffer> subFieldNames = ((UserType) type).fieldNames();// ok
            List<AbstractType<?>> subfieldTypes = ((UserType) type).fieldTypes();

            // create entity with type_name and populate fields, set entity
            // in parent object after exit
            Object embeddedObjectChild = KunderaCoreUtils.createNewInstance(embeddedClass);

            Object processedEntity = populateEmbeddedRecursive(field, subfieldTypes, subFieldNames,
                    embeddedObjectChild, metaModel);
            PropertyAccessorHelper.set(entity, fieldToSet, processedEntity);
        } else {
            boolean flag = true;

            if (type.getClass().getSimpleName().equals("MapType")) {
                if (((MapType) type).getValuesType().getClass().getSimpleName().equals("UserType")) {
                    flag = false;
                    // create instance of embedded object (UserType)
                    setElementCollectionMap((MapType) type, field, entity, fieldToSet, metaModel, embeddedClass,
                            false);
                }
            } else if (type.getClass().getSimpleName().equals("ListType")) {
                if (((ListType) type).getElementsType().getClass().getSimpleName().equals("UserType")) {
                    flag = false;
                    setElementCollectionList((ListType) type, field, entity, fieldToSet, metaModel,
                            embeddedClass, false);
                }
            } else if (type.getClass().getSimpleName().equals("SetType")) {
                if (((SetType) type).getElementsType().getClass().getSimpleName().equals("UserType")) {
                    flag = false;
                    setElementCollectionSet((SetType) type, field, entity, fieldToSet, metaModel, embeddedClass,
                            false);
                }
            }
            if (flag) {
                TypeSerializer serializer = type.getSerializer();
                serializer.validate(field);

                Object finalValue = serializer.deserialize(field);
                PropertyAccessorHelper.set(entity, fieldToSet, finalValue);
            }

        }

    }
    return entity;
}