Wednesday, December 18, 2013

Grails access HttpSession from service or from other src files


import org.springframework.web.context.request.RequestContextHolder; 
HttpSession session = RequestContextHolder.currentRequestAttributes().getSession();

##### 
/* Code */
session.properties.each {
    println it;
}

/* Output from above code */
creationTime=1387331940863
maxInactiveInterval=1800
sessionContext=org.apache.catalina.session.StandardSessionContext@1ff04376
attributeNames=java.util.Collections$2@2aec16fa
class=class org.codehaus.groovy.grails.web.servlet.mvc.GrailsHttpSession
servletContext=org.apache.catalina.core.ApplicationContextFacade@3dc6e9f8
new=false
id=A2791167D1EC3D21025078895357147D
valueNames=[Ljava.lang.String;@71d88d19
lastAccessedTime=1387332146960

#####
/* Code */
session.attributeNames.each {
    println it;
}

/* Output from above code */
SETTING
org.codehaus.groovy.grails.FLASH_SCOPE
USER

#####
/* Code */
session.attributeNames.each {
    println session.getAttribute(it.toString());
}

/* Output from above code */
[paginationCount:10, generateClientID:On]
[:]
[name:Admin, id:1, username:admin, isSystemAdmin:true]

Friday, December 13, 2013

Convert string to ascii and ascii to string using JAVA

Java code to convert string to ascii code and ascii code to string back

Java Regex to convert string to ascii code and ascii code to string back



/**
 *
 * @author Pritom K Mondal
 */
public class AsciiString {
    public static void main(String[] args) {
        String inputString = "Hi, THAI(คุณ), HINDI:(तुम मेरी हो), HANGERIO:(تو مال منی), CHINA:(您), ARBI(أنت), FARSI(شما)";
        System.out.println("ORIGINAL:      " + inputString);
        String encoded = AsciiString.encode(inputString);
        System.out.println("ASCII ENCODED: " + encoded);
        String decoded = AsciiString.decode(encoded);
        System.out.println("ASCII DECODED: " + decoded);
    }
    
    public static String encode(String word) {
        String encoded = "";
        for(Integer index = 0; index < word.length(); index++) {
            int ascii = (int) word.charAt(index);
            Boolean keepAscii = true;
            if(ascii >= 48 && ascii <= 57) {
                keepAscii = false;
            }
            if(ascii >= 65 && ascii <= 90) {
                keepAscii = false;
            }
            if(ascii >= 97 && ascii <= 122) {
                keepAscii = false;
            }
            if(ascii == 32 || ascii == 43 || ascii == 45 || ascii == 46) {
                keepAscii = false;
            }
            if(keepAscii) {
                encoded += "&#" + ascii + ";";
            } else {
                encoded += word.charAt(index);
            }
        }
        return encoded;
    }
    
    public static String decode(String word) {
        String decoded = "";
        for(Integer index = 0; index < word.length(); index++) {
            String charAt = "" + word.charAt(index);
            if(charAt.equals("&") && index < word.length() && ("" + word.charAt(index + 1)).equals("#")) {
                try {
                    Integer length = word.indexOf(";", index);
                    String sub = word.substring(index + 2, length);
                    decoded += Character.toString((char) Integer.parseInt(sub));
                    index = length;
                } catch (Exception ex) {
                    decoded += charAt;
                }
            } else {
                decoded += charAt;
            }
        }
        return decoded;
    }
}

Output for the above program is as following:


ORIGINAL:      Hi, THAI(คุณ), HINDI:(तुम मेरी हो), HANGERIO:(تو مال منی), CHINA:(您), ARBI(أنت), FARSI(شما)
ASCII ENCODED: Hi&#44; THAI&#40;&#3588;&#3640;&#3603;&#41;&#44; HINDI&#58;&#40;&#2340;&#2369;&#2350; &#2350;&#2375;&#2352;&#2368; &#2361;&#2379;&#41;&#44; HANGERIO&#58;&#40;&#1578;&#1608; &#1605;&#1575;&#1604; &#1605;&#1606;&#1740;&#41;&#44; CHINA&#58;&#40;&#24744;&#41;&#44; ARBI&#40;&#1571;&#1606;&#1578;&#41;&#44; FARSI&#40;&#1588;&#1605;&#1575;&#41;
ASCII DECODED: Hi, THAI(คุณ), HINDI:(तुम मेरी हो), HANGERIO:(تو مال منی), CHINA:(您), ARBI(أنت), FARSI(شما)

Now create a html file named 'index.html' such:


<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>String and ASCII</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    </head>
    <body>
        <div>ORIGINAL:      Hi, THAI(คุณ), HINDI:(तुम मेरी हो), HANGERIO:(تو مال منی), CHINA:(您), ARBI(أنت), FARSI(شما)</div>
        <div>ASCII ENCODED: Hi&#44; THAI&#40;&#3588;&#3640;&#3603;&#41;&#44; HINDI&#58;&#40;&#2340;&#2369;&#2350; &#2350;&#2375;&#2352;&#2368; &#2361;&#2379;&#41;&#44; HANGERIO&#58;&#40;&#1578;&#1608; &#1605;&#1575;&#1604; &#1605;&#1606;&#1740;&#41;&#44; CHINA&#58;&#40;&#24744;&#41;&#44; ARBI&#40;&#1571;&#1606;&#1578;&#41;&#44; FARSI&#40;&#1588;&#1605;&#1575;&#41;</div>
        <div>ASCII DECODED: Hi, THAI(คุณ), HINDI:(तुम मेरी हो), HANGERIO:(تو مال منی), CHINA:(您), ARBI(أنت), FARSI(شما)</div>
    </body>
</html>

Now showing in browser:


In browser they are appearing same. Such original string, after ascii conversion and the back to again string. So why you convert them?
  • It is easy to maintain them.
  • Easy to insert to and get from database.
  • You have no worry about what characters are you inserting to database.
  • If you create xml from those data, you do not need to worry about what characters support in xml file, specially when you transport data via api server.

Thursday, December 12, 2013

Base64 encode/decode with JAVA

Java code to encode and decode base64


package pritom;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * Created with IntelliJ IDEA.
 * User: pritom
 * Date: 12/12/13
 * Time: 10:41 AM
 * To change this template use File | Settings | File Templates.
 */
public class EncodeDecodeBase64 {
    public static void main(String[] args) throws Exception{
        String baseString = "TESTING BASE 64 ENCODING AND DECODING WITH JAVA";
        System.out.println("Base string:     " + baseString);

        /**
         * Encoding to base 64
         */
        BASE64Encoder base64Encoder = new BASE64Encoder();
        String encodedString = base64Encoder.encodeBuffer(baseString.getBytes());
        System.out.print("Encoded base 64: " + encodedString);

        /**
         * Decoding from base 64
         */
        BASE64Decoder base64Decoder = new BASE64Decoder();
        String decodedString = new String(base64Decoder.decodeBuffer(encodedString), "UTF-8");
        System.out.println("Decoded base 64: " + decodedString);
    }
}

Output


Base string:     TESTING BASE 64 ENCODING AND DECODING WITH JAVA
Encoded base 64: VEVTVElORyBCQVNFIDY0IEVOQ09ESU5HIEFORCBERUNPRElORyBXSVRIIEpBVkE=
Decoded base 64: TESTING BASE 64 ENCODING AND DECODING WITH JAVA

Checking if an element is hidden with jQuery

Html

<form style='display: none'>
<input id='name' type='text' style='display: inline-block' />
</form>

jQuery code


if( $("input#name").is(":visible") ) {
    /* do something, selector is not hidden */
}

This will select the input field and then test correctly whether it is visible in the DOM. This check will make sure all parent elements are visible too, as well as the original CSS display attribute.

Wednesday, December 11, 2013

Change i18n locale from a controller before calling the view


package pritom.com.locale;

import org.springframework.web.servlet.support.RequestContextUtils as RCU

class FormController {
    def test() {
        Locale locale = new Locale("fr");
        RCU.getLocaleResolver(request).setLocale(request, response, locale);
        render g.message(code: "default.paginate.prev");
    }
}

Monday, December 9, 2013

Reading i18n messages from the database with Grails

At first create a model class:


package com.locale.messaging

class Message {
    String code
    Locale locale
    String text

    static constraints = {
        code(unique: ["locale"])
    }

    static mapping = {
        version(false);
    }
}

Then implement a class that extends the org.springframework.context.support.AbstractMessageSource class. In the example below I am using simple GORM finders to lookup a message using the code and locale


package com.locale.messaging

import com.locale.messaging.Message
import net.sf.ehcache.Ehcache
import org.springframework.context.support.AbstractMessageSource
import net.sf.ehcache.Element;
import java.text.MessageFormat

/**
 * Created with IntelliJ IDEA.
 * User: pritom
 * Date: 9/12/13
 * Time: 9:09 AM
 * To change this template use File | Settings | File Templates.
 */
class DatabaseMessageSource extends AbstractMessageSource {
    Ehcache messageCache
    def messageBundleMessageSource

    @Override
    protected MessageFormat resolveCode(String code, Locale locale) {
        def key = "${code}_${locale.language}_${locale.country}_${locale.variant}";
        def format = messageCache.get(key)?.value;
        if (!format) {
            Message message = Message.findByCodeAndLocale(code, locale);
            if (message) {
                format = new MessageFormat(message.text, message.locale);
            } else {
                format = messageBundleMessageSource.resolveCode(code, locale);
            }
            messageCache.put(new Element(key, format))
        } else {
            format = (MessageFormat) format;
        }
        return format;
    }
}

Then configure an appropriate cache bean (I'm using Ehcache) in Spring and wire it into your MessageSource in grails-app/conf/spring/resources.groovy file:

import com.bitmascot.util.DatabaseMessageSource
import org.springframework.cache.ehcache.EhCacheFactoryBean

// Place your Spring DSL code here
beans = {
    messageCache(EhCacheFactoryBean) {
        eternal = false;
        timeToIdle = 5000;
    }
    messageSource(DatabaseMessageSource) {
        messageCache = messageCache
        messageBundleMessageSource = ref("messageBundleMessageSource")
    }

    messageBundleMessageSource(org.codehaus.groovy.grails.context.support.PluginAwareResourceBundleMessageSource) {
        basenames = "grails-app/i18n/messages"
    }
}

http://graemerocher.blogspot.com/2010/04/reading-i18n-messages-from-database.html

Sunday, December 8, 2013

How to Set up a Foreign Key Constraint in MySQL

The following is a simple example that illustrates Foreign Key constraints:

Creating a table for manager:
CREATE TABLE manager (id integer primary key auto_increment, name text) ENGINE=InnoDB;

Creating a table for club and creating a foreign key constraint for manager_id with key 'manager_id_reference':
CREATE TABLE club (id integer primary key auto_increment, is_accessible char, manager_id integer NOT NULL) ENGINE=InnoDB;
ALTER TABLE club ADD CONSTRAINT manager_id_reference FOREIGN KEY(manager_id) REFERENCES manager (id);

Inserting some managers:
INSERT INTO manager(name) VALUES('Pritom');
INSERT INTO manager(name) VALUES('Kumar');

Inserting some clubs:
INSERT INTO club(is_accessible, manager_id) VALUES(true, 1);
INSERT INTO club(is_accessible, manager_id) VALUES(false, 2);

Let see manager table data (SELECT * FROM manager):
ID NAME
1 Pritom
2 Kumar

Let see club table data (SELECT * FROM club):
ID IS_ACCESSIBLE MANAGER_ID
1 1 1
2 0 2
Let see manager table structure (DESCRIBE manager):
FIELD TYPE NULL KEY DEFAULT EXTRA
id int(11) NO PRI (null) auto_increment
name text YES (null)
Let see club table structure (DESCRIBE club):
FIELD TYPE NULL KEY DEFAULT EXTRA
id int(11) NO PRI (null) auto_increment
is_accessible char(1) YES (null)
manager_id int(11) NO MUL (null)

Now try to delete manager:
DELETE FROM USER WHERE id = 1;

will error as:
Schema Creation Failed: Cannot delete or update a parent row: a foreign key constraint fails (`db_2_8d4a1`.`club`, CONSTRAINT `manager_id_reference` FOREIGN KEY (`manager_id`) REFERENCES `manager` (`id`)):

This happens because the data in the manager table depends on the data in the club table.

Full sqlFiddle