URL Request Parameters using Javascript

function getRequestParameter( name ) {
name = name.replace(/[\[]/,”\\\[”).replace(/[\]]/,”\\\]”);
var regexS = “[\\?&]”+name+”=([^&#]*)”;
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return “”;
else
return results[1];
}

Let’s say you have the following URL:
http://www.foo.com/index.html?customerCode=123&consignment=321&debug=true

You want to get the value of consignment parameter so you call the javascript function as follows:
var value = getRequestParameter( ‘consignment’ );

Written by Ravi Nallakukkala on April 17th, 2008 with no comments.
Read more articles on Uncategorized.

Understanding JavaScript Prototypes

I never got the basics of Javascript Prototypes correct until i found this excellent article by Morris Johns

Written by Ravi Nallakukkala on January 14th, 2008 with no comments.
Read more articles on Uncategorized.

Exception handling framework using Spring AOP

In this article I will look at the various features which are required for a good exception handling framework and how Aspect oriented programming (AOP) will be used in the design of an Exception handling framework.

Lets Look at the key feature of an Exception handling framework

Learn more about exceptions here and find out the difference between Checked and unchecked exceptions here.

By large, Exceptions are classified in two categories

(more…)

Written by Ravi Nallakukkala on November 29th, 2007 with no comments.
Read more articles on Design.

Converting an Array to a Collection

Sometimes the common problems seems to be we don’t remember the API, one such scenario I recently faced was how to convert a Array to a collection? Following seems to decent ways :-)

// Fixed-size list
List list = Arrays.asList(array);

// Growable list
list = new LinkedList(Arrays.asList(array));

// Duplicate elements are discarded
Set set = new HashSet(Arrays.asList(array));

Written by Ravi Nallakukkala on November 29th, 2007 with no comments.
Read more articles on Java/ J2EE.

Spring Based JUnit Integration Test approach

The Spring Framework provides a good support for the integration tests for enterprise software applications and these support classes are packaged in spring-mock.jar and all these classes are JUnit test classes. org.springframework.test package provides valuable JUnit TestCase superclasses for integration testing using a Spring container and this approach is completely independent of any web/ application server.

Spring out-of-box provide the following functionality:

On Surface, Spring Test cases can be classified into

Spring based JUnit with Dependency Injection

For JUnit Classes which need to use Dependency injection feature of spring will be required to inherit the class ‘AbstractDependencyInjectionSpringContextTests‘ and the subclass need to implement the superclass’s abstract method ‘protected abstract String[] getConfigLocations();’ the implementation of this method need to return the spring configuration XML files which are required to participate in the execution of the test case.
E.g,
public final class ExampleDaoTests extends AbstractDependencyInjectionSpringContextTests {

// this reference will be automatically injected through spring
private ExampleDao exampleDao;

// a setter method to enable DI for the Dao instance variable
public void setExampleDao(ExampleDao exampleDao) {
this.exampleDao = exampleDao;
}

public void testExample() throws Exception {
BusinessObject bo = this.exampleDao.loadBo(new Long(10));
assertNotNull(title);
}

// specifies the Spring configuration to load for this test.
protected String[] getConfigLocations() {
return new String[] { “classpath:com/foo/daos.xml” };
}
}

<< Spring Configuration file >>

<?xml version=”1.0? encoding=”UTF-8??>
<!DOCTYPE beans PUBLIC “-//SPRING//DTD BEAN 2.0//EN”
“http://www.springframework.org/dtd/http://www.springframework.org/dtd/spring-beans-2.0.dtd”>
<beans>

<!– this bean will be injected into the ExampleDaoTests class –>
<bean id=”exampleDao” class=”com.foo.dao.hibernate.ExampleDao”>
<property name=”sessionFactory” ref=”sessionFactory”/>
</bean>

<bean id=”sessionFactory” class=”org.springframework.orm.hibernate3.LocalSessionFactoryBean”>
<!– dependencies elided for clarity –>
</bean>
</beans>

Spring based JUnit with Transaction Control/ Integration Test

One common problem in integration test is to access a database and still maintain the state of the persistence store. When using a database, changes to the database state may effect the future tests and CURD operations can’t be verified outside a transaction.

The org.springframework.test.AbstractTransactionalSpringContextTests superclass (and subclasses) exist to meet this requirement. By default, this class create and rollback a transaction for each test. From a developer/ development point of view, You simply write code that assumes the existence of a transaction.

It has to be noted that, this class extends AbstractDependencyInjectionSpringContextTests so spring dependency injection is made available for all the subclasses extending AbstractTransactionalSpringContextTests

Spring based JUnit also provides an option to commit a transaction (unusual, but occasionally useful when you want a particular test to populate the database) - you can invoke the setComplete() method inherited from AbstractTransactionalSpringContextTests. This will force the transaction to commit instead of roll back.

There is also convenient ability to end a transaction before the test case ends, through calling the endTransaction() method. This will roll back the transaction by default, and commit it only if setComplete() had previously been called. This functionality is useful if you want to test the behavior of ‘disconnected’ data objects, such as Hibernate-mapped objects that will be used in a web or remoting tier outside a transaction. Often, lazy loading errors are discovered only through UI testing; if you call endTransaction() you can ensure correct operation of the UI through your JUnit test suite.
E.g,

//as this test class extends ‘AbstractTransactionalSpringContextTests’ so all the
//test methods will be bound with a transaction.
public final class ExampleDaoTests extends AbstractTransactionalSpringContextTests {

// this reference will be automatically injected through spring
private ExampleDao exampleDao;

// a setter method to enable DI for the Dao instance variable
public void setExampleDao(ExampleDao exampleDao) {
this.exampleDao = exampleDao;
}

public void testExample() throws Exception {
BusinessObject bo = createBo(); // create the Business Objects
exampleDao.create(bo); // Business Object will be created

//now retrieve the object which was created in the database.
BusinessObject returnedObject = exampleDao.findById(bo.getId());
assertEquals(bo.getId(), returnedObject.getId());
//after the execution of the test method, data which was inse
}

// specifies the Spring configuration to load for this test.
protected String[] getConfigLocations() {
return new String[] { “classpath:com/foo/daos.xml” };
}
}

<< Spring Configuration file >>

<?xml version=”1.0? encoding=”UTF-8??>
<!DOCTYPE beans PUBLIC “-//SPRING//DTD BEAN 2.0//EN”
“http://www.springframework.org/dtd/http://www.springframework.org/dtd/spring-beans-2.0.dtd”>
<beans>

<!– this bean will be injected into the ExampleDaoTests class –>
<bean id=”exampleDao” class=”com.foo.dao.hibernate.ExampleDao”>
<property name=”sessionFactory” ref=”sessionFactory”/>
</bean>

<bean id=”sessionFactory” class=”org.springframework.orm.hibernate3.LocalSessionFactoryBean”>
<!– dependencies elided for clarity –>
</bean>

</beans>

Written by Ravi Nallakukkala on November 29th, 2007 with no comments.
Read more articles on Design and Spring.

JBoss Seam EnitityQuery and Restrictions

http://www.jboss.com/index.html?module=bb&op=viewtopic&t=120649

Additional info on restrictions usage:

1) Seam forces one EL expression per restriction.
2) can combine a EL expression and constants in one restriciton
Example:
“name like concat(lower(#{customer.name}),’%') or description like ‘ILI%’)”
3) Multiple restrictions are treated as “and” operation.

Written by Ravi Nallakukkala on November 21st, 2007 with no comments.
Read more articles on Hibernate.

Displaying Classpath in ANT

Here’s a sample code by which one can display the classpath

<path id=”test.classpath“>
   <path path=”${test.dir}”/>
   <fileset dir=”${config.dir}/lib”>
    <include name=”*-all.jar” />
   </fileset>
  </path>
  <property name=”current.test.classpath” refid=”test.classpath“/>
  
                <echo level=”info”> message=”test.classpath=${current.test.classpath}” </echo>

Written by Ravi Nallakukkala on October 17th, 2007 with no comments.
Read more articles on Build.

Oracle/PLSQL: Sequences (Autonumber)

In Oracle, you can create an autonumber field by using sequences. A sequence is an object in Oracle that is used to generate a number sequence. This can be useful when you need to create a unique number to act as a primary key.

The syntax for a sequence is:

CREATE SEQUENCE sequence_name
MINVALUE  value
MAXVALUE value
START WITH value
INCREMENT BY value
CACHE value;

For example:
CREATE SEQUENCE supplier_seq
MINVALUE 1
MAXVALUE 999999999999999999999999999
START WITH 1
INCREMENT BY 1
CACHE 20;

Now that you’ve created a sequence object to simulate an autonumber field, we’ll cover how to retrieve a value from this sequence object.

To retrieve the next value in the sequence order, you need to use nextval.
For example:supplier_seq.nextval

Written by Ravi Nallakukkala on August 1st, 2007 with no comments.
Read more articles on Database.

Limit & offset functionality in mySql, PostgreSQL

One of the excellent features provided by both my mySql & Postgre is support for Limit and offset functionality.

Usage syntax

SELECT <select_list>
    FROM <table_expression>
    [LIMIT { number | ALL }] [OFFSET number]

This is a useful feature when implementing pagination!
unfortunately this functionality is not currently being supported by Oracle!

Written by Ravi Nallakukkala on July 5th, 2007 with no comments.
Read more articles on Database.

String IndexOf Case in-sensitive search - PHP

PHP supports a function “strpos” which return the position index of the element found or a false if the element is not found.

Usage

$content = ‘ Search for a string here…’;
$find = ’search’;//lower case s
$pos =strpos($content, $pos);
now $pos will hold a value false;

For case in-sensitive searches, PHP version >5 supports a function “stripos” which return the position index of the element found or a false if the element is not found; Lower versions of the PHP can use the following code to achieve the same functionality

if (!function_exists(”stripos”)) {
function stripos($str,$needle) {
return strpos(strtolower($str),strtolower($needle));
}
}

Written by Ravi Nallakukkala on June 4th, 2007 with no comments.
Read more articles on Uncategorized.

« Older articles

No newer articles