Tuesday, October 11, 2011

A dirty secret

"The dirty secret of syndicated feeds is that XML best practices often have been flushed right down the toilet." - ROME in a Day: Parse and Publish Feeds in Java (Mark Woodman).

Tuesday, September 27, 2011

Argh!

Being a good programmer requires much patience.

Monday, September 26, 2011

How to process large data sets in Hibernate

Here's a good post discussing three ways to handle the processing of large data sets in Hibernate.

Thursday, September 22, 2011

Eclipse Mercurial plugin uses executable with version different than locally installed executable

I encountered the following issue:

1. Cloned a Mercurial repository from the command line.
2. In Eclipse, went to File -> New -> Other... -> "Create New Mercurial Repository"
3. Browsed for the newly cloned local repository and clicked the "Finish" button.
4. G0t this error in Eclipse: "abort: requirement 'dotencode' not supported!"

This was happening due to a version mismatch between the Mercurial executable used by Eclipse and the Mercurial executable installed locally. To fix this:

1. Went to Windows -> Preferences -> Team -> Mercurial in Eclipse.
2. Unchecked the "Use default (built-in) Mercurial executable" check-box.
3. Browsed for local Mercurial executable in the "Mercurial Executable" field.
4. Clicked the "OK" button on the dialog.

Thursday, April 28, 2011

Calling Postgres function with integer[] input from Hibernate

Situation: Running Hibernate, Postgres. Have function defined in Postgres taking integer[] as input. Have defined in a Hibernate mapping file which calls the function using a variable parameter ":ids". Want to call this , passing in a List. How to do it?

Answer: Create a class named HibernateArrayLongType which extends UserType. Define the two core methods in this class as follows:

public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
throws HibernateException, SQLException {
Array value = rs.getArray(names[0]);
if (value != null) {
return value.getArray();
}
else
return null;
}

public void nullSafeSet(PreparedStatement statement, Object value, int index)
throws HibernateException, SQLException {

if (value == null) {
statement.setNull(index, Types.ARRAY);
} else {
List list = (List)value;
Long[] array = new Long[list.size()];
for (int i = 0; i < list.size(); i++) {
array[i] = list.get(i);
}
// the "integer" type name may be specific to Postgres
Array sqlArray = statement.getConnection().createArrayOf("integer", array);
statement.setArray(index, sqlArray);
}

}


In a class which extends HibernateDaoSupport, add this code:

List ids = new ArrayList();
Query query = getSession().getNamedQuery("queryName");
query.setParameter("ids", ids, Hibernate.custom(HibernateArrayLongType.class));
List result = (List)query.list();

Monday, January 10, 2011

Tip for testing database function performance

Suppose you've got a database function that's slow. You want to determine where the bottlenecks are, so you decide to pepper your function with RAISE NOTICE statements to output the current time at different points within the function. You try "RAISE NOTICE current_timestamp" and note that every time this statement is executed, the same timestamp is output. It's as if current_timestamp has been pre-set to be the moment in time when the function began executing. What to do? The solution is to use "RAISE NOTICE timeofday()" - this will reflect the actual time, not the time the function began executing.

(Please note that this applies to Postgres. I don't know if this post translates precisely for other DBMS's.)

Postgres performance considerations

A great article about Postgres performance considerations that the guys from thoughtbot have been so kind to share - read and enjoy!

Monday, December 27, 2010

Preserve the history while re-adding a file to Subversion

To re-add a file previously deleted in Subversion execute this command...

svn copy --revision [rev number] [path to file in repository] [path on local file system to copy to]

... then commit the file. This will retain the history of the file.

Here's an example:

(1) svn copy --revision 20471 http://tools.symposium.org/svn/smp/dev/config/classes/velocity/donor/feedback.vm config/classes/velocity/donor/feedback.vm

(2) svn commit -m "resurrected feedback.vm from rev 20471" config/classes/velocity/donor/feedback.vm

Monday, August 30, 2010

Type conversion: Spring 2 vs. Spring 3

To my dismay, there is no generalized String <-> Object conversion mechanism in Spring 2.0 unless you're using Webflow. Spring 3, however, includes it for non-Webflow situations.

Tuesday, July 20, 2010

Unlimited strong passwords in a flash

Ever needed to generate a bunch of strong passwords in a flash? Look no further than this amazing tool!

Friday, May 7, 2010

A great article on email deliverability

In today's world, due to spam's proliferation, ensuring that the maximum number of emails your organization sends ends up in your recipients' inboxes can be complicated.This wonderful article illustrates both the DYI approach and also lists some SMTP relay providers that will do the heavy-lifting for you.

Friday, February 19, 2010

Interesting Java tidbit

If you pass a number in String form, padded with spaces, like " 89.99 " for example, to Double.parseDouble(), no exceptions will be thrown. If you pass the same number to Integer.parseInt(), a NumberFormatException will be thrown.

Wednesday, January 13, 2010

Spring Lessons

In the past few days, the veils obstructing a couple rules-of-thumb have slowly melted away:

1. Don't use model objects as form backing objects. Use simple value objects instead. This adds an extra layer, but eliminates complications. This is especially true in Spring WebFlow (2.0.7) which does not instrument the "open session in view" pattern, unlike Spring MVC which odes.

2. Make sure your value object fields are Strings as much as possible. If a field represents a number, you can convert it later. Doing this avoids the need to configure pretty binding error messages for the user, when, for example, the user enters characters into a form field which binds to an Integer field in the value object.

Added (2-19-2010): More generally, if you are discontinuing the use of a database column that has been in existence for a long time, do not delete the column, because removing all mentions of that column from all the code can prove to be quite hairy.

Tuesday, January 5, 2010

Is Spring WebFlow worth the trouble?

This post begs to be fleshed out, and I hope to return and do so. The question is: Is Spring WebFlow really worth all the trouble? It's a nice abstraction, but does it save time? I'm not so sure. For one, there's no OpenSessionInViewFilter pattern in WebFlow.

A work colleague pointed out that WebFlow saves you from writing a lot of controller code for transitioning from screen to screen. Also, it aims to solve the "back button" problem and related issues.

Monday, January 4, 2010

Invoke window.open() from a link

Jennifer Madden does a great job explaining how to invoke JavaScript's window.open() method from a link, avoiding some common pitfalls. Thanks Jennifer!

Sunday, December 27, 2009

Do transactions re-associate detached objects?

It appears that wrapping a manager method in a transaction via Hibernate's AOP functionality, does not automatically re-associate any detached objects passed into the method to the session. You'll need to do this manually.

Thursday, December 24, 2009

A Spring WebFlow Gotcha

If your flow definition has a element and its "if" attribute contains two or more clauses joined by the AND logical connector, make sure to use & amp;& amp; (no spaces) instead of &&, because && breaks XML validation.

Wednesday, December 16, 2009

Hibernate Intricacies

Forgive me please if the following post is not 100% clear.

Imagine this: You've got a Proposal object with a List<Resource> field. Each Resource has a Map<ResourceAttribute> field. You're using Hibernate, and the Resource.hbm.xml contains a <map> element with cascade="all-delete-orphan". If you call Session.save(resource) for a particular resource belonging to a proposal, and later call Session.update(proposal) for that proposal, you will get the following exception - Don't change the reference to a collection with cascade="all-delete-orphan".

Obviously you haven't directly changed the resource's reference to its map of attributes, but, somehow, by saving the resource directly, instead of jumping up a level and saving the proposal to which it belongs, you're causing Hibernate to consider the reference changed.

Tuesday, December 8, 2009

Extending JavaScript's onsubmit method

Suppose your page has a form, and you want to extend its onsubmit method programatically. Here's one way to go about it:

form = document.getElementById("form");
form.realSubmit = form.onsubmit;
form.onsubmit = function () {
  alert("before the original onsubmit");
  this.realSubmit();
}

(Based on Orc Scorcher's response to this thread on www.webdeveloper.com)