Sunday, October 31, 2010

JSON, Java, JSONSimple, Exposed Web services and all that jazz.

First off JSON is fun stuff and its everywhere. JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. Almost all web services when invoked return JSON data.

Here is a sample show JSON looks like:


There's even a 3 minute tutorial on how to understand it.

Once you understood what JSON is, then its time to do something with it. In my case, is to process (or parse) it using Java and taking the lazy way out, use a toolkit that somebody else wrote to parse it - JSONSimple.

The thing with JSONSimple is that it maps JSON stuff into Java Collections types like List and Map.
Here is another sample on how to use it:
 String s="[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";  
 Object obj=JSONValue.parse(s);  
 JSONArray array=(JSONArray)obj;  
 System.out.println("======the 2nd element of array======");  
 System.out.println(array.get(1));  
 System.out.println();  
           
 JSONObject obj2=(JSONObject)array.get(1);  
 System.out.println("======field \"1\"==========");  
 System.out.println(obj2.get("1"));       


See their website for more encoding and decoding examples using JSONSimple.

Friday, September 3, 2010

Playing with Vaadin + Spring

A few weeks ago, I stumbled on Vaadin Framework and it came with a catchy tag-line:
Vaadin is a Java Framework for building modern web applications that looks great, perform well, make you and your users happy.
That got my attention and so like a kid with a new toy, I started playing around with it and try to do stuff with it. It took me a while, read a few blogs but I have managed to get the hang of Vaadin and even got it to work with Hibernate and Spring. Vaadin with Hibernate was easy to figure out but Vaadin, Spring and Hibernate along with a DAO was a bit of a bitch of figure out but I eventually did. Nicolas Fränkel's blog gave me new insight on how to approach the problem. I sort of agree with his opinions on how to get Vaadin to work with Spring as compared to the techniques posted in the Vaadin wiki page. To quote Nicolas:
The first one uses the Helper “pattern”, a class with static method that has access to the Spring application context. IMHO, those Helper classes should be forgotten now we have DI since they completely defeat its purpose. If you need to explicitly call the Helper static method in order to get the bean, where’s the Inversion of Control?
The second solution uses Spring proprietary annotation @Autowired in order to use DI. Since IoC is all about decoupling, I’m vehemently opposed to coupling my code to the Spring framework.
 The trick was understanding how to isolate the Spring Context from the Vaadin application. With them separated, all that was needed was configuration files.

The thing was not to forget to configure the listener in web.xml file:
 <listener>  
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
 </listener>  
 <context-param>  
     <param-name>contextConfigLocation</param-name>  
     <!-- <param-value>classpath*:WebApplicationContext.xml</param-value> -->  
     <param-value>WEB-INF/WebApplicationContext.xml</param-value>  
 </context-param>  

After that the WebApplicationContext.xml file in the WEB-INF folder.
 <?xml version="1.0" encoding="UTF-8" standalone="no"?>  
 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"  
      xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"  
      xmlns:p="http://www.springframework.org/schema/p"  
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
                  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd  
                  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
                  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">  
      <context:property-placeholder location="classpath*:*.properties" />  
     <context:annotation-config />  
     <!-- add autowired or explicitly configured beans here -->  
     <bean id="myVaadinApplication" class="edu.chuybook.app.MyVaadinApplication"/>  
 </beans>  

With that, you can start doing some really interesting stuff!

Monday, July 12, 2010

Web design color schemes

You don't know this, but I suck at coming up with original web designs. I usually just rip one off from the oswd.org or somewhere. But somehow you got to evolve to become better at what you love to do and I happen to love the web.

So I started playing around trying to understand Color until I stumbled across this pretty neat piece of software that allows me to play with color schemes. Its called ColorSchemer Studio. They also have it as a Windows sofware. It's pretty cool. You get to play around with various color schemes for various projects.

For example, I have a new project and my clients inspiration for color is from a piece of abstract art.

Hey, I don't really undestand it myself. But its the clients money so....

Move on to ColorSchemer. You see ColorSchemer has this nifty little feature that can create color schemes from a picture. In this case, it was scan, digitized and saved as a high-resolution JPG file.

Here is a screenshot of the artwork inside ColorSchemer. Notice that you get to pick colors that are already in the picture.












Here, I am trying out the color schemes at various elements on a dummy web layout also within ColorSchemer.