[flexcoders] Re: Trouble Converting java List to ArrayCollection
I believe anything implementing java.util.Collection, such as List, is already being converted to an ArrayCollection on the Flex side. http://livedocs.macromedia.com/flex/2/docs/1104.html#270405 http://livedocs.macromedia.com/flex/2/docs/1104.html#270405 In your result handler, try: var col:ArrayCollection = re.result as ArrayCollection; --- In flexcoders@yahoogroups.com, ytseshred [EMAIL PROTECTED] wrote: I'm using RemoteObjects to connect to my backend. In one of my calls I'm trying to return a List of VO's I create after hitting my database. On the Flex side, my result event is firing properly, but when I try to convert the result to an ArrayCollection, instead of getting a proper ArrayCollection I get single element that displays the following when I output it in an alert: obj is: '[object TestVO], [object TestVO], ... [object TestVO]'. If I return a single TestVO object directly instead of a List, I can properly cast the result to my ActionScript TestVO object and view the data properly. It's just returning the List that's giving me problems. Can anyone offer any suggestions please? My Flex result code is: public function result(evt:Object):void { if(evt.type evt.type == ResultEvent.RESULT) { var re:ResultEvent = evt as ResultEvent; var col:ArrayCollection = new ArrayCollection(ArrayUtil.toArray(re.result)); if(col) { Alert.show(obj is: ' + col.getItemAt(0) + '); } else Alert.show(Didn't cast properly!); } } My Java DAO code that returns the list is: public List getList() throws DAOException { List list = new ArrayList(); Connection c = null; try { c = ConnectionHelper.getConnection(); Statement s = c.createStatement(); ResultSet rs = s.executeQuery(SELECT * FROM test); while(rs.next()) { TestVO vo = new TestVO(); vo.id = rs.getInt(id); vo.data = rs.getString(data); list.add(vo); } } catch(Exception e) { e.printStackTrace(); throw new DAOException(e); } finally { ConnectionHelper.close(c); } return list; }
[flexcoders] Re: Trouble Converting java List to ArrayCollection
Thanks, the casting directly to ArrayCollection using as did work. I was doing this initially, and then made some other changes to get rid of another error and forgot to change back. Andre, this isn't production code and I got it off of Cristophe's TestDriver server (http://coenraets.org/blog/2007/01/flex-data-management-services-tutorial/) but thanks for pointing this out. --- In flexcoders@yahoogroups.com, André Rodrigues Pena [EMAIL PROTECTED] wrote: Man.. Just remove the throws of your getList() method. Never use throws at a service method. (specially in this example where the throws is USELESS because you are already treating the exception with the try clause) The throws clause passes the exception treatment to a higher scope, this scope may not import de DAOException needed to process it properly. Simply never do this. On 2/12/07, ytseshred [EMAIL PROTECTED] wrote: I'm using RemoteObjects to connect to my backend. In one of my calls I'm trying to return a List of VO's I create after hitting my database. On the Flex side, my result event is firing properly, but when I try to convert the result to an ArrayCollection, instead of getting a proper ArrayCollection I get single element that displays the following when I output it in an alert: obj is: '[object TestVO], [object TestVO], ... [object TestVO]'. If I return a single TestVO object directly instead of a List, I can properly cast the result to my ActionScript TestVO object and view the data properly. It's just returning the List that's giving me problems. Can anyone offer any suggestions please? My Flex result code is: public function result(evt:Object):void { if(evt.type evt.type == ResultEvent.RESULT) { var re:ResultEvent = evt as ResultEvent; var col:ArrayCollection = new ArrayCollection(ArrayUtil.toArray(re.result)); if(col) { Alert.show(obj is: ' + col.getItemAt(0) + '); } else Alert.show(Didn't cast properly!); } } My Java DAO code that returns the list is: public List getList() throws DAOException { List list = new ArrayList(); Connection c = null; try { c = ConnectionHelper.getConnection(); Statement s = c.createStatement(); ResultSet rs = s.executeQuery(SELECT * FROM test); while(rs.next()) { TestVO vo = new TestVO(); vo.id = rs.getInt(id); vo.data = rs.getString(data); list.add(vo); } } catch(Exception e) { e.printStackTrace(); throw new DAOException(e); } finally { ConnectionHelper.close(c); } return list; } -- André Rodrigues Pena LOCUS www.locus.com.br Blog www.techbreak.org
[flexcoders] Re: Trouble Converting java List to ArrayCollection
I have to disagree about not throwing the Exception. If you actually handle the exception in the method then you shouldn't throw it, but the exception is not being handled. The user needs to know that something went wrong. And the bit about the Higher scope not importing the DAOException, that's just plain wrong. The higher scope will see it as an Exception if it doesn't handle the specific Exception. Not throwing the exception will simply return an empty list. The real question is what is the Exception that is being thrown in the try block? That will tell us what the issue is and lead us to the conclusion as to how to fix it. Is there a Stack Trace available that I can look at? Also... vo.id = rs.getInt(id); vo.data = rs.getString(data); ... this is bad from. You shouldn't be accessing data members directly, they should be encapsulated, and you should access them using accesser methods. Paul --- In flexcoders@yahoogroups.com, André Rodrigues Pena [EMAIL PROTECTED] wrote: Man.. Just remove the throws of your getList() method. Never use throws at a service method. (specially in this example where the throws is USELESS because you are already treating the exception with the try clause) The throws clause passes the exception treatment to a higher scope, this scope may not import de DAOException needed to process it properly. Simply never do this. On 2/12/07, ytseshred [EMAIL PROTECTED] wrote: I'm using RemoteObjects to connect to my backend. In one of my calls I'm trying to return a List of VO's I create after hitting my database. On the Flex side, my result event is firing properly, but when I try to convert the result to an ArrayCollection, instead of getting a proper ArrayCollection I get single element that displays the following when I output it in an alert: obj is: '[object TestVO], [object TestVO], ... [object TestVO]'. If I return a single TestVO object directly instead of a List, I can properly cast the result to my ActionScript TestVO object and view the data properly. It's just returning the List that's giving me problems. Can anyone offer any suggestions please? My Flex result code is: public function result(evt:Object):void { if(evt.type evt.type == ResultEvent.RESULT) { var re:ResultEvent = evt as ResultEvent; var col:ArrayCollection = new ArrayCollection(ArrayUtil.toArray(re.result)); if(col) { Alert.show(obj is: ' + col.getItemAt(0) + '); } else Alert.show(Didn't cast properly!); } } My Java DAO code that returns the list is: public List getList() throws DAOException { List list = new ArrayList(); Connection c = null; try { c = ConnectionHelper.getConnection(); Statement s = c.createStatement(); ResultSet rs = s.executeQuery(SELECT * FROM test); while(rs.next()) { TestVO vo = new TestVO(); vo.id = rs.getInt(id); vo.data = rs.getString(data); list.add(vo); } } catch(Exception e) { e.printStackTrace(); throw new DAOException(e); } finally { ConnectionHelper.close(c); } return list; } -- André Rodrigues Pena LOCUS www.locus.com.br Blog www.techbreak.org
[flexcoders] Re: Trouble Converting java List to ArrayCollection
That was a typo on my Part. They are being set using methods: TestVO vo = new TestVO(); vo.setID(rs.getInt(id)); vo.setData(rs.getString(data)); The problem has been solved by casting the result to ArrayCollection using the as keyword. I'm not sure what was causing my first problem, but I originally had my VO's defined with constructors, so I could create the VO like: TestVO vo = new TestVO(rs.getInt(id), rs.getString(data)); But Flex gave a runtime error when it was constructing the array collection saying something like TestVO init expected 2 arguments and got 0. --- In flexcoders@yahoogroups.com, lepusmars [EMAIL PROTECTED] wrote: I have to disagree about not throwing the Exception. If you actually handle the exception in the method then you shouldn't throw it, but the exception is not being handled. The user needs to know that something went wrong. And the bit about the Higher scope not importing the DAOException, that's just plain wrong. The higher scope will see it as an Exception if it doesn't handle the specific Exception. Not throwing the exception will simply return an empty list. The real question is what is the Exception that is being thrown in the try block? That will tell us what the issue is and lead us to the conclusion as to how to fix it. Is there a Stack Trace available that I can look at? Also... vo.id = rs.getInt(id); vo.data = rs.getString(data); ... this is bad from. You shouldn't be accessing data members directly, they should be encapsulated, and you should access them using accesser methods. Paul --- In flexcoders@yahoogroups.com, André Rodrigues Pena andre.ufrj@ wrote: Man.. Just remove the throws of your getList() method. Never use throws at a service method. (specially in this example where the throws is USELESS because you are already treating the exception with the try clause) The throws clause passes the exception treatment to a higher scope, this scope may not import de DAOException needed to process it properly. Simply never do this. On 2/12/07, ytseshred ytseshred@ wrote: I'm using RemoteObjects to connect to my backend. In one of my calls I'm trying to return a List of VO's I create after hitting my database. On the Flex side, my result event is firing properly, but when I try to convert the result to an ArrayCollection, instead of getting a proper ArrayCollection I get single element that displays the following when I output it in an alert: obj is: '[object TestVO], [object TestVO], ... [object TestVO]'. If I return a single TestVO object directly instead of a List, I can properly cast the result to my ActionScript TestVO object and view the data properly. It's just returning the List that's giving me problems. Can anyone offer any suggestions please? My Flex result code is: public function result(evt:Object):void { if(evt.type evt.type == ResultEvent.RESULT) { var re:ResultEvent = evt as ResultEvent; var col:ArrayCollection = new ArrayCollection(ArrayUtil.toArray(re.result)); if(col) { Alert.show(obj is: ' + col.getItemAt(0) + '); } else Alert.show(Didn't cast properly!); } } My Java DAO code that returns the list is: public List getList() throws DAOException { List list = new ArrayList(); Connection c = null; try { c = ConnectionHelper.getConnection(); Statement s = c.createStatement(); ResultSet rs = s.executeQuery(SELECT * FROM test); while(rs.next()) { TestVO vo = new TestVO(); vo.id = rs.getInt(id); vo.data = rs.getString(data); list.add(vo); } } catch(Exception e) { e.printStackTrace(); throw new DAOException(e); } finally { ConnectionHelper.close(c); } return list; } -- André Rodrigues Pena LOCUS www.locus.com.br Blog www.techbreak.org
[flexcoders] Re: Trouble Converting java List to ArrayCollection
Another way is: if(re.result is ArrayCollection) { var col:ArrayCollection = ArrayCollection(re.result); } The as operation is more brief but accomplishes the same thing. as will leave your object as null, where as doing the is check you can catch potential errors and not create any unnecessary objects. I don't think either way is more right or wrong. --- In flexcoders@yahoogroups.com, ytseshred [EMAIL PROTECTED] wrote: That was a typo on my Part. They are being set using methods: TestVO vo = new TestVO(); vo.setID(rs.getInt(id)); vo.setData(rs.getString(data)); The problem has been solved by casting the result to ArrayCollection using the as keyword. I'm not sure what was causing my first problem, but I originally had my VO's defined with constructors, so I could create the VO like: TestVO vo = new TestVO(rs.getInt(id), rs.getString(data)); But Flex gave a runtime error when it was constructing the array collection saying something like TestVO init expected 2 arguments and got 0.
[flexcoders] Re: Trouble Converting java List to ArrayCollection
Flex wants a public constructor with no arguments available, so the solution is to just add your second constructor with whatever arguments you want while keeping the default constructor. That would make both of the folowing valid: TestVO vo = new TestVO(); TestVO vo = new TestVO(rs.getInt(id), rs.getString(data)); It may arguably be bad form to access data members directly, but strict adherence to good Java form in value objects just leads to problems on the Flex side. If, for example, you went the full route and declared your data members private to force the use of accessor methods, your Flex object would contain no properties since Flex would have no visibility to the private members. I usually just let it slide, and create a Java good form wrapper object if I must for accessing on the server side. Doug --- In flexcoders@yahoogroups.com, ytseshred [EMAIL PROTECTED] wrote: That was a typo on my Part. They are being set using methods: TestVO vo = new TestVO(); vo.setID(rs.getInt(id)); vo.setData(rs.getString(data)); The problem has been solved by casting the result to ArrayCollection using the as keyword. I'm not sure what was causing my first problem, but I originally had my VO's defined with constructors, so I could create the VO like: TestVO vo = new TestVO(rs.getInt(id), rs.getString(data)); But Flex gave a runtime error when it was constructing the array collection saying something like TestVO init expected 2 arguments and got 0. --- In flexcoders@yahoogroups.com, lepusmars paul@ wrote: I have to disagree about not throwing the Exception. If you actually handle the exception in the method then you shouldn't throw it, but the exception is not being handled. The user needs to know that something went wrong. And the bit about the Higher scope not importing the DAOException, that's just plain wrong. The higher scope will see it as an Exception if it doesn't handle the specific Exception. Not throwing the exception will simply return an empty list. The real question is what is the Exception that is being thrown in the try block? That will tell us what the issue is and lead us to the conclusion as to how to fix it. Is there a Stack Trace available that I can look at? Also... vo.id = rs.getInt(id); vo.data = rs.getString(data); ... this is bad from. You shouldn't be accessing data members directly, they should be encapsulated, and you should access them using accesser methods. Paul --- In flexcoders@yahoogroups.com, André Rodrigues Pena andre.ufrj@ wrote: Man.. Just remove the throws of your getList() method. Never use throws at a service method. (specially in this example where the throws is USELESS because you are already treating the exception with the try clause) The throws clause passes the exception treatment to a higher scope, this scope may not import de DAOException needed to process it properly. Simply never do this. On 2/12/07, ytseshred ytseshred@ wrote: I'm using RemoteObjects to connect to my backend. In one of my calls I'm trying to return a List of VO's I create after hitting my database. On the Flex side, my result event is firing properly, but when I try to convert the result to an ArrayCollection, instead of getting a proper ArrayCollection I get single element that displays the following when I output it in an alert: obj is: '[object TestVO], [object TestVO], ... [object TestVO]'. If I return a single TestVO object directly instead of a List, I can properly cast the result to my ActionScript TestVO object and view the data properly. It's just returning the List that's giving me problems. Can anyone offer any suggestions please? My Flex result code is: public function result(evt:Object):void { if(evt.type evt.type == ResultEvent.RESULT) { var re:ResultEvent = evt as ResultEvent; var col:ArrayCollection = new ArrayCollection(ArrayUtil.toArray(re.result)); if(col) { Alert.show(obj is: ' + col.getItemAt(0) + '); } else Alert.show(Didn't cast properly!); } } My Java DAO code that returns the list is: public List getList() throws DAOException { List list = new ArrayList(); Connection c = null; try { c = ConnectionHelper.getConnection(); Statement s = c.createStatement(); ResultSet rs = s.executeQuery(SELECT * FROM test); while(rs.next()) { TestVO vo = new TestVO(); vo.id = rs.getInt(id); vo.data = rs.getString(data); list.add(vo); } } catch(Exception e) { e.printStackTrace(); throw new DAOException(e); } finally { ConnectionHelper.close(c); } return list; } -- André Rodrigues Pena LOCUS www.locus.com.br Blog www.techbreak.org
[flexcoders] Re: Trouble Converting java List to ArrayCollection
I think you misinterpreted, the TestVO is a java class. Coding it correctly should not affect the Flex versions of the class. When the bean is transfered to Flex it will contain publicly accessable properties representing the private members that have public getters, ie if you have getData there will be a property data on the transfered object. That is how Java Beans work. If this convention is not followed then if you start working with Enterprise level projects you run into problems. --- In flexcoders@yahoogroups.com, Doug Lowder [EMAIL PROTECTED] wrote: Flex wants a public constructor with no arguments available, so the solution is to just add your second constructor with whatever arguments you want while keeping the default constructor. That would make both of the folowing valid: TestVO vo = new TestVO(); TestVO vo = new TestVO(rs.getInt(id), rs.getString(data)); It may arguably be bad form to access data members directly, but strict adherence to good Java form in value objects just leads to problems on the Flex side. If, for example, you went the full route and declared your data members private to force the use of accessor methods, your Flex object would contain no properties since Flex would have no visibility to the private members. I usually just let it slide, and create a Java good form wrapper object if I must for accessing on the server side. Doug --- In flexcoders@yahoogroups.com, ytseshred ytseshred@ wrote: That was a typo on my Part. They are being set using methods: TestVO vo = new TestVO(); vo.setID(rs.getInt(id)); vo.setData(rs.getString(data)); The problem has been solved by casting the result to ArrayCollection using the as keyword. I'm not sure what was causing my first problem, but I originally had my VO's defined with constructors, so I could create the VO like: TestVO vo = new TestVO(rs.getInt(id), rs.getString(data)); But Flex gave a runtime error when it was constructing the array collection saying something like TestVO init expected 2 arguments and got 0.
[flexcoders] Re: Trouble Converting java List to ArrayCollection
Nope, I didn't misinterpret. I know TestVO is a Java class (probably with a Flex counterpart class), and the way the Java class is coded can definitely have an effect when it is accessed from Flex. It isn't a case of correctness, but a case of what Flex expects from Java classes. You can test this all out yourself by implementing multiple constructors and/or declaring the properties as private in your Java class. Best, Doug --- In flexcoders@yahoogroups.com, lepusmars [EMAIL PROTECTED] wrote: I think you misinterpreted, the TestVO is a java class. Coding it correctly should not affect the Flex versions of the class. When the bean is transfered to Flex it will contain publicly accessable properties representing the private members that have public getters, ie if you have getData there will be a property data on the transfered object. That is how Java Beans work. If this convention is not followed then if you start working with Enterprise level projects you run into problems. --- In flexcoders@yahoogroups.com, Doug Lowder douglowder@ wrote: Flex wants a public constructor with no arguments available, so the solution is to just add your second constructor with whatever arguments you want while keeping the default constructor. That would make both of the folowing valid: TestVO vo = new TestVO(); TestVO vo = new TestVO(rs.getInt(id), rs.getString(data)); It may arguably be bad form to access data members directly, but strict adherence to good Java form in value objects just leads to problems on the Flex side. If, for example, you went the full route and declared your data members private to force the use of accessor methods, your Flex object would contain no properties since Flex would have no visibility to the private members. I usually just let it slide, and create a Java good form wrapper object if I must for accessing on the server side. Doug --- In flexcoders@yahoogroups.com, ytseshred ytseshred@ wrote: That was a typo on my Part. They are being set using methods: TestVO vo = new TestVO(); vo.setID(rs.getInt(id)); vo.setData(rs.getString(data)); The problem has been solved by casting the result to ArrayCollection using the as keyword. I'm not sure what was causing my first problem, but I originally had my VO's defined with constructors, so I could create the VO like: TestVO vo = new TestVO(rs.getInt(id), rs.getString(data)); But Flex gave a runtime error when it was constructing the array collection saying something like TestVO init expected 2 arguments and got 0.
[flexcoders] Re: Trouble Converting java List to ArrayCollection
--- In flexcoders@yahoogroups.com, Doug Lowder [EMAIL PROTECTED] wrote: Nope, I didn't misinterpret. I know TestVO is a Java class (probably with a Flex counterpart class), and the way the Java class is coded can definitely have an effect when it is accessed from Flex. It isn't a case of correctness, but a case of what Flex expects from Java classes. You can test this all out yourself by implementing multiple constructors and/or declaring the properties as private in your Java class. Ok, I hate that I keep dragging this out but I also don't like when people promote bad coding conventions. Here is a simple example java bean, taken from Adobe. package flex.testdrive.store; public class Product { private int productId; private String name; private String description; private String image; private String category; private double price; private int qtyInStock; public Product() { } public Product(int productId, String name, String description, String image, String category, double price, int qtyInStock) { this.productId = productId; this.name = name; this.description = description; this.image = image; this.category = category; this.price = price; this.qtyInStock = qtyInStock; } public String getCategory() { return category; } public void setCategory(String category) { this.category = category; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getImage() { return image; } public void setImage(String image) { this.image = image; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getProductId() { return productId; } public void setProductId(int productId) { this.productId = productId; } public int getQtyInStock() { return qtyInStock; } public void setQtyInStock(int qtyInStock) { this.qtyInStock = qtyInStock; } } You see all of the properties are private with public getters and setters. When the Flex Data Services transfers the object to the Flash Player it has all of the expected properties. This is proper, correct and safe coding. Adobe even recommends this kind of coding in ActionScript, which is why you use the set and get methods. Paul
[flexcoders] Re: Trouble Converting java List to ArrayCollection
The multiple public constructors in the example is part of what I was talking about. I wasn't aware Flex would play nice with private properties, however. Perhaps I had that in my mind from classes that didn't implement getters/setters, or maybe it's an improvement over the Flex 1.5 days. In any case, thanks for the tip Paul. --- In flexcoders@yahoogroups.com, lepusmars [EMAIL PROTECTED] wrote: --- In flexcoders@yahoogroups.com, Doug Lowder douglowder@ wrote: Nope, I didn't misinterpret. I know TestVO is a Java class (probably with a Flex counterpart class), and the way the Java class is coded can definitely have an effect when it is accessed from Flex. It isn't a case of correctness, but a case of what Flex expects from Java classes. You can test this all out yourself by implementing multiple constructors and/or declaring the properties as private in your Java class. Ok, I hate that I keep dragging this out but I also don't like when people promote bad coding conventions. Here is a simple example java bean, taken from Adobe. package flex.testdrive.store; public class Product { private int productId; private String name; private String description; private String image; private String category; private double price; private int qtyInStock; public Product() { } public Product(int productId, String name, String description, String image, String category, double price, int qtyInStock) { this.productId = productId; this.name = name; this.description = description; this.image = image; this.category = category; this.price = price; this.qtyInStock = qtyInStock; } public String getCategory() { return category; } public void setCategory(String category) { this.category = category; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getImage() { return image; } public void setImage(String image) { this.image = image; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getProductId() { return productId; } public void setProductId(int productId) { this.productId = productId; } public int getQtyInStock() { return qtyInStock; } public void setQtyInStock(int qtyInStock) { this.qtyInStock = qtyInStock; } } You see all of the properties are private with public getters and setters. When the Flex Data Services transfers the object to the Flash Player it has all of the expected properties. This is proper, correct and safe coding. Adobe even recommends this kind of coding in ActionScript, which is why you use the set and get methods. Paul