Tuesday, March 28, 2006

Google Adsense Ads gets disabled

Google disabled my ads today saying "my AdSense account was found to be related to an account previously disabled for invalid click activity" as if I have all the time in the world to create Google accounts and they approve it!!! This means I am not allowed to participate further in Adsense and my money is properly returned to the affected advertisers. Woooooooooo....what a statement...I got scared.

Upon contacting them further, they kept on beating around the bush without any strong proof....haha (they never had one!) Google disabled all my friends Adsense accounts when they reached close to $40 and so unless you are a big website with CTR<1% dont ever use Adsense....it sucks BIG TIME!

Monday, March 20, 2006

Email Etiquettes

Communication has come a long way. And, with the ever-rising popularity of the Internet as a vehicle for formal business communication, Netiquette, a relatively new concept, is a must-know for everyツ professional.ツ It is a set of standards of acceptable behaviour you need to follow when online, and includes rules you need to follow whileツ sending and receiving formal business missives.

Why do you need netiquette? For two reasons: It creates an impression of professionalism. And it ensures the correct message gets across. Some simple rules, if followed while sending e-mail, make all the difference
between a good professional impression and a bad one.

Want to improve your English?

Why are you writing to me?

State the purpose of your e-mail -- it is a good practice to have a subject line that explains what follows and how high on the priority list it should be. This information has to be in two places:

i. The subject box, which is part of the compose e-mail form. Here, state the reason for your mail. If you are writing it to apply for leave, you could say 'Leave application'.

ii. Subject line in the main e-mail body;ツ just as youツ would in any formal business letter. Here, you could say:
Sub: Leave application, April 1-April 15 2006

Conference call rules you must know
Greet me right!

Address people you don't know as Mr, Mrs, Ms or Dr. Address someone by first name only if you are on a first name basis with each other; it isツ okay, under these circumstances, to useツ first names on aツ formal business missive. If you do not know the name of the person, or whether it is a man or a woman, it is best to address the person concerned as: Dear Sir / Madam,ツ Whomsoever it may concern,ツ The Manager; etc.

And your point is?

Get to the point. Verbosity and extreme terseness are two ends of a spectrum, you should try for the middle -- state your point without sounding rudely brief or chatty. Remember to state your point and what you
expect from the reader of your mail in clear terms. There should also be a clear structure -- an introduction, body and conclusion.

Do introductions frighten you?

2 use or not 2 use窶ヲ.

Never use 'sms-ese' or informal abbreviations in your email. U instead of you, 2 instead of to or too, plz instead of please, thanx instead of thanks and 4 instead of for are a strict no-no. While are all right for personal e-mail, they show a level of informality not encouraged in formal business communication. Frequently used abbreviations you may use include FYI (for your information), Pvt., Ltd., Co., etc.,ツ OK.

The magic of spells窶ヲ

Though using a spell check is a must, don't rely on it completely. The most common areas of errors and confusion are -- two, too and to.
For eg:

Wrong: I would like two order too other books two.
Correct: I would like to order two other books, too.

A hit or miss effort with spellings does not help -- use a dictionary.

Write right窶ヲ

Good grammar is very important. A correctly framed sentence, with proper punctuation in place, is what you should aim at. Be very careful with commas, especially, as they change the meaning of a sentence.

A good example would be:
Wrong: All foreign tea, tree, oils are free from duty.
Correct: All foreign tea tree oils are free from duty.

Use action words and 'I' statements -- they evoke a sense of reassurance in the reader.
For eg:

On receipt of your earlier mail, I/ we have already set things in motion and I/ we assure you that you will receive your order on time.

I was responsible for the day-to-day working and administration of the office; planning, scheduling, and achieving targets were my areas of contribution.

Mind your P's and Q's

Though a friendly tone is encouraged, basic corporate etiquette rules do apply. So, maintain a well-mannered, friendly polite stance. Gender-neutral language is politically correct -- couch your e-mail accordingly. This essentially means you should not assume a person's gender on the basis of the designation, keep the e-mail neutral.

Attachment breeds detachment窶ヲ

With worms, viruses, and spam, nobody wants to open attachments anymore, not even if the e-mail is from one's own mother. If you do need to send an attachment, confirm this with the recipient first.

Like an arrow shot from a bow窶ヲ.

An e-mail is like the spoken word -- once sent, you can't recall it. By the time you press the recall button, chances are it has already reached and, with it, your recall message. This compounds the embarrassment. So,
think before you dash off something.

The KISS rules窶ヲ.

Keep It Short and Simple. Use simple sentences, words that don't need a dictionary. Use universal formats -- not all systems support HTML rich style, or tables and tabs. You could lose much by way of appearances if
your recipient's system can't support all that fancy formatting you spent hours working on.

Smile please!

Smileys and other emoticons are a way to add 'body language' to e-mail. When used appropriately and sparingly, smileys do bring a touch of personalisation to otherwise impersonal mail. Of course, due care with
regards to the appropriateness of the communication must be taken. You would not use emoticons in job application covering letters, while delivering bad news (delay in order, loss of job, etc). Use discretion.
Emoticons are generally used to add comfort to the communication or soften the blow. The most common smiley faces are probably these:

:-) OR :)ツ ツ ツ ツ ツ ツ ツ ツ ツ ツ ツ Just a smile / can be used for greeting, or making a point gently.

:-( OR :(ツ ツ ツ ツ ツ ツ ツ ツ ツ ツ ツ To show mild displeasure or that something is not going the way you want it to.

;-)ツ ツ ツ ツ ツ ツ ツ ツ ツ ツ ツ ツ ツ ツ ツ ツ ツ ツ ツ ツ Equivalent to a wink -- used to convey that a particular comment is aツ joke andツ not to beツ taken too seriously.

;->ツ ツ ツ ツ ツ ツ ツ ツ ツ ツ ツ ツ ツ ツ ツ ツ ツ ツ ツ To be used sparingly, and with people you know well, as this signifies a provocative comment.

Humour doesn't travel
What seems funny to you may be offensive to someone else. Remember, humour doesn't travel well. Jokes about religion, sports, political figures, and women may come across as tasteless and should be avoided at all costs.

Swift and prompt
Be prompt in replying. That is why we have the Internet. If a prompt response were not expected, one would use the postal service.

CAPITAL, MY DEAR FELLOW
IF YOU WRITE IN CAPITALS, IT SEEMS AS IF YOU ARE SHOUTING. You could get really angry responses to your e-mail if you do so, and trigger a flame war.

Fw. Fw. Fw.
Do not forward chain letters -- simply delete them. Also avoid forwarding them to professional contacts.

Shh窶ヲ It's a secret窶ヲ

If it is a secret, don't send it via e-mail -- you never know where it will end up.

Connections窶ヲ

Keep the thread of the message as part of your mail; this will help keep the context handy. The thread is the previous message/s in context to which this e-mail is being written. Last but not least, please, please read the e-mail before you click the 'Send' button. It will save you a lot of embarrassment and misunderstanding.

Thursday, March 09, 2006

Bridge the gap between Struts and Hibernate

Extend Struts for a more object-oriented relationship with Hibernate

Summary
Hibernate and Struts are currently among the most popular open source libraries on the market. Effectively, they are the default developer selections among competing libraries when building Java enterprise applications. Although they are often used in conjunction with one another, Hibernate was not primarily designed to be used with Struts, and Struts was released years before the birth of Hibernate. To put them to work together, some challenges remain. This article identifies some of the gaps between Struts and Hibernate, particularly related to object-oriented modeling. It also describes a solution for bridging these gaps that involves an extension to the Struts framework. All Web applications built upon Struts and Hibernate can derive benefit from this generic extension. (1,870 words; March 6, 2006)

In the book Hibernate in Action (Manning, October 2004), authors Christian Bauer and Gavin King reveal the paradigm mismatch between the two worlds of the object-oriented domain model and the relational database. Hibernate does an excellent job at the persistence layer of gluing these paradigms together; however, a mismatch remains between the domain model (the Model-View-Controller model layer) and HTML pages (the MVC view layer). In this article, we examine this mismatch and consider an approach for resolving the disparity.

The paradigm mismatch rediscovered
Let's look at a classic parent-child relationship example (illustrated in the code below): product and category. The Category class defines an identifier id of type Long and a property name of type String. The Product class also has an id of type Long as well as a property category of type Category, representing a many-to-one relationship with instances of the Category class (i.e., many Products can belong to one Category).

/**
* @hibernate.class table="CATEGORY"
*/
public class Category {
private Long id;

private String name;

/**
* @hibernate.id generator-class="native" column="CATEGORY_ID"
*/
public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

/**
* @hibernate.property column="NAME"
*/
public String getName() {
return name;
}

public void setName(Long name) {
this.name = name;
}
}

/**
* @hibernate.class table="PRODUCT"
*/
public class Product {
private Long id;
private Category category;

/**
* @hibernate.id generator-class="native" column="PRODUCT_ID"
*/
public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

/**
* @hibernate.many-to-one
* column="CATEGORY_ID"
* class="Category"
* cascade="none"
* not-null="false"
*/
public Category getCategory() {
return category;
}

public void setCategory(Category category) {
this.category = category;
}
}

It is desirable that a product be re-categorized, so our HTML view provides a drop-down list of all categories to which products can be assigned:


The categoryId field specifies the user selection of a Category. In Struts, we define ProductForm as an ActionForm subclass to collect the user's inputs from the HTTP request:

public class ProductForm extends ActionForm {
private Long id;
private Long categoryId;
...
}

Here's the mismatch: in the Product domain object, the category property is of type Category, whereas the ProductForm only has a categoryId property of type Long. This mismatch not only increases inconsistency, but also involves special code for converting between primitive type identifiers and associated objects.

Part of this discrepancy is due to the HTML form itself: it resembles a relational model, instead of an object-oriented model. The object-oriented versus relational mismatch in the persistence layer is addressed by object-relational mapping (O/RM). The similar mismatches in the view layer still remain. The key is finding a solution to make them work together seamlessly.

Struts capabilities and limitations
Fortunately, Struts is able to render and interpret nested object properties. The category drop-down list can be rewritten using the Struts page-construction (html) tag library:

property="category.id">
No Category


We assume that categories is a list of all the Category objects. Now we can change ProductForm to be more object-oriented by exchanging the categoryId property for a category property of type Category. This change will render the copying of property values between Product and ProductForm a trivial task, because they have mostly the same properties and types:

public class ProductForm extends ActionForm {
private Long id;
private Category category;
...
}

Once we have created and configured the rest of the Struts actions, the configuration, validators, the JavaServer Pages (JSP), and the data persistence layer with Hibernate, and start to test, we immediately run into a NullPointerException when accessing ProductForm.category.id. Of course! The category reference has not been set yet, and Hibernate also sets many-to-one associated objects to null if no value is in the corresponding database field. Struts requires all objects to be instantiated before rendering (when displaying a form) and populating (when submitting a form).

Let's look at how we bridge this gap by using ActionForm.reset().

(Not so) notorious Struts ActionForm
During my first week using Struts, one of my main questions was why I had to maintain exactly the same two copies of properties and getter and setter methods between domain objects and ActionForm beans. This tedious exercise has become one of the major complaints within the Struts community.

In my opinion, ActionForm beans exist for good reasons. First, they can be distinguished from domain objects because they serve different roles. In the MVC pattern, domain objects are part of the model layer, while ActionForm beans are part of the view layer. Because the fields on the Webpages may differ from those in the database, some custom conversion is common. Second, the ActionForm.validate() function comes in handy for nonstandard validation rules not covered by the validator framework. Third, there may be other custom, view-specific behavior that we require, as shown below, but don't want to implement as part of the domain layer, especially when a persistence framework manages domain objects.

Submitting a form
Let's leverage one of ActionForm's built-in methods—reset()—to address the mismatch between the view and model layers. The reset() method is called before the ActionForm properties are populated by the controller servlet when handing a request. The method is typically used when checkbox fields must be explicitly set to false so unchecked checkboxes can be correctly recognized. reset() is also a perfect place for instantiating associated objects required during view rendering. The code will look like this:

public class ProductForm extends ActionForm {
private Long id;
private Category category;
...
public void reset(ActionMapping mapping, HttpServletRequest request)
{
super.reset( mapping, request );
if ( category == null ) { category = new Category(); }
}

}

Before Struts populates ProductForm with the user submitted values, it calls reset() so the category property will have a non-null reference. Please note that it is necessary to check category to see if it is null as explained next.

Editing a form
So far, I have addressed the problem that occurs when a form is submitted. What about when rendering a form? The html:select tag also expects a non-null reference, so we're going to call the reset() method again before the form is rendered. We do that by adding a line in the action class. In our case, the EditProductAction:

public class EditProductAction extends Action {
public final ActionForward execute( ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response ) throws Exception
{
...
Product product = createOrLoadProduct();
ProductForm productForm = (ProductForm)form;
PropertyUtils.copyProperties( productForm, product );
productForm.reset( mapping, request );
...
}
}

I assume readers are familiar with Struts action classes and the Jakarta Commons Beanutils package. The createOrLoadProduct() method creates a new Product instance or loads an existing record from the database, depending on whether a create or modify action is being invoked. After productForm is populated, the productForm.category property value is set; therefore, it is ready for rendering. We must also ensure we don't accidentally overwrite associated objects if they are indeed valid and loaded by Hibernate. So before the object is instantiated, we must check to see if it is null.

Because the reset() method is defined in ActionForm, we can generically rewrite the above code and move it into a superclass, e.g., CommonEditAction, that deals with Struts action routines:

...
Product product = createOrLoadProduct();
PropertyUtils.copyProperties( form, product );
form.reset( mapping, request );
...

If you want a read-only view of a form without editing it, either check to see if the associated object is null on the JSP page or copy the domain object to the ActionForm bean and call the ActionForm bean's reset() method.

Saving a domain object
We solved the problem when submitting and rendering a form, so Struts is happy. What about Hibernate? When a user selects a null ID option—in our example, the "no category" option—and submits the form, productForm.category is a newly created Category instance with id equal to null. When the category property is copied to the product object and then persisted, Hibernate complains that product.category is a transient object and needs to be persisted first. Of course, we know it is empty and don't want it to be persisted; so we need to set product.category to null before the product is persisted. We also don't want to change the way Hibernate works; so we choose to clean up these temporary objects before they are copied to the domain object by adding a method in ProductForm:

public class ProductForm extends ActionForm {
private Long id;
private Category category;
...
public void reset(ActionMapping mapping, HttpServletRequest request) {
super.reset( mapping, request );
if ( category == null ) { category = new Category(); }
}

public void cleanupEmptyObjects() {
if ( category.getId() == null ) { category = null; }
}

}

We call the cleanupEmptyObjects() method in SaveProductAction right before the property values are copied from the ActionForm bean to the domain object; so if ProductForm.category is just the "placeholder" instance with a null ID, it will be set to null. Then ProductForm.category will be copied to the domain object, and the corresponding property in the domain object will also be set to null:

public class SaveProductAction extends Action {
public final ActionForward execute( ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response ) throws Exception
{
...
Product product = new Product();
((ProductForm)form).cleanupEmptyObjects();
PropertyUtils.copyProperties( product, form );
SaveProduct( product );
...
}
}

One-to-many relationships
I have not yet addressed the one-to-many relationship of Category to Product. We can add it to the Category metadata:

public class Category {
...
private Set products;
...

/**
* @hibernate.set
* table="PRODUCT"
* lazy="true"
* outer-join="auto"
* inverse="true"
* cascade="all-delete-orphan"
*
* @hibernate.collection-key
* column="CATEGORY_ID"
*
* @hibernate.collection-one-to-many
* class="Product"
*/

public Set getProducts() {
return products;
}

public void setProducts(Set products) {
this.products = products;
}
}

Note that setting the Hibernate cascade property to all-delete-orphan indicates that Hibernate should automatically persist all Product objects in the set when persisting the containing Category. It's not usually necessary to persist child objects along with the parent object; often, it's better to control child persistent operations separately. In our case, it is convenient to do so if we are allowing the user to edit the Category and its Products on the same page. Dealing with the contained set of Products is fairly straightforward:

public class CategoryForm extends ActionForm {
private Set productForms;
...
public void reset(ActionMapping mapping, HttpServletRequest request) {
super.reset( mapping, request );

for ( int i = 0; i < productform =" new">
}

public void cleanupEmptyObjects() {
for ( Iterator i = productForms.iterator(); i.hasNext(); ) {
ProductForm productForm = (ProductForm) i.next();
productForm.cleanupEmptyObjects();
}

}
}

Every project will have different requirements and variations, so I am not going describe every aspect of this particular implementation. The idea here is to demonstrate how one-to-many relationships can fit into the generic solution.

Work a bit smarter
We now have no problem viewing, editing, or submitting forms, and saving associated objects, but it remains somewhat cumbersome to reset and clean up all the associated placeholder objects for each ActionForm bean. We facilitate this task by implementing an abstract ActionForm class to manage this routine job.

For the generic implementation, we must iterate over all the domain objects managed by Hibernate, discover their identifiers, and test the values. Fortunately, the org.hibernate.metadata package contains two utility classes to retrieve domain object metadata. We use the ClassMetadata class to check if the object is Hibernate-managed, and if it is, we obtain the value of its identifier property. We also leverage the utility functionality in the Jakarta Commons Beanutils package to more easily obtain JavaBean metadata:

import java.beans.PropertyDescriptor;
import org.apache.commons.beanutils.PropertyUtils;
import org.hibernate.metadata.ClassMetadata;

public abstract class AbstractForm extends ActionForm {
public void reset(ActionMapping mapping, HttpServletRequest request) {
super.reset( mapping, request );

// Get PropertyDescriptor of all bean properties
PropertyDescriptor descriptors[] =
PropertyUtils.getPropertyDescriptors( this );

for ( int i = 0; i < propclass =" descriptors[i].getPropertyType();" classmetadata =" HibernateUtil.getSessionFactory()"> // This is a Hibernate object
String propName = descriptors[i].getName();
Object propValue = PropertyUtils.getProperty( this, propName );


// Evaluate property, create new instance if it is null
if ( propValue == null ) {
PropertyUtils.setProperty( this, propName, propClass.newInstance() );
}
}
}

}

public void cleanupEmptyObjects() {
// Get PropertyDescriptor of all bean properties
PropertyDescriptor descriptors[] =
PropertyUtils.getPropertyDescriptors( this );

for ( int i = 0; i < propclass =" descriptors[i].getPropertyType();" classmetadata =" HibernateUtil.getSessionFactory()"> // This is a Hibernate object
Serializable id = classMetadata.getIdentifier( this, EntityMode.POJO );

// If the object id has not been set, release the object.
// Define application specific rules of not-set id here,
// e.g. id == null, id == 0, etc.
if ( id == null ) {
String propName = descriptors[i].getName();
PropertyUtils.setProperty( this, propName, null );
}


}
}

}
}

Exception handling code has been removed from the above code for readability.

Our new abstract AbstractForm class extends Struts ActionForm and provides our generic behavior for resetting and cleaning up many-to-one associated objects. When the relationship cardinality is reversed (i.e., for one-to-many relationships), each case is quite different, so just implementing the custom behavior in the specific ActionForm bean implementation proves a better approach.

Our final task is to change all of our ActionForm bean classes (ProductForm, CategoryForm, etc.) to inherit from AbstractForm, instead of ActionForm.

Conclusion
Struts and Hibernate are popular and powerful frameworks that can be extended to work more effectively with one other by bridging the gap between the domain model and the MVC view. This article describes a generic implementation of such a bridge that can be used in any Struts and Hibernate project with no major changes to the existing code.

Monday, March 06, 2006

Matrix - Believe in yourself!


I hate Matrix and such type of movies have never caught my attention. I am not for Special effects, strength, pace and action. I like movies which have meaning and convey some message. Matrix is about a computer hacker learns from mysterious rebels about the true nature of his reality and his role in the war against the controllers of it. This were my views until one day I was talking to Gaurav "haa haa main bhi likh dunga ek message ........"who ever appears to be online is actually offline ,this is how matrix works. Do not believe ur eyes, believe ur instincts" He says there is a meaning to Matrix "Believe in Yourself! and everything will go fine.....doesn't it tell us everytie when we r upto someone difficult and big. Well..I am still in thoughts..Dude!, did someone headshot me?!