Expand my Community achievements bar.

SOLVED

Flex with Java - DB data access

Avatar

Level 2
Hello, folks,



I am developing a Java application and realized that I need
some advice. My application is simple:

I have a MSSQL server, tomcat 5.5 ,flex data services.



In my application , i have two combo boxes.. country, town..

i have populated the country combobox with the values from
DB(country table) obtained using the fill(List ListArgs) in
Assembler Class.

Now, based on the country selected in have to populate my
town combobox from (town table)

How can i populate the town combobox from the DB based on the
country code?

Is there any function to pass the selected index and populate
the town combo box..????



My code is as below:

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="
http://www.adobe.com/2006/mxml"
layout="absolute" >

<mx:DataService id="ds" destination="quick-search-country"
autoCommit="false" autoSyncEnabled="false"/>

<mx:DataService id="tds" destination="quick-search-town"
autoCommit="false" autoSyncEnabled="false"/>

<mx:ArrayCollection id="cntry_list"/>

<mx:ArrayCollection id="town_list"/>

mx:Panel layout="absolute" backgroundColor="#c6daf5"
verticalCenter="0" horizontalCenter="0">

<mx:Form id="SearchForm">

<mx:TabNavigator >

<mx:Canvas label="Hotel" width="339" height="234"
backgroundColor="#ffffff">

<mx:Label x="10" y="91" text="Country :" alpha="0.12"
/>

<mx:ComboBox x="93" y="89" fontSize="10" id="Country"
dataProvider="{cntry_list}"

labelField="countryName"
creationComplete="ds.fill(cntry_list)"

change="tds.getItem(Country.selectedItem)" prompt="United
Kingdom" >

</mx:ComboBox>

<mx:Label x="10" y="126" text="Town / City :"
fontFamily="Georgia" alpha="0.12"/>

<mx:ComboBox id="town" x="94" y="124" fontSize="10"
dataProvider="{town_list}"

labelField="townName" prompt="London WC1" >

</mx:ComboBox>

</mx:Canvas>

</mx:TabNavigator>

</mx:Form>

</mx:Panel>

</mx:Application>

this is my main.mxml . I have CountryAssembler to fill the
country combo and a DAO class . Have a Town assembler..



Please advice me on how to populate the town combo based on
country selected from the DB...

I am really stuck up... and also dont have any documentation
on Flex data services with Java support..

please give me some suggestions on this.



Thanks in Advance,

Ambili





1 Accepted Solution

Avatar

Correct answer by
Former Community Member
Ambili,



Mete is suggesting that since Country.selectedItem is your
Country object,

the country_code should be accessible via
Country.selectedItem.country_code

(although I'm personally not much of a fan of direct binding
syntax, one

thing I saw in your posts was that you have different
conventions for how

you name these properties, sometimes you refer to them in the
style country_name

and then others as countryName... so depending on how you've
actually implemented

it, it may be Country.selectedItem.countryCode or
Country.selectedItem.country_code).



Pete





View solution in original post

6 Replies

Avatar

Former Community Member
Hi Ambili,



First, assuming that you're using LCDS 2.5 (new name for FDS
after 2.0), here's the developer's guide that has a lot of
information:
http://www.adobe.com/go/lcds_devguide



Second, I think what you're trying to achieve can be done
with a custom DataService.fill method. DataService.fill can take in
optional arguments that can be processed in your Assembler class.



On the client side, assuming that you have an ArrayCollection
named towns where you keep track of towns, and a String of
countryName where you keep track of the current name of the country
being displayed, when a user selects a country, you can call
tds.fill(towns, 'by-country', countryName).



On the server side, in your TownAssembler, you will need to
handle the parameterized fill and do something like this in the
fill method:



public Collection fill(List fillParameters)

{

if (fillParameters.size() == 0)

return dao.getTowns(); // This will return all the towns
probably.

String queryName = (String)fillParameters.get(0);

if (queryName.equals("by-country"))

return
dao.getTownsByCountry(((Integer)fillParameters.get(1)).intValue());

}



Of course, I'm assuming here that you have a TownDAO that
performs the DB operations to actually retrieve the list of towns
and return it back.

Avatar

Level 2
Hi Mete,



Thanks for ur reply , Mete. Your reply helped me a lot. Am
using FDS 2.0.



I have a doubt in combo box area itself.



I have two tables by name country_list and town_list



country_list consists of identity column , country_name and
country_code

town_list has identity column, country_code, town_code and
town_name



Normally while we use combobox in jsp, we have key value
pairs for combo box..

For eg:- if we have a Country combo , we have the country
name(United States) as text displayed and its country code (US) as
key.



So if i select the United States , i will be returned with
the value 'US' so that i can use it in query directly as



Select town_name, town_code from town_list where country_code
='US'



But while using flex , am not able to retieve the
country_code from country combo. its only returning the text and
the id.



Here am forced to write a nested query as follows



Select town_name, town_code from town_list where country_code
=( Select country_code from country_list where country_name='United
States')



Is there any way to get the country_code as key while i
select the country from country combo .

And also how to keep 'United States' as selected during the
intial load of the page.



<mx:ArrayCollection id="cntry_list"/>

<mx:ComboBox x="93" y="89" fontSize="10" id="Country"
dataProvider="{cntry_list}"

labelField="countryName"
creationComplete="ds.fill(cntry_list)"

prompt="Select Country" />



please give me some suggestions on this.



Thanks in Advance,

Ambili

Avatar

Former Community Member
Hi Ambili, as long as your cntry_list array contains Country
objects, the ComboBox's selectedItem property should have the
currently selected Country with all of its properties.



Are you using lazy loading by any chance in your
DataService?

Avatar

Level 2
hi Mete,



Combo box's selecteditem property is not returning
country_code..

My country list contains country objects... Country Code and
Country name.

is this problem occurs bcoz am not mapping country_code any
where in mxml ,

Should i map it some where like CountryName?





hw i will retrieve the data?





What is lazy loading.. dint understand that?



thx,

Ambili





Avatar

Correct answer by
Former Community Member
Ambili,



Mete is suggesting that since Country.selectedItem is your
Country object,

the country_code should be accessible via
Country.selectedItem.country_code

(although I'm personally not much of a fan of direct binding
syntax, one

thing I saw in your posts was that you have different
conventions for how

you name these properties, sometimes you refer to them in the
style country_name

and then others as countryName... so depending on how you've
actually implemented

it, it may be Country.selectedItem.countryCode or
Country.selectedItem.country_code).



Pete





Avatar

Level 2
hi Pete,



Good morning... thank you for your reply..

Actually i dint go thru any documentation for flex.. just
looked at the sample application and started coding..



The convension i use in coding is countryName and
countryCode.

In the post i dint take care of convension. sorry for that.



Thanks for the reply. It really helped me a lot..



Thx,

Ambili

The following has evaluated to null or missing: ==> liqladmin("SELECT id, value FROM metrics WHERE id = 'net_accepted_solutions' and user.id = '${acceptedAnswer.author.id}'").data.items [in template "analytics-container" at line 83, column 41] ---- Tip: It's the step after the last dot that caused this error, not those before it. ---- Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: #assign answerAuthorNetSolutions = li... [in template "analytics-container" at line 83, column 5] ----