cimitero.rest.InvoiceRESTService.java Source code

Java tutorial

Introduction

Here is the source code for cimitero.rest.InvoiceRESTService.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.InvoiceDto;
import cimitero.dto.ItemWrapper;
import cimitero.dto.ResponseDto;
import cimitero.entities.TInvoice;
import cimitero.entities.TTomb;
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("/invoice")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public class InvoiceRESTService {

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

    public InvoiceRESTService() {

    }

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

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

    // save or update invoice
    @POST
    public ResponseDto updateInvoice(InvoiceDto invoiceDto) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        TInvoice tmpInvoice = new TInvoice(invoiceDto.getInvoiceId(), invoiceDto.getInvoiceDate(),
                invoiceDto.getPaymentDate(), invoiceDto.getInvoiceNumber(), invoiceDto.getTotal(),
                invoiceDto.getCurrency());
        TTomb tmpTomb = (TTomb) session.get(TTomb.class, invoiceDto.getTombId());
        tmpInvoice.setTomb(tmpTomb);
        if (invoiceDto.getInvoiceId() == -1)
            tmpInvoice.setInvoiceId(null);
        session.saveOrUpdate(tmpInvoice);
        ResponseDto response = new ResponseDto(true);
        session.getTransaction().commit();
        return response;
    }

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