Category Archives: Uncategorized

An elegant DAO design for un-elegant queries

I am a big Hibernate fan, but sometimes you can’t use Hibernate. When you can’t, Spring’s JDBC support is awesome. One of the reasons I encounter frequently for not being able to use Hibernate is some ridiculously complicated queries (I’m looking at you, Peoplesoft). When you have a giant long SQL query, you often encounter some code that looks like this:

String sql = “SELECT ” +
“RM.RTITLE1, ” +
“RM.RNAME, ” +
“SR.USER_SEQ_NO, ” +
“SR.INT_SEQ_NO AS SUBREQ, ” +
“SRT.USER_SEQ_NO, ” +
“SRT.TEXT, ” +
“(SELECT SRA.COURSE FROM SUB_REQ_AR SRA WHERE SR.INT_SEQ_NO = SRA.SUB_INT_SEQ_NO AND SRA.USER_SEQ_NO = 1) AS COURSE ” +
“FROM DPROG D JOIN DPROG_REQ DR ON D.INSTIDQ = DR.INSTIDQ ” +
“AND D.INSTID = DR.INSTID ” +
“AND D.INSTCD = DR.INSTCD ” +
“AND D.DPROG = DR.DPROG ” +
“AND D.DPFYT = DR.DPFYT JOIN REQ_MAIN RM ON D.INSTIDQ = RM.INSTIDQ ” +

Ugly, isn’t it? Well, some of my coworkers that were running into this came up with the idea of putting the SQL into its own files. It is a great idea and I doubt they are the first to think of it. They don’t use Spring, though, and I thought Spring would be able to provide some good glue to put it all together.

It turns out, Spring has something called Resource that can automatically load things from various places. I chose to use the classpath, and put the SQL files into the same package as the DAO. The only problem here, is with Maven, you end up with the SQL file in src/main/resources and the code in src/main/java, which is a little annoying. The Resource object doesn’t get the whole job done either; it just provides an InputStream, and having to write code to read the contents of the file has no place in my elegant solution. So, this is where commons-io comes into play. In the setter for the Resource object, I use IOUtils to read the file and stick it in a String.

So, the overall strategy goes like this:

  • Put SQL in file in src/main/resources
  • Create DAO object with a Resource setter for the SQL file
  • Wire the SQL file resource in your applicationContext.xml
  • Read in the file using IOUtils in the setter

Here’s the DAO definition in applicationContext.xml


<bean id="darsCourseDao" class="edu.asu.eadvisor.onoff.dao.DarsCourseDaoImpl">
<property name="dataSource" ref="darsDataSource">
<property name="subreqsForCourseQuery" value="classpath:/edu/asu/eadvisor/onoff/dao/GetSubreqForCourse.sql">
</property>
</property></bean>

And the setter in the DAO looks like this:

    public void setSubreqsForCourseQuery(Resource subreqsForCourseQuery) throws IOException {
InputStream is = subreqsForCourseQuery.getInputStream();
try {
this.subreqsForCourseQuery = IOUtils.toString(is);
} finally {
IOUtils.closeQuietly(is);
}
}
Advertisements

Why you SHOULD use a case sensitive file system on your Mac

There is a lot of noise on the Internet saying case sensitive file systems on the Mac are bad. I guess if you are running Adobe apps, you can listen to that, since they are apparently developed sloppily and depend on a lack of case sensitivity. Any real developer, however, will find that the lack of case sensitivity causes problems. Here are two I recently ran into that ended up wasting a LOT of time debugging:

  • In Tapestry 5, a lot of effort has been made to make things not case sensitive. Developing a t5 app on my Mac, I had that in my head, and created some page templates using all lower case, and their Java classes in typical upper camel case. I ran the app and tested it out until I was satisfied it was working. I deployed it to the Unix dev server and it mysteriously stopped working. It turns out, it searches for page templates based on the case sensitive class name. So my Index class was looking for the file Index.tml. I had index.tml. On the Mac, the OS returns index.tml when you look for Index.tml. Worst of all, I had to use a Unix machine to rename the files in the SVN repository because the Mac refused to acknowledge any change.
  • Working on another project, I had a directory structure that had a directory in all caps. I needed to change it to lower case. Once that was done, I tried to commit the changes to SVN. I got this:

    svn: Commit failed (details follow):
    svn: Server sent unexpected return value (405 Method Not Allowed) in response to MKCOL request for ‘/svn/!svn/wrk/626a333b-f635-42b4-896e-5df2dd0475c2/ecampus/src/com’

    Not cool.

Pictures

I think my blog is really boring because it doesn’t have any pictures.  I’m jealous of those people that get themselves to carry their camera and take random pictures of interesting stuff during their day.  My brother Bob was doing a picture a day project, but that seems to have died.  What I’d like to do, though, is capture a photo that complements whatever I happen to be writing about, as opposed to the picture being the star of the show.  I have a really nice camera, but it is too big to carry around all the time.  If you feel like getting me a smaller one, have a visit to my Amazon wish list and pick one up :).

Security

So, it is tax season and I have been taking care of some housekeeping items with my personal finances. If you have messed around with banks and online and even telephone access, you probably noticed that there are some seriously draconian security measures in place these days. In most cases it seems like there is some combination of impossible requirements for the password combined with a requirement that you change it frequently and don’t use a password you have used before.

The end result of all this is that it is impossible to come up with passwords that you can actually remember. With that in mind, you have two choices. You can write down your password (which pretty much destroys the purported security being enforced) or you can call up their support line and have them reset your password each time you need to access whatever obscure service it is they are providing. In other words, the situation is a mess.

Various technical solutions exist for this problem, and sometimes they are used. For example, the place I work uses SecurID for VPN access sometimes. The problem is, they generally cost a lot of money and aren’t standardized. Anyway, I think there’s a lot of opportunity in this field, but I think it would make sense to approach it in an innovative way. Instead of trying to capitalize on it immediately, I think it makes sense to build something everyone can use, maybe making it free, standardized or open source. Once a standard takes hold, then there will be opportunity to provide enhancement for profit.

Friday words

  • altura – height
  • olor – scent
  • esquina – corner
  • crecer – to grow
  • sanidad – health
  • baloncesto – basketball
  • mirador – viewpoint
  • rango – rank
  • rascacielos – skyscraper
  • dirección – address

Languages

I’ve always been a fan of traveling, I think because I am constantly looking for things that are out of the ordinary. Visiting foreign cultures, one is surrounded by strange environments where things are totally different from the typical day-to-day. This probably leads to my recent increase in interest in foreign languages. It is cool to explore other countries, but you can only learn so much by observation and that which an English speaking guide will tell you. Anyway, it is probably not worth the effort it would take, but I have recently renewed my interest in getting better at new languages. I’ve had four years of high school Spanish, so I am pretty decent with the basics of that, but I still can barely understand a native speaker. At various points, I have tried to read some Spanish news sites to get some experience, but it is pretty hard to jump in and understand. Most recently, after my trip to Germany this summer, I bought some language software to start learning German. It is pretty interesting. I think I’m making some good progress, but am nowhere near my level of understanding of Spanish. I do wonder, however, if it is even possible to learn a language well by self study, and if so, how long it would take if you were pretty dedicated to it.

Getting closer

Today the laminate floor was almost finished in the living/dining area. We could have finished, but it got a little too late to be using the table saw, so there are about 3 rows left for me to wrap up tomorrow. Should only take about a half hour. We also finished with the installation of the new toilets. They are Toto Drake toilets, with the “G-Max” flush system. My goal is to be able to not own a plunger. We’ll see. So far, the toilet shutoff valves have been the only ones to not leak. Last night, when my dad and I installed the first toilet, however, the valve started spraying water out of the handle all over the place when it was turned on all the way. We went to Home Depot and bought more shutoff valves. We were able to get the quarter-turn style, which are pretty fancy since they’re really easy to turn on and off. I’m not sure if the second toilet valve was leaking, but I figured if they were using crap parts, it probably would be soon. Both of those shutoff valves installed pretty easily and didn’t leak. Score. So, really all thats left now before I can move in is the carpet and the bathroom vanities. There is a little touchup work left on the moldings in the rooms that are getting carpet, so that needs to be done before I bring in the carpet installers. I took some pictures, but I forgot my camera at the condo, so I’ll post them tomorrow hopefully.