Saturday, March 3, 2012

wm_concat for Oracle

I need to add a style description to a Phone domain.  However Style has a many to many relationship to Phone in Database.  Usually, one would just create the Style Domain and the Mapping in Hibernate to add it as a set in Phone Domain.  But in my case, all I want is a concatenated String of all style names a Phone has. And to do so, I'll have to iterate through the set to join all the style names as a string.  Consider Phone is already heavily packed and we usually query for large list of Phones throughout our application, plus that we only display style on one newly designed page, the easy way will not be an efficient way.

So I tried to google out a better way to do it.  What I gathered most is that for MySql, group_concat can achieve what I want.  Unfortunately, it's not valid for Oracle.  And, there are a lot discussions on Oracle forum on how to achieve the same effort through very complicated queries and I got lost about half way through.

Then I found this little post on wm_concat for Oracle.  On my first tryout, I was able to get it working in my project.

In my hibernate mapping, instead of:
        <set
            name="stylePhoneMap"
            table="style_phone_map"
            batch-size="5"
            lazy="false"
            cascade="none">
            <key column="phone_id"/>
            <one-to-many class="com.vzw.my.company.domain.phone.StylePhoneMap"/>
        </set>
I used:
        <property
            name="style"
            type="java.lang.String"
            formula="(select wm_concat(a.style_name) from style a, style_phone_map b where a.style_id=b.style_id and b.phone_id = phone_id)"
            insert="false"
            update="false"
        />
The result of the formula will return as a CLOB in SQL Navigator.  I would put a length attribute on the property if for fact the result will be a long String.  But for my case, I know for sure I don't need to.  All style names are concatenated as one String with comma as separator.  No extra domain object, no extra Java code to convert the Set to a String.  What a great trick!
 

Monday, January 9, 2012

L10n in Flex Quick Guide

Recently I was looking into localization of my Flex project, not to support multiple language, but to support different server environment setup.

So I came up with two options:
  1. Using l10n property file:
    1.  Create a locale directory right under the project.
    2. Add a sub directory under locale named with the locale you want to add.
    3. Add the property file under the sub directory (Note this is different than the Java i18n setup which you just create property file in the pattern <Resource Name>_<locale>.properties)
    4. Repeat 2 and 3 if you have more locales to add.
    5. Go to Project Properties and then Flex Compiler, in the Additional compiler argument section add "-locale <locale list seperated by comma> -source-path=locale/{locale} -allow-source-path-overlap=true"
    6. In mxml file add [ResourceBundle("<Resource File Name>")] to metadata.
    7. Wherever you need a resource, use resourceManager.getString('<Resource File Name>', '<Resource Key>').
  2. Passing environment variables as FlashVars:
    1. Add flashvars as parameter in object in jsp file
    2.  In mxml, using FlexGlobals.topLevelApplication.parameters.<parameter key> to get the passed in value
 

Option 2 is obviously easier to setup.  We don't need to add additional property files but reference existing resource setup in Java project.