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.dto.TombRequestDto; import cimitero.entities.TCustomer; import cimitero.entities.TTomb; import cimitero.entities.TTombRequest; 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("/tombrequest") @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) public class TombRequestRESTService { Logger log = Logger.getLogger(TombRequestRESTService.class); public TombRequestRESTService() { } // get all tomb request entries @GET public ResponseDto getAllTombRequest() { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); List<TTombRequest> results = session.createCriteria(TTombRequest.class).list(); ResponseDto response = new ResponseDto(true); if (results.isEmpty()) { response.setOk(false); response.addError(1, "no entries found"); } else { response.setItems(new ItemWrapper<TTombRequest>(results)); } session.getTransaction().commit(); return response; } // get tomb request by id @GET @Path("{id}") public ResponseDto getTombRequestById(@PathParam("id") Integer id) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); TTombRequest result = (TTombRequest) session.get(TTombRequest.class, id); ResponseDto response = new ResponseDto(true); if (result == null) { response.setOk(false); response.addError(1, "reminder with id " + id + " not found"); } else { List<TTombRequest> results = new ArrayList<TTombRequest>(); results.add(result); response.setItems(new ItemWrapper(results)); } session.getTransaction().commit(); return response; } // save or update tomb request @POST public ResponseDto updateTombRequest(TombRequestDto tombRequestDto) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); TTombRequest tmpTombRequest = new TTombRequest(tombRequestDto.getTombRequestId(), tombRequestDto.getRequestDate(), tombRequestDto.getRequestText()); TTomb tmpTomb = (TTomb) session.get(TTomb.class, tombRequestDto.getTombId()); tmpTombRequest.setTomb(tmpTomb); TCustomer tmpCustomer = (TCustomer) session.get(TCustomer.class, tombRequestDto.getPersonId()); tmpTombRequest.setCustomer(tmpCustomer); if (tombRequestDto.getTombRequestId() == -1) tmpTombRequest.setTombRequestId(null); session.saveOrUpdate(tmpTombRequest); ResponseDto response = new ResponseDto(true); session.getTransaction().commit(); return response; } // delete tomb request @DELETE @Path("{id}") public ResponseDto removeTombRequest(@PathParam("id") Integer id) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); TTombRequest result = (TTombRequest) session.get(TTombRequest.class, id); session.delete(result); ResponseDto response = new ResponseDto(true); session.getTransaction().commit(); return response; } }