cimitero.rest.CustomerRESTService.java Source code

Java tutorial

Introduction

Here is the source code for cimitero.rest.CustomerRESTService.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.CustomerDto;
import cimitero.dto.InvoiceTombDto;
import cimitero.dto.ItemWrapper;
import cimitero.dto.ResponseDto;
import cimitero.dto.TombRequestDto;
import cimitero.dto.TombRequestTombDto;
import cimitero.entities.TCustomer;
import cimitero.entities.TTomb;
import cimitero.entities.TTombRequest;
import cimitero.persistence.HibernateUtil;
import java.sql.Date;
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("/customer")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public class CustomerRESTService {

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

    public CustomerRESTService() {

    }

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

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

    // save or update customer
    @POST
    public ResponseDto updateCustomer(CustomerDto customerDto) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        if (customerDto.getGebDatum() != null) {
            customerDto.setGebDatum(new Date(customerDto.getGebDatum().getTime()));
        }
        TCustomer tmpCustomer = new TCustomer(customerDto.getPersonId(), customerDto.getTitle(),
                customerDto.getFirstName(), customerDto.getLastName(), customerDto.getOtherFirstNames(),
                customerDto.getGebDatum(), customerDto.getAddress(), customerDto.getTelephoneNumber());
        List<TTomb> tmpTombs = new ArrayList<TTomb>();
        if (customerDto.getTombIds() != null) {
            for (Integer tombId : customerDto.getTombIds()) {
                TTomb tmpTomb = (TTomb) session.get(TTomb.class, tombId);
                if (tmpTomb != null)
                    tmpTombs.add(tmpTomb);
            }
        }
        //if(tmpTombs.size() > 0 && customerDto.getTombIds() != null) tmpCustomer.setTombs(tmpTombs);
        if (customerDto.getPersonId() == -1)
            tmpCustomer.setPersonId(null);
        session.saveOrUpdate(tmpCustomer);
        ResponseDto response = new ResponseDto(true);
        session.getTransaction().commit();
        return response;
    }

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

    // get all invoices of customer
    @GET
    @Path("{customerId}/invoice")
    public ResponseDto getAllInvoicesOfCustomer(@PathParam("customerId") Integer customerId) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<TTomb> tombs = session.createCriteria(TTomb.class)
                .add(Restrictions.eq("customer.personId", customerId)).list();
        ResponseDto response = new ResponseDto(true);
        if (tombs == null) {
            response.setOk(false);
            response.addError(1, "customer with id " + customerId + " has 0 invoices");
        } else {
            List<InvoiceTombDto> invoices = new ArrayList<InvoiceTombDto>();
            for (TTomb tomb : tombs) {
                log.info("tomb" + tomb.getTombId() + ": number of invoices: " + tomb.getInvoices().size());
                InvoiceTombDto tmpInvoiceDto = new InvoiceTombDto(tomb.getTombId(), tomb.getTombNo(),
                        tomb.getInvoices());
                invoices.add(tmpInvoiceDto);
            }
            response.setItems(new ItemWrapper(invoices));
        }
        session.getTransaction().commit();
        return response;
    }

    // get all tomb requests of customer
    @GET
    @Path("{customerId}/tombrequest")
    public ResponseDto getAllTombRequestsOfCustomer(@PathParam("customerId") Integer customerId) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<TTombRequest> tombRequests = session.createCriteria(TTombRequest.class)
                .add(Restrictions.eq("customer.personId", customerId)).list();
        ResponseDto response = new ResponseDto(true);
        if (tombRequests == null) {
            response.setOk(false);
            response.addError(1, "customer with id " + customerId + " has 0 tomb requests");
        } else {
            List<TombRequestDto> tombRequestDtoList = new ArrayList<TombRequestDto>();
            for (TTombRequest tombRequest : tombRequests) {
                tombRequestDtoList
                        .add(new TombRequestDto(tombRequest.getTombRequestId(), tombRequest.getRequestDate(),
                                tombRequest.getRequestText(), tombRequest.getTomb().getTombId(),
                                tombRequest.getTomb().getTombNo(), tombRequest.getCustomer().getPersonId()));
            }
            response.setItems(new ItemWrapper(tombRequestDtoList));
        }
        session.getTransaction().commit();
        return response;
    }

    // get all tombs of customer
    @GET
    @Path("{customerId}/tomb")
    public ResponseDto getAllTombsOfCustomer(@PathParam("customerId") Integer customerId) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<TTomb> tombs = session.createCriteria(TTomb.class)
                .add(Restrictions.eq("customer.personId", customerId)).list();
        ResponseDto response = new ResponseDto(true);
        if (tombs == null) {
            response.setOk(false);
            response.addError(1, "customer with id " + customerId + " has 0 tombs");
        } else {
            response.setItems(new ItemWrapper<TTomb>(tombs));
        }
        session.getTransaction().commit();
        return response;
    }
}