Saturday, October 6, 2007

Active Record v/s Hibernate - Round I

You will have heard of shiny new web application framework called Ruby-on-Rails also known as RoR or simply Rails. It provides complete stack from ORM framework to Web Service. From this post onwards for next couple of posts I will compare and contrast between different components of JEE and Rails. I will begin with Hibernate from JEE and Active Record from Rails. The Round I of the Active Record v/s Hibernate will be on the basis of Mapping

The very first aspect is mapping. Hibernate requires the POJO (Java objects that represents the tables) be mapped with the tables using XML. That means if there is even a slight change in the Database schema such as the length of a particular field is changed, then you will have to make changes not only to the corresponding POJO but also to the mapping file. If you fail to do so, then run-time exception is the most common and the least problem that you will face.

Now lets look at Active Record. The driving principle of Rails is "Convention-over-Configuration" (but version 2.0 is deviating from it, more on that later). So , to map a class to a database table you just have to follow conventions. The conventions are

  1. The class derives from ActiveRecord::Base i.e. Base class of ActiveRecord module
  2. The name of the class must be same as that of the table
  3. The name of the class should start with a capital letter (though this applies to all Ruby classes)
Thats it. No XML, no configuration. Even if there is a change in the table, the corresponding class will adapt to it automatically at runtime through reflection and meta-programming. So that is the difference between Active Records and Hibernate on the basis of Mapping. In the next post I will continue comparing the two on how ORM classes are defined.

Authors Note: I am really sorry for the gap between the posts. It wont happen again (this time for sure). For now on you can expect atleast one post in a week.

Thursday, October 4, 2007

Accessing Managed Beans from backing bean


Sun created JSF as a competition for ASP.net. Though the idea is good, yet, the Sun stuck with the aspect of configuration. So if I need the framework to manage any of my beans automatically, then I need to specify it in faces-config.xml. Lets say my bean is named ResultBean, then I will have to put it as follows

<managed-bean>


<managed-bean-name>Result</managed-bean-name>

<managed-bean-class>org.me.ResultBean</managed-bean-class<

<managed-bean-scope>session</managed-bean-scope>

<managed-bean>

Now let us say there is another bean named ValueSetter, which is also a managed bean, wants to access the ResultBean, then the technique is classic Java technique. It goes something like as under

  • Get a reference to FacesContext which essentially is ServletContex when compared to Servlet
  • From FacesContext reference get current Application object
  • From Application object get a reference to VariableResolver object
  • Call resolveVariable method on it passing reference of the FacesContext and the name of the bean as string (not the class name)
In code it will be as follows

FacesContext context = FacesContext.getCurrentInstance();

ResultBean bean = (ResultBean) context.getApplication().getVariableResolver().resolveVariable(context, "Result");

As you have seen how many objects need to be used. Cant it be made simpler?

Tuesday, October 2, 2007

JSP and Servlet - Implicit objects



JSP provides following implicit objects
  1. request
  2. response
  3. session
  4. page
  5. application
  6. exception
  7. pageContext
  8. out
The first question any interviewer or external asks is to name all the implicit object. The next question is which Servlet object corresponds to which JSP implicit object. So from this post onwards I will discuss this aspect. This post looks at request object.

You will be knowing that when JSP is compiled, it becomes a Servlet. HttpServletRequest is one of the two objects passes as arguments to the service method of the Servlet class. In case of compiled JSP, the name of the service method is _jspService, the only method that a developer cannot override. When the compilation takes place the request object is accessed as an object of HttpServletRequest. To cut a long story short request object corresponds to HttpServletRequest. In the next post I will discuss about response and out objects. Till then keep visiting...

Sunday, September 30, 2007

Why Annotations cant be used always?

That exactly what my question is. JEE 5 gives you a choice to use annotations instead of configuration files almost everywhere - even in Servlets. However, why such a facility be extended to frameworks such as JSF where I still have to use configuration files.

Frameworks including JSF needs to be compilation stage as well as configuration stage to deploy them successfully. If servlets can be deployed without configuration file(web.xml), then why such a facility is not being extended to JSF? I am leaving out Struts because its outside Sun's control. And get me wrong, I am not against configuration. My only question is when an alternative is available and such an alternative can ease development, then why is not being implemented? With that I stopping my rant.

Saturday, September 29, 2007

EJB 3.0: A Step in the Right Direction



In the last post I ranted about problems with GMap. However, this time its not a rant. Thats right EJB 3.0 is a step in the right direction. The reason are as follows

1. No more configuration files. Annotations takes care of all the configuration. For example to tell container that a class Test is a Session bean, you will annotate it as follows
@Stateless public class Test{ //rest of the class }
2. No more home or remote interfaces. Just a simple Java class with annotations. Simple Java classes are known as Plain Old Java Objects or POJO. All the types of Beans in EJB 3.0 are based on POJO. So you dont need to extend or implement any interface. Take a look at the above example. It is a Stateless Session Bean, yet, does not extends or implements any Remote, Home or Bean interface.

3. Clients dont need to depend on JNDI. Instead dependency injection is used. More about dependency injection in next post.

4. Entity Beans now have full support for CRUD operations. CRUD is short for Create, Retrieve, Update and Delete. Prior to version 3.0, Entity Beans supported only limited Retrieve operation (also known as Select operation) that did not include full joins.

Thats all about new features of EJB 3.0. In the future posts, I will discuss about development using EJB 3.0. Visit again...


Friday, September 28, 2007

Back again

It has been a long time since the last post. However, from now on I will be posting on a regular basis. There is a chance of changing the name of the blog as well. But thats for the future. Now its time for ranting. Yes rant. Its about GMap API.

The simple app I had created works well from my home where there is no router or proxy per se. But, when I tried it from my friend's home pc which is behind a proxy, it started to show Paulo Alto even when I gave coordinates of India. Whats the problem here? Any idea anybody?

Friday, May 11, 2007

Sorry For No Post

I am extremely sorry as my schedule is not allowing to make any new posts. Once I am through the schedule I will start again.

Tuesday, April 10, 2007

GMap Animation and Events -II: Events

A quick look at events supported by GMap. GMap supports event handling through the Event listener pattern. The class that provides support for event handling is GEvent class. The addEventListener() method of GEvent class. It takes three parameters:

1. Object of GMap:

The map on which event has to be applied.

2. The event to be Handled:

It is a string. The string contains event names. There are two commonly used events:

i. click:

It is the most common of all the events. It is generated (or happens) when one presses and releases the left mouse button on the map.

ii. moveend:

It is generated when the user stops navigating or panning the map. It essentially means end of moving a map.

Following code shows how to use both types of events:

The code is a slight modification of the initial code to display map.

function load() {

if (GBrowserIsCompatible()) {

var map = new GMap2(document.getElementById("map"));

//map.setMapType(GMapType.G_HYBRID_MAP);

map.addControl(new GSmallMapControl());

map.addControl(new GMapTypeControl());

map.addControl(new GScaleControl());

map.setCenter(new GLatLng(31.122027, 77.111664), 13);

GEvent.addListener(map,"click", function(){

alert(map.getCenter())

}

);

//for the event moveend

GEvent.addListener(map,"moveend", function(){

alert(map.getCenter())

}

)

}

}

The getCenter() method of the GMap2 returns the current focus latitude and longitude of the map. That’s all for this quickie.

Friday, March 23, 2007

Animations and Events in GMap - I : Basic Animation

The Hello World experiment has impressed upon me the ease of setting up Google Maps for a website. However, it had only limited functionality. Also I wanted to check how it works with ‘conventional’ JavaScript APIs. The best way is to cook-up some animation (eventhough it may not mean much). So when I saw a function to change the focus of the area displayed, I thought let me create an animation with it. In JavaScript, there are two kinds of timers available –

1. One that executes a given function exactly once after a specified amount of delay

2. Another that executes the function repeatedly after a specified interval.

The corresponding functions are:

1. setTimeout and

2. setInterval

respectively. Both are the methods of window object. So now how to use it to animate google map. For that the API I am going to use is:

panTo:

This is a method of GMap2. What it does is it changes the center of the map to the given point represented by latitude and longitude.

So how to use this to achieve animation? The methods are as follows:

1. By using setTimout with panTo:

Adding the setTimeout just after setting the focus point of map, would create a one shot animation. That means it would be panned only once. The code would look like :

function load() {

if (GBrowserIsCompatible()) {

var map = new GMap2(document.getElementById("map"));

//map.setMapType(GMapType.G_HYBRID_MAP);

map.addControl(new GSmallMapControl());

map.addControl(new GMapTypeControl());

map.addControl(new GScaleControl());

map.setCenter(new GLatLng(31.122027, 77.111664), 13);

window.setTimeout(function() {

map.panTo(new GLatLng(31.152027, 77.111664));

},18000

);

}

}

the setTimeout is given an anonymous function as the function to be called and nearly 15 sec as the time to wait before function is executed.

2. By using SetInterval with panTo:

This comes handy when u want to the focus return to a particular point again and again after a specified time interval. The syntax is similar to that of setTimeout. The only difference is in the way both functions. Following is the same code implemented with setInterval:

function load() {

if (GBrowserIsCompatible()) {

var map = new GMap2(document.getElementById("map"));

//map.setMapType(GMapType.G_HYBRID_MAP);

map.addControl(new GSmallMapControl());

map.addControl(new GMapTypeControl());

map.addControl(new GScaleControl());

map.setCenter(new GLatLng(31.122027, 77.111664), 13);

window.setInterval(function() {

map.panTo(new GLatLng(31.152027, 77.111664));

},18000

);

}

}

That’s it for this post. By the next post I would have experimented the event handling provided by GMap API.

.

Thursday, March 15, 2007

Getting Latitude and Longitude for GMap API- The easy way

A quick update. One of the most important and the thing of frustration for anyone starting with GMap API is getting hold of desired latitude and longitude. There are many ways. I am listing here one of the easiest ways to get the latitude and longitude of desired location. Here are the steps:

  1. Go to the following site : http://www.infosports.com/m/map.htm
  2. Navigate the map till you get the desired location. For example, I required the lat and long. of Shimla. So I navigated (panned and zoomed) till I got to Shimla.
  3. Then click on the spot. In my case its Shimla. A marker would be generated at that point.
  4. click on the marker to get the latitude and the longitude. Mine is 77.16496467590332, 31.102995000257405

That’s it. If there are easier ways do share.

.

Tuesday, March 13, 2007

Hello World the Google Map API Way: Adding Controls

In the last post I had said I wanted to add something extra to the Hello world of GMap. The extras are controls. So the second part is:

2. Adding Controls to the map:

The map looks plain without the controls for scaling and mapping. So I decided to jazz it up with the following controls:

i. GSmallMapControl:

It shows a control with four buttons to pan(move the map) in four directions alongwith buttons to zoom in and out. The constructor is GSmallMapControl().

ii. GMapTypeControl:

It provides the control to change the type of map. The types are map, satellite image and hybrid which shows map superimposed on satellite image. GMapTypeControl() is the constructor.

iii. GScaleControl:

As the name suggests, it shows a scale within the map.

So how to add them to the map? The way is pretty simple- use the addControl() method of GMap2 instance. Here is the way in code:

function load() {

if (GBrowserIsCompatible()) {

var map = new GMap2(document.getElementById("map"));

//add the controls to the map

map.addControl(new GSmallMapControl());

map.addControl(new GMapTypeControl());

map.setCenter(new GLatLng(31.122027, 77.111664), 13);

map.addControl(new GScaleControl());

}

}

If you have observed, there is a difference in adding GScaleControl when compared to others. The first two controls (for that most of the controls that I have tested) are added before setting the center of the map where as the GScaleControl() is set after the center of the map has been set. What would have happened if I had called it before setting the center? The result would have been a blank page with a JavaScript error. That’s what happened with me. If you get any different result please share.

That’s it for this post. Next I would be looking at event handling in Google Map.

Sunday, March 4, 2007

Hello World - The Google Maps Way

Last week I had said that I would be cooking up a "Hello World" kind of app. I did it. There were two parts:

  1. Display default location provided by GMap API:

In nutshell, just make the sample app provided by Google while registration, work on my local system. It also clarified one of my doubts - where is the API library of GMap located? 'Coz it was not provided when I registered. For some, it may be a silly question. Ok. So the answer is in the following line:

<script src=http://maps.google.com/maps?file=api&v=2&key=[yourkey]"

type="text/javascript"></script>

The src attribute is pointing to the place where APIs are available. The place is maps.google.com – the site of Google maps. Along with the URL key received while registering with Google maps. Next comes the implementation of custom logic. It starts with the function load():

<script type="text/javascript">

//<![CDATA[

function load() {

}

//]]>

</script>

The next step is to ensure browser compatibility with client browser. This is done using GBrowserIsCompatible().

<script type="text/javascript">

//<![CDATA[

function load() {

if (GBrowserIsCompatible()) {

}

}

//]]>

</script>

Once compatible browser is found, then an instance of GMap2 class is created. The parameter passed id the div object in which the map has to be shown:

<script type="text/javascript">

//<![CDATA[

function load() {

if (GBrowserIsCompatible()) {

var map = new GMap2(document.getElementById("map"));

}

}

//]]>

</script>

The last step is to display the map using setCenter() method of GMap2. the method takes latitude , longitude and zoom factor as parameters. Of these zoom is optional.

<script type="text/javascript">

//<![CDATA[

function load() {

if (GBrowserIsCompatible()) {

var map = new GMap2(document.getElementById("map"));

map.setCenter(new GLatLng(31.122027, 77.111664), 13);

}

}

//]]>

</script>

The last but not least is the HTML especially the div part:

<body onload="load()" onunload="GUnload()">

<div id="map" style="width: 500px; height: 500px"></div>

</body>

The load() has to be called in body’s onLoad is obvious. Calling GUnload() is a good practice to avoid memory leaks. That’s for the first part.

Friday, February 23, 2007

Google Maps : Getting the Key

I started my exploration of Google Map API. For those who came late, Google Map API provide way to harness the power of Google Maps for your site. The API itself is JavaScript based. So if you have intermediate knowledge of JavaScript, then you are ready to work with GMap API.

The first step is to get a registration key. The key is a string which GMap server uses to 'recognize' and authenticate you. To get the key the registration page asks for your web-site address. If you already have a website, then everything is well and dandy. However what if you are like me (I still dont have a page of my own)? My first thought was to grab a free web host such as geo-cities. But then decided to check the tried and tested 'localhost'. I was not sure about Google accepting it. I was in for a surprise. It accepted the URL http://localhost readily and provided me the key. And thus I was well and ready to do my exploration and experimentation with GMap API.

Next step is to test the API with GMap's version of Hello World. That I will be doing tonite. More about it tomorrow.

Thursday, February 22, 2007

Testing 123

I will be using this blog to give u a look into my experimentations with different web development APIs. You can also term it my ramblings if you wish. I am starting with Google Map API. Check out my blog again to know my first impressions about it.