OK. I'm now in a bit of time crunch, so I just want to get something working.
assuming I have a route
new ERXRoute(NoteType.ENTITY_NAME, "/NoteType/{name:String}",
ERXRoute.Method.Get, NoteTypeController.class, "fetchByName")
What is the url I need to actually pass in a string using this
fetchByNameAction?
If I do /ra/NotificationType/fetchByName I get 0 objects (because name == null)
How do I pass in a string? What is the url I need to use for this? (every
rest tutorial I have looked at either doesn't apply at all, or doesn't help)
Thanks
On Mar 30, 2011, at 11:12 AM, Mike Schrag wrote:
> admittedly i've only sort of half-read this thread, but i would think you
> could make a custom NodeTypeRestDelegate that has your own implementation of
> createObjectOfID that checks to see if there already is one and just returns
> it ... maybe. there are a bunch of ways you can do things by hooking into
> those delegates. you can also probably do some tricks by using key filter
> delegates.
>
> ms
>
> On Mar 30, 2011, at 10:58 AM, Andrew Kinnie wrote:
>
>> Sorry about all this, Pascal. Didn't mean to make you write your session
>> early. :-) I guess I'm just dense about this stuff, never having used Rest
>> before, ERRest in particular, or, for that matter, ever had to deal directly
>> with HTTP response codes. This seems to suggest that you can't, from within
>> a controller for a given entity (in this case "Device"), create a method
>> with, based on a passed in String, fetch an object of a different entity
>> ("DeviceNoteType") which points to (via a to-one) a NoteType object with the
>> name passed in as a variable?
>>
>> Basically I want to do this:
>> - create a single action method (in my DeviceController) which allows the
>> client to pass in a String variable "noteTypeName", then
>> - uses that String to fetch a DeviceNoteType object where the noteType has
>> that noteTypeName, and the device is the current device
>> --> If there is, set the status to active,
>> --> if not, create a new DeviceNoteType
>>
>> The issue I am having is that the passed in string ("noteTypeName") is not a
>> key on Device, but is rather used for fetching a different object of a
>> different entity. I am always getting null from the routeObjectForKey
>> method.
>>
>> I would really rather not have the client make two requests, and have to
>> figure out http return calls. I'd rather have them be able to simply call
>> the addNoteType action and pass in a typeName (and eventually, be able to do
>> this with an array of typeNames)
>>
>> __________________________________________
>>
>> I am willing to make the client call several action methods via curl if
>> needed.
>>
>> In any event, I don't seem to know how to actually call the route you
>> mentioned in your post. In my Application class I added the route:
>>
>> routeRequestHandler.addRoute(new ERXRoute(NoteType.ENTITY_NAME,
>> "/NoteType/{name:String}", ERXRoute.Method.Get, NoteTypeController.class,
>> "fetchByName"));
>>
>> Thing is, I have no idea at all how to call this from the command line to
>> see if it returns anything. (and I have no idea how the http response codes
>> would appear)
>>
>> I tried:
>>
>> curl -X GET
>> http://MacBook-Pro.local:9001/cgi-bin/WebObjects/ra/NoteType/[name='alert'].json
>> (curl: (3) [globbing] error: bad range specification after pos 80)
>> curl -X GET
>> http://MacBook-Pro.local:9001/cgi-bin/WebObjects/ra/NoteType/'alert'.json
>> (- Unable to get contents of file
>> '/Library/WebServer/Documents/cgi-bin/WebObjects/ra/NoteType/alert.json' for
>> uri: /cgi-bin/WebObjects/ra/NoteType/alert.json)
>> curl -X GET
>> http://MacBook-Pro.local:9001/cgi-bin/WebObjects/ra/NoteType/'alert'
>> (no alertAction method)
>>
>> And various other things. I've tried to find rest tutorials to indicate
>> what the syntax is supposed to be, but everything seems to be assuming
>> you're passing in an id, which, obviously, I don't have.
>>
>> Assuming I eventually get this basic bit of syntax down, it looks like the
>> client would need to call the create on the DeviceNoteType and pass in fully
>> formed json representations of the NoteType and the Device? Presumably they
>> would do this by getting the json from the response?
>>
>> Sorry for not getting this stuff.
>>
>> Andrew
>>
>>
>> On Mar 29, 2011, at 4:21 PM, Pascal Robert wrote:
>>
>>>
>>> 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
>> _______________________________________________
>> Do not post admin requests to the list. They will be ignored.
>> Webobjects-dev mailing list ([email protected])
>> Help/Unsubscribe/Update your Subscription:
>> http://lists.apple.com/mailman/options/webobjects-dev/mschrag%40pobox.com
>>
>> This email sent to [email protected]
>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list ([email protected])
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com
This email sent to [email protected]