I started using Play! along with Scala and so I've been using Anorm as my ORM. It reminds me very much of a project that I used, myBatis. Anorm (not Another ORM) is a SQL data mapper.
In my application, I need to create a summary of trades. Here is the code for the controller and the model. The code uses a trade date (yyyymmmdd) to call the correct trade summary.
Controller:
Domain/model:
Friday, December 21, 2012
Tuesday, December 18, 2012
Playing around with Play framework
I did a presentation on the Play framework using Scala for the Miami Java User Group (JVM). Below is the presentation along with the code to create a small application.
The first thing that you need to do is to download/install Scala and Play into your computer.
To create the application, go into your terminal console and do the following:
The structure is very similar to Ruby on Rails.
Looking at the conf/routes we can see the following: This says that the moment that the application goes to the "/" it will call the controller Application and invoke the "index" method.
The controller has the following code: The index method calls an Action and sends an OK, which is nothing but a HTTP 200 code, and calls the page views.html.index with the string parameter: "Your new application is ready". If you look at the apps/views/index.scala.html you will see the following:
The message that was passed by the Application controller is stored in the message: String. The application calls a main html page and passes two parameters: "Welcome to Play 2.0" and "message".
If you look at the app/views/main.scala.html you can see that the first parameter is the "title" and the second parameter is the HTML content:
Remove the "@play20.welcome(message) so that the page looks like this: Render the page again, and you will see only the parameter passed by the controller. Lets go to the controller and edit the text by changing it to "Playing around".
Go back to the web browser and refresh the page. As you can see, the application renders automatically. The one thing that I like about the Scala programming language is that it is statically typed, so I can check in my editor. Lets configure the application as an Eclipse project.
Go to the console application and type control "D" or control "C" to stop the application. Now, type "play". You should see the play console. By typing "eclipsify" the play application will be ready to be imported as a project for Eclipse. Just go into Eclipse and import the project (file, import, select foobar directory). Once you are done type "run" to start the application.
As I mentioned, the conf/routes has the configuration for all the routes. If we add a route:
And go to http://localhost:9000/foo
You should see the same as if you go to http://localhost:9000.
Lets create an application that create a bar. The first thing we need to do is to create a model object named bar. Create a model by creating a Scala class in apps/models/Bar
The object will have only an id and a name. Now, we need to create a way to persist to a database. For Play the DB of choice is H2. It is a memory database and it is easy to work for development. To enable the database go to conf/application.conf and uncomment two fields:
Now that we have the drivers and the database, we need to create a script to create the database. Create a file for the DB schema inside conf/evolutions/default/1.sql
Refresh the page and you should be prompted to load up this script (or evolution). Just click on "apply this script now!"
Now, lets go back to the Bar model class and add the following:
This section will be the domain, which simply creates the bar and inserts it into the database. As you can see, we use the anorm API to map the SQL. This is not an ORM but more like a MyBatis (an SQL mapper).
Now, lets go to the Application controller and add the two things: the controller and the view. The first thing we need is to intercept the HTML form and add the action for the controller. Here we add the action and send a message depending on the type of transaction (error or success). We need to create the view form, but first we need to add the route to the controller.
Now, lets create the form view in the views/index.scala.html: You should be able to see the form in the application. Lets try to add a "test" bar. You shouldn't have any errors.
Now, we need to displays all the bars added. To do this, lets first modify our domain/model Bar object and controller. Using the allBars, we will be able to get all of the records from the bar table. For the controller, we will add the following: Now, we need to add the route in the conf/routes: If we go to http://localhost:9000/bars
You should be able to see something like this: We will want to render this URL and get all the JSON objects and put them into the index page asynchronously via CoffeeScript. First, lets create a folder app/assets/javascripts/index.coffee Here, the application will go to bars URL and fetch all the records, then it will iterate through the records, and append a list of all the bar names. Lets add the JavaScript header and bars into the index.scala.html
Now, lets add the JavaScript into the app/views/index.scala.html You should be able to notice two things. You should be able to see all the inserted bars, and when you upload a bar, it should render automatically.
The first thing that you need to do is to download/install Scala and Play into your computer.
To create the application, go into your terminal console and do the following:
- play new foobar
- Keep the same name of the application
- Select the Scala template (1)
- Change the directory to the foobar directory
- type "play run"
- Go into http://localhost:9000
The structure is very similar to Ruby on Rails.
- app: is the directory for all classes in the application
- controllers: all the controllers/actions for the applications
- models: domain objects
- views: the scala html pages
- conf: has all the properties files including the routes
- logs: the logs of the project
- project: build classes
- public: CSS and others
- target: compiled classes
Looking at the conf/routes we can see the following: This says that the moment that the application goes to the "/" it will call the controller Application and invoke the "index" method.
The controller has the following code: The index method calls an Action and sends an OK, which is nothing but a HTTP 200 code, and calls the page views.html.index with the string parameter: "Your new application is ready". If you look at the apps/views/index.scala.html you will see the following:
The message that was passed by the Application controller is stored in the message: String. The application calls a main html page and passes two parameters: "Welcome to Play 2.0" and "message".
If you look at the app/views/main.scala.html you can see that the first parameter is the "title" and the second parameter is the HTML content:
Remove the "@play20.welcome(message) so that the page looks like this: Render the page again, and you will see only the parameter passed by the controller. Lets go to the controller and edit the text by changing it to "Playing around".
Go back to the web browser and refresh the page. As you can see, the application renders automatically. The one thing that I like about the Scala programming language is that it is statically typed, so I can check in my editor. Lets configure the application as an Eclipse project.
Go to the console application and type control "D" or control "C" to stop the application. Now, type "play". You should see the play console. By typing "eclipsify" the play application will be ready to be imported as a project for Eclipse. Just go into Eclipse and import the project (file, import, select foobar directory). Once you are done type "run" to start the application.
As I mentioned, the conf/routes has the configuration for all the routes. If we add a route:
And go to http://localhost:9000/foo
You should see the same as if you go to http://localhost:9000.
Lets create an application that create a bar. The first thing we need to do is to create a model object named bar. Create a model by creating a Scala class in apps/models/Bar
The object will have only an id and a name. Now, we need to create a way to persist to a database. For Play the DB of choice is H2. It is a memory database and it is easy to work for development. To enable the database go to conf/application.conf and uncomment two fields:
Now that we have the drivers and the database, we need to create a script to create the database. Create a file for the DB schema inside conf/evolutions/default/1.sql
Refresh the page and you should be prompted to load up this script (or evolution). Just click on "apply this script now!"
Now, lets go back to the Bar model class and add the following:
This section will be the domain, which simply creates the bar and inserts it into the database. As you can see, we use the anorm API to map the SQL. This is not an ORM but more like a MyBatis (an SQL mapper).
Now, lets go to the Application controller and add the two things: the controller and the view. The first thing we need is to intercept the HTML form and add the action for the controller. Here we add the action and send a message depending on the type of transaction (error or success). We need to create the view form, but first we need to add the route to the controller.
Now, lets create the form view in the views/index.scala.html: You should be able to see the form in the application. Lets try to add a "test" bar. You shouldn't have any errors.
Now, we need to displays all the bars added. To do this, lets first modify our domain/model Bar object and controller. Using the allBars, we will be able to get all of the records from the bar table. For the controller, we will add the following: Now, we need to add the route in the conf/routes: If we go to http://localhost:9000/bars
You should be able to see something like this: We will want to render this URL and get all the JSON objects and put them into the index page asynchronously via CoffeeScript. First, lets create a folder app/assets/javascripts/index.coffee Here, the application will go to bars URL and fetch all the records, then it will iterate through the records, and append a list of all the bar names. Lets add the JavaScript header and bars into the index.scala.html
Now, lets add the JavaScript into the app/views/index.scala.html You should be able to notice two things. You should be able to see all the inserted bars, and when you upload a bar, it should render automatically.
Wednesday, October 31, 2012
Groovy's Eclipse GroovyRuntimeException error with Log4J
When I updated my Eclipse's Juno plugins, all my Groovy projects came in with exception. The error seems to be with the Log4J annotation "@log4j". This is what they said:
I was able to solve it by adding the latest snapshot here:
Once I was able to add this, all my errors went away.
Sunday, October 14, 2012
What is the product of your company? - is a byproduct
I follow Zach Holman, he works at GitHub. I follow him not because the guy is a techy, or because he works at GitHub, but because the guy just makes sense. Many of his blogs resonate through the different start-ups and companies I've worked. Specially, how to run a company which he talks in hist post the product is a byproduct.
I worked in many places that cared more about the "perception" of the company rather than the people. Many manager/directors worry about the image of the company without realizing that they are setting the tone of the company's culture (their workers), and this will affect their bottom-line/product. For example, "we expect you to work from 9 am - 6 pm", "we expect you to work AT the company, not at your house", "we expect you to dress a certain way - we have a dress code", "we need to ship this feature, we don't care how you get it done…that's your problem."
When we started a my former start-up, we thought we understood our customers, we thought we knew what they wanted, and we just wanted an application that WE would like. After all, "build it, and they will come", right? WRONG! The bottom line is that we were assholes. We really didn't have it right. It was until hard lessons learned that we started listening to our customers and focused on what they wanted rather on what we thought they wanted. But also, it was until we started changing our culture and hiring the right people that the company finds its product.
Zach talked about things like:
As Zach mentions:
I worked in many places that cared more about the "perception" of the company rather than the people. Many manager/directors worry about the image of the company without realizing that they are setting the tone of the company's culture (their workers), and this will affect their bottom-line/product. For example, "we expect you to work from 9 am - 6 pm", "we expect you to work AT the company, not at your house", "we expect you to dress a certain way - we have a dress code", "we need to ship this feature, we don't care how you get it done…that's your problem."
When we started a my former start-up, we thought we understood our customers, we thought we knew what they wanted, and we just wanted an application that WE would like. After all, "build it, and they will come", right? WRONG! The bottom line is that we were assholes. We really didn't have it right. It was until hard lessons learned that we started listening to our customers and focused on what they wanted rather on what we thought they wanted. But also, it was until we started changing our culture and hiring the right people that the company finds its product.
Zach talked about things like:
Your product should be a byproduct of the people, process, and technology of your company.
Foster a good environment, be more likely to create a good product.
Hire those bothered by suck. You want fixersI'm very committed to my companies, but I'm also in LOVE with my family. This is constant tough of war in my head. I do want to help the company in everything I can, but I also want to help my son with homework, and spend some time with my daughter and wife. I do feel that where I work is MY company. I know that there are some people that said that it shouldn't be like this, "you shouldn't live for work, but work to live". But that's not what I mean. I work as it's my company in the perspective of shutting down my MacBook Pro,with the feeling of "I kicked ASS today". It is not always like this, but most of the time, they are. I like the fact that people (coworkers and directors) say, "ever since we hired Marcelo, this place is TIGHT", but I also want my family to say, "we love were you work, because we can see you and spend quality time with us".
As Zach mentions:
Any time you interview a potential hire, you need to ask yourself not only if they're talented or collaborative but also if they're capable of literally running this company, because they will." - VALVe
Hire broad people. Hire diverse people.
Good culture attracts good people
Be family friendly
Flexible location, hours, workload
Wednesday, August 29, 2012
Groovy's each does not exit when using return
Groovy's each is a very useful closure in collections. However, if you want to exit out of the code using return, it does not work. Below is the code. In this case, if the listId is empty, it sets the value for the "isValid" variable but never returns or break from the loop.
The way that I was able to solve it was using a "find" method. Inside the if statement, I set the value for the "isValid" and then return true. The "true" value tells the "find" statement that the value was found, and therefore, it exits out.
Wednesday, June 20, 2012
Installing Graphite on Ubuntu 12.04
Install dependencies:
Install whisper:
Install and configure Carbon (data aggregator)
Install Graphite (webapp):
At the moment, uncomment the following line
Configure Apache:
Edit the following file:/etc/apache2/sites-available/default Make sure that the configuration for WSGISocketPrefix is set as follows:
Otherwise, you will get the following error:[Tue Jun 19 13:21:28 2012] [error] [client 192.168.0.112] (2)No such file or directory: mod_wsgi (pid=19506): Unable to connect to WSGI daemon process 'graphite' on '/etc/apache2/run/wsgi.19365.1.1.sock' after multiple attempts.Create initial database: Create the admin for graphite: Start carbon (data aggregator)
Check the log file for any error: /opt/graphite$ find . -name '*.log' | xargs tail -F
To Feed the data do the following:
Go into the server localhost in your prefer browser and you should see graphite site.Tuesday, April 24, 2012
First and last day of the month using Joda text
I had to create a SQL dump right after a new quarter starts. For example, if today is April 1st, then I had to create a dump file with the dates from 2012-01-01 to 2012-03-31. The only problem was generating the first and end of the month using Java. The best calendar/date API is by far Joda Date. Here is the script on how I generated the two dates:
Subscribe to:
Posts (Atom)