*
}
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public abstract class GenericControllerImpl> implements
<<<<<<< HEAD
GenericController {
protected S service;
@PersistenceContext
private EntityManager em;
public GenericControllerImpl() {
}
public void setService(S service) {
this.service = service;
public S getService() {
return this.service;
}
/*
* (non-Javadoc)
*
* @see org.resthub.web.controller.GenericController#create(T)
*/
@Override
@POST
public T create(T entity) {
return this.service.create(entity);
}
/*
* (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((Class) 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);
}
/*
* (non-Javadoc)
*
* @see org.resthub.web.controller.GenericController#getEntities()
*/
@Override
@GET
@Path("/all")
public List findAll() {
return this.service.findAll();
}
/*
* (non-Javadoc)
*
* @see org.resthub.web.controller.GenericController#findAll(java.lang.Integer, java.lang.Integer)
*/
@Override
@GET
public PageResponse findAll(@QueryParam("page") @DefaultValue("0") Integer page,
@QueryParam("size") @DefaultValue("5") Integer size) {
return new PageResponse(this.service.findAll(new PageRequest(page, size)));
}
/*
* (non-Javadoc)
*
* @see org.resthub.web.controller.GenericController#getResource(ID)
*/
@Override
@GET
@Path("/{id}")
public T findById(@PathParam("id") ID id) {
T entity = this.service.findById(id);
if (entity == null) {
throw new NotFoundException();
}
return entity;
}
/*
* (non-Javadoc)
*
* @see org.resthub.web.controller.GenericController#delete()
*/
@Override
@DELETE
@Path("/all")
public void delete() {
this.service.deleteAllWithCascade();
}
/*
* (non-Javadoc)
*
* @see org.resthub.web.controller.GenericController#delete(ID)
*/
@Override
@DELETE
@Path("/{id}")
public void delete(@PathParam("id") ID id) {
this.service.delete(id);
}
/**
* 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((Class) ClassUtils.getGenericTypeFromBean(this.service),
em.getMetamodel());
return (ID) utils.getIdFromEntity(obj);
}
=======
GenericController {
protected S service;
private EntityManager em;
@PersistenceContext
public void setEntityManager(EntityManager em) {
this.em = em;
}
public GenericControllerImpl() {
}
public void setService(S service) {
this.service = service;
}
public S getService() {
return this.service;
}
/*
* (non-Javadoc)
*
* @see org.resthub.web.controller.GenericController#create(T)
*/
@Override
@POST
public T create(T entity) {
return this.service.create(entity);
}
/*
* (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((Class) 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);
}
/*
* (non-Javadoc)
*
* @see org.resthub.web.controller.GenericController#getEntities()
*/
@Override
@GET
@Path("/all")
public List findAll() {
return this.service.findAll();
}
/*
* (non-Javadoc)
*
* @see
* org.resthub.web.controller.GenericController#getEntities(java.lang.Integer
* , java.lang.Integer)
*/
@Override
@GET
public PageResponse findAll(@QueryParam("page") @DefaultValue("0") Integer page,
@QueryParam("size") @DefaultValue("5") Integer size) {
return new PageResponse(this.service.findAll(new PageRequest(page, size)));
}
/*
* (non-Javadoc)
*
* @see org.resthub.web.controller.GenericController#getResource(ID)
*/
@Override
@GET
@Path("/{id}")
public T findById(@PathParam("id") ID id) {
T entity = this.service.findById(id);
if (entity == null) {
throw new NotFoundException();
}
return entity;
}
/*
* (non-Javadoc)
* @see org.resthub.web.controller.GenericController#delete()
*/
@Override
@DELETE
@Path("/all")
public void delete() {
this.service.deleteAllWithCascade();
}
/*
* (non-Javadoc)
*
* @see org.resthub.web.controller.GenericController#delete(ID)
*/
@Override
@DELETE
@Path("/{id}")
public void delete(@PathParam("id") ID id) {
this.service.delete(id);
}
/**
* 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((Class) ClassUtils.getGenericTypeFromBean(this.service),
em.getMetamodel());
return (ID) utils.getIdFromEntity(obj);
}
>>>>>>> cced174f5ded0640f16abdf78478e9ef4221c256
} |