How do I use OData4J with an unknown number of child objects? - odata4j

I am trying to build a createEntity OEntity for an object that has multiple child collections within it.
I have looked over many of the example projects, but they all seem to assume that you have a known number of child objects in a collection so that you can use .inLine(“ObjectName”, ObjectOEntity1, ObjecteOEntity2…)
I have tried looking at the documentation and have not identified anything that leads me to think I can create a collection of OEntity objects that can then be added to my parent object with the inline.
The closest I found was the example listed on:
http://code.google.com/p/odata4j/source/browse/odata4j-fit/src/test/java/org/odata4j/producer/jpa/northwind/test/CreateTest.java?name=0.6
Has anyone else run into this problem?
If so how did you get around it?

You can pass in an array of OEntity objects. The core4j library that is used by odata4j contains some helper methods that can - for example - be used to get an array from an Iterable:
OEntity[] entitiesArray = Enumerable.create(entitiesIterable)
.toArray(OEntity.class);
But as there are also two variants of the properties method...
OCreateRequest<T> properties(OProperty<?>... props);
OCreateRequest<T> properties(Iterable<OProperty<?>> props);
... it might make sense to add an inline method that directly takes an Iterable<OEntity>.

Related

Is it possible to test if two types are of the same unknown inheritance in VB.net?

Is it possible to dynamically identify the closest common hierarchy or inheritance of two or more unknown typed objects? In other words, I'd like to test if, say, Integer and String have a common hierarchy, without knowing the objects I'm testing are going to be an Integer and String due to user selection. I found a C++ question posted that seems similar to my issue here: Check if two types are of the same template
However, I'm not familiar with any VB.net equivalents of the answers posted there, and online translators simply provide an error when I attempt to translate them. So is this even possible in VB.net in the first place?
The closest to this action that I know of is the .IsAssignableFrom() function, but in my case I don't know what the parent class/interface/whatever is to test against in the first place. the above function is the only thing even remotely related to this issue that pops up on any search I do.
The context I need this is in the Revit API; I'm trying to see if user selected elements have a similar hierarchy/inheritance that is not the Object Type, and if so to allow an action, otherwise, give a warning dialog box.
EDIT: Due to the nature of the Revit API and the desired effects of my command, the users of my plugin could select anything in the model, and I'm not able to determine which of the MANY common ancestors I could be looking for to compare using IsAssignableFrom. I could test for the (I think universal) common ancestor of Element type, but I don't want to allow users to run the command if you select a wall and an element tag. I need to find the common ancestors of the user-selected elements and confirm that the closest common ancestor is below Element type in hierarchy.
For example, the room tag element in the API has a hierarchy sort of like this:
Object -> Element -> SpatialElementTag -> RoomTag
There may be more intermediate inheritances, but I'm not going to track them down in the API documentation. And then each element may have a slightly different ancestry. IsAssignableFrom would be great if I knew the base ancestry I wanted to test for.
TnTinMn's answer gives me the type of solution I'm looking for.
The Type.BaseType Property returns:
The Type from which the current Type directly inherits, or null if the current Type represents the Object class or an interface.
Using this information, it is possible to define an iterator to enumerate the inheritance tree.
Private Iterator Function SelfAndAncestors(srcType As Type) As IEnumerable(Of Type)
Do Until srcType Is Nothing
Yield srcType
srcType = srcType.BaseType
Loop
End Function
Now you can use the Enumerable.Intersect Method to find all common types in the inheritance between two ancestry enumerations and return the first common ancestry type.
Dim t1 As Type = GetType(Form)
Dim t2 As Type = GetType(UserControl)
Dim highestCommonAncestor As Type = Enumerable.Intersect(SelfAndAncestors(t1), SelfAndAncestors(t2)).First()
For this case, the highest common ancestor is ContainerControl.

Yii CActiveRecord with Column Named "attributes"

I used the CRUD generator from a legacy database. When searching for a column value I get the following error:
htmlspecialchars() expects parameter 1 to be string, array given (/usr/local/share/yii/framework/web/helpers/CHtml.php:103)
The problem is that the model has an existing column named "attributes" which is creating a conflict. I removed the entry from the _search.php and commented out all instances in the model hoping to at least get it working but no luck. Any suggestions would be appreciated.
Thanks.
Every CActiveRecord instance (or CModel instance for that matter) has a getter/setter named attributes with which all the attributes can be set. This leads to a conflict because the generated crud code uses the attributes attribute expecting it works as described before.
The controller does something like:
$model->attributes=$_POST['ModelClassName'];
// or
$model->attributes=$_GET['ModelClassName'];
This is meant to set al the (safe) attributes of the model at once. Instead this overwrites the database attribute attributes of your legacy DB model.
This in turn leads to the error you describe, because $_GET['ModelClassName'] and $_POST['ModelClassName'] typically contain arrays of data.
I guess the easiest fix would be to directly call the setter function for the "normal" attributes behavior which would lead to replacing the lines mentioned above with something like the following:
// in the controller
$model->setAttributes($_POST['ModelClassName']);
// and
$model->setAttributes($_GET['ModelClassName']);
I think rest of the generated CRUD code (the views) could and should be left untouched to make it work.
If you want to know how and why this works, it's best to do some research into the __get and __set magic functions and how they're used in the yii framework.

How to implement introspection on RealBasic?

RealBasic's Introspection is kinda of different than what I expected.
My intention is:
Create a MainObject from which other objects will inherit two, three methods, to simplify.
Method 1-> Returns to the child class itself all of its properties, types and values.
Method 2-> Would call Method1 and with the information, save the child Object.
So, for method 1 I thought about writing a generalised introspection which for each child class would easily return what I need for Method 2 to do its work.
Why do I want this? So I can have tens of objects knowing how to save, draw themselves without worrying too much about a modification here or there on the properties etc...
But using what RealBasic tutorials and reference offers doesn't work, since it requires me to have it happening outside the object etc... i.e.: I can easily, inside ObjectA, get ObjectB's properties, methods etc, but I want to get inside ObjectA, A's properties, not B's
Thanks in advance...
I've found out how... Very simple, create the MainClass and inside of it a simple method WhoAmI which could return an array, dictionary etc...
Dim thisClassTypeInfo As Introspection.TypeInfo = Introspection.GetType(Self)
Dim thisClassProperties() As Introspection.PropertyInfo = thisClassTypeInfo.GetProperties
Dim thisClassMethods() As Introspection.MethodInfo = thisClassTypeInfo.GetMethods
For Each myProperty As Introspection.PropertyInfo In thisClassProperties
// Then here use myProperty.Name, myProperty.Value(Self).StringValue
// or myProperty.Value(Self).anyotheroption and create a dictionary
// or array with the results and return it.
Next

Whats is methodLists attribute of the structure objc_class for?

Looking inside the runtime.h, I found the definition of the structure objc_class.
Among various members, We have this :-
struct objc_method_list **methodLists
We definitely need to know what all methods a class has,
But a list of methods should be fine, but why do we have "lists" ?
Why not just one list ?
Also, can anyone specify that, Are methods inherited from superclass part of that list or we get to them via superclass pointer that points to parent class's structure.
Here is my detail investigation into struct objc_method_list **methodLists : http://blog.csdn.net/jasonblog/article/details/7303618
And in short, methodLists stores SEL-IMP mapping of the instance methods by default. In this situation, it has only one list.
As the name 'methodLists' suggests, it can contain more than one list. If you add a category to a class, the runtime system will insert one more list into methodLists, which points to the method-list of the category.
I tried to answer this question several months ago, but at that time SO discard my answer due to network problem. Now I meet it again :)
The purpose is explained in objc-class.m, as linked by Georg:
cls->methodLists may be in one of three forms:
NULL: The class has no methods.
non-NULL, with CLS_NO_METHOD_ARRAY set: cls->methodLists points
to a single method list, which is the class's only method list.
non-NULL, with CLS_NO_METHOD_ARRAY clear: cls->methodLists points to
an array of method list pointers. The end of the array's block
is set to -1. If the actual number of method lists is smaller
than that, the rest of the array is NULL.
Attaching categories and adding and removing classes may change
the form of the class list. In addition, individual method lists
may be reallocated when fixed up.
Classes are initially read as #1 or #2. If a category is attached
or other methods added, the class is changed to #3. Once in form #3,
the class is never downgraded to #1 or #2, even if methods are removed.
Classes added with objc_addClass are initially either #1 or #3.
The short answer is therefore "because of categories." When a category is injected, rather than try to combine its method list with the one existing list, a new entry is simply added to methodLists, and set to the list coming from the category. This probably makes category injection faster, since it avoids (potential) large reallocations and copying.

Finding variables that share common properties

I'm using Mathematica and have a set of variables (A,B,C,D,...) with properties A=(blue, big, rounded), B=(red, small, spiky), and so forth. Those properties can be common between variables. What would be the best, general way to find all variables that share a common property (of being, for instance, small)? Thanks.
Here's a list of possible properties:
In[1]:= properties={"red","green","blue","big","small","rounded","spiky"};
And here's a list of objects with some of those properties
In[2]:= list={{"blue","big","rounded"},{"red","small","spiky"},
{"red","big","rounded"},{"blue","small","spiky"}};
You can find all objects that have the property of, e.g., being "blue" using Select
In[3]:= Select[list, MemberQ[#,"blue"]&]
Out[3]= {{blue,big,rounded},{blue,small,spiky}}
This could be wrapped up into a function. Although how I would write that function would depend on the data structures and usage that you're planning.
Actually, I just reread you question you have a list of objects with some properties and you want to refer to those objects by name. So you probably want something more like
In[1]:= listProperties["A"]:={"blue","big","rounded"}
listProperties["B"]:={"red","small","spiky"}
listProperties["C"]:={"red","big","rounded"}
listProperties["D"]:={"blue","small","spiky"}
Above I defined some properties that are associated with certain strings. You don't have to use strings in the above or below, and you can create a better structure than that if you want. You could also make a constructor to create the above, such a constructor could also check if the list of properties supplied is of the right form - i.e. does not have contradictory properties, are all in a list of known properties etc...
We then define a function to test if an object/string has a certain property associated with it
In[2]:= hasProperty[obj_, property_]:=MemberQ[listProperties[obj],property]
You might want to return an error or warning message if listProperties[obj] does not have a definition/rule associated with it.
Use Select to find all "objects" in a list that have the associated property "blue":
In[3]:= Select[{"A","B","C","D"}, hasProperty[#,"blue"]&]
Out[3]= {A,D}
There are other ways (probably better ways) to set up such a data structure. But this is one of the simplest ways in Mathematica.