Re: ERRest and to many relationships
Re: ERRest and to many relationships
- Subject: Re: ERRest and to many relationships
- From: Pascal Robert <email@hidden>
- Date: Tue, 29 Mar 2011 16:21:30 -0400
Le 2011-03-29 à 16:03, Andrew Kinnie a écrit :
> Thanks for the tip on using create that way. I didn't know you could do that. In any event, that's the problem. NoteType is basically a lookup table. I would want to fetch an existing noteType based on some unique attribute, such as "typeName" (which ensure is unique in the EO class) I don't want to create a new noteType, I want to create the intervening object (DeviceNoteType) which has a to-one to NoteType and a to-one to Device. AND (here's the tough part - for me at least) I want to only create a new one if one doesn't already exist. So I need to fetch first, and I want to fetch based on the value passed in for the name key
So you will do:
new ERXRoute(NoteType.ENTITY_NAME, "/NoteType/{name:String}", ERXRoute.Method.Get, NoteTypesController.class, "fetchByName");
public WOActionResults fetchByNameAction() {
String typeName = routeObjectForKey("name");
NoteType type = NoteType.fetchNoteType(editingContext(), User.NAME.eq(typeName));
return response(type, yourerxkeyfilter());
}
If fetchByNameAction didn't find an object, ERRest will return the HTTP code 404, so your client can know that the object was not found, and call:
new ERXRoute(NoteType.ENTITY_NAME, "/NoteType/", ERXRoute.Method.Post, NoteTypesController.class, "create"); // If you called addDefaultRoutes for the NoteType entity, that route already exist)
public WOActionResults createAction() {
NoteType type = create(NoteType.ENTITY_NAME, yourerxkeyfilter());
editingContext().saveChanges();
return response(type, yourerxkeyfilter());
}
If you get a response with HTTP code in the 20x range, the object was created and now you can create your DeviceNoteType with a route like this:
new ERXRoute(DeviceNoteType.ENTITY_NAME, "/DeviceNoteType", ERXRoute.Method.Post, DeviceNoteTypesController.class, "create");
When you call POST /cgi-bin/WebObjects/MyApp.woa/ra/DeviceNoteType, you will have to pass the a Device and a NoteType object in JSON as the body of the request.
I feel like I'm writing my WOWODC session in real-time :-P
> So I need to fetch on the passed in value from the json, get the correct NoteType at the other end of the many to many, check if there is an existing row in the join table for the device and noteType, and create a new one, only if the intervening object doesn't exist. If it does exist, I set it's status to active and return that, otherwise, I return the new one.
>
> That's my theory at least.
>
>
>
> On Mar 29, 2011, at 3:47 PM, Pascal Robert wrote:
>
>>
>> Le 2011-03-29 à 15:31, Andrew Kinnie a écrit :
>>
>>> Well, first I tried to simply create the intervening object manually, but then I realized I wanted to know if such an object already existed, so I was going to do a fetch, but as random client wouldn't know the primary key of the NoteType, I am not sure how to do that. I could expose the primary key, but I'd much rather not.
>>>
>>> Presumably, the client could simple call the curl commands to create the intervening object, then add the current object with a second line, but I wanted to be able to do this in the java itself so the client could do something like:
>>>
>>> curl -X PUT -d "{noteType:{type:'NoteType', name:'Alert'}}" http://My-MacBook-Pro.local:9001/cgi-bin/WebObjects/MyApp.woa/ra/Device/1/addNoteType.json
>>>
>>> Then I would get the device, get the NoteType represented by the routeObjectForKey("noteType") and set add the noteType to the relationship.
>>>
>>> public WOActionResults addNoteTypeAction() {
>>> Device device = Device();
>>> NoteType type = routeObjectForKey("noteType");
>>> // set the relationships and do whatever else
>>> ...
>>
>> If you want to create a new NoteType to a Device, you should call the create() method in addNoteTypeAction instead of routeObjectForKey, and set the relation between the NoteType and the Device:
>>
>> public WOActionResults addNodeTypeAction() {
>> NoteType nodeType = create(NoteType.ENTITY_NAME, yourerxkeyfilter);
>> Device device = routeObjectForKey("device");
>> // set the relationship and call ec.saveObjects();
>> return response(nodeType, yourerxkeyfilter);
>> }
>>
>> And your route:
>>
>> requestHandler.addRoute(new ERXRoute(Device.ENTITY_NAME, "/Device/{device:Device}/addNoteType", ERXRoute.Method.Put, DevicesController.class, "addNoteType"));
>>
>> This is untested, of course.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden