0

I have all these case classes :-

case class Role(id:UUID,
                roleName:String,
                description:Option[String],
                isVisible:Boolean,
                isDefault:Option[Boolean],
                contacts:Contact_Permissions,
                status:String) 

 case class 
Contact_Permissions(name:String,description:String,Type:String,permission: 
   Seq[Contact_Permission])

case class 
Contact_Permission(name:String,Types:String,options:Option[List[String]],value:List[String])

and these Types:-

CREATE TYPE IF NOT EXISTS myuser.contact_permission(
name text,
types text,
options list<text>,
value list<text>
)

CREATE TYPE IF NOT EXIXSTS myuser.contact_permissions(
name text,
description text,
types text,
permission frozen<contact_permission>
)

And my table is table:-

        """
          |CREATE TABLE IF NOT EXISTS myuser.roles(
          |role_id uuid,
          |role_name text,
          |description text,
          |is_visible  boolean,
          |is_default  boolean,
          |contacts_permission frozen<contact_permission>,
          |status text
          |)
          |""".stripMargin)

I have made codecs for both types:-

class PermissionCodec(private val innerCodec: TypeCodec[UDTValue],
                  javaType: Class[Contact_Permission])
  extends TypeCodec[Contact_Permission](innerCodec.getCqlType, javaType) {


  private val userType: UserType = innerCodec.getCqlType.asInstanceOf[UserType]

  override def serialize(value: Contact_Permission,
                         protocolVersion: ProtocolVersion): ByteBuffer =
    innerCodec.serialize(toUDTValue(value), protocolVersion)

  override def deserialize(bytes: ByteBuffer,
                           protocolVersion: ProtocolVersion): Contact_Permission =
    toFilter(innerCodec.deserialize(bytes, protocolVersion))

  override def parse(value: String): Contact_Permission =
    if (value == null || value.isEmpty || value == null) null
    else toFilter(innerCodec.parse(value))

  override def format(value: Contact_Permission): String =
    if (value == null) null else innerCodec.format(toUDTValue(value))

  protected def toFilter(value: UDTValue): Contact_Permission =
    if (value == null) null
    else new Contact_Permission(value.getString("name"),
      value.getString("Types"),
      Some(value.getList("options", classOf[String]).toList),
      value.getList("value", classOf[String]).toList
    )


  protected def toUDTValue(value: Contact_Permission): UDTValue =
    if (value == null) null
    else
      userType
        .newValue()
        .setString("name", value.name)
        .setString("Types", value.Types)
        .setList("options", value.options.get)
        .setList("value", value.value)

}

class Contact_Permission_Codec(private val innerCodec: TypeCodec[UDTValue],
                         javaType: Class[Contact_Permissions])
  extends TypeCodec[Contact_Permissions](innerCodec.getCqlType, javaType) {

  private val userType: UserType = innerCodec.getCqlType.asInstanceOf[UserType]

  override def serialize(value: Contact_Permissions,
                         protocolVersion: ProtocolVersion): ByteBuffer =
    innerCodec.serialize(toUDTValue(value), protocolVersion)

  override def deserialize(bytes: ByteBuffer,
                           protocolVersion: ProtocolVersion): Contact_Permissions =
    toFilter(innerCodec.deserialize(bytes, protocolVersion))

  override def parse(value: String): Contact_Permissions =
    if (value == null || value.isEmpty || value == null) null
    else toFilter(innerCodec.parse(value))

  override def format(value: Contact_Permissions): String =
    if (value == null) null else innerCodec.format(toUDTValue(value))

  protected def toFilter(value: UDTValue): Contact_Permissions =
    if (value == null) null
    else new Contact_Permissions(value.getString("name"),
      value.getString("description"),
      value.getString("Type"),
      value.getList("permission",classOf[Contact_Permission]).toList
    )


  protected def toUDTValue(value: Contact_Permissions): UDTValue =
    if (value == null) null
    else
      userType
        .newValue()
        .setString("name",value.name)
        .setString("description",value.description)
        .setString("Type",value.Type)
        .setList("permission",value.permission)
}

My question is How to set values in these nested UDT.

any help pls ??

  • This question is better for StackOverflow than to the dba.stackexcange.com – Alex Ott Sep 28 at 18:17
1

It would be easier to use Object Mapper that is a separate part of the DataStax Java driver - in this case you can easily map UDT and table into case classes. Something like this (I wrote an article on using Object Mapper with Scala - it's too long to past it here completely):

@UDT(name = "scala_udt")
case class UdtCaseClass(id: Integer, @(Field @field)(name = "t") text: String) {
  def this() {
    this(0, "")
  }
}

@Table(name = "scala_test_udt")
case class TableObjectCaseClassWithUDT(@(PartitionKey @field) id: Integer,
                                      udt: UdtCaseClass) {
  def this() {
    this(0, UdtCaseClass(0, ""))
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.