How to handle calls to multiple functions in the same web service? - apache-flex

This is a super newbie question on Flex. While I am a seasoned programmer, this is my first ever Flex application; please bear with me for my Flex codes.
I have a web service written in ColdFusion. In this web service, there are two functions; one is to return all the questions in a quiz and the other one is to return all the answer selections to the questions in a quiz.
[Bindable]
private var questionArray:ArrayCollection;
private var cfquiz:RemoteObject;
private function loadQuestions():void {
currentQuestionCounter = 0;
btnPrev.enabled = false;
btnNext.enabled = false;
cfquiz = new RemoteObject("ColdFusion");
cfquiz.source = "CFCertExam.cfquiz";
cfquiz.addEventListener(ResultEvent.RESULT, resultHandler);
}
private function resultHandler(event:ResultEvent):void {
questionArray = event.result as ArrayCollection;
txt1Questions.htmlText = questionArray.getItemAt(currentQuestionCounter).Question_Text;
btnNext.enabled = true;
}
I have the codes above. loadQuestions is called at creationComplete to retrieve the questions. Things are working fine. What I want to do is to call another function within the same web service, returnAnswers, to return the answer options for a question. Since I have cfquiz associated to the web service already, I was using cfquiz to call returnAnswers. However, there is an event listener associated to cfquiz already, resultHandler is being called when returnAnswers comes back with the results.
My questions are, first, is it possible to check which function returns the results within resultHandler? If so, how? And second, what is the best way to handle calls to multiple functions within the same web service?
Thanks in advance,
Monte

Yes you can. You need to specify a handler function for each method which in turn calls a different webservice.

The better way to do this is using AsyncToken and AsyncResponder instead of addEventListener, as following code.
tokenA = cfquiz.methodA();
tokenA.addResponder(new AsyncResponder(onResultForMethodA, onFaultMethodA));
tokenB = cfquiz.methodA();
tokenB.addResponder(new AsyncResponder(onResultForMethodB, onFaultMethodB));
tokenC = cfquiz.methodA();
tokenC.addResponder(new AsyncResponder(onResultForMethodC, onFaultMethodC));
or
tokenA = cfquiz.methodA();
var responderA:IResponder = new AsyncResponder(onResult, onFault, "methodA");
tokenB = cfquiz.methodB();
var responderB:IResponder = new AsyncResponder(onResult, onFault, "methodB");
tokenA.addResponder(responderA);
tokenB.addResponder(responderB);
private function onResult(evt:ResultEvent, token:Object):void {
if(token == "methodA" ) {
//logic for methodA
}
if(token == "methodB" ) {
//logic for methodB
}
}

I'm a little confused as I can't see where you are calling the actual web service function, for example from these examples, I am expecting to see:
cfquiz = new RemoteObject("ColdFusion");
cfquiz.source = "CFCertExam.cfquiz";
cfquiz.addEventListener(ResultEvent.RESULT, resultHandler);
cfquiz.myCFCFunctionCall(); /* where is this? */
Anyway, AFAIK you can create a new instance of the remote object and set that up to have it's own event listener.
Hope that helps.

Related

How can I write a unit test against a WebPartBase class and mock out the controls?

I have a bunch of WebPartBase classes that I'm trying to invoke a method on. Some of the methods call controls on the UI. I want to stub out those controls somehow so that an exception doesn't get thrown when attempting to set values on them. Does anyone know of a good way to do this? It would also be fine if they were initialized but I'm not sure how to initialize them without running a web server.
My test code looks like this. IntializeOnAdd() is a method on KPIWebPartBase and KPIWebPartBase inherits from WebPartBase.
[TestMethod]
public void InvokeAllWidgets()
{
var user = new User("adminqaphi");
user.CustomerID = TestConfig.ClientID;
var classes = typeof(KPIWebPartBase)
.Assembly
.GetTypes()
.Where(t => t.IsSubclassOf(typeof(KPIWebPartBase)) && !t.IsAbstract );
foreach (var c in classes)
{
Console.WriteLine(c.Name);
var instance = (KPIWebPartBase)Activator.CreateInstance(c);
foreach (var billingMetric in Enum.GetValues(typeof(BillingMetric)))
{
instance.CurrentUser = user;
instance.BillingMetric = (BillingMetric)billingMetric;
if (instance is StartEndKPIWebPartBase)
{
var startEndKPI = (StartEndKPIWebPartBase)instance;
startEndKPI.StartDate = new DateTime(2007, 1, 1);
startEndKPI.EndDate = new DateTime(2008, 1, 1);
}
instance.InitializeOnAdd();
}
}
}
I would create interfaces for them (if possible) and use moq to create Mocks or stubs for the external dependencies.
What behavior does InvokeAllWidgets() test?
Response to comment:
I think you should mock the database as well, so that you just test the "internal" logic of the code(unit). Otherwise you will en up testing the database, and I guess that is not what you want to. And if you do call the database, what happens if some data in it changes? Will that fail your tests? If so I think you are doing integration tests and not unit tests. In a unit test try to mock all external dependencies and test your own logic (what can go wrong?). If your code don't allow you test it like, then change it!

Flex Async Madness - Any way to wait for a rpc.Responder to get a response before going to the next statement?

I feel like I must be doing this wrong. I've got a function that is supposed to construct a query based on an API's description of available fields. Here's what I've got:
var query_fields = getQueryFieldsFor(sobject_name);
// Need query fields for the next statement, which actually does the query
public function getQueryFieldsFor(sObject:String):String{
//helper function to get queryfields for given sobject
var queryFields:String = '';
app.connection.describeSObject(sObject,
new mx.rpc.Responder(
function(result:DescribeSObjectResult):void{
var returnFields:String = '';
for ( var field:Field in result.fields ){
if(field.active){
returnFields.concat(field.name+',')
}
}
returnFields.slice(0, returnFields.length-1); //remove last comma
queryFields = returnFields;
}, function(error):void{
Alert.show('error in getQueryFieldsFor function');
})
);
return queryFields;
}
I know this doesn't work, and I think I understand why. However, I keep running into this type of issue and I believe I'm just thinking about it/designing it wrong. So what's a better pattern here? Would really appreciate any insight on this. Many thanks in advance.
It would be better to externalize your functions and execute your next line of code after the fact:
public function getQueryFieldsFor(sObject:String):String
{
var responder:Responder = new Responder( onResult, onFault);
app.connection.describeSObject(sObject, responder);
}
private function onResult(result:DescribeSObjectResult):void
{
var returnFields:String = '';
for ( var field:Field in result.fields ){
if(field.active){
returnFields.concat(field.name+',')
}
}
returnFields.slice(0, returnFields.length-1); //remove last comma
queryFields = returnFields;
}
Your main problem though is not the code, but a lack of thinking asynchronously. You cannot have a function called "getQueryFields" that will return it instantly. What you want to do is think in the request/response way. You're trying to get some data, a request is made to a service, gets the data back, updates a property which is then binded to a view which gets redrawn. This is the proper way to do any webapp.
It might be beneficial for you to also look at application frameworks like RobotLegs and Parsley since it helps you manage these situations. Parsley also has a task library which lets you perform several asynchronous task one after another.

netStream with groupSpecifier not sending handler, FLEX

I have a simple video/chat application built in FLEX. I've created a groupSpecifier, netGroup and a NetStream for the different functionalities.
NetGroup is mainly used for the messaging (Posting) and keeping track of the users who enter.
NetStream is (or would be used) to control some functions like "start video, stop video" for everyone under the same group.
The most important functions I will post on here. The first is setupGroup.
private function setupGroup():void{
var groupspec:GroupSpecifier = new GroupSpecifier("vid"+GROUP_ID+"_sid_"+SESSION_ID);
groupspec.serverChannelEnabled = true;
groupspec.postingEnabled = true;
groupspec.multicastEnabled = true;
groupspec.ipMulticastMemberUpdatesEnabled = true;
trace("Groupspec: "+groupspec.groupspecWithoutAuthorizations());
netGroup = new NetGroup(nc,groupspec.groupspecWithoutAuthorizations());
netGroup.addEventListener(NetStatusEvent.NET_STATUS,netStatus);
netVideo = new NetStream(nc,groupspec.groupspecWithoutAuthorizations());
netVideo.addEventListener(NetStatusEvent.NET_STATUS,netStatus);
user = "user"+Math.round(Math.random()*10000);
}
The second is sendMessage
private function sendMessage():void{
var message:Object = new Object();
message.sender = netGroup.convertPeerIDToGroupAddress(nc.nearID);
message.user = txtUser.text;
message.text = txtMessage.text;
netGroup.post(message);
receiveMessage(message);
txtMessage.text = "";
}
And then startVideo
private function startVideo():void{
netVideo.send("publishVideo");
ns.togglePause();
}
There is another function called "publishVideo" which I hope would be called for the rest of the members in the group, but this is not happening. Most of this code is straight from the example Tom has provided from http://www.flashrealtime.com. Any help / suggestions would be appreciated!
p.s. I am not using group object replication because of the latency.
Answer is prosaically simple.
You need to start video with:
netVideo.publish("channel");
NetStream.send() is for invoking callback method on receiver NetStream.client object.
http://www.flashrealtime.com/multicast-explained-flash-101-p2p/

Multiple instances of views in PureMVC: Am I doing this right?

What I'm doing NOW:
Often multiple instances of the view component would be used in multiple places in an application. Each time I do this, I register the same mediator with a different name.
When a notification is dispatched, I attach the name of the mediator to the body of the notification, like so:
var obj:Object = new Object();
obj.mediatorName = this.getMediatorName();
obj.someParameter = someParameter;
sendNotification ("someNotification", obj);
Then in the Command class, I parse the notification body and store the mediatorName in the proxy.
var mediatorName:String = notification.getBody().mediatorName;
var params:String = notification.getBody().someParameter;
getProxy().someMethod(params, mediatorName);
On the return notification, the mediatorName is returned with it.
var obj:Object = new Object();
obj.mediatorName = mediatorName;
obj.someReturnedValue= someReturnedValue;
sendNotification ("someReturnedNotification", obj);
In the multiple mediators that might be watching for "someReturnedNotification," in the handleNotification(), it does an if statement, to see
if obj.mediatorName == this.getMediatorName
returns true. If so, process the info, if not, don't.
My Question is:
Is this the right way of using Multiton PureMVC? My gut feeling is not. I am sure there's a better way of architecting the application so that I don't have to test for the mediator's name to see if the component should be updated with the returned info.
Would someone please help and give me some direction as to what is a better way?
Thanks.
I checked with Cliff (the puremvc.org guy) and he said it's fine.

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor) and another like this=>InsertJCDC(fakeBox).
When I stub out the parent I want to return fakePor. But when I run the code it returns null instead. Here is the unit test.
[Test]
public void PorBLL_InsertByPorInsertCV_DoingGoodCase()
{
// Startup object mapper
_Bootstrapper.Bootstrap();
// create the mock for generic Crud
IGenericCrud mockGenericCrud = MockRepository.GenerateMock<IGenericCrud>();
PorInsertCV fakePor = new PorInsertCV();
PorBoxInsertCV fakeBox = new PorBoxInsertCV();
// build fake return
PorEO fakePorNewRow = new PorEO();
fakePorNewRow.PorId = 22;
// stub parent and child insert routines.
mockGenericCrud.Stub(c => c.InsertJCDC<PorEO, PorInsertCV>(fakePor)).Return(fakePorNewRow);
mockGenericCrud.Stub(c => c.InsertJCDC<PorBoxEO, PorBoxInsertCV>(fakeBox)).Return(null);
ObjectFactory.Inject(typeof(IGenericCrud), mockGenericCrud);
IPorBLL localWithMock = ObjectFactory.GetInstance<IPorBLL>();
// build user args to csll bll with and use for insert
PorInsertCV userArgs = new PorInsertCV();
userArgs.AccessionNbr = "364-80-0007";
userArgs.NbrBoxes = 11;
userArgs.RegId = 20;
userArgs.TransmitedDt = Convert.ToDateTime("1/30/1980");
// call the bll using the stub
localWithMock.InsertByPorInsertCV(userArgs);
}
Any help is greatly appreciated
I can't really follow your code that well, but I'll give it a shot.
From my skills of deduction, this line here is the one giving you issues:
mockGenericCrud.Stub(c => c.InsertJCDC<PorEO, PorInsertCV>(fakePor)).Return(fakePorNewRow);
Because you're expecting fakePorNewRow to be returned when you call localWithMock.InsertByPorInsertCV(userArgs); - yeah?
If that's your case, what your problem is, is that it will only return fakePorNewRow when it is given fakePor ... not userArgs as you have given it.
Tell me if I'm completely off track.
HTHs,
Charles
Ps. You might want to add the tag of which mocking framework you are using to the question.

Resources