Showing posts with label JSF. Show all posts
Showing posts with label JSF. Show all posts

Thursday, July 17, 2014

Displaying amounts in differnt formats in JAVA, SQL and JSF

I'm working in a product company in Finance domain. We have a clients all across the globe.
Being a product company, it is required to cater the requirements of all clients as per their needs.

Following blog is very helpful to serve the amount display in various formats

One of the requirements is to show the amounts on screen in clients own format
e.g. INTEGER QTY: 123456    DOUBLE AMT: 345987.24 should be displayed as follows


LOCALE: English
INTEGER QTY: 123,456      DOUBLE AMT: 345,987.24

LOCALE: English (Canada)
INTEGER QTY: 123,456      DOUBLE AMT: 345,987.24

LOCALE: French (France)
INTEGER QTY: 123 456      DOUBLE AMT: 345 987,24

LOCALE: German (Germany)
INTEGER QTY: 123.456      DOUBLE AMT: 345.987,24

LOCALE: Italian (Italy)
INTEGER QTY: 123.456      DOUBLE AMT: 345.987,24

LOCALE: Japanese
INTEGER QTY: 123,456      DOUBLE AMT: 345,987.24

LOCALE: German (Germany)
INTEGER QTY: 123.456      DOUBLE AMT: 345.987,24

Prerequisite : We will have to set a locale per environment for the clients region e.g. FR, EN, DE, etc

JAVA :  In java we can format the field in specific locale format in String and use it to display in that locale's format as shown in below TestUtil.java class which is used to format numbers, amounts, rates, quantity, percentage, etc


import java.text.NumberFormat;
import java.util.Locale;

public class TestUtil {

    static public void displayNumber(Locale currentLocale, Integer quantity,
            Double amount) {

        NumberFormat numberFormatter;
        String quantityOut;
        String amountOut;

        numberFormatter = NumberFormat.getNumberInstance(currentLocale);
        quantityOut = numberFormatter.format(quantity);
        amountOut = numberFormatter.format(amount);

        System.out.println("LOCALE: " + currentLocale.getDisplayName());
        System.out.println("INTEGER QTY: " + quantityOut);
        System.out.println("DOUBLE AMT: " + amountOut);
    }

    static public void displayCurrency(Locale currentLocale, Double currency) {

        NumberFormat currencyFormatter;
        String currencyOut;

        currencyFormatter = NumberFormat.getCurrencyInstance(currentLocale);
        currencyOut = currencyFormatter.format(currency);
        System.out.println(currencyOut + "   " + currentLocale.toString());
    }

    static public void displayPercent(Locale currentLocale, Double percent) {

        NumberFormat percentFormatter;
        String percentOut;

        percentFormatter = NumberFormat.getPercentInstance(currentLocale);
        percentOut = percentFormatter.format(percent);
        System.out.println(percentOut + "   " + currentLocale.toString());
    }

    static public void main(String[] args) {

        Locale localeTmp = new Locale("de", "DE");
        Locale[] locales = { Locale.ENGLISH, Locale.CANADA, Locale.FRANCE,
                Locale.GERMANY, Locale.ITALY, Locale.JAPANESE, localeTmp };

        Integer quantity = new Integer(123456);
        Double amount = new Double(345987.24);
        Double currency = new Double(9876543.21);
        Double percent = new Double(0.75);

        System.out.println("INTEGER QTY: " + quantity);
        System.out.println("DOUBLE AMT: " + amount);
        // System.out.println("DOUBLE CURRENCY: " + currency);
        // System.out.println("DOUBLE PERCENTAGE: " + percent);

        for (int i = 0; i < locales.length; i++) {
            System.out.println();
            displayNumber(locales[i], quantity, amount);
            // displayCurrency(locales[i], currency);
            // displayPercent(locales[i], percent);
        }
    }

}



SQL : In SQL we have NLS (National Language Support) which can be used to fetch and alter current session’s values. Current values can be fetched by below query.

SELECT * FROM V$NLS_PARAMETERS;

--To alter language for current session:
ALTER SESSION SET NLS_LANGUAGE=GERMAN;
select * from nonexistent_table;

--To alter territory for current session
ALTER SESSION SET NLS_TERRITORY=GERMANY;

--This will display amount in this locale’s format.
select to_char(a.amount), a.amount from my_table a;


JSF : Simple change would be on all JSFs (jspx) files, wherever amounts are shown. We can use application level parameter for locale and show amounts in that locale’s format. For <f:convertNumber> tag, there is one attribute ‘locale’. Its value can be dynamically set from system params. Refer below code snippet with hardcoded parameter as 'DE'.



<h:outputText value="Total Amount" />
<h:panelGroup>
    <h:outputText value="#{mtBean.myVo.amount}">
        <f:convertNumber maxFractionDigits="2" minFractionDigits="2" locale="DE"/>
    </h:outputText>
</h:panelGroup>


Wednesday, May 18, 2011

Pre-populate values in h:selectManyListbox

I was able to add h:selectManyListbox on my JSF page, select some values, store in database. But while pre-populating the selected values facing issue.

But in following code h:selectManyListbox was not pre-populating the selected values.

code snippet was like this


<h:selectManyListbox styleClass="inputNameSel"

value="#{accountKeyBean.editSegment.actualValuesList}"

rendered="#{accountKeyBean.editSegment.methodType == 'Map'}">

<f:selectItems

value="#{accountKeyBean.validGLBusinessEntities}" var="misc"

itemLabel="#{misc.code}" itemValue="#{misc.code}" />

</h:selectManyListbox>


where
actualValuesList is List<MiscObject> and validGLBusinessEntities is also List<MiscObject>
I was able to populate the actualValuesList with List<MiscObject> which should be pre-selected.

I thought this will work because it was my understanding that 'value' element for h:selectManyListbox
should be of same type as that of 'value' element of f:selectItems.

I wondered, though I'm giving correct input to the actualValuesList, it is not showing the pre-selected items.

After lot of analysis, with my friend's help, could figure out that the datatype of actualValuesList should be List<String>, which worked :)

Friday, February 11, 2011

Rich modal panel, submitting values

It was a big problem for me, to open a pop-up (rich:modalPanel) and putting some values and submitting the form. The values entered were not getting populated in server bean.

While developing I was trying following different things
1. Put ajaxSingle = "true" on link
2. Put immediate = "true" on link
3. Place modal panel inside form
4. Place modal panel outside form

and various combination for above tries....nothing worked.
It was pure trial and error as I'm a newbee to JSF.

But finally one try hit the server with correct values.
And the combination is
1. Write rich modal panel with
2. Put the panel outside the main form
3. Don't put ajaxSingle = "true" on link


If you just want to open a modal panel, and not submitting any values, ajaxSingle = "true" will work.

Write me (ashtaganesh@gmail.com) if there is still any problem, will like to look if above doesn't works in any case.

Friday, January 21, 2011

Populate the values at runtiime on some value selection - JSF

In JSF, if you select a value from drop down box and depending on this value if you want to forward a request to server and populate related other values on the screen.
This can be handled with the following example

If my screen shows following fields on the screen
1. drop down of student's roll numbers
2. Text box for student's name
3. Text box for student's class
4. Text box for student's address

Information is stored in database in 'STUDENTS' table.

On selection of student's roll number, to populate the other fields my code will look like


<h:outputtext id="rollNumberLbl" value="Roll Number">

<h:selectonemenu id="rollNumComboBx" styleclass="normalTxtBox" value="#{studentBean.rollNo}" valuechangelistener="#{studentBean.getStudentByRollNumber}">

<f:selectitems id="rollNumList" itemvalue="#{rollNum.rollNo}" value="#{studentBean.rollNumbersList}" var="rollNum">

<a4j:support ajaxsingle="true" event="onchange" rerender="studNameTxt,studClassTxt,studAddrTxt">

</a4j:support>



<h:outputtext id="nameLbl" value="Student Name : ">

<h:inputtext id="studNameTxt" value="#{studentBean.studentName}">



<h:outputtext id="classLbl" value="Student Class : ">

<h:inputtext id="studClassTxt" value="#{studentBean.studentClass}">



<h:outputtext id="addrLbl" value="Student Address : ">

<h:inputtext id="studAddrTxt" value="#{studentBean.studentAddress}">





</h:inputtext></h:outputtext></h:inputtext></h:outputtext></h:inputtext></h:outputtext></f:selectitems></h:selectonemenu></h:outputtext>

SpringBoot: Features: SpringApplication

Below are a few SpringBoot features corresponding to SpringApplication StartUp Logging ·          To add additional logging during startup...