Flex + Report Builder tutorial
A while ago, I posted about jumping through some hoops to get a Report Builder report from a filter page in Flex. I never posted code and it seems no one else ever did out there, though this is becoming a common pattern. So here we go.
You've got a report done in Report Builder (a cfr). Congrats for not killing yourself in frustration over the tool, though I'm sure you thought about it. It generates a nice PDF or whatever, now you want to invoke it from your Flex front end. The Flex front end gives a list of departments, leaders, and job types and you can select any number of each to narrow down what's on your report. How do you get your filter responses to the cfr?
One idea would be to loop over your selections, creating a list of IDs to pass through the URL and having the cfr pick up on that. We'll say that reports/rpt.cfm is the ColdFusion page that queries for the data and contains your cfreport tag.
var deptList:String;
var leadList:String;
var typeList:String;
/*code to populate these lists*/
var url:String = "reports/rpt.cfm?dept=" + deptList + "&leadList=" + leadList + "typeList=" + typeList;
var webPageURL:URLRequest = new URLRequest( url );
navigateToURL(webPageURL, "_blank");
}
This would be pretty easy. The code I left would loop over the selectedItems attribute of your lists and build up the string. But if your filters become more complex, passing more and more through the URL becomes cumbersome. But it does let people bookmark a report. But what if I had security in my Flex app such that I could only run reports on department 1, 2, and 5. If the department list is plainly in the url, what's to keep me from adding more departments and getting at data I shouldn't? You'd have to mirror your security on the ColdFusion side. So we'll do a lot more work to allow more complex filter data and add in security at the same time.
Instead of our "run report" button going to the openReport() function above, let's do this instead...
var reportFilter:Object =
{
Departments : lstDept.selectedItems,
EmployeeTypes : lstEmpType.selectedItems,
Leaders : lstLeaders.selectedItems
};
roReport.setReportFilter(reportFilter);
}
That'll create an object of our filters and pass that to our RemoteObject.
id="roReport"
destination="ColdFusion"
source="cfc.report"
showBusyCursor="true">
<mx:method
name="setReportFilter"
result="openReport(event)"/>
</mx:RemoteObject>
Note when it returns, THEN we'll go to our openReport() function. The CFC method will save our filters to the session scope.
<cfargument name="Departments" type="struct">
<cfargument name="EmployeeTypes" type="struct">
<cfargument name="Leaders" type="struct">
<cfset var returnme = true>
<cfset session.reportVariables.Departments = arguments.Departments>
<cfset session.reportVariables.EmployeeTypes = arguments.EmployeeTypes>
<cfset session.reportVariables.Leaders = arguments.Leaders>
<cfreturn returnme>
</cffunction>
Normally you should put a cflock around that session setting, bad me. Note that while only the reportFilter object was passed from Flex, the CFC takes the three arguments that are elements of that object. When you pass only a single structure to a CFC, ColdFusion assumes it's an argument collection and cuts it up. The stuff is saved to session variables, the CFC returns true and Flex goes to the result function.
var url:String = "reports/rpt.cfm";
var webPageURL:URLRequest = new URLRequest( url );
navigateToURL(webPageURL, "_blank");
}
Now we don't have to do anything crazy with the url, it'll go to rpt.cfm every the time. rpt.cfm will pick up variables from the session scope do whatever is needed to run the report.
Doing the session variable method is more secure in that an average Joe with knowledge of what a URL is can't simply change their report data, but it is still possible to intercept and change the data as it's sent clear text from Flex to ColdFusion.
So to recap, you can determine if something is a pro or con
URL method: Easier to make, can bookmark reports, can change report filter manually
Session method: Requires an extra CFC method, supports very complex filter data, requires one more trip to the server, cannot bookmark reports, far more difficult to change your filters outside the filter


There are no comments for this entry.
[Add Comment]