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.ReminderDto; import cimitero.dto.ResponseDto; import cimitero.entities.TInvoice; import cimitero.entities.TReminder; 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("/reminder") @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) public class ReminderRESTService { Logger log = Logger.getLogger(ReminderRESTService.class); public ReminderRESTService() { } // get all reminder entries @GET public ResponseDto getAllPersons() { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); List<TReminder> results = session.createCriteria(TReminder.class).list(); ResponseDto response = new ResponseDto(true); if (results.isEmpty()) { response.setOk(false); response.addError(1, "no entries found"); } else { response.setItems(new ItemWrapper<TReminder>(results)); } session.getTransaction().commit(); return response; } // get reminder by id @GET @Path("{id}") public ResponseDto getReminderById(@PathParam("id") Integer id) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); TReminder result = (TReminder) session.get(TReminder.class, id); ResponseDto response = new ResponseDto(true); if (result == null) { response.setOk(false); response.addError(1, "reminder with id " + id + " not found"); } else { List<TReminder> results = new ArrayList<TReminder>(); results.add(result); response.setItems(new ItemWrapper(results)); } session.getTransaction().commit(); return response; } // save or update reminder @POST public ResponseDto updateReminder(ReminderDto reminderDto) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); TReminder tmpReminder = new TReminder(reminderDto.getReminderId(), reminderDto.getInvoiceDate(), reminderDto.getPaymentDate(), reminderDto.getCharges()); TInvoice tmpInvoice = (TInvoice) session.get(TInvoice.class, reminderDto.getInvoiceId()); tmpReminder.setInvoice(tmpInvoice); if (reminderDto.getReminderId() == -1) tmpReminder.setReminderId(null); session.saveOrUpdate(tmpReminder); ResponseDto response = new ResponseDto(true); session.getTransaction().commit(); return response; } // delete reminder @DELETE @Path("{id}") public ResponseDto removeReminder(@PathParam("id") Integer id) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); TReminder result = (TReminder) session.get(TReminder.class, id); session.delete(result); ResponseDto response = new ResponseDto(true); session.getTransaction().commit(); return response; } }