cimitero.rest.TombRESTService.java Source code

Java tutorial

Introduction

Here is the source code for cimitero.rest.TombRESTService.java

Source

/*
 * 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.TombDto;
import cimitero.entities.TBody;
import cimitero.entities.TCemetryGround;
import cimitero.entities.TCustomer;
import cimitero.entities.TInvoice;
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.Hibernate;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.jboss.logging.Logger;

/**
 * REST Web Service
 *
 * @author Markus
 */
@Stateless
@Path("/tomb")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public class TombRESTService {

    Logger log = Logger.getLogger(TombRESTService.class);

    public TombRESTService() {

    }

    // get all tomb entries
    @GET
    public ResponseDto getAllTombs() {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<TTomb> results = session.createCriteria(TTomb.class).list();
        ResponseDto response = new ResponseDto(true);
        if (results.isEmpty()) {
            response.setOk(false);
            response.addError(1, "no entries found");
        } else {
            response.setItems(new ItemWrapper<TTomb>(results));
        }
        session.getTransaction().commit();
        return response;
    }

    // get tomb by id
    @GET
    @Path("{id}")
    public ResponseDto getTombById(@PathParam("id") Integer id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        TTomb result = (TTomb) session.get(TTomb.class, id);
        ResponseDto response = new ResponseDto(true);
        if (result == null) {
            response.setOk(false);
            response.addError(1, "tomb with id " + id + " not found");
        } else {
            List<TTomb> results = new ArrayList<TTomb>();
            results.add(result);
            response.setItems(new ItemWrapper(results));
        }
        session.getTransaction().commit();
        return response;
    }

    // save or update tomb
    @POST
    public ResponseDto updateTomb(TombDto tombDto) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        TTomb tmpTomb = new TTomb(tombDto.getTombId(), tombDto.getTombNo());
        TCemetryGround tmpCemetryGround = (TCemetryGround) session.get(TCemetryGround.class,
                tombDto.getCemetryGroundId());
        tmpTomb.setCemetryGround(tmpCemetryGround);
        TCustomer tmpCustomer = (TCustomer) session.get(TCustomer.class, tombDto.getPersonId());
        tmpTomb.setCustomer(tmpCustomer);
        if (tombDto.getTombId() == -1)
            tmpTomb.setTombId(null);
        session.saveOrUpdate(tmpTomb);
        ResponseDto response = new ResponseDto(true);
        session.getTransaction().commit();
        return response;
    }

    // delete tomb
    @DELETE
    @Path("{id}")
    public ResponseDto removeTomb(@PathParam("id") Integer id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        TTomb result = (TTomb) session.get(TTomb.class, id);
        session.delete(result);
        ResponseDto response = new ResponseDto(true);
        session.getTransaction().commit();
        return response;
    }

    @GET
    @Path("/{tombId}/tombrequest")
    public ResponseDto getAllTombRequestsOfTomb(@PathParam("tombId") Integer tombId) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<TTombRequest> results = session.createCriteria(TTombRequest.class)
                .add(Restrictions.eq("tomb.tombId", tombId)).list();
        ResponseDto response = new ResponseDto(true);
        if (results.isEmpty()) {
            response.setOk(false);
            response.addError(1, "no tomb requests found");
        } else {
            response.setItems(new ItemWrapper<TTombRequest>(results));
        }
        session.getTransaction().commit();
        return response;
    }

    @GET
    @Path("/{tombId}/body")
    public ResponseDto getAllBodiesOfTomb(@PathParam("tombId") Integer tombId) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<TBody> results = session.createCriteria(TBody.class).add(Restrictions.eq("tomb.tombId", tombId))
                .list();
        ResponseDto response = new ResponseDto(true);
        if (results.isEmpty()) {
            response.setOk(false);
            response.addError(1, "no bodies found");
        } else {
            response.setItems(new ItemWrapper<TBody>(results));
        }
        session.getTransaction().commit();
        return response;
    }

    @GET
    @Path("/{tombId}/invoice")
    public ResponseDto getAllInvoicesOfTomb(@PathParam("tombId") Integer tombId) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<TInvoice> results = session.createCriteria(TInvoice.class).add(Restrictions.eq("tomb.tombId", tombId))
                .list();
        ResponseDto response = new ResponseDto(true);
        if (results.isEmpty()) {
            response.setOk(false);
            response.addError(1, "no invoices found");
        } else {
            response.setItems(new ItemWrapper<TInvoice>(results));
        }
        session.getTransaction().commit();
        return response;
    }

    @GET
    @Path("/{tombId}/customer")
    public ResponseDto getCustomerOfTomb(@PathParam("tombId") Integer tombId) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        TTomb tomb = (TTomb) session.get(TTomb.class, tombId);
        ResponseDto response = new ResponseDto(true);
        if (tomb == null) {
            response.setOk(false);
            response.addError(1, "tomb with id " + tombId + " not found");
        } else {
            if (tomb.getCustomer() != null) {
                TCustomer customer = new TCustomer(tomb.getCustomer().getPersonId(), tomb.getCustomer().getTitle(),
                        tomb.getCustomer().getFirstName(), tomb.getCustomer().getLastName(),
                        tomb.getCustomer().getOtherFirstNames(), tomb.getCustomer().getGebDatum(),
                        tomb.getCustomer().getAddress(), tomb.getCustomer().getTelephoneNumber());
                List<TCustomer> result = new ArrayList<TCustomer>();
                result.add(customer);
                response.setItems(new ItemWrapper(result));
            } else {
                response.setOk(false);
                response.addError(1, "tomb with id " + tombId + " has no customer");
            }
        }
        session.getTransaction().commit();
        return response;
    }
}