开发者

ArrayList of custom Java objects over BlazeDS into AS3.0

Right away i just try to explain my problem:

Using BlazeDS i got the following Javaclasses:

DatabaseService.java:

public class Database {

 private Category helpCat = null;


 private Category root = new Category(1, "root", null, null);
 private List<Article> database;


 public Database()
 {    


  // ------------ tree -----------------------------------------------------------------------
  List<Category> level_one = new ArrayList<Category>();
  List<Category> level_two_computer = new ArrayList<Category>();
  List<Category> level_tree_hardware = new ArrayList<Category>();

  // Level 1
  Category buecher  = new Category(2, "buecher", root, null);
  Category computer  = new Category(3, "computer", root, level_two_computer);
  level_one.add(buecher);
  level_one.add(computer);

  //Level 2
  Category hardware  = new Category(4, "hardware", computer, level_tree_hardware);
  Category software  = new Category(5, "software", computer, null);
  level_two_computer.add(hardware);
  level_two_computer.add(software);

  //Level 3
  Category graphic  = new Category(6, "graphic", hardware, null);
  Category sound   = new Category(7, "sound", hardware, null);
  level_tree_hardware.add(graphic);
  level_tree_hardware.add(sound);

  // Level 0
  root.addChilds(level_one);
  // ------ tree end ----------------------------------------------------------------------------

  database = new ArrayList<Article>();
  try {
   add(new Book("Per Anhalter durch die Galaxis", "42", Articletype.BOOK, 795, "Per Anhalter durch die Galaxiss", "Douglas Adams", "Heyne Verlag", "Taschenbuch", "3453146972"), buecher);
   add(new Book("Harry Potter und der Stein der Weisen", "descriptionShort", Articletype.BOOK, 1299, "Harry Potter und der Stein der Weisen", "Joanne K. Rowling", "Carlsen Verlag GmbH", "gebunden", "3551551677"), buecher);
   add(new Book("Harry Potter und die Kammer des Schreckens", "descriptionShort", Articletype.BOOK, 1499, "Harry Potter und die Kammer des Schreckens", "Joanne K. Rowling", "Carlsen Verlag GmbH", "gebunden", "3551551677"), buecher);
   add(new Hardware("nVidia GeForce 8400GS", "Graphikkarte", Articletype.HARDWARE, 2665, "512 GDDR5 Speicher, DVI, 1 GPU", "MSI", "neu"), graphic);
   add(new AKW("Biblis C", "Druckwasserreaktor, Preis auf Anfrage, Nur Selbstabholer", Articletype.AKW, -1, "Biblis", 0, 2525, "Siemens", 1, 2012), software);
  } catch (Exception e) {

   e.printStackTrace();
  }
 }

 public List<Category> getCategories(String node) {
  if(node.equalsIgnoreCase("root"))
   return root.getChildren();

  Category baum = null; 
  baum = get_node_by_name(root, node);

  return baum.getChildren();

 }

 private Category get_node_by_name(Category localroot, String lookfor)
 {
  helpCat = null;
  if(localroot.getChildren() != null)
  {
   for (int i = 0; i < localroot.getChildren().size(); ++i)
   {
    if(!(localroot.getChild(i).getName().equals(lookfor)))
    {
     get_node_by_name(localroot.getChild(i), lookfor);
    }
    else
    {
     helpCat = localroot.getChild(i);
     helpCat.setParent(null);
    }
   }
  }
  return helpCat;
 }

 public List<Article> search(int artID, String name, Category categorie){
  List<Article> ergebnis = new ArrayList<Article>();
  if (artID >= 0)
  {
   for(int i = 0; i< database.size(); ++i){
    if(database.get(i).getID() == artID)
    {
     ergebnis.add(database.get(i));
     return ergebnis;
    }
   }   
  }

  if (name != null){
   for(int i = 0; i<database.size(); ++i){
    if (database.get(i).getName().equalsIgnoreCase(name))
     ergebnis.add(database.get(i));
   }
   return ergebnis;
  }
  if (categorie != null){
   {
    ergebnis.addAll(categorie.getArticles());
   }
   return ergebnis;
  }

  return database;
 }


 public Article add(Article newArticle, Category cat) throws Exception
 {
  newArticle.addCategory(cat);
  if(newArticle.getID() != 0)
  {
   throw new Exception("Die Artikel ID wird vom DBS festgelegt!");
  }

  if (database.isEmpty())
  {
   newArticle.setID(0);
  }
  else
  {
   newArticle.setID(database.get(database.size() - 1).getID()+1);
  }

  database.add(newArticle);

  return newArticle;
 }

}

And the Category Class:

  public class Category {
 private int idCat;
 private String nameTEST;
 private Category parent = null;
 private List<Article> articles = new ArrayList<Article>();
 private List<Category> children = new ArrayList<Category>();


 public Category(int _id, String _name, Category _parent, List<Category> _children)
 {
  if(_id > 0)
   idCat = _id;
  if(_name != null)
   nameTEST = _name;
  if(_parent != null)
   parent = _parent;
  if(_children != null)
   children = _children;
 }

 public String toString()
 {
  return nameTEST;
 }

 void addArticle(Article article){
  articles.add(article);
 }

 public List<Article> getAllArticles(){
  List<Article> ergebnis = this.getArticles();

  for (int i = 0; i<children.size();++i){
   ergebnis.addAll(children.get(i).getAllArticles());
  }

  return ergebnis;
 }

 public void setID(int iD) {
  idCat = iD;
 }

 public int getID() {
  return idCat;
 }

 public void setName(String name) {
  this.nameTEST = name;
 }

 public String getName() {
  return nameTEST;
 }

 /**
  * @param parent the parent to set
  */
 public void setParent(Category parent)
 {
  this.parent = parent;
 }

 /**
  * @return the articles
  */
 public List<Article> getArticles()
 {
  return articles;
 }

 public void addChilds(List<Category> _next)
 {
  for (int i = 0; i < _next.size(); ++i)
  {
   children.add(_next.get(i));
  }
 }

 public void addChild(Category one_next)
 {
  children.add(one_next);
 }

 public Category getChild(int index)
 {
  return children.get(index);
 }

 public void removeChild(Article article){
  articles.remove(article);
 }

 public List<Category> getChildren()
 {
  return this.children;
 }
}

also there are of course classes for articles and so on, but thats not important at that point.

the counterpart in flex looks like this:

Category.as

[RemoteClass(alias="PACKAGE.Category")]
 public class Category
 {
  private var idCat:int     = -1;
  private var nameTEST:String    = null;
  private var parent:Category    = null;
  private var articles:ArrayCollection = new ArrayCollection;
  private var children:ArrayCollection = new ArrayCollection;

  public function Category(id:int, name:String, parent:Category, childlist:ArrayCollection, articles:ArrayCollection = null)
  {
   this.idCat   = id;
   this.nameTEST  = name;
   this.parent  = parent;
   this.articles = articles;
   this.children = childlist;
  }

  public function setChildren(childList:ArrayCollection):void
  {
   this.children = childList;
  }

  public function getChildren():ArrayCollection
  {
   return this.children;
  }
开发者_StackOverflow中文版
  public function getName():String
  {
   return this.nameTEST; 
  }
 }

Then i got a Flex service class calling BlazeDS and executing the getCategories java method. Since Flash dosn't seem to understand typed arrays, the result from that method which i get back in flex is a simple array of untyped objects (the mapping dosn't seem to work here, even tought the class category exists in flex and has the same properties). thats the first thing. but however, i'm converting the untyped objects manually into objects of the category.as class.

the second thing is that categories have child-categories within the java object, which are also ArrayLists of the type category.java. the problem about that: my result event object only contains the first level of categories, looking into them the children are allways null. i dunno why they are empty, since they ARE part of the java object category.

and the third thing (the strangest by fast), you maybe noticed i named the properties of the category.java class strange, like idCat and nameTest instead of simply id and name. why that? because the property names of my flex result objects dont seem to change when i change the java objects properties names (result object properties are named "id" and "name" but the java class object properties are named "idCAT" and "nameTEST"). that it REALLY strange, since if i set the properties, like you see at nameTEST = "TESTNAME" it IS recogniced by flex, only the proertyNAMES dont seem to be recognized at all.

is blaze DS saving / caching the mapping configuration somewhere? how do i get it to rebuild the hole mappings IF so?

that could also explain my problem about the untyped objects i get from java, since before i changed the lists into ArrayLists they where vectors ( which blazeDS dosn't support AFAIK), and maybe not only the propertynames, but also the propertytypes are hard-mapped at some wired place and blazeds just dosn't get them refreshed.

i really like checked everything 5 times by now, even redeployed blazeds on the server to make sure no mappings left, but it didnt help at all.

ANY ideas what i could do? (exept changing to another serializer then blazeds (thats what i'm going to do if everything else fails...))


i have the same issues, but if you can warm up the tree before call the method, it will be ok. what i mean "warm up" is you iterator the arraylist without doing anything. it is flunky why this work!


I have had similar problems with a list returned from a service not including the child elements in the list. I have found that BlazeDS can return a typed list. The two things that fixed this for me were:

a) Ensure that the returned list is of type java.util.List (not java.util.ArrayList for example).

b) Ensure that the class for the elements in the list have both public setters and getters for all entities to be returned.

For example - the following code works for me:

public class OrganisationService {
    ...
    public List<Organisation> getOrganisations() {
        List<Organisation> list = new ArrayList<Organisation>();
        ...
        return list;
    }
    ...
}


As mentioned elsewhere, you need to initiailize your AS3 remote objects so that it is included in the SWF during compilation.

Somewhere in your code, add:

var cat:Category = new Category(); var art:Article = new Article();

That should fix your generic object issue. (I add mine all in one spot, in an application start up method).


It turns out that a simple missmatch of the classnames was the cause of all evil. still some problems to solve, but atleast i get sub-arrays returned now, only the objects are still simple AS 3 objects and not the specified "Category" and "Article" objects, but i think thats because i dont have all methods included and mapping is failing because of that.

but thanks for your offer, i appreciate that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜