One of the most frustrating items I’ve come across recently is that alot of WordPress plugins include their own version of jQuery or similar JavaScript library. Please Stop!!!!

What you may not realize is that WordPress comes bundled with the jQuery library and to boot they provide a simple method to not only include it in your website but to ensure that it only gets included once. Please familiarize yourself with the wp_enqueue_script function as it will make your plugins cleaner and help to mitigate conflicts with other plugins and templates.

To include jQuery, Scriptaculous, or similar libraries is a simple 2 step process.

  1. Register a method for the init hook
    This is necessary as the wp_enqueue_script should be called upon the init stage of WordPress execution.
    add_action('init', 'plugin_init');
  2. Enqueue the jQuery library
    function plugin_init() {
        wp_enqueue_script('jquery');
    }

There you have it you have now included jQuery into the website that uses your plugin.

, , , , , ,

Unfortunately Microsoft over the years has left us disappointed year after year with their web browser Internet Explorer. Yes, I know we all have had frustration with the little quirks that all browsers have but Internet Explorer has been the bane of the web developer. IE 8 like previous incarnations appears at first to be step up until you look closely and notice the issues with its animation rendering capability.

Anyhow enough rambling, Recently I came across a nifty little meta tag that IE 8 supports that tells it to render the page content as IE 7 would. Quickly and easily removing any quirks introduced to your website that are IE 8 specific. Now instead of 2 or 3 version of IE that you need to support you are back down to simply IE 7 or if you are one of the unfortunate ones, IE 6 as well.

<meta content="IE=EmulateIE7" http-equiv="X-UA-Compatible">

Enjoy, and here is to hoping that for IE 9 they do a full rewrite of this web browser known as Internet Explorer

, , , ,

When developing within a large web application it may be needed to check if a JavaScript function/object exists prior to being calling. The benefits to checking if the method exists are:

  1. No JavaScript errors are Generated.
  2. if needed you can dynamically load and initialize your method or object

In most cases you won’t need to test for a function but in the rare cases that you find yourself not sure of what has been loaded into the DOM (Document Object Model) you can check by simply calling window.methodName. By wrapping it in a if statement it will let you know if the method exists.

if( window.methodname ) {
  // method exists
} else {
  // method does not exist
  function methodName() {
 
  }
}
,

When utilizing the iBATIS framework you define your SQL Maps to perform the basic CRUD functionality and additional business logic for an individual item. Very often you will be only updating single objects at a time and this is great, but what happens on those occasions where you find yourself processing hundreds of objects at the same time. The simple answer Batch Processing.

Batch Processing is the execution of a set of SQL statements without any user intervention. In essence it takes a group of commands executes them one at a time and then returns the updated count. Well in most cases some drivers won’t return the actual update count and instead returns 0.

Below is a short tutorial on how to perform Batch Processing in Java utilizing the Spring and iBATIS frameworks. In the example we will define and implement the method processInBatch method that will handle the processing. It assumes you have basic knowledge of each of the frameworks and focuses strictly on how to setup batch processing.

Initial Setup

Lets start by assuming for this example that we have a table in our database called APPLICATION and have defined the following basic SQL MAP which has been shortened for brevity.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap
        PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
        "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="Application">
 
	<!-- applicationMap - Application Object Map  -->
	<resultMap id="applicationMap" class="com.example.model.Application" groupBy="id">
		<result property="appId" column="APP_ID" />
		<result property="name" column="NAME" />
		...
	</resultMap>
 
	<!-- list -->
	<select id="Application.list" resultMap="applicationMap" >
		...
	</select>
 
	<!-- insert -->
	<insert id="Application.insert" parameterClass="com.example.model.Application">
		...
	</insert>
 
	<!-- update -->
	<update id="Application.update" parameterClass="com.example.model.Application">
		...
	</update>
 
	<!-- delete -->
	<delete id="Application.delete" parameterClass="com.example.model.Application">
		...
	</delete>
 
</sqlMap>

In addition to created the SQLMap we’ve also create our basic DAO interface ApplicationDAO and implementation ApplicationDAOImpl which extends Spring’s SqlMapClientDaoSupport class.

package com.example.dao;
 
import java.util.List;
import com.example.model.Application;
 
public interface ApplicationDAO {
 
	List<Application> list();
 
	int update( Application obj );
 
	Integer insert( Application obj );
 
	int delete( Application obj );
 
}
package com.example.dao.impl;
 
import java.util.List;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
 
import com.example.dao.ApplicationDAO;
import com.example.model.Application;
 
public class ApplicationDAOImpl extends SqlMapClientDaoSupport implements ApplicationDAO {
 
	@SuppressWarnings("unchecked")
	public List<Application> list(){
		return (List<Application>) getSqlMapClientTemplate().queryForList( "Application.list" );
	}
 
	public Integer insert( Application obj ){
		Integer result = Integer.valueOf(-1);
		try {
			result = (Integer) getSqlMapClientTemplate().insert( "Application.insert", obj );
		} catch ( Exception e ) {
			log.error( "Failed to insert Application: " + e.getMessage() );
		}
		return result;
	}
 
	public int update( Application obj ){
		return getSqlMapClientTemplate().update( "Application.update", obj );
	}	
 
	public int delete( Application obj ){
		return getSqlMapClientTemplate().delete( "Application.delete", obj );
	}
 
}

Define the new processInBatch method

The processInBatch method will be responsible for setting up and executing the batch. It will take a list of Applications that are to be updated and perform the update SQL Map operation for each of them. To begin lets define the method stub in the ApplicationDAO interface and ApplicationDAOImpl class

// ApplicationDAO
int processInBatch( List<Application> applications );
// ApplicationDAOImpl
public int processInBatch( List<Application> applications ) {
 
}

Understanding the Batch Logic

To perform Batch Processing it is important to understand the two main classes that you will be interacting with SqlMapClientCallback and SqlMapExecutor

The SqlMapClientCallback class

The SqlMapClientCallback is a class to be overwritten in line that will perform the actual batch process. In it you define the doInSqlMapClient method. The SqlMapClientCallback object creates and passes the SqlMapExecutor instance to this method for your usage in setting up and executing the batch. The doInSqlMapClient method like the rest of the methods returns an Object so you can return any object you want.

try {
    Integer updateCount = (Integer) getSqlMapClientTemplate().execute( new SqlMapClientCallback() {
        public Object doInSqlMapClient( SqlMapExecutor executor ) throws SQLException {
 
        }
    });
    return updateCount.intVal();
catch (Exception e ) {
    return -1;
}

Looking at the above base implementation you can see that the general setup is the same for any batch processing you will need to perform the only difference is how you define the doInSqlMapClient method.

Implementing the doInSqlMapClient

The main batch worker is the SqlMapExecutor object. This object has 2 additional methods in addition to the basic insert, update, and execute: startBatch and executeBatch. The startBatch method is used to specify that you are performing the following statements as a batch, instead of executing them in line. Once you have set all your statements the executeBatch method is used to execute all of the statements in a batch and return the update count.

In our example as we are updating all the Applications passed to the processInBatch method we will start the batch, iterate over all applications, for each application call update on the executor for that object, then execute the batch.

executor.startBatch();
Iterator<Application> iter = applications.iterator();
while( iter.hasNext() ) {
    executor.update( "Application.update", iter.next() );
}
return new Integer( executor.executeBatch() );

The complete processInBatch method

public int processInBatch( List<Application> applications ) {
    try {
        Integer updateCount = (Integer) getSqlMapClientTemplate().execute( new SqlMapClientCallback() {
            public Object doInSqlMapClient( SqlMapExecutor executor ) throws SQLException {
                executor.startBatch();
                Iterator<Application> iter = applications.iterator();
                while( iter.hasNext() ) {
                    executor.update( "Application.update", iter.next() );
                }
                return new Integer( executor.executeBatch() );	
            }
        });
        return updateCount.intVal();
    catch (Exception e ) {
        return -1;
    }
}

Wrapping it all Up

There you have it we’ve created a method that executes multiple SQL statements as a batch. This reduces the overhead of performing multiple statements individually and can increase the performance of your applications especially as you get to large sets of statements. The greatest thing is that you don’t need to define any new sql statements and can use the ones you’ve already defined for doing single modifications.

, , , , , , , , ,

Ever find yourself wanting to create a custom feed of your WordPress managed website? Lucky for you they have the handy add_feed hook that allows you to define as many feeds as you want, Just be careful not to duplicate any existing feeds. I personally find myself using this feature to output xml of data that I then utilize in Flash presentations. The add_feed function works like all other WordPress hooks in that it calls the user defined function when the named feed is to be rendered.

For illustration purposes lets walk through the basic steps for creating a Custom Feed.

Define a function to output the xml

Before adding the feed you first need to define the function that will render the xml to be returned when the feed is called. Depending on your needs you can define this in a custom plugin or in the functions file of your theme. The below snippet outputs your posts in a basic xml structure.

function outputXMLFeed()
{
    $posts = query_posts('showposts=5');
 
    echo '<?xml version="1.0"?>';
    echo '<items>';
    foreach ($posts as $post) {
        echo '<item>';
        echo '<title>' . get_the_title($post->ID) . '</title>';
        echo '<link>' . permalink($post->ID) . '</link>';
        echo '<guid>' . get_permalink($post->ID) . '</guid>';
        echo '</item>';
    }
    echo '</items>';
}

Add the feed to WordPress

Now to add the feed we simply need to call add_feed where we pass in the name of the feed and the function that will output the feed contents (xml). One caveat exists however in that WordPress needs to be fully initialized before the add_feed function can be called. This forces us to wrap up the add_feed call behind another function that gets called on the WordPress init hook.

add_action('init', 'add_my_feed');
 
function add_my_feed(  ) {
  add_feed("myFeed", "outputXMLFeed");
}

Calling your Custom Feed

To access your feed you simply need to append ?feed=Name of your Feed to your site’s url. In this case the final url would be http://www.example.com/?feed=myFeed

That is all there is to creating your custom feeds. Depending on your needs you can create them in your theme functions or in a standalone plugin. If you are using additional plugins like Pods CMS then feeds can be extremely useful in rendering pods contents into xml for use by Flash, JavaScript or other applications.

, , , ,