GWT JsArray of self, recursive object array - arrays

I am building a tree structure which an object references itself in like so:
public class ProjectObjectOL extends JavaScriptObject {
protected ProjectObjectOL() { }
public final native boolean getStatus() /*-{ return this.status; }-*/;
public final native String getError() /*-{ return this.error_message; }-*/;
public final native JsArray<ProjectObjectOL> getChildren() /*-{ this.children; }-*/;
}
My problem is that I can't seem to get the children of the object. I've tested it out, and I'm quite sure the JSON structure being passed back consists of an object that contains an array of children of that type which can contain children, etc.
...but when trying to access even the simplest information about the children, the length of the array, it returns 0 every time. I've tried with no success to figure out what it's doing wrong, all the other data returns fine, but this one piece will NOT retrieve the children. Here is an example of how I might (directly) access the length for testing:
JSONObject oResults = (JSONObject) JSONParser.parse(response.getText());
ProjectListOL testoutputOL = oResults.isObject().getJavaScriptObject().cast();
ProjectObjectOL testObject = testoutputOL.getProjectList().get(1);
Window.alert(testObject.getChildren().length()+"");
A ProjectListOL contains an array of ProjectObjectOLs. In the example above I simply grabbed the one I KNOW has children. I'm using ColdFusion for the backend that returns the JSON object. Once again, I have output this object multiple times, both in GWT and outside (directly dumping the JSON object from the file) verifying that the object is indeed set up how I expect it to be set up.

I missed an obvious mistake:
public final native JsArray<ProjectObjectOL> getChildren() /*-{ this.children; }-*/;
OOPS:
public final native JsArray<ProjectObjectOL> getChildren() /*-{ **return** this.children; }-*/;

Related

CodenameOne: PropertyIndex.toJSON() not generating correct JSON for Lists of Objects

I have a mobile app where a user fills out a form, say an Event, and when they save I want to submit the data as JSON to the server. CN1 has the feature to generate JSON easily using PropertyBusinessObject so my Event is defined as follows:
public class Event implements PropertyBusinessObject {
public final Property<Long, Event> eventId = new Property<>("eventId");
public final Property<EventLocation, Event> eventLocation = new Property<>("eventLocation", EventLocation.class);
public final Property<List<EventItinerary>, Event> eventItineraryList = new Property<>("eventItineraryList", XXX);
private final PropertyIndex idx...
}
Both EventLocation and EventItinerary implement PropertyBusinessObject and I've found that when I generate the JSON for Event, EventLocation generates fine but not EventItinerary. When I try and use EventItinerary.class in the "XXX" section above, I get the following error...
Exception: java.lang.ClassCastException - class java.util.ArrayList cannot be cast to class com.codename1.properties.PropertyBusinessObject
which occurs at line 484 of com.codename1.properties.PropertyIndex.toMapRepresentationImpl()
When I use List.class for "XXX" or nothing i.e. new Property<>("eventItineraryList"); then it posts to the server but the JSON contains the name of the class and its memory address i.e.
{
"eventId": 3425567,
"eventLocation" : {
...
},
"eventItineraryList": [
"com.myapp.event.EventItinerary#cdc543c",
"com.myapp.event.EventItinerary#39987ocb",
"com.myapp.event.EventItinerary#cd5t776c",
]
}
My question is what should I put in "XXX" to have the EventItinerary objects have the correct JSON representation?
You need to use a ListProperty so we can traverse into it and the ListProperty should refer to a different PropertyBusinessObject. So this should look roughly like this:
public final ListProperty<EventItinerary, Event> eventItineraryList = new ListProperty<>("eventItineraryList", EventItinerary.class);
Notice the EventItinerary.class which is important. The generic valuegets lost due to erasure. The argument lets us reconstruct the object with the right object types when loading from JSON.'
Also again, for this to work EventItinerary must be a PropertyBusinessObject too.

iBatis unable to read property from Map when using isEqual

I'm seeing a very bizarre issue with iBatis when trying to read a property from a Java map using isEqual, but not with other iBatis operators. For example it is able to read the map properties fine when using isNotNull and iterate. The xml:
<isNotNull property="filterCriteria.account">
AND
id
<isEqual property="filterCriteria.account.meetsCriteria" compareValue="false">
NOT
</isEqual>
IN
(SELECT DISTINCT id
FROM account
WHERE some other criteria....
)
</isNotNull>
The 2 java classes we're using here:
public class SearchProfile {
private Map<String, SearchProfileCriteria> filterCriteria;
public SAOSearchProfile() {
filterCriteria = new HashMap<>();
}
public Map<String, SAOSearchProfileCriteria> getFilterCriteria() {
return filterCriteria;
}
public void setFilterCriteria(Map<String, SAOSearchProfileCriteriaBase> filterCriteria) {
this.filterCriteria = filterCriteria;
}
}
Above is the container object that is passed to iBatis for the querying, and below is the criteria object that will be the value of the map. In this example it is keyed with the String "account"
public class SearchProfileCriteria {
boolean meetsCriteria;
public String getCriteriaAsString() {
return StringUtils.getStringValueFromBoolean(meetsCriteria);
}
public boolean isMeetsCriteria() {
return meetsCriteria;
}
public void setMeetsCriteria(boolean meetsCriteria) {
this.meetsCriteria = meetsCriteria;
}
public String getSQLString(){
return meetsCriteria ? "" : "NOT";
}
}
And the exception:
Cause: com.ibatis.common.beans.ProbeException: There is no READABLE property named 'account' in class 'java.util.Map'; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:
The getSQLString() method was my half baked attempt at a work around, the String gets escaped in the query and throws a syntax error.
When I remove the <isEqual> block the query executes find, which indicates it is able to read the "account" key when checking the to see if it is null. As I mentioned above, we're also able to use the map keys in <iterate> tags without issue. It seems <isEqual> and <isNotEqual> are the only tags causing issues. Does anyone have experience with this or know what may be going on?
Beware: Using isNotNull, isEqual, iterate is iBatis, they don't exist anymore in Mybatis, so referencing to Mybatis indifferently is confusing.
Reference documentation.
For your issue, how does it behave if replacing Map with a class (property will be known at compile time)?
Or try using <isPropertyAvailable>.
The work around could work with right syntax: $ instead of #: $filterCriteria.account.SQLString$ instead of #filterCriteria.account.SQLString#, then the value is just concatenated instead of bound as parameter.

How to receive Map parameters in Resteasy?

I would like to receive these HTTP parameters (POST) in my Resteasy service:
customFields[my_key]=some_value
customFields[my_key2]=some_value2
Something like this doesn't work:
#Form(prefix="customFields")
Map<String, String> customFields
... what happens here is that on the server the new Map is initialized, and the key for the Map entry is set (i.e. "my_key") but value is not set.
Does anyone know how to handle the case like mine, where I need to receive unknown number of fields (within a Map), but each of them properly structured (HTTP map/dictionary notation).
This is a known bug. The workaround is to use your own string wrapper as the map value type. For example:
public class StringWrapper implements Serializable {
private static final long serialVersionUID = 1L;
#FormParam("value")
public String value;
}
Redefine your map as:
#Form(prefix="customFields")
Map<String, StringWrapper> customFields;
And then pass the values to it as customFields[my_key].value=some_value

c# returning arrays via properties

Id like to firstly apologise for what may appear to be a stupid question but im confused regarding the following.
Im writting a class library which will not be running on the UI thread. Inside the CL i need an array which im going populate with data received from a stored procedure call. I then need to pass this data back to the UI thread via an event.
Originally i was going to write the following.
public class ColumnInformation
{
public string[] columnHeaderNames;
public string[] columnDataTypes;
}
but im pretty sure that would be frowned upon and i instead should be using properties.
public class ColumnInformation
{
public string[] columnHeaderNames {get; set;}
public string[] columnDataTypes {get; set;}
}
but then i came across the following.
MSDN
so am i correct in assuming that i should actually declare this as follows:
public class ColumnInformation
{
private string[] _columnHeaderNames;
public Names(string[] headerNames)
{
_columnHeaderNames = headerNames;
}
public string[] GetNames()
{
// Need to return a clone of the array so that consumers
// of this library cannot change its contents
return (string[])_columnHeaderNames.Clone();
}
}
Thanks for your time.
If your concern is the guideline CA1819: Properties should not return arrays,
It will be same whether you are exposing Array as a Public Field, or Property (making readonly does not matter here). Once your original Array is exposed, its content can be modified.
To avoid this, as the link suggest, make Field private, and return Clone from the Getter.
However major concern is that there may be multiple copies of your array if retrieved many times. It is not good for performance and synchronization.
Better solution is ReadOnlyCollection.
Using ReadOnlyCollection, you can expose the collection as read only which cannot be modified. Also any changes to underlying collection will be reflected.

Dapper Correct Object / Aggregate Mapping

I have recently started evaluating Dapper as a potential replacement for EF, since I was not too pleased with the SQL that was being generated and wanted more control over it. I have a question regarding mapping a complex object in my domain model. Let's say I have an object called Provider, Provider can contain several properties of type IEnumerable that should only be accessed by going through the parent provider object (i.e. aggregate root). I have seen similar posts that have explained using the QueryMultiple and a Map extension method but was wondering how if I wanted to write a method that would bring back the entire object graph eager loaded, if Dapper would be able to do this in one fell swoop or if it needed to be done piece-meal. As an example lets say that my object looked something like the following:
public AggregateRoot
{
public int Id {get;set;}
...//simple properties
public IEnumerable<Foo> Foos
public IEnumerable<Bar> Bars
public IEnumerable<FooBar> FooBars
public SomeOtherEntity Entity
...
}
Is there a straightforward way of populating the entire object graph using Dapper?
I have a similar situation. I made my sql return flat, so that all the sub objects come back. Then I use the Query<> to map the full set. I'm not sure how big your sets are.
So something like this:
var cnn = sqlconnection();
var results = cnn.Query<AggregateRoot,Foo,Bars,FooBar,someOtherEntity,AggregateRoot>("sqlsomething"
(ar,f,b,fb,soe)=>{
ar.Foo = f;
ar.Bars = b;
ar.FooBar = fb;
ar.someotherentity = soe;
return ar;
},.....,spliton:"").FirstOrDefault();
So the last object in the Query tag is the return object. For the SplitOn, you have to think of the return as a flat array that the mapping will run though. You would pick the first return value for each new object so that the new mapping would start there.
example:
select ID,fooid, foo1,foo2,BarName,barsomething,foobarid foobaritem1,foobaritem2 from blah
The spliton would be "ID,fooid,BarName,foobarid". As it ran over the return set, it will map the properties that it can find in each object.
I hope that this helps, and that your return set is not too big to return flat.

Resources