Example usage for org.apache.cassandra.db.marshal UserType getInstance

List of usage examples for org.apache.cassandra.db.marshal UserType getInstance

Introduction

In this page you can find the example usage for org.apache.cassandra.db.marshal UserType getInstance.

Prototype

public static UserType getInstance(TypeParser parser) 

Source Link

Usage

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

License:Apache License

/**
 * Sets the udt value./*w  w  w. j a  v a 2 s  .  co  m*/
 * 
 * @param entity
 *            the entity
 * @param thriftColumnValue
 *            the thrift column value
 * @param metaModel
 *            the meta model
 * @param attribute
 *            the attribute
 * @return the object
 */
private Object setUdtValue(Object entity, Object thriftColumnValue, MetamodelImpl metaModel,
        Attribute attribute) {
    List<ByteBuffer> fieldNames = new ArrayList<ByteBuffer>();
    List<AbstractType<?>> fieldTypes = new ArrayList<AbstractType<?>>();

    String val = null;

    // get from cqlMetadata, details of types and names (for maintaining
    // order)
    Map<ByteBuffer, String> schemaTypes = this.clientBase.getCqlMetadata().getValue_types();
    for (Map.Entry<ByteBuffer, String> schemaType : schemaTypes.entrySet()) {
        UTF8Serializer utf8Serializer = UTF8Serializer.instance;
        String key = utf8Serializer.deserialize((schemaType.getKey()));
        if (key.equals(((AbstractAttribute) attribute).getJavaMember().getName())) {
            val = schemaType.getValue();
            break;
        }
    }

    UserType userType = null;

    try {
        userType = UserType.getInstance(new TypeParser(val.substring(val.indexOf("("), val.length())));
    } catch (ConfigurationException | SyntaxException e) {
        log.error(e.getMessage());
        throw new KunderaException("Error while getting instance of UserType " + e);
    }

    fieldNames = userType.fieldNames();
    fieldTypes = userType.allTypes();

    Field field = (Field) ((AbstractAttribute) attribute).getJavaMember();
    Class embeddedClass = ((AbstractAttribute) attribute).getBindableJavaType();

    Object embeddedObject = KunderaCoreUtils.createNewInstance(embeddedClass);

    Object finalValue = populateEmbeddedRecursive((ByteBuffer.wrap((byte[]) thriftColumnValue)), fieldTypes,
            fieldNames, embeddedObject, metaModel);

    PropertyAccessorHelper.set(entity, field, finalValue);

    return entity;
}