Highlighted

List<MyType> to ArrayCollection of MyType instances?

Avatar

Avatar

tptackab

Avatar

tptackab

tptackab

29-05-2008

Hi,



Is it possible to have a generically typed List
(e.g.List<MyType>)

instance variable on my object serialized to an
ArrayCollection of

MyType objects in the corresponding AS object? I've struggled
for days

on this and would greatly appreciate if anyone could tell me
if it is

possible.



For example, I have a Java DTO object (AccountProfileDTO)
that

assembles various objects related to a user for transfer to
the

client (see code below). In Flex, I call a remote Java method
that

returns an AccountProfileDTO object.



The Java method correctly populates the user (User) and

userMedications (List<UserMedication>) instance
variables on the DTO.

However, when I examine the resulting AccountProfileDTO in
Flex, I see

the user field is correctly populated but the userMedications

ArrayCollection is populated with AS Object instances instead
of

UserMedication instances. The Object instances contain the
correct

properties and values, but I would like to have the Objects
strongly

types as UserMedication instances just like the Java object.



How do I do this, or is it not possible?



Please see my source code below. I'm using Tomcat + Spring +
Flex3. I have no idea where to find the "Attach Code" options, so I
apologize if the code is hard to read.




Thanks,

Tom



----- AccountProfileDTO (Java) --------------

package com.ghs.dto;



import java.util.List;

import com.ghs.model.User;

import com.ghs.model.UserMedication;



public class AccountProfileDTO {



private User user;

private List<UserMedication> userMedications;



public User getUser() {

return user;

}

public void setUser(User user) {

this.user = user;

}

public List<UserMedication> getUserMedications() {

return userMedications;

}

public void setUserMedications(List<UserMedication>
userMedications) {

this.userMedications = userMedications;

}

}





----- AccountProfileDTO (ActionScript) --------------

package com.ghs.dto

{

import com.ghs.model.*;

import com.ghs.dto.*;

import mx.collections.*;



[Bindable]

[RemoteClass(alias="com.ghs.dto.AccountProfileDTO")]

public class AccountProfileDTO {



public var user:User;

public var userType:UserType;

public var userAuthorities:ArrayCollection;

public var userMedications:ArrayCollection;

public var lastModUser:User;

public var lastModUserType:UserType;

public var lastModProvider:Provider;

}

}





---------------- UserMedication (Java) -------------------

package com.ghs.model;



public class UserMedicationKey {





private String medication;



private Long userId;



public String getMedication() {

return medication;

}

public void setMedication(String medication) {

this.medication = medication;

}

public Long getUserId() {

return userId;

}

public void setUserId(Long userId) {

this.userId = userId;

}

}



---------------- UserMedication (ActionScript)
-------------------

package com.ghs.model

{

import com.ghs.model.*;

import com.ghs.dto.*;

import mx.collections.*;



[Bindable]

[RemoteClass(alias="com.ghs.model.UserMedication")]

public class UserMedication extends UserMedicationKey {



public var lastModBy:Number;

public var lastModDate:Date;

public var frequency:String;

public var dateStarted:Date;

public var strength:String;

}

}



--------------UserMedicationKey (Java) ---------------------



package com.ghs.model;



public class UserMedication extends UserMedicationKey {





private String strength;



private String frequency;



public String getStrength() {

return strength;

}



public void setStrength(String strength) {

this.strength = strength;

}



public String getFrequency() {

return frequency;

}



public void setFrequency(String frequency) {

this.frequency = frequency;

}



}



--------------UserMedicationKey (ActionScript)
---------------------

package com.ghs.model

{

import com.ghs.model.*;

import com.ghs.dto.*;

import mx.collections.*;



[Bindable]

[RemoteClass(alias="com.ghs.model.UserMedicationKey")]

public class UserMedicationKey {



public var userId:Number;

public var medication:String;

}

}

Replies

Highlighted

Avatar

Avatar

tptackab

Avatar

tptackab

tptackab

29-05-2008

Ok, it's starting to get clearer. Apparently one must
explicitly include a reference to the typed object class held in
the ArrayCollection for Flex to properly cast the objects.



For example, here's my handler:



public function loadProfileHandler(event:ResultEvent):void

{

var accountProfile:AccountProfileDTO = event.result as
AccountProfileDTO;



for each(var userMed:UserMedication in
accountProfile.userMedications)

{

trace(userMed.medication);

}



trace(accountProfile.userMedications);

}



If I comment out the "for each" loop, I get back AS Object
instances in the ArrayCollection. With the loop in place I get back
UserMedication instances in the ArrayCollection (in the result even
before the loop executes). Flex does not perform the cast without a
reference to the typed object's class. That's strange!



I seem to recall reading about this in the documentation, but
it didn't make sense...until now. Is this a bug (I'm using BlazeDS
so I'll probably wrongly assume FDS has the same problem)?



I've designed several DTOs with Lists several levels deep in
the object graph. Am I going to have to drill down and loop through
all the lists in my DTO's object graph just to make sure
ArrayCollection elements are properly cast to their appropriate
type. Yikes!



Hopefully someone will chime in and tell me there's a better
way. 🙂