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.ItemWrapper; import cimitero.dto.ResponseDto; import cimitero.entities.TCemetry; import cimitero.entities.TCemetryGround; 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.hibernate.criterion.Restrictions; import org.jboss.logging.Logger; /** * REST Web Service * * @author Markus */ @Stateless @Path("/cemetry") @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) public class CemetryRESTService { Logger log = Logger.getLogger(CemetryRESTService.class); public CemetryRESTService() { } /* * get all cemetry entries */ @GET public ResponseDto getAllCemetries() { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); List<TCemetry> results = session.createCriteria(TCemetry.class).list(); ResponseDto response = new ResponseDto(true); if (results.isEmpty()) { response.setOk(false); response.addError(1, "no entries found"); } else { response.setItems(new ItemWrapper<TCemetry>(results)); } session.getTransaction().commit(); return response; } /* * get cemetry by id */ @GET @Path("{id}") public ResponseDto getCemetryById(@PathParam("id") Integer id) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); TCemetry result = (TCemetry) session.get(TCemetry.class, id); ResponseDto response = new ResponseDto(true); if (result == null) { response.setOk(false); response.addError(1, "cemetry with id " + id + " not found"); } else { List<TCemetry> results = new ArrayList<TCemetry>(); results.add(result); response.setItems(new ItemWrapper(results)); } session.getTransaction().commit(); return response; } /* save or update cemetry */ @POST public ResponseDto updateCemetry(TCemetry cemetry) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); if (cemetry.getCemetryId() == -1) cemetry.setCemetryId(null); session.saveOrUpdate(cemetry); ResponseDto response = new ResponseDto(true); session.getTransaction().commit(); return response; } /* delete cemetry */ @DELETE @Path("{id}") public ResponseDto removeCemetry(@PathParam("id") Integer id) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); TCemetry result = (TCemetry) session.get(TCemetry.class, id); session.delete(result); ResponseDto response = new ResponseDto(true); session.getTransaction().commit(); return response; } /* get all cemetry grounds of cemetry */ @GET @Path("/{cemetryId}/cemetryground") public ResponseDto getAllCemetryGroundsOfCemetry(@PathParam("cemetryId") Integer cemetryId) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); List<TCemetryGround> results = session.createCriteria(TCemetryGround.class) .add(Restrictions.eq("cemetry.cemetryId", cemetryId)).list(); ResponseDto response = new ResponseDto(true); if (results.isEmpty()) { response.setOk(false); response.addError(1, "no entries found"); } else { response.setItems(new ItemWrapper<TCemetryGround>(results)); } session.getTransaction().commit(); return response; } }