/** * Automatically retrieve ID from entity instance. * * @param obj The object from whom we need primary key * @return The corresponding primary key. */ @SuppressWarnings({"rawtypes", "unchecked"}) protected ID getIdFromEntity(T obj) { MetamodelUtils utils = new MetamodelUtils<T, ID>( (Class<T>) ClassUtils.getGenericTypeFromBean(this.service), em.getMetamodel()); return (ID) utils.getIdFromEntity(obj); }
/* * (non-Javadoc) * * @see org.resthub.web.controller.GenericController#update(ID, T) */ @SuppressWarnings({"rawtypes", "unchecked"}) @Override @PUT @Path("/{id}") public T update(@PathParam("id") ID id, T entity) { Assert.notNull(id, "id cannot be null"); T retreivedEntity = this.service.findById(id); if (retreivedEntity == null) { throw new NotFoundException(); } MetamodelUtils utils = new MetamodelUtils<T, ID>( (Class<T>) ClassUtils.getGenericTypeFromBean(this.service), em.getMetamodel()); Serializable entityId = utils.getIdFromEntity(entity); if ((entityId != null) && !id.equals(this.getIdFromEntity(retreivedEntity))) { throw new WebApplicationException(Response.Status.CONFLICT); } if (null == entityId) { utils.setIdForEntity(entity, id); } return this.service.update(entity); }