Tarun Pareek « Intelligrape Groovy & Grails Blogs
Subscribe via E-Mail:

Tarun Pareek

http://www.intelligrape.com

Grails and Open Source Fanatic, Agile Practitioner, JQuery Devotee, PhoneGap Explorer. LinkedIn : http://in.linkedin.com/in/tarunpareek, Twitter : http://twitter.com/TarunPareek_

Posts by Tarun Pareek:

  • Mapping duplicate domain class name having different packages using autoImport in Grails

    14 Sep 2012 in Database&Grails&Groovy

    Hi,

    Recently one of my friend asked me whether there is a mechanism in grails to handle the use-case where we have different packages but having same domain name. Here we are having duplicate domain class names in different package and by default the domain classes are auto-imported in HQL queries which doesn’t required to specify whole class name. Hence due to it class name are no longer unique because of this it cause DuplicateMappingException in our case. For this I refer my friend to set autoImport false in mapping.

    Grails provide us a mapping key named ‘autoImport‘. We can disable the auto-import for any one of the domain or both which will fix our problem to some extent. Lets see how it helps :

    Suppose we have 2 packages named :
    1. com.tarun.poc.project having Domain named “Issue”.

    package com.tarun.poc.project
    class Issue {
        String summary
        static constraints = {
        }
    }
    

    2. com.tarun.poc.company having Domain named “Issue”.

    package com.tarun.poc.company
    class Issue {
        String description
        static constraints = {
        }
    }
    

    When you run your app you will recieve the exception some thing like this :

    nested exception is org.hibernate.DuplicateMappingException: duplicate import: Issue refers to both com.tarun.poc.project.Issue and com.tarun.poc.company.Issue
    

    Now we add autoImport to the mapping of domain ‘Issue’ one or both and set it to false as specified below :

    package com.tarun.poc.project
    class Issue {
        String summary
        static constraints = {
        }
        static mapping = {
            autoImport false
            table 'project_issue'
        }
    }
    

    It will fix your problem regarding ‘DuplicateMappingException’. If you see above we have also specified table mapping because if we didn’t specify table mapping it will create a single table in database with name ‘issue’ and having properties of both the domains into that table.

    Auto Import without Table Mapping

    Hence to create different tables for Domain Class or customizes the name of the database table associated with the domain class we used table mapping.

    Image of Auto Import with Table Mapping

    Hope it helps :)

    Thanks,
    Tarun Pareek
    tarun@intelligrape.com
    http://in.linkedin.com/in/tarunpareek
    https://twitter.com/TarunPareek_
    More Blogs by Me

  • Appending anchor tag to the url on server side using fragment parameter in Grails

    28 Aug 2012 in Grails

    Hi,

    Recently I had come across one of the cool grails redirect parameter named ‘fragment’ providing cool feature of forming relative link with an anchor tag/name.


    Generally, we can simply call

    window.location.href=http://yourLink#anchorName

    It will focus you to the anchor name specified in relative link means scroll the page to the anchored tag who’s anchor name is specified by you on HTML element as attribute id.

     
    anchorName specified here in url will specify the initial focus on the content whose anchor name is specified in url after loading of the page”
     
    For Example :
    Suppose we have three divs with id tea, colddrink and coffee. When this page load with its url ending with hash tag #coffee, it will focus on the page content containing the div with id coffee . i.e Page will be scrolled to the content where div with id coffee is found, while loading the page.
     
    To generate the url with anchor name on server side, we use grails redirect parameter named ‘fragment’.

    Defined as in Grails Docs for redirect , Fragment : The link fragment (often called anchor tag) to use.

    we simply need to write in our grails controller :

    redirect(controller:'product',action:'list',fragment:'coffee');

    It will form the url http://domainname.com/product/list#coffee. When the view is being rendered and while loading of HTML, page will be scrolled to the content where div with id coffee is found.

    I hope it will help you in solving your use cases related to it :)

    Thanks,
    Tarun Pareek
    tarun@intelligrape.com
    http://in.linkedin.com/in/tarunpareek
    https://twitter.com/TarunPareek_
    More Blogs by Me

  • Handling corrupted references through ignoreNotFound database mapping in Grails

    17 Apr 2012 in Database&Grails

    Hi,
     
    Recently i had a use-case where we have the legacy database and it contains the corrupted references of a non existent record in many-to-one relationship, and i have to populate a grid that contain info also from referenced record. As referenced record doesn’t exist, So when we refer to the certain record will result in Hibernate throwing exception :

    org.hibernate.ObjectNotFoundException

    and not a single record loaded on grid.
     
    As it is legacy database i am not sure what should be the correct value for the particular record.
     
    After few searches, i come accross a property in grails database mapping to handle such cases, let see how it work :

    class Task{
       String name
       ParentTask parent
    
       static mapping = {
           parent ignoreNotFound: true
       }
    }
    
    class ParentTask{
       String name
    }
    

    Basically, ignoreNotFound simply mapping hibernate not-found property.
     
    Note : A record loaded with ignoreNotFound: true will throw an exception during call of save() because of the missing reference.
     
    Either you can first update the invalid property or corrupted reference to the correct reference and then update other property that will solve your problem. It depend how you handle such scenario on your usecase.
     
    Thanks,
    Tarun Pareek
    tarun@intelligrape.com
    http://in.linkedin.com/in/tarunpareek
    More Blogs by Me
     

  • Duplicate headers received from server issue on chrome

    11 Apr 2012 in Grails&Groovy

    Hi,
     
    Recently, in my project while downloading some files(not on all files) i am getting exception on chrome, that say :
     
    Duplicate headers received from server

    Error 349 (net::ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION): Multiple Content-Disposition headers received. This is disallowed to protect against HTTP response-splitting attacks.
     

    But when i download the same files on IE, Firefox and other browsers that causing exception, no problem occur it work like charm. :)
     
    But its weird that why is chrome giving exception only on some files while downloading, then i searched, i found that new release of chrome has this problem. It seem like Content-disposition header become very strict and sensitive, in the spec that chrome uses.
     
    Solution that worked for me to solve the problem :
     
    Wrong :

    response.setHeader(“Content-disposition”, “attachment; filename=${fileName}”)

    Correct :

    response.setHeader(“Content-disposition”, “attachment; filename=\”"+fileName+”\”")
     
    It worked for me. Hope it helps you also :)
     
    Thanks,
    Tarun Pareek
    tarun@intelligrape.com
    http://in.linkedin.com/in/tarunpareek
    More Blogs by Me

  • Forgot Root Password in Linux, How to Reset Root Password through GRUB?

    28 Mar 2012 in Linux&System

    Hi,
     
    It is a major problem when we forgot our root password in linux. But don’t know how to recover it. First thought that came to mind reinstall machine. But I found a way that might help you without reinstall and save your hours. It is a interesting trick in linux to get back our root password in few minutes. I used it on ubuntu 11.10 it work fine for me. Some people might know about this method, but I thought i share with you all for those who don’t know.
     
    Caution : But please don’t play with this for fun, only use it when in need.
     
    Here are the steps to get back your password :
    1. Reboot your system.
    2. When Grub Loads, Either move arrow to stop clock/timer.
    3. Select Ubuntu Kernel, and then Press ‘e’ key to open it in Edit Mode.
     

     
    4. In edit mode, screen will look like this in Ubuntu…
     

     
    If you see the 2nd line from bottom, that says info about kernel add ’1′ in end of command as given below,
    linux /boot/vmlinuz-2.6.31.9 root=UUID=904bf39-9234 ro quiet splash 1
     
    5. Then Press Ctrl + b to boot kernel in single user mode.
    6. By using passwd command reset your root password,
    root# passwd
    New UNIX Password:
    Retype UNIX Password:
    passwd updated successfully
     
    7. Now reboot the machine, machine will reboot with your new password.
     
    I hope it helps. ;)
     
    Thanks,
    Tarun Pareek
    tarun@intelligrape.com
    http://in.linkedin.com/in/tarunpareek
    More Blogs by Me

  • mysqldump of particular records without create – drop of tables

    27 Mar 2012 in Database

    Hi,
     
    Recently i was in a situation where i need to take the dump of particular records in mysql. We are pretty much familiar with the normal mysqldump statement given below :

    mysqldump --user=usr --password=pwd database_name table_name > dumpFile.sql;
    

    The above statement will provide us the dump of the respective database and table into the dumpFile.sql. But if you open the dump file you will see something like this :
     

      //dumpFile.sql
    DROP TABLE IF EXISTS `table_name`;
    /*!40101 SET @saved_cs_client     = @@character_set_client */;
    /*!40101 SET character_set_client = utf8 */;
    CREATE TABLE `table_name` (
      ...
    ) ENGINE=InnoDB AUTO_INCREMENT=204182 DEFAULT CHARSET=latin1;
    
    --
    -- Dumping data for table `table_name`
    
    LOCK TABLES `table_name` WRITE;
    /*!40000 ALTER TABLE `table_name` DISABLE KEYS */;
    INSERT INTO `table_name` VALUES ('1','data1');
    /*!40000 ALTER TABLE `table_name` ENABLE KEYS */;
    UNLOCK TABLES;
    

    Because of the above dump, it will always first drop and recreate the table before sourcing your dump into the database.
    So our first task to remove the creation and drop of table from the mysqldump, because when we take dump of particular records, What we want is just inserting the records in database without create and drop of tables and no loss of existing data.
     

      //Creating Dump without create and drop of table :
    mysqldump --user=usr --password=pwd --no-create-info database_name table_name > dumpFile.sql;
    

    If you compare the first and above statement there is only one change, –no-create-info option :
    It don’t write CREATE TABLE statements that re-create each dumped table. So our dumpFile.sql now contains only this :

    --
    -- Dumping data for table `table_name`
    
    LOCK TABLES `table_name` WRITE;
    /*!40000 ALTER TABLE `table_name` DISABLE KEYS */;
    INSERT INTO `table_name` VALUES ('1','data1');
    /*!40000 ALTER TABLE `table_name` ENABLE KEYS */;
    UNLOCK TABLES;
    

     

      //Creating Dump of particular records using conditional clause and without create and drop of table :
    mysqldump --user=usr --password=pwd --no-create-info database_name table_name --where="id in (1,2,3)"> dumpFile.sql;
    

    In above statement with –no-create-info, we removed the create-drop of table and by –where option we can specify our condition on basis of which we want to fetch the particular records. In above statement it will fetch the record in table that has value 1, 2 or 3 in field ‘id’. So our dumpFile.sql now contains only this :

    --
    -- Dumping data for table `table_name`
    --
    -- WHERE:  id in (1,2,3)
    
    LOCK TABLES `table_name` WRITE;
    /*!40000 ALTER TABLE `table_name` DISABLE KEYS */;
    INSERT INTO `table_name` VALUES ('1','data1'),('2','data2'),('3','data3');
    /*!40000 ALTER TABLE `table_name` ENABLE KEYS */;
    UNLOCK TABLES;
    

     
    I hope it helps. :)
     
    Thanks,
    Tarun Pareek
    tarun@intelligrape.com
    http://in.linkedin.com/in/tarunpareek
    More Blogs by Me

  • Increasing the connection timeout between browser and the tomcat server

    21 Dec 2011 in System

    Hi,

    There is a case stuck recently in my project where i need to increase the connection timeout between browser and the server, because of the reason before the response get completed server leave the connection which result in no response. My colleague Himanshu told me to change the setting in server.xml, which help in my purpose.

    To increase the connection timeout on tomcat server follow the following steps :

    1. Open the file server.xml residing in tomcat6/conf/.
    2. You just need to set variable connectionTimeout in it to Value in Milliseconds.(i.e 1000 milliseconds = 1 second)

    For example :

    File : server.xml

    <Connector port="8080" protocol="HTTP/1.1"  connectionTimeout="20000" URIEncoding="UTF-8"            redirectPort="8443" />

    I thought it will be useful to share, hope it helps.
     

    Thanks,
    Tarun Pareek
    tarun@intelligrape.com
    http://in.linkedin.com/in/tarunpareek

  • JQGrid Expanding SubGrid on page load

    13 Feb 2011 in Javascript/Ajax/JQuery

    Hi,
     
    Recently guys, i faced problem while expanding the JQGrid SubGrid onLoad of the page.
     
    Initially, i used the following code on gridComplete Event of JQGrid, Using the code given below, i am only able to expand grid but without data populated in subgrid.

    gridComplete: function() {
                    var rowIds = $("#testTable").getDataIDs();
                    $.each(rowIds, function (index, rowId) {
                            $("#testTable").expandSubGridRow(rowId);                   
                    });
                }

    After trying different ways, and many efforts i came up with this solution and it worked for me.
    Solution to above problem given below :

    gridComplete: function() {
                    var timeOut = 50;
                    var rowIds = $("#testTable").getDataIDs();
                    $.each(rowIds, function (index, rowId) {
                        setTimeout(function() {
                            $("#testTable").expandSubGridRow(rowId);
                        }, timeOut);
                        timeOut = timeOut + 200;
                    });
                }

    It worked for me. Hope it help you too.

    Thanks & regards,
    Tarun Pareek
    LinkedIn

  • JQGrid Powerful Plugin With Cool Features

    12 Jan 2011 in Javascript/Ajax/JQuery

    Recently i worked on a JQuery plugin named JQGrid, I found it very productive if you know how to code in JQuery and Javascript. As it also provide event handler and user API it make it more flexible and easier to code.
     
    JQgrid provide plenty of features, some of its cool features such as,
     
    - Solve problem regarding fixing table header while inner scroll is applied to the table data(body).
    - Easy to implement Basic Grid, with searching, sorting and filtering with fix header and scalable table structure.
    - Provide both XML and JSON support for data. You can also use local data and generate grid using it.
    - Provide pagination, and virtual scrolling(Auto loading of data while scrolling)
    - Multi language support(i18n)
    - Provide subgrid implementation
    - Provide Grouping
    - Provide Tree grid
    - Event Handling and various methods gives more flexibility
    - Formatting is another cool feature, you can format the content of the cell and create your custom formatters also.
    - Inline Editing in supported(text, textarea, checkbox, select, image, button type).
    - Client side validation of entered data is supported.
    - Form Editing is supported, full control of form, all features of editing including client side validation.
    - Similarly support cell Editing.
    - Searching and filtering,
       - custom search
       - toolbar search
       - complex form search
       - Advanced searching with criteria defined
    - Support loadonce(load all the data at once)
    - hideGrid Feature, work like accordian, collapse/expand of table on click of header.
    - Remote data upload
    - Navigator footer, you can add your own button to provide functionality on footer.
    - Also provide row footer,
    - UI DatePicker support, JQuery UI Themes etc.
     
    Some Tricks :
    - Use ignoreCase:true feature to apply case-insensitive filtering.
    - To display All rows, either simply set the number of rows to rowNums or you can use ‘-1′, set rowNum:-1 it will display all the rows.
    Note : Precaution leave always last row blank, as ‘-1′ deduct one row from all the record in the table.(I face the same problem)
    - Use afterInsertRow Event, But Note: this event does not fire if gridview option is set to true.
     
    A DrawBacks :
    - Doesn’t support row and colspan :( That the one thing i feel is missed in plugin.
     
    Else very powerful plugin. :)
     
    This plugin provide so much feature which really make the table implementation with so much functionality on the tip of your hand.
     
    Following Links that may Help you in implementation of JQgrid 3.8,
     
    Demo : http://trirand.com/blog/jqgrid/jqgrid.html
    Documentation : http://www.trirand.com/jqgridwiki/doku.php?id=wiki:jqgriddocs
    Events : http://www.trirand.com/jqgridwiki/doku.php?id=wiki:events
    Options : http://www.trirand.com/jqgridwiki/doku.php?id=wiki:options
    Methods : http://www.trirand.com/jqgridwiki/doku.php?id=wiki:methods
    Treegrid : http://www.trirand.com/jqgridwiki/doku.php?id=wiki:treegrid
     
    Hope this, JQgrid plugin help you to improve your UI and implementation made much easier. (Powerful plugin with cool features.)
     
    Thanks & regards,
    Tarun Pareek
    LinkedIn

  • Linux Trick for screen collaboration or shell sharing

    14 Nov 2010 in Linux&System

    In recent week, i got to know about really good way of collaborating the screen in a linux.
     
    Let us take a scenario, Suppose User having name ‘B’ on the 5th floor of the company is having a problem/difficulty related to code execution, unable to figure out what to do?, and then he call the fellow guy who can help him out, but he is on 1st floor his name is ‘Lazy A’.
     
    Lazy A guy is really lazy, so here come handy screen collaboration trick to help him out and save him from walking 4 Floors to reach 5th one (:phew!:)
     
    Lazy A support guy open its terminal and perform the following tricks consisting of 4 steps:
     
    1. ‘Lazy A’ remote login into the User B system through following command:

    lazyA# ssh B

    2. After that Lazy A logged in as super user B, on B machine

    B# su - B

    3. Now Lazy A is on the B machine logged in as super user B, and ‘Lazy A’ runs the following command,

    B# screen -S sos

    4. Then Lazy A guy ask B to run following command on his/her system :

    B# screen -x sos

     
    Note that same alias ‘sos’ is being used by both in last 2 commands, and both guy are logged in as same user.
     
    Actually the last two command cause both user session joined together in linux shell and now both users can type on there shell and see what other is typing or doing to fix the issue, and this is how lazy A help out the B without walking to 5th floors.
     
    After providing help, ‘Lazy A’ terminate the session by performing ctrl-C and then ctrl-D command on its terminal. Thats the way they get detach from each other and get back to there respective work.
     
    And code happily ever after :)
     
    Example to Test command on your machine:
     
    To test the ‘screen -S’ and ‘screen -x’ command you can open two separate terminal or shell on your linux system, then run following command on your 1st terminal or shell
     
    Terminal 1:

    # su - tarun
    tarun# screen -S mytest

    And run another command on Terminal 2:

    tarun# screen -x mytest

    Then type on any screen you will see what you are doing on one is appearing on both the shells. To terminate session use ctrl-C and then ctrl-D. :)
     
    Regards,
    Tarun Pareek
    LinkedIn