2
\$\begingroup\$

I had an overloaded method, but Grails doesn't allow overloaded methods in controller actions. The overloaded method looked like this:

def punch(User user, User source) {
    user.userVersion++
    punch(user, source, null, null)
  }


 def punch(User user, User source, String latitude, String longitude) {

     UserPunch userPunch = new UserPunch()
     userPunch.punchClockStatus = user.punchClockStatus
     userPunch.user = user
     userPunch.job = user.job
     userPunch.task = user.task
     userPunch.date = new Date()
     userPunch.source = source
     userPunch.save(flush: true)
     if (latitude != null) {
        userPunch.latitude = latitude
        userPunch.longitude = longitude
     }
     if (userPunch.hasErrors())
        userPunch.errors.each {
            log.error it
     }
}

In order to get around the restrictions on overloaded methods, I used a try/catch, like this:

def punch(User user, User source, String latitude, String longitude) {

    UserPunch userPunch = new UserPunch()
    userPunch.punchClockStatus = user.punchClockStatus
    userPunch.user = user
    userPunch.job = user.job
    userPunch.task = user.task
    userPunch.date = new Date()
    userPunch.source = source
    userPunch.save(flush: true)
    try {
        userPunch.latitude = latitude
        userPunch.longitude = longitude
    } catch (MissingPropertyException e) {
        user.userVersion++
    }
    if (userPunch.hasErrors())
        userPunch.errors.each {
            log.error it
        }
}

Does this look like it will work right? Does anyone have a better solution?

\$\endgroup\$
  • \$\begingroup\$ Shouldn't this also be tagged with the "Groovy" tag? \$\endgroup\$ – syb0rg Jan 11 '14 at 2:08
  • 4
    \$\begingroup\$ Why are you asking if it will work right? Have you tried? \$\endgroup\$ – rolfl Jan 11 '14 at 3:00
  • \$\begingroup\$ I'm upgrading someone else's code from Grails 1.7 to Grails 2.3.4, so it has multiple compiler errors. I haven't even gotten it to run yet. This was one of them that I'm hoping I fixed, but just because it cleared the compiler error doesn't mean it will work correctly in runtime. I can't do unit testing until I fix all the other compiler errors. \$\endgroup\$ – AnthonyMDev Jan 11 '14 at 19:02
2
\$\begingroup\$

If you use default parameters, you won't need to overload the function.

def punch(User user, User source, String latitude=null, String longitude=null)
\$\endgroup\$
  • 1
    \$\begingroup\$ Default parameter values are not allowed in controller methods, same as overloading. \$\endgroup\$ – Gregor Petrin Nov 17 '14 at 8:02
2
\$\begingroup\$

Controller actions may not be overloaded but non-action methods can be. So you can do something like this to keep the code as similar to the original as possible:

def punch(User user, User source, String latitude, String longitude) {
    if (latitude != null) punchImpl(user, source, latitude, longitude)
    else punchImpl(user, source)
}

protected punchImpl(user, source) {
    ...
}

protected punchImpl(user, source, latitude, longitude) {
    ...
}

Then you may as well think about putting the punch method overloads inside a service since controllers shouldn't contain "business" logic anyway.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.