Flex and CFMX 7 using an Array of queries

I am starting to experiment on how to send an ‘array of queries’ from CFMX 7 with 1 httpservice call from Flex. This service will send 1 xml packet of data for 4 data grids. So, instead of sending 4 httpserice calls from flex to cfmx and sending back 4 xml packets, I thought maybe less data and less requests would speed up the response time. I will add more content as I finish this code (8/26/2009).

1.) You will notice that I modified querytoxml in cflib for my purposes.

Flex (send data to cfmx)
=============================================================================
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml&#8221; layout=”absolute”
creationComplete=”_init_app()”
width=”1280″ height=”100%”
themeColor=”#D6DADC” backgroundGradientAlphas=”[1.0, 1.0]”
backgroundGradientColors=”[#7AB4F6, #C2DCF8]” >

<mx:Script>
<![CDATA[

// get XML to data grids
public function _init_app(): void {

idloadSHOWALL.send();

} // end of init….

// show errors from server
public function handleFault(event:FaultEvent,a_descr:String):void {

Alert.show(event.fault.faultString + ” ” + a_descr, “Error”);
} // end of handle fault…

 
]]>
</mx:Script>

 

<mx:HTTPService
id=”idloadSHOWALL”
url=”/flex/get_datagrids.cfm”
useProxy=”false” showBusyCursor=”true”
result=”set_datagrids(event);”
fault=”handleFault(event,’Get Data Grid’);” />

CFMX 7 (build xml and send back to Flex)
===============================================================================

<cfparam name=”url.a_tabid” type=”any” default=”” >
<cfparam name=”url.a_deptid_emplid” type=”any” default=”” >
<cfparam name=”url.a_panelid” type=”any” default=”” >
<cfparam name=”url.a_sort” type=”any” default=”1″ >

<cfsetting enablecfoutputonly=”yes” showdebugoutput=”No”>

<cfsilent>

<cfscript>
/**
* Generates an XMLDoc object from a basic CF Query.
*
* @param query The query to transform. (Required)
* @param rootElement Name of the root node. (Default is “query.”) (Optional)
* @param row Name of each row. Default is “row.” (Optional)
* @param nodeMode Defines the structure of the resulting XML. Options are 1) “values” (default), which makes each value of each column mlText of individual nodes; 2) “columns”, which makes each value of each column an attribute of a node for that column; 3) “rows”, which makes each row a node, with the column names as attributes. (Optional)
* @param basicinfo Y/N (Y=default) Basic information. (Optional)
* @param v_no_record_col Compare this column name if there is no records. (Optional)
* @param v_no_record_descr Add this description to the column name if there is no records. (Optional)

* @return Returns a string.
* @author Patrick Whittingham
* @version 3, August 2009
*/
function queryToXML(query){

//the default name of the root element
var root = “query”;
//the default name of each row
var row = “row”;
//make an array of the columns for looping
var cols = listToArray(query.columnList);
var v_rows = query.recordCount;
//which mode will we use?
var nodeMode = “values”;
// default : basic info
var basicinfo = “Y”;
var v_no_record_col = “”; var v_no_record_descr = “No Data Found”;
//vars for iterating
var ii = 1; var rr = 1;
//vars for holding the values of the current column and value
var thisColumn = “”; var thisValue = “”;
//a new xmlDoc
var xml = xmlNew();
//if there are 2 arguments, the second one is name of the root element
if(structCount(arguments) GTE 2) root = arguments[2];
//if there are 3 arguments, the third one is the name each element
if(structCount(arguments) GTE 3) row = arguments[3];
//if there is a 4th argument, it’s the nodeMode
if(structCount(arguments) GTE 4) nodeMode = arguments[4];
//if there is a 5th argument, its the basic info.
if(structCount(arguments) GTE 5) basicinfo = arguments[5];
//if there is a 6th argument.
if(structCount(arguments) GTE 6) v_no_record_col = arguments[6];
//if there is a 7th argument.
if(structCount(arguments) GTE 7) v_no_record_descr = arguments[7];

//create the root node
xml.xmlRoot = xmlElemNew(xml,root);
//capture basic info in attributes of the root node
if (basicinfo eq “Y” ) {
xml[root].xmlAttributes[“columns”] = arrayLen(cols);
xml[root].xmlAttributes[“rows”] = query.recordCount;
}
//loop over the recordcount of the query and add a row for each one
for(rr = 1; rr LTE query.recordCount; rr = rr + 1){
arrayAppend(xml[root].xmlChildren,xmlElemNew(xml,row));
//loop over the columns, populating the values of this row
for(ii = 1; ii LTE arrayLen(cols); ii = ii + 1){
thisColumn = lcase(cols[ii]);
thisValue = query[cols[ii]][rr];
switch(nodeMode){
case “rows”:
xml[root][row][rr].xmlAttributes[thisColumn] = thisValue;
break;
case “columns”:
arrayAppend(xml[root][row][rr].xmlChildren,xmlElemNew(xml,thisColumn));
xml[root][row][rr][thisColumn].xmlAttributes[“value”] = thisValue;
break;
default:
arrayAppend(xml[root][row][rr].xmlChildren,xmlElemNew(xml,thisColumn));
xml[root][row][rr][thisColumn].xmlText = thisValue;
}
}
} // end of rr loop….

// NO RECORDS FOUND
if (v_rows eq 0) {
arrayAppend(xml[root].xmlChildren,xmlElemNew(xml,row));
thisColumn = lcase(cols[ii]); thisValue = “”; rr = 1;
//loop over the columns, populating the values of this row
for(ii = 1; ii LTE arrayLen(cols); ii = ii + 1){
thisColumn = lcase(cols[ii]); thisValue = “”;
if (UCASE(thisColumn) eq UCASE(v_no_record_col) ) { thisValue = v_no_record_descr; }
switch(nodeMode){
case “rows”:
xml[root][row][rr].xmlAttributes[thisColumn] = thisValue;
break;
case “columns”:
arrayAppend(xml[root][row][rr].xmlChildren,xmlElemNew(xml,thisColumn));
xml[root][row][rr][thisColumn].xmlAttributes[“value”] = thisValue;
break;
default:
arrayAppend(xml[root][row][rr].xmlChildren,xmlElemNew(xml,thisColumn));
xml[root][row][rr][thisColumn].xmlText = thisValue;
}
}
} // end of if…
//return the xmlDoc
return xml;
}
</cfscript>

<!— show all —>
<cfset v_tabs = “” >
<cfif url.a_sort eq “1” > <cfset url.a_sort = “Ttabno, Tdept ” >
<cfelse> <cfset url.a_sort = “Tdept, Ttabno ” >
</cfif>
<cfinvoke component=”cat”
method=”get_show_all” argumentcollection=”#url#” returnvariable=”ret_showall” />

<cfdump var=”#ret_showall#”>

<cfscript>
v_count = 0; v_content = “”;
v_rows = ArrayLen(ret_showall);
//
v_tab_list = ret_showall[1].columnlist; v_tab_arr = ListToArray(v_tab_list); v_row1 = ret_showall[1].RecordCount;
v_panel_list = ret_showall[2].columnlist; v_panel_arr = ListToArray(v_panel_list); v_row2 = ret_showall[2].RecordCount;
v_link_list = ret_showall[3].columnlist; v_link_arr = ListToArray(v_link_list); v_row3 = ret_showall[3].RecordCount;
v_parms_list = ret_showall[4].columnlist; v_parms_arr = ListToArray(v_parms_list); v_row4 = ret_showall[4].RecordCount;
//
</cfscript>

<cfset xmlObj1 = queryToXML(ret_showall[1],”rows”,”row”,”rows”,”N”,”tname”,”No Data Found”) >
<cfset xmlObj1 = Replace(xmlObj1,’table_type=””‘,’table_type=”1″‘,’All’) >
<cfset xmlObj2 = queryToXML(ret_showall[2],”rows”,”row”,”rows”,”N”,”pname”,”No Data Found”) >
<cfset xmlObj2 = Replace(xmlObj2,’table_type=””‘,’table_type=”2″‘,’All’) >
<cfset xmlObj3 = queryToXML(ret_showall[3],”rows”,”row”,”rows”,”N”,”Lurl”,”No Data Found”) >
<cfset xmlObj3 = Replace(xmlObj3,’table_type=””‘,’table_type=”3″‘,’All’) >
<cfset xmlObj4 = queryToXML(ret_showall[4],”rows”,”row”,”rows”,”N”,”pmname1″,”No Data Found”) >
<cfset xmlObj4 = Replace(xmlObj4,’table_type=””‘,’table_type=”4″‘,’All’) >

<cfset xmlobj = xmlObj1 & xmlObj2 & xmlObj3 & xmlObj4 >
<cfset xmlobj = Replace(xmlobj,'<?xml version=”1.0″ encoding=”UTF-8″?>’,”,’All’) >
<cfset xmlobj = Replace(xmlobj,”<rows>”,””,”All”) >
<cfset xmlobj = Replace(xmlobj,”</rows>”,””,”All”) >
<cfset xmlobj = ‘<?xml version=”1.0″ encoding=”UTF-8″?><rows>’ & xmlobj & ‘</rows>’ >

</cfsilent>

<cfsetting enablecfoutputonly=”no” />
<cfcontent reset =”yes” type=”text/xml; charset=UTF-8″>
<cfoutput>#xmlObj#</cfoutput>
<cfsetting showdebugoutput=”No” >
<cfabort>

Flex : How to use the source path

I’ve wanted to use some of my ActionScript packages in a shared folder for multiple projects. The current default of the namespace is relative to the “SRC” folder. This isn’t a solution since I will have a “COM” folder under every “SRC” folder.

e.g. for src folder )
FOLDER ==> src –> com –> x –> y
NAMESPACE ==> xmls:example=”com.x.y.*”

 <example:wrap  x=”10″ y=”10″ id=”idtest” styleName=”style1″ themColor=”#77CCE1″ >
….
</example>

If you want to use a shared folder for some common code (AS packages), then you must first add a namespace and a source path to the package. I’ve tweaked my package location and import statement which Flex Builder nicely helps me greatly …:)
NAMESPACE ==> xmlns:exanple=”x.y.*”
SOURCE PATH ==> j:/shared_folder/flex_as/com (You will notice it is using a mapped drive to a shared folder for my flex actionscript code )

ACTUAL CODE ==>

<example:wrap x=”10″ y=”10″ id=”idtest1″ styleName=”style1″ themeColor=”#77CC1E” >
….
</example>

General Flex frameworks

Here is a bunch of links on the various Flex frameworks.

Mate
Swiz
Pure MVC
Cairngorm
Parsley