/** * a more concrete example of custom action<br> * resource level determines the granularity of the action<br> * mismatching the resource level in the request throws exception and will respond HTTP 400 * * @param resource Instance of the resource class. This is not part of the action method and is * needed because this implementation is not an actual resource. */ @Action(name = "updateTone", resourceLevel = ResourceLevel.ENTITY) // The base resource parameter gets special handling in the generator. It is set to the actual // resource class instance, and is not part of the generated REST method. public Greeting updateTone( BaseResource resource, @ActionParam("newTone") @Optional Tone newTone, @ActionParam("delOld") @Optional("false") Boolean delOld) { // the way to get entity key in action Long key = resource.getContext().getPathKeys().get(_resourceName + "Id"); Greeting g = _db.get(key); if (g == null) { // HTTP 404 return g; } // delete existing Greeting and assign new key if (delOld) { _db.remove(key); key = _idSeq.incrementAndGet(); g.setId(key); } Tone t; // newTone is an optional parameter // omitting it in request results a null value if (newTone == null) { t = DEFAULT_TONE; } else { t = newTone; } g.setTone(t); _db.put(key, g); return g; }
private static Map<Long, Greeting> generateUpdates(Long[] ids) { Map<Long, Greeting> updates = new HashMap<Long, Greeting>(); for (long l : ids) { Greeting greeting = new Greeting(); greeting.setId(l).setMessage("update message").setTone(Tone.SINCERE); updates.put(l, greeting); } return updates; }
// These CRUD annotations are MANDATORY for the code generator because we want to generate // implementations which do not use the templates, e.g. Task @RestMethod.Create public CreateResponse create( Greeting entity, @QueryParam("isNullId") @Optional("false") boolean isNullId) { entity.setId(_idSeq.incrementAndGet()); _db.put(entity.getId(), entity); if (isNullId) { return new CreateResponse(null, HttpStatus.S_201_CREATED); } return new CreateResponse(entity.getId()); }