Pravin
Pravin

Reputation: 471

Grails method code too large issue

I am building a grails application and the groovy controller throws an error - "Method code too large"

Code:

class ComController {



    def ajaxGetMv = {
        if(params.metric == 'Marketing visits')
        {
            //All-4 "All" condition
            if (params.mv == 'All' && params.browser == 'All' && params.platform == 'All' && params.device == 'All')
            {
                def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.time_period])
                render com as JSON
            }
            // 1 All conditions
            else if(params.mv == 'All' && params.browser != 'All' && params.platform != 'All' && params.device != 'All')
            {
                def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.device = ? and p.browser = ? and p.platform = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.device, params.browser, params.platform, params.time_period])
                render com as JSON
            }
            else if(params.mv != 'All' && params.browser == 'All' && params.platform != 'All' && params.device != 'All')
            {
                def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.device = ? and p.mv = ? and p.platform = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.device, params.mv, params.platform, params.time_period])
                render com as JSON
            }
            else if(params.mv != 'All' && params.browser != 'All' && params.platform == 'All' && params.device != 'All')
            {
                def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.device = ? and p.mv = ? and p.browser = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.device, params.mv, params.browser, params.time_period])
                render com as JSON
            }
            else if(params.mv != 'All' && params.browser != 'All' && params.platform != 'All' && params.device == 'All')
            {
                def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.platform = ? and p.mv = ? and p.browser = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.platform, params.mv, params.browser, params.time_period])
                render com as JSON
            }

            // 2 All conditions
            else if(params.mv == 'All' && params.browser == 'All' && params.platform != 'All' && params.device != 'All')
            {
                def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.platform = ? and p.device = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.platform, params.device, params.time_period])
                render com as JSON
            }
            else if(params.mv == 'All' && params.browser != 'All' && params.platform == 'All' && params.device != 'All')
            {
                def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.device = ? and p.browser = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.device, params.browser, params.time_period])
                render com as JSON
            }
            else if(params.mv == 'All' && params.browser != 'All' && params.platform != 'All' && params.device == 'All')
            {
                def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.platform = ? and p.browser = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.platform, params.browser, params.time_period])
                render com as JSON
            }
            else if(params.mv != 'All' && params.browser == 'All' && params.platform == 'All' && params.device != 'All')
            {
                def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.mv = ? and p.device = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.mv, params.device, params.time_period])
                render com as JSON
            }
            else if(params.mv != 'All' && params.browser != 'All' && params.platform == 'All' && params.device == 'All')
            {
                def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.mv = ? and p.browser = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.mv, params.browser, params.time_period])
                render com as JSON
            }
            else if(params.mv != 'All' && params.browser == 'All' && params.platform != 'All' && params.device == 'All')
            {
                def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.mv = ? and p.platform = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.mv, params.platform, params.time_period])
                render com as JSON
            }

            //3 All conditions
            else if(params.mv == 'All' && params.browser == 'All' && params.platform == 'All' && params.device != 'All')
            {
                def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.device = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.device, params.time_period])
                render com as JSON
            }
            else if(params.mv != 'All' && params.browser == 'All' && params.platform == 'All' && params.device == 'All')
            {
                def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.mv = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.mv, params.time_period])
                render com as JSON
            }
            else if(params.mv == 'All' && params.browser != 'All' && params.platform == 'All' && params.device == 'All')
            {
                def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.browser = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.browser, params.time_period])
                render com as JSON
            }
            else if(params.mv == 'All' && params.browser == 'All' && params.platform != 'All' && params.device == 'All')
            {
                def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.platform = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.platform, params.time_period])
                render com as JSON
            }


            //No all - basic condition
            else
            {
                def com = Com.executeQuery("select p.date_hour, (p.marketing * 100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.device = ? and p.browser = ? and p.platform = ? and p.mv = ? and p.time_period = ? order by col_0_0_ asc ",[params.department, params.device, params.browser, params.platform, params.mv, params.time_period])
                render com as JSON
            }
        }

This entire code inside the if will repeat 15 times as there are 15 metrics (params.metric), hence the error. The only option after looking up the error is to split the code.

So I was splitting it using "redirect" action in grails and will use different methods for each if-else statement to prevent the method code to be too large

New code :

def ajaxGetMv = {
        if(params.metric == 'Marketing visits')
        {
            redirect ( action : "ajaxGetMv2" )
        }

But, this is not working and is not even throwing an error. Why is this redirect not working?

What are the other ways I can split my code ?

All approaches/suggestions are most welcome.

Upvotes: 0

Views: 373

Answers (2)

Pravin
Pravin

Reputation: 471

Solved it by simply calling a method within a method

Code :

def ajaxGetMv = {
        if(params.metric == 'Marketing visits')
        {
            ajaxGetMv2();
        }

def ajaxGetMv2 = {
//All if-else conditions

}

Writing in this manner makes sure that no method code > 64kB which is the limit here

Upvotes: 0

injecteer
injecteer

Reputation: 20699

have a look at criteria query. It might help you to parametrize your query and get rid of those terrible if-else-blocks

Upvotes: 0

Related Questions