Wednesday, May 8, 2013

Play 2.1.x Writes Troubles

SO, This week's assignment was to learn how to use Reads/Writes Combinators. I started off with the play documentation on the same( http://www.playframework.com/documentation/2.1.1/ScalaJsonCombinators ) ,I wrote the functions for reads and writes which is given below.
But what I actually need to do was make an implicit call to the Reads and Writes. So i rewrote my function and this is the modified code is:
This code gave me no explicit error, but when i ran the code the:


The error in this output was that it should show me the id, name, age and address. Not just the address.
After doing a lot of checking, I realized there was no error from the db or any other functions. The only error was somewhere in the detailsWrite(), and no matter what i did it only printed the last value from the json.

So, with a little help, this is what i learnt:
The detailsWrite() function was giving an error because of the 2- level path that i was using
( __ \ "user" \ "id" )

Once I changed that into a simple path, it worked perfectly.
So the final code is here:

And the correct output.



Friday, May 3, 2013

BigDecimal And Anorm

Today i came across a new Data Type.. Its called BigDecimal/BigInteger,etc.... I actually did not use this in my program but chanced on it because of an error i got while i tried to run my simple code for user details.


case class UserInfo (
  id: Int,
name: String,
age: Int,
address: String
)

object UserInfo{

 val ds = DB.getDataSource()

 val data = {
  get[Pk[Int]]("id") ~
  get[String] ("name") ~
  get[Int]("age") ~
  get[String]("address") map {
  case id~name~age~address => UserInfo(id, name, age, address)
  }                                        
 }

This was a simple code and should've run without any errors. I had no errors on my eclipse window... But on executing the code i got the following error for output.

[RuntimeException: TypeDoesNotMatch(Cannot convert 1:class java.math.BigInteger to Int for column ColumnName(userinfo.id,Some(id)))]

So i did what every newbie does... I googled the error.... To my surprise I wasn't the only one who came across this error. And after fiddling around with the code i figured out the following :

- The Database returns BigDecimal values that are part of the java.math.BigDecimal package, which is not a scala package. So we need to explicitly import this package.
import java.math.BigDecimal

So, this is my finally working code,

import java.math.BigInteger

case class UserInfo (
       id: Int,
name: String,
age: Int,
address: String
)

object UserInfo{

 val ds = DB.getDataSource()

 val data = {
  get[BigInteger]("id") ~
  get[String] ("name") ~
  get[Int]("age") ~
  get[String]("address") map {
  case id~name~age~address => UserInfo(id.intValue, name, age, address)
  }                                         //> data  : anorm.RowParser[models.UserInfo] = <function1>
 }

Yep, I made a few changes.. Well, thats what I'm coming to next... Here's what i couldn't figure out...

I couldn't figure out how to get Pk[Int] to be compatible with BigInteger. So even after importing the java package and changing my id type to Pk[BigInteger] i couldn't return a compatible id value.

If anyone has the answer to that, please let me know. Else whenever I figure it out I will update it.







One month later...

So I've been at this job for a month now, and i can finally say that i have managed to learn bits of everything thrown my way... Before i go further i should mention that i was out of the loop for almost two years so what might seem silly were actually genuine doubts and problems for me...

So far, I have managed to understand the concepts of Scala and Play. I have learnt how to use Json, XML and Forms with my Play Applications. Just today I finally worked through how to use Anorm to have persistent data... And I came across a data type know as BigDecimal. So that's what my next post is about.