Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package cimitero.rest; import cimitero.dto.BodyDto; import cimitero.dto.ItemWrapper; import cimitero.dto.ResponseDto; import cimitero.entities.TBody; import cimitero.entities.TTomb; import cimitero.persistence.HibernateUtil; import java.util.ArrayList; import java.util.List; import javax.ejb.Stateless; import javax.ws.rs.Consumes; import javax.ws.rs.Path; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.DELETE; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import org.hibernate.Session; import org.jboss.logging.Logger; /** * REST Web Service * * @author Markus */ @Stateless @Path("/body") @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) public class BodyRESTService { Logger log = Logger.getLogger(InitRESTService.class); public BodyRESTService() { } // get all body entries @GET public ResponseDto getAllBodies() { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); List<TBody> results = session.createCriteria(TBody.class).list(); ResponseDto response = new ResponseDto(true); if (results.isEmpty()) { response.setOk(false); response.addError(1, "no entries found"); } else { response.setItems(new ItemWrapper<TBody>(results)); } session.getTransaction().commit(); return response; } // get body by id @GET @Path("{id}") public ResponseDto getBodyById(@PathParam("id") Integer id) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); TBody result = (TBody) session.get(TBody.class, id); ResponseDto response = new ResponseDto(true); if (result == null) { response.setOk(false); response.addError(1, "body with id " + id + " not found"); } else { List<TBody> results = new ArrayList<TBody>(); results.add(result); response.setItems(new ItemWrapper(results)); } session.getTransaction().commit(); return response; } // save or update body @POST public ResponseDto updateBody(BodyDto bodyDto) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); TBody tmpBody = new TBody(bodyDto.getPersonId(), bodyDto.getTitle(), bodyDto.getFirstName(), bodyDto.getLastName(), bodyDto.getOtherFirstNames(), bodyDto.getGebDatum(), bodyDto.getAddress(), bodyDto.getTelephoneNumber(), bodyDto.getDateOfDeath()); TTomb tmpTomb = (TTomb) session.get(TTomb.class, bodyDto.getTombId()); tmpBody.setTomb(tmpTomb); if (bodyDto.getPersonId() == -1) tmpBody.setPersonId(null); session.saveOrUpdate(tmpBody); ResponseDto response = new ResponseDto(true); session.getTransaction().commit(); return response; } // delete body @DELETE @Path("{id}") public ResponseDto removeCoordinate(@PathParam("id") Integer id) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); TBody result = (TBody) session.get(TBody.class, id); session.delete(result); ResponseDto response = new ResponseDto(true); session.getTransaction().commit(); return response; } }