Groovy adoption continues
There are a lot of heated debates lately about Java vs dynamic languages such as Ruby and Groovy, the Ruby vs Groovy, RoR vs Grails, etc. Some folks say that Groovy is a failure, that it's a hacker's toy language and that it's much slower (cause it's doing all the wonderful meta object magic at runtime) than "raw" Java. Of course it's slower, but come on, we live in the 21st century and with the computing power available and type of work we do (for typical web applications, anyway) does it really matter? And of course, as we all might know, that the most time is spent in the database and passing data over the wire. After all, if one needs pure real time performance or builds aircraft management system, than yes, choose the right tool for the job i.e. real time C, Ada or even Assembly.
Anyway, I've got this new project assignment - it's a small and simple CRUD application with several screens backed by Oracle. I didn't choose Grails yet, as the DB schema is so twisted that it wouldn't map naturally to the domain. Instead I've decided to introduce some Groovy via Spring 2.0 dynamic language integration. I've written controllers, validators and repository in Groovy. Here's the code snippet from simple Spring controller implemented in Groovy:
class AvailableCourseSearchController extends AvailableCourseBaseController implements InitializingBean {
AvailableCourseRepository availableCourseRepository
public ModelAndView processFormSubmission(HttpServletRequest request, HttpServletResponse response,
Object command, BindException errors) throws Exception {
def availableCourses =
this.availableCourseRepository.findAllByCourse(
new Course(unitOfRegCode:request.getParameter('unitOfRegCode'),
offeringUnitCode:request.getParameter('offeringUnitCode'),
subjectCode:request.getParameter('subjectCode'),
courseId:request.getParameter('courseId')))
if (availableCourses.isEmpty()) {
errors.reject("courses.not.found")
return showForm(request, errors, getFormView())
}
def courseHolder = new CourseHolder('courses':availableCourses)
request.getSession().setAttribute(getCommandName(), courseHolder)
new ModelAndView(getSuccessView(), ['courseHolder':courseHolder,'suffixAList':suffixAList,'athleteCodeList':athleteCodeList])
And then the implementation of the course repository uses groovy.Sql to query and construct Course object. It's no JPA of course, but for simple case like this it works like a charm:
class AvailableCourseRepositoryImpl implements AvailableCourseRepository {
Sql db
void setDataSource(DataSource dataSource) {
this.db = new Sql(dataSource)
}
List findAllByCourse(Course criteria) {
def query = buildDynamicQueryString(criteria)
def courses = []
this.db.eachRow(query) { courseRow ->
courses << new Course('unitOfRegCode':courseRow.unit_of_reg_cd,
'offeringUnitCode':courseRow.offering_unit_cd,
'subjectCode':courseRow.subj_cd,
'courseId':courseRow.course_no,
'courseTitle':courseRow.course_title,
'suffixA':courseRow.suffix_a,
'unavailable':courseRow.course_deleted_ind == 'N'?false:true,
'required':courseRow.required_ind == 'N'?false:true,
'athleteCode':courseRow.athlete_cd,
'id':courseRow.id) }
return courses
}
}This usage of Groovy in these simple scenarios proved to be simple and expressive. The next step is a full-blown Grails application!
Later...

0 comments:
Post a Comment