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 3 : Taking a snapshot of a component.

Even though, Flex doesn’t do printing that well, it can take a snapshot of a component.

Taking screenshots in Flex 3 using the ImageSnapshot.captureImage() method

<mx:Script>
<![CDATA[

// needed imports
import flash.net.FileReference;
import mx.graphics.codec.*;
import mx.graphics.ImageSnapshot;

// save PNG chart to user PC.
private function saveChart(source:IBitmapDrawable):void {

//var image:ImageSnapshot = ImageSnapshot.captureImage(idchartgrid, 300, new PNGEncoder());
var image:ImageSnapshot = ImageSnapshot.captureImage(source);
var file:FileReference = new FileReference();
file.save(image.data, “chart.png”);
} // end of savechart….

]]>
</mx:Script>

<mx:Panel id=”idchartgrid” width=”940″ height=”550″ title=”A9 Summary Chart”
horizontalScrollPolicy=”off” verticalScrollPolicy=”off” toolTip=”A9 Summary Chart” >

<mx:HBox id=”idIMAGE3″ >
<mx:Image source=”../../images/chart_bar.gif”
toolTip=”Create Chart” buttonMode=”true”
click=”saveChart(idchartgrid); ” />
<mx:Label text=”Create Chart” />

</mx:HBox>

<mx:ColumnChart id=”mychart” dataProvider=”{chart1}”
secondDataProvider=”{chart2}”
axisTitleStyleName=”axisTitleStyle”
showDataTips=”true” dataTipMode=”multiple” mouseSensitivity=”200″
height=”350″ width=”800″ >