Avatar of meena_r
meena_r asked on

Mappling Java Objects to ActionScript Object with RemoteClass and BlazeDS

Hello,
I want to pass a java object which has an arraylist, this arraylist has another object added into it, through RemoteObject and blazeDS I am stuck at one place though. My java class has one ArrayList of Objects (which is another java class) (sorry in advance for long post however I wanted to make it clear) for example
Java class:
public class UserAccountDTO{

Private String loginID;
Private String name;
private  ArrayList<AssignedFunctionDTO> assignedFunction;
&&&
&&..

//getter and setter method
//no args constructor
// constructor to set all these values from outside
Public UserAccountDTO(String loginID,String name, ArrayList assignedFunction){
            this.setLoginID(loginID);
            this.setName(name);
            this.setAssignedFunction(assignedFunction);
}
}

public class AssignedFunctionDTO(){
      private int function_id;
      private String function_name;
     
//getter and setter methods
//no args constructor
//constructor to set all these value from out side      
}

Then the method which Remote Object service calls
Public loginUtil(){

//fetches user records from database
//creates arraylist of AssignedFunctionDTO by
assignedFunction.add(new AssignedFunctionDTO(functioned,functionName);
//returns UserAccountDTO object by
      Return new UserAccountDTO(loginID,name,assignedFunciton);
}

I am using blazeds for remoting and cairngorm for structure and created two DTO classes in flex like this:
Package;
{
[RemoteClass(alias=myPackage.UserAccountDTO)]      
Public class UserAccountDTO{
      Public var loginID:String;
      Public var name:String;
Public var assignedFunctaion:Object //I tried using AssignedFunctionDTO/ArrayCollection too
}
}

Package;
{
[RemoteClass(alias=myPackage.AssignedFunctionDTO)]      
Public class AssignedFunctionDTO {
      Public var function_id:int;
      Public var function_name:String;
}
}

In my command class I am not getting value of AssignedFunctionDTO its null
I am doing : modelLocator.userAccountDTO = event.result;

This gives me everything other then AssignedFunctionDTO dont know why, if I pass a simple arraylist I get its value, however I do not get value of ArrayList which contains another object inside it&I think I am missing something here however I have no idea about it&please bare with me... please help
Apache FlexJavaJava EE

Avatar of undefined
Last Comment
Siva Prasanna Kumar

8/22/2022 - Mon
Siva Prasanna Kumar

>>if I pass a simple arraylist I get its value, however I do not get value of ArrayList which contains another object inside

arrayList can contain only object right :) so it must not make any difference, you got the result for passing the normal arraylist? just by using event.result?
Siva Prasanna Kumar

I think this is the second time you are posting this question right? First time I didn't know much about BlazeDS to help, but later when I used it, realized that its just a sub set of flex data service. so let see if I can help :)
Siva Prasanna Kumar

I think you may have to use this class for successful usage of arraylist

http://livedocs.adobe.com/blazeds/1/javadoc/flex/messaging/io/ArrayList.html
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER
meena_r

Hello Shiva,
Thanks for response, I could not get your point very clearly, would you like to use flex.messaging.io.arrayList instead of java.util.arraylist at the time of object creation at server side or something else?, I guess the problem here is I can use generics in java to tell the compiler that this ArrayList has AssignedFunctionDTO type of objects, however I do not have any way in flex to define generics, at flex side I can convert this arraylist into arraycollection which is not recognizing the type of objects in that collection so I get null values, I am not sure about this reason, this is just what I could think of. Please help.

Thanks.
Siva Prasanna Kumar

Meena what ever you tell java with generics is onbly valid during compilation, at the run time they are nothing more than a set of objects the problem is, the way flex interprets your arraylist, it will not be directly a arraylist any more it will be a sub class of arraycollection at the flex end.

any way you still didn't clear one particular thing what you meant by simple arraylist which was successfully passed b/w java and flex?

also your remote class definition, make it ArrayCollection don't try to get objects of it or any thing just try to print the size of the ArrayCollection

>>This gives me everything other then AssignedFunctionDTO

Make sure you are getting the array collection first later we can see how to retrieve.





Package;
{
[RemoteClass(alias=myPackage.UserAccountDTO)]      
Public class UserAccountDTO{
      Public var loginID:String;
      Public var name:String;
Public var assignedFunctaion:ArrayCollection 
}
}

Open in new window

ASKER
meena_r

Hello Shiva,
I am not getting the arraycollection when I print size I get error message saying

TypeError: Error #1009: Cannot access a property or method of a null object reference.

No idea why I am not getting this, at server side I am printing results (size and objects in the arraylist) and it is giving me size of arraylist and client side it does not. Thanks.
      
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Siva Prasanna Kumar

You are able to get loginId and name?? and only  assignedFunctaion is null??

Meena try to print it like this and let me know what it is showing on the alert window

Alert.show(assignedFunctaion +"");
ASKER
meena_r

It is showing null.
As I said earlier I am getting loginID, name in fact simple array list which contains String type of objects or I don't know how I should say that may be the objects which are defined at flex side. forget about returning your own type of objects, if I create an arraylist which has hashmap into it, Flex does not recognize the hashmap and gives me null, however if I send something like this
myArrayList.add("abc"); I get "abc" in the result. So I guess actual problem is getting the result at flex side. Thanks for constant help. I really appreciate it.
Siva Prasanna Kumar

Meena, can you please add a default no arg constructor to  AssignedFunctionDTO class and try, I think its must for AS3 to serialize it, if this fails do try add implements Serializable at the serverside also.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Siva Prasanna Kumar

Oh great you seem to have added this question at Sujit's Blog, just saw that when googled :)
ASKER
meena_r

At server side default no arg constructor is present and serializable is implemented too, I tried adding default no args constructor on flex side but no luck.
ASKER
meena_r

Yes and Flex support forum too...still awaiting response from both :(
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Siva Prasanna Kumar

Meena one last try, before me try it out ;)

Please change the ArrayList in the UserAccountDTO to flex.messaging.io.ArrayList

I am sure that must work because it a subclass of java ArrayList only try it and lets see what happens, can you let me know the result, I will try to simulate the same after going home today but will be tough as I need to configure every thing to simulate this example.
ASKER
meena_r

I have created one simple example for simulation and sent it to hobbit70 (I guess) however could not get any response from him, though expert exchange discourages seding personal emails however that is the only option I have as when I try attaching zip it does not allow me to do so. If you want you could give me your email address and I can send it across meanwhile I am following your suggestion and would let you know the results.
Thanks
ASKER
meena_r

Do you want me to change it at server side or client side or at both the places?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Siva Prasanna Kumar

not at the client side just do it at the server side i.e in java

public class UserAccountDTO{

Private String loginID;
Private String name;
private  ArrayList<AssignedFunctionDTO> assignedFunction;
//your getter and setter

change

import java.util.ArrayList;

to

import flex.messaging.io.ArrayList;

and I hope you already have the appropriate jar files in the class path, else add the jars in that lib folder of the blazeds to your class path.
ASKER
meena_r

no luck :( it is showing null.
ASKER
meena_r

I am attaching zip to simulate the case however I changed all file's extension to txt (otherwise it was not allowing me to upload) so you could use them to simulate it at your end, I hope this would take less time for simulation. Thanks
src---Copy.zip
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Siva Prasanna Kumar

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
meena_r

I am sure you will be getting the results, I don't know what I am missing because it is suppose to give me the result. Actually I am not sending the ArrayList in return it is UserAccountDTO type of object which contains problemetic ArrayList, so I can not retrieve the result in Arraycollection. I tried retriving the result in UserAccountDTO type of object but no luck :(, Will post you detail results tomorrow.
Siva Prasanna Kumar

did u check your mappings??
Siva Prasanna Kumar

ok lets start debugging :) add a system out in the getAssignedFunction and see if it is printing.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER
meena_r

Mappings are correct and I am getting response in system out that ArrayList at server side has some data in it. so I guess first step is done what could be the next one :)
Siva Prasanna Kumar

write a simple mxml like the one what I have written, just a button which invokes the remoteobject and in the result handler, just put Alert.show(event.result+""); you must see a Object.

then if you are able to see an object which will be UserAccountDTO, try to print various attributes of it

i.e var obj:Object = event.result;
    Alert.show(obj.loginID+"");
    Alert.show(obj.name+"");
    Alert.show(obj.assignedFunction+"");

you must find another object in the last (top most alert)
ASKER
meena_r

I get loginID, name however assignedFunction is null :(
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Siva Prasanna Kumar

Do you have AssignedFunctionDTO in the mapping or not??
ASKER
meena_r

RemoteClass alias mapping... I have, is there any other mapping too?
ASKER
meena_r

In the sample code you provided Address is mapped with the employee like this :
public var address:Address;
so I changed UserAccountDTO and put
public var assignedfunction:AssignedFunctionDTO; (which is actually an arraylist) so how would flex know that this is an arraylist which contains AssignedFunctionDTO's object?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Siva Prasanna Kumar

>>public var address:Address; this adress is the Flex Class Address not the array List.

But even before that the mapping I am taking about is the remote-config.xml files mapping, have you explicitly mapped the AssignedFunctionDTO Object or not??

The above address thing is not same as assignedfunction because your assignedfunction is a list of AssignedFunctionDTO where as my address was a single Address Object.

Forget about this mapping for the time being just check the remote config xml and also show me your mxml code.

ASKER
meena_r

I see..I did not provide mapping into Remote-config.xml however I am giving full path of java file into RemoteClass alias... I think it should work same way, if not then what is the difference between those two? I mean  on top of class writng this
[RemoteClass(alias="rjs.Address"]
or in remote-config.xml I could define destination
<destination id="Address">
    <properties>
        <source>rjs.Address</source>
    </properties>
</destination>
and in RemoteClass tag I could define alias as Address
[RemoteClass(alias="Address")]
I think they both should work in same way except clean coding, isn't it? I am trying the other way too and will let you know the results.
Siva Prasanna Kumar

Oh great :) how come it was working with out remote mapping all this time?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
meena_r

It is working...I have given main destination which is EmployeeTest in your example and rest of the places I put RemoteClass alias and full path of my java class, I am not getting what is wrong in this approach, could you please explain?

And I am getting all the properties of UserAccountDTO like userID, userName, and an ArrayList which I am creating like this, here subnet_id is int.
 assignedSubnet.add(subnet_id);

Thanks.
Siva Prasanna Kumar

I have no idea how was your previous stuff working but the way I did it, is when you say remote alias it searched for a mapping in that remote config xml  and uses the appropriate Java Object and invokes the remote method, may be you previous one was working only because you have clearly specified the complete name.
ASKER
meena_r

no luck :(  result are same , below is my remote class mapping
 <destination id="Server">
        <properties>
            <source>com.test.mypackage.Server</source>
        </properties>
    </destination>
    <destination id="userAccount">
        <properties>
            <source>com.test.mypackage.UserAccountDTO</source>
        </properties>
    </destination>
    <destination id="userFunction">
        <properties>
            <source>com.test.mypackage.AssignedFunctionDTO</source>
        </properties>
    </destination>

And below is the mxml code, how come you are getting the results and I am not, let me try running your example.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
    <![CDATA[
	import com.test.vo.LoginVO;
	import mx.collections.ArrayCollection;
	import mx.rpc.events.ResultEvent;
	import mx.controls.Alert;
	import mx.rpc.remoting.mxml.RemoteObject;
			
	public function init():void
	{
	  var loginVO :LoginVO = new LoginVO();
	  loginVO.userID = 'admin';
	  loginVO.password = 'admin';
	  var ro:RemoteObject = new RemoteObject();
	  ro.destination = "Server";
	  ro.doLogin.addEventListener("result",  getHeaderResultHandler);
           ro.doLogin(loginVO);
   	}
   			
   	public function getHeaderResultHandler(event:ResultEvent):void
   	{
   	     //Alert.show(event.result+"");
   	     var ac:Object = event.result;
   	     Alert.show(ac.length +""); //undefined
   	     Alert.show(ac+""); //[Object,Object]
   	     Alert.show(ac.userID +"");//admin
              //arrayList which contains AssignedFunctionDTO object
   	     Alert.show(ac.assignedFunction +""); //null 
              //ArrayList which contains int values
   	     Alert.show(ac.assignedSubnet+""); //1,1,2,2    		}
	]]>
</mx:Script>
<mx:Button label="Click me" click="init()"/>
</mx:Application>

Open in new window

Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER
meena_r

I tried running your example as it is, its working fine, then I checked if it gives me one Record for employee correctly, which is returning correct records too, next step is I want to put ArrayList of Address (One Employee multiple address) with one record, lets see...and to surprise me that is also working...I don't know what I am doing wrong yet, I would try and let you know if the same approach works in UserAccountDTO and AssignedFunction case...Thank you so much  for all the help, will let you know the results soon.
Siva Prasanna Kumar

I can just say "All the very best" :)