Wednesday, November 28, 2012

Handling styles and style classes using javascript - Quick guide

In this short post I am going to show you how handle styles and style classes using javascript. Having a good understanding on this is vital for any developer who works on web applications.

Ok, first assume that you have following CSS classes and a DIV as below.
<style>
 .oldClass{
    background-color:yellow;
    border:solid;
 }
 .newClass {
    background-color:green;
 }
</style>

<div id="myDiv" class="oldClass">
Content here...
</div>

As you already know you can apply a style to the div as below. (Here I am applying a background color to the DIV)

document.getElementById("myDiv").style.backgroundColor = 'red';

You know that the css property for background color is background-color. But in above javascript it is used as backgroundColor. Similarly any css property which contains hyphens are written in camel case when using with javascript as above.

Following script adds a css class to the div.
document.getElementById("myDiv").className = "newClass";

Note that if your element already has one or more css classes defined, all of them are removed when new class is applied.
Following also do the same thing, but doesn't work in IE.
document.getElementById("myDiv").setAttribute("class", 'newClass');

Below one works in IE only and also do the same thing.
document.getElementById("myDiv").setAttribute("className", 'newClass'); 


Sometimes you may need to add another class to the element without replacing the existing classes. This is how to do that.
Here both classes are applied to the DIV.
Note the space before the class name. It is required.
document.getElementById("myDiv").className += " newClass";
Here both classes are applied to the DIV.

Now assume that your div already has more than one classes applied to it like this.
<div id="myDiv" class="oldClass1 oldClass2">
    Content here...
</div>
Now think that you want to remove only the class oldClass2 .

You can simply do it as below.
document.getElementById('myDiv').className =
    (document.getElementById('myDiv').className).replace('oldClass2', '');

Sunday, November 25, 2012

Hibernate InvalidStateException - How to identify invalid fields

    If you are using Hibernate, you should be familiar with the InvalidStateException which is thrown when you are going to persist data to a table with invalid values. Simply by looking at the stack trace it is impossible to find the table columns or Entity fields for which the validations are failed.
    But actually it is not very difficult to find those fields. First you need to catch the InvalidStateException by using a try-catch block. The InvalidStateException class has a method getInvalidValues() which returns an array of InvalidValue s. In this InvalidValue class there are several methods to find the information you need. So by Iterating through this array you can get the info you need.
try {
  //Persistence logic here...
} catch (InvalidStateException e) {
  for(InvalidValue invalidValue : e.getInvalidValues()) {
    System.out.println(invalidValue.getPropertyName());//Property on which validations
                                                       are failed
    System.out.println(invalidValue.getValue());//invalid value
    System.out.println(invalidValue.getMessage());//message for invalid value
    System.out.println(invalidValue.getBeanClass());//Entity bean which belongs the
                                                    property
  }
}